blob: b40b5f8dbae8e8aa4f99ac016f9a3f939eff74c3 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Valerio Settibf999cb2023-12-28 17:48:13 +01009#include "mbedtls/psa_util.h"
10
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020011/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
12 * uses mbedtls_ctr_drbg internally. */
13#include "mbedtls/ctr_drbg.h"
14
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020016#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020017
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010018/* For psa_can_do_hash() */
19#include "psa_crypto_core.h"
20
Gilles Peskine8e94efe2021-02-13 00:25:53 +010021#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010022#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010023#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053024#if defined(PSA_CRYPTO_DRIVER_TEST)
25#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053026#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
27#else
28#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053029#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010030
Gilles Peskine4023c012021-05-27 13:21:20 +020031/* If this comes up, it's a bug in the test code or in the test data. */
32#define UNUSED 0xdeadbeef
33
Dave Rodgman647791d2021-06-23 12:49:59 +010034/* Assert that an operation is (not) active.
35 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010036#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
37#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010038
Przemek Stekiel7c795482022-11-15 22:26:12 +010039#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010040int ecjpake_operation_setup(psa_pake_operation_t *operation,
41 psa_pake_cipher_suite_t *cipher_suite,
42 psa_pake_role_t role,
43 mbedtls_svc_key_id_t key,
44 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010045{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (key_available) {
53 PSA_ASSERT(psa_pake_set_password_key(operation, key));
54 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010055 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010056exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010057 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010058}
59#endif
60
Jaeden Amerof24c7f82018-06-27 17:20:43 +010061/** An invalid export length that will never be set by psa_export_key(). */
62static const size_t INVALID_EXPORT_LENGTH = ~0U;
63
Gilles Peskinea7aa4422018-08-14 15:17:54 +020064/** Test if a buffer contains a constant byte value.
65 *
66 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020067 *
68 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020069 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 * \param size Size of the buffer in bytes.
71 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020072 * \return 1 if the buffer is all-bits-zero.
73 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020076{
77 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 for (i = 0; i < size; i++) {
79 if (((unsigned char *) buffer)[i] != c) {
80 return 0;
81 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020082 }
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020084}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010085#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020086/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010087static int asn1_write_10x(unsigned char **p,
88 unsigned char *start,
89 size_t bits,
90 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020091{
92 int ret;
93 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (bits == 0) {
95 return MBEDTLS_ERR_ASN1_INVALID_DATA;
96 }
97 if (bits <= 8 && x >= 1 << (bits - 1)) {
98 return MBEDTLS_ERR_ASN1_INVALID_DATA;
99 }
100 if (*p < start || *p - start < (ptrdiff_t) len) {
101 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
102 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200103 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 (*p)[len-1] = x;
105 if (bits % 8 == 0) {
106 (*p)[1] |= 1;
107 } else {
108 (*p)[0] |= 1 << (bits % 8);
109 }
110 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
111 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
112 MBEDTLS_ASN1_INTEGER));
113 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200114}
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116static int construct_fake_rsa_key(unsigned char *buffer,
117 size_t buffer_size,
118 unsigned char **p,
119 size_t bits,
120 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200121{
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200123 int ret;
124 int len = 0;
125 /* Construct something that looks like a DER encoding of
126 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
127 * RSAPrivateKey ::= SEQUENCE {
128 * version Version,
129 * modulus INTEGER, -- n
130 * publicExponent INTEGER, -- e
131 * privateExponent INTEGER, -- d
132 * prime1 INTEGER, -- p
133 * prime2 INTEGER, -- q
134 * exponent1 INTEGER, -- d mod (p-1)
135 * exponent2 INTEGER, -- d mod (q-1)
136 * coefficient INTEGER, -- (inverse of q) mod p
137 * otherPrimeInfos OtherPrimeInfos OPTIONAL
138 * }
139 * Or, for a public key, the same structure with only
140 * version, modulus and publicExponent.
141 */
142 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (keypair) {
144 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* q */
151 asn1_write_10x(p, buffer, half_bits, 1));
152 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
153 asn1_write_10x(p, buffer, half_bits, 3));
154 MBEDTLS_ASN1_CHK_ADD(len, /* d */
155 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200156 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
158 asn1_write_10x(p, buffer, 17, 1));
159 MBEDTLS_ASN1_CHK_ADD(len, /* n */
160 asn1_write_10x(p, buffer, bits, 1));
161 if (keypair) {
162 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
163 mbedtls_asn1_write_int(p, buffer, 0));
164 }
165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 {
167 const unsigned char tag =
168 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200172}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100173#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int exercise_mac_setup(psa_key_type_t key_type,
176 const unsigned char *key_bytes,
177 size_t key_length,
178 psa_algorithm_t alg,
179 psa_mac_operation_t *operation,
180 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100181{
Ronald Cron5425a212020-08-04 14:58:35 +0200182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
186 psa_set_key_algorithm(&attributes, alg);
187 psa_set_key_type(&attributes, key_type);
188 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 /* If setup failed, reproduce the failure, so that the caller can
194 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (*status != PSA_SUCCESS) {
196 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 }
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201
202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 psa_destroy_key(key);
204 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100205}
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int exercise_cipher_setup(psa_key_type_t key_type,
208 const unsigned char *key_bytes,
209 size_t key_length,
210 psa_algorithm_t alg,
211 psa_cipher_operation_t *operation,
212 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100213{
Ronald Cron5425a212020-08-04 14:58:35 +0200214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
218 psa_set_key_algorithm(&attributes, alg);
219 psa_set_key_type(&attributes, key_type);
220 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100225 /* If setup failed, reproduce the failure, so that the caller can
226 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (*status != PSA_SUCCESS) {
228 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
229 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234
235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 psa_destroy_key(key);
237 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241{
242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200244 uint8_t buffer[1];
245 size_t length;
246 int ok = 0;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 psa_set_key_id(&attributes, key_id);
249 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
250 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
251 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
252 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
253 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200254 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200256 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
258 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
259 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
260 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
261 TEST_EQUAL(psa_get_key_type(&attributes), 0);
262 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
265 PSA_ERROR_INVALID_HANDLE);
266 TEST_EQUAL(psa_export_public_key(key,
267 buffer, sizeof(buffer), &length),
268 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200270 ok = 1;
271
272exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100273 /*
274 * Key attributes may have been returned by psa_get_key_attributes()
275 * thus reset them as required.
276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200280}
281
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200282/* Assert that a key isn't reported as having a slot number. */
283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200285 do \
286 { \
287 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 TEST_EQUAL(psa_get_key_slot_number( \
289 attributes, \
290 &ASSERT_NO_SLOT_NUMBER_slot_number), \
291 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200292 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200294#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295#define ASSERT_NO_SLOT_NUMBER(attributes) \
296 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
298
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530299#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
300
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301/* An overapproximation of the amount of storage needed for a key of the
302 * given type and with the given content. The API doesn't make it easy
303 * to find a good value for the size. The current implementation doesn't
304 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305#define KEY_BITS_FROM_DATA(type, data) \
306 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100307
Darryl Green0c6575a2018-11-07 16:05:30 +0000308typedef enum {
309 IMPORT_KEY = 0,
310 GENERATE_KEY = 1,
311 DERIVE_KEY = 2
312} generate_method;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100315 DO_NOT_SET_LENGTHS = 0,
316 SET_LENGTHS_BEFORE_NONCE = 1,
317 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100318} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100321 USE_NULL_TAG = 0,
322 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100323} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100324
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530325
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326/*!
327 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 * \param key_type_arg Type of key passed in
329 * \param key_data The encryption / decryption key data
330 * \param alg_arg The type of algorithm used
331 * \param nonce Nonce data
332 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100333 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100334 * feed additional data in to be encrypted /
335 * decrypted. If -1, no chunking.
336 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100337 * \param data_part_len_arg If not -1, the length of chunks to feed
338 * the data in to be encrypted / decrypted. If
339 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100340 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100341 * expected here, this controls whether or not
342 * to set lengths, and in what order with
343 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100346 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100347 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
351 int alg_arg,
352 data_t *nonce,
353 data_t *additional_data,
354 int ad_part_len_arg,
355 data_t *input_data,
356 int data_part_len_arg,
357 set_lengths_method_t set_lengths_method,
358 data_t *expected_output,
359 int is_encrypt,
360 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100361{
362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
363 psa_key_type_t key_type = key_type_arg;
364 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100365 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 unsigned char *output_data = NULL;
367 unsigned char *part_data = NULL;
368 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100371 size_t output_size = 0;
372 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100373 size_t output_length = 0;
374 size_t key_bits = 0;
375 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100376 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100377 size_t part_length = 0;
378 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t ad_part_len = 0;
381 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
384 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100387 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (is_encrypt) {
392 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
393 } else {
394 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100396
397 psa_set_key_algorithm(&attributes, alg);
398 psa_set_key_type(&attributes, key_type);
399
400 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
401 &key));
402
403 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
404 key_bits = psa_get_key_bits(&attributes);
405
406 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
407
408 if (is_encrypt) {
409 /* Tag gets written at end of buffer. */
410 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
411 (input_data->len +
412 tag_length));
413 data_true_size = input_data->len;
414 } else {
415 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
416 (input_data->len -
417 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100418
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100419 /* Do not want to attempt to decrypt tag. */
420 data_true_size = input_data->len - tag_length;
421 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100422
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100423 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (is_encrypt) {
426 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
427 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
428 } else {
429 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
430 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100431 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100433 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (is_encrypt) {
436 status = psa_aead_encrypt_setup(&operation, key, alg);
437 } else {
438 status = psa_aead_decrypt_setup(&operation, key, alg);
439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
441 /* If the operation is not supported, just skip and not fail in case the
442 * encryption involves a common limitation of cryptography hardwares and
443 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (status == PSA_ERROR_NOT_SUPPORTED) {
445 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
446 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
452 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
453 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
454 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
455 data_true_size));
456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
457 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
458 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
461 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100469 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 part_offset += part_length, part_count++) {
471 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100472 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100474 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100476 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation,
480 additional_data->x + part_offset,
481 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
487 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 data_part_len = (size_t) data_part_len_arg;
493 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
494 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100495
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100496 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100499 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 part_offset += part_length, part_count++) {
501 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100502 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 } else if ((data_true_size - part_offset) < data_part_len) {
504 part_length = (data_true_size - part_offset);
505 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100506 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 }
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 PSA_ASSERT(psa_aead_update(&operation,
510 (input_data->x + part_offset),
511 part_length, part_data,
512 part_data_size,
513 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (output_data && output_part_length) {
516 memcpy((output_data + output_length), part_data,
517 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100518 }
519
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 output_length += output_part_length;
521 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100523 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
525 data_true_size, output_data,
526 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100527 }
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (is_encrypt) {
530 PSA_ASSERT(psa_aead_finish(&operation, final_data,
531 final_output_size,
532 &output_part_length,
533 tag_buffer, tag_length,
534 &tag_size));
535 } else {
536 PSA_ASSERT(psa_aead_verify(&operation, final_data,
537 final_output_size,
538 &output_part_length,
539 (input_data->x + data_true_size),
540 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100541 }
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (output_data && output_part_length) {
544 memcpy((output_data + output_length), final_data,
545 output_part_length);
546 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100547
548 output_length += output_part_length;
549
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100550
551 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
552 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (is_encrypt) {
554 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (output_data && tag_length) {
557 memcpy((output_data + output_length), tag_buffer,
558 tag_length);
559 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100560
561 output_length += tag_length;
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 TEST_EQUAL(output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
565 input_data->len));
566 TEST_LE_U(output_length,
567 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
568 } else {
569 TEST_EQUAL(output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
571 input_data->len));
572 TEST_LE_U(output_length,
573 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 }
575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100577 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100578 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100582
583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 psa_destroy_key(key);
585 psa_aead_abort(&operation);
586 mbedtls_free(output_data);
587 mbedtls_free(part_data);
588 mbedtls_free(final_data);
589 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100592}
593
Neil Armstrong4766f992022-02-28 16:23:59 +0100594/*!
595 * \brief Internal Function for MAC multipart tests.
596 * \param key_type_arg Type of key passed in
597 * \param key_data The encryption / decryption key data
598 * \param alg_arg The type of algorithm used
599 * \param input_data Data to encrypt / decrypt
600 * \param data_part_len_arg If not -1, the length of chunks to feed
601 * the data in to be encrypted / decrypted. If
602 * -1, no chunking
603 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000604 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100605 * \param do_zero_parts If non-zero, interleave zero length chunks
606 * with normal length chunks.
607 * \return int Zero on failure, non-zero on success.
608 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
610 int alg_arg,
611 data_t *input_data,
612 int data_part_len_arg,
613 data_t *expected_output,
614 int is_verify,
615 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100616{
617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
618 psa_key_type_t key_type = key_type_arg;
619 psa_algorithm_t alg = alg_arg;
620 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
621 unsigned char mac[PSA_MAC_MAX_SIZE];
622 size_t part_offset = 0;
623 size_t part_length = 0;
624 size_t data_part_len = 0;
625 size_t mac_len = 0;
626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
628
629 int test_ok = 0;
630 size_t part_count = 0;
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (is_verify) {
635 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
636 } else {
637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
638 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 psa_set_key_algorithm(&attributes, alg);
641 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
644 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (is_verify) {
647 status = psa_mac_verify_setup(&operation, key, alg);
648 } else {
649 status = psa_mac_sign_setup(&operation, key, alg);
650 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 part_offset += part_length, part_count++) {
661 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100662 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 } else if ((input_data->len - part_offset) < data_part_len) {
664 part_length = (input_data->len - part_offset);
665 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100666 part_length = data_part_len;
667 }
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 PSA_ASSERT(psa_mac_update(&operation,
670 (input_data->x + part_offset),
671 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100672 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100674 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
676 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (is_verify) {
680 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
681 expected_output->len));
682 } else {
683 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
684 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100685
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100686 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100687 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100688 }
689
690 test_ok = 1;
691
692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 psa_destroy_key(key);
694 psa_mac_abort(&operation);
695 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100698}
699
Neil Armstrong75673ab2022-06-15 17:39:01 +0200700#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100701static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
702 psa_pake_operation_t *server,
703 psa_pake_operation_t *client,
704 int client_input_first,
705 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200706{
707 unsigned char *buffer0 = NULL, *buffer1 = NULL;
708 size_t buffer_length = (
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
711 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200712 /* The output should be exactly this size according to the spec */
713 const size_t expected_size_key_share =
714 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
715 /* The output should be exactly this size according to the spec */
716 const size_t expected_size_zk_public =
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
718 /* The output can be smaller: the spec allows stripping leading zeroes */
719 const size_t max_expected_size_zk_proof =
720 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200721 size_t buffer0_off = 0;
722 size_t buffer1_off = 0;
723 size_t s_g1_len, s_g2_len, s_a_len;
724 size_t s_g1_off, s_g2_off, s_a_off;
725 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
726 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
727 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
728 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
729 size_t c_g1_len, c_g2_len, c_a_len;
730 size_t c_g1_off, c_g2_off, c_a_off;
731 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
732 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
733 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
734 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
735 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200736 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100738 TEST_CALLOC(buffer0, buffer_length);
739 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200742 case 1:
743 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
745 buffer0 + buffer0_off,
746 512 - buffer0_off, &s_g1_len));
747 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200748 s_g1_off = buffer0_off;
749 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
751 buffer0 + buffer0_off,
752 512 - buffer0_off, &s_x1_pk_len));
753 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200754 s_x1_pk_off = buffer0_off;
755 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
757 buffer0 + buffer0_off,
758 512 - buffer0_off, &s_x1_pr_len));
759 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200760 s_x1_pr_off = buffer0_off;
761 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
763 buffer0 + buffer0_off,
764 512 - buffer0_off, &s_g2_len));
765 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200766 s_g2_off = buffer0_off;
767 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
769 buffer0 + buffer0_off,
770 512 - buffer0_off, &s_x2_pk_len));
771 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200772 s_x2_pk_off = buffer0_off;
773 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
775 buffer0 + buffer0_off,
776 512 - buffer0_off, &s_x2_pr_len));
777 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200778 s_x2_pr_off = buffer0_off;
779 buffer0_off += s_x2_pr_len;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500782 buffer0[s_x1_pr_off + 8] ^= 1;
783 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200784 expected_status = PSA_ERROR_DATA_INVALID;
785 }
786
Neil Armstrong51009d72022-09-05 17:59:54 +0200787 /*
788 * When injecting errors in inputs, the implementation is
789 * free to detect it right away of with a delay.
790 * This permits delaying the error until the end of the input
791 * sequence, if no error appears then, this will be treated
792 * as an error.
793 */
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200796 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
798 buffer0 + s_g1_off, s_g1_len);
799 if (inject_error == 1 && status != PSA_SUCCESS) {
800 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 } else {
803 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 }
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
807 buffer0 + s_x1_pk_off,
808 s_x1_pk_len);
809 if (inject_error == 1 && status != PSA_SUCCESS) {
810 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200811 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 } else {
813 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 }
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
817 buffer0 + s_x1_pr_off,
818 s_x1_pr_len);
819 if (inject_error == 1 && status != PSA_SUCCESS) {
820 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200821 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 } else {
823 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
827 buffer0 + s_g2_off,
828 s_g2_len);
829 if (inject_error == 1 && status != PSA_SUCCESS) {
830 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200831 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 } else {
833 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 }
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
837 buffer0 + s_x2_pk_off,
838 s_x2_pk_len);
839 if (inject_error == 1 && status != PSA_SUCCESS) {
840 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200841 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 } else {
843 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
847 buffer0 + s_x2_pr_off,
848 s_x2_pr_len);
849 if (inject_error == 1 && status != PSA_SUCCESS) {
850 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200851 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 } else {
853 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200854 }
855
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200856 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (inject_error == 1) {
858 TEST_ASSERT(
859 !"One of the last psa_pake_input() calls should have returned the expected error.");
860 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 }
862
863 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
865 buffer1 + buffer1_off,
866 512 - buffer1_off, &c_g1_len));
867 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200868 c_g1_off = buffer1_off;
869 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
871 buffer1 + buffer1_off,
872 512 - buffer1_off, &c_x1_pk_len));
873 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200874 c_x1_pk_off = buffer1_off;
875 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
877 buffer1 + buffer1_off,
878 512 - buffer1_off, &c_x1_pr_len));
879 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200880 c_x1_pr_off = buffer1_off;
881 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
883 buffer1 + buffer1_off,
884 512 - buffer1_off, &c_g2_len));
885 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200886 c_g2_off = buffer1_off;
887 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
889 buffer1 + buffer1_off,
890 512 - buffer1_off, &c_x2_pk_len));
891 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200892 c_x2_pk_off = buffer1_off;
893 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
895 buffer1 + buffer1_off,
896 512 - buffer1_off, &c_x2_pr_len));
897 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200898 c_x2_pr_off = buffer1_off;
899 buffer1_off += c_x2_pr_len;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
904 buffer0 + s_g1_off, s_g1_len);
905 if (inject_error == 1 && status != PSA_SUCCESS) {
906 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200907 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 } else {
909 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 }
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
913 buffer0 + s_x1_pk_off,
914 s_x1_pk_len);
915 if (inject_error == 1 && status != PSA_SUCCESS) {
916 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 } else {
919 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 }
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
923 buffer0 + s_x1_pr_off,
924 s_x1_pr_len);
925 if (inject_error == 1 && status != PSA_SUCCESS) {
926 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200927 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 } else {
929 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 }
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
933 buffer0 + s_g2_off,
934 s_g2_len);
935 if (inject_error == 1 && status != PSA_SUCCESS) {
936 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200937 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 } else {
939 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 }
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
943 buffer0 + s_x2_pk_off,
944 s_x2_pk_len);
945 if (inject_error == 1 && status != PSA_SUCCESS) {
946 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200947 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 } else {
949 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 }
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
953 buffer0 + s_x2_pr_off,
954 s_x2_pr_len);
955 if (inject_error == 1 && status != PSA_SUCCESS) {
956 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200957 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 } else {
959 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200960 }
961
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200962 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (inject_error == 1) {
964 TEST_ASSERT(
965 !"One of the last psa_pake_input() calls should have returned the expected error.");
966 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500970 buffer1[c_x1_pr_off + 12] ^= 1;
971 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200972 expected_status = PSA_ERROR_DATA_INVALID;
973 }
974
975 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
977 buffer1 + c_g1_off, c_g1_len);
978 if (inject_error == 2 && status != PSA_SUCCESS) {
979 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200980 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 } else {
982 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200983 }
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
986 buffer1 + c_x1_pk_off, c_x1_pk_len);
987 if (inject_error == 2 && status != PSA_SUCCESS) {
988 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 } else {
991 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200992 }
993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
995 buffer1 + c_x1_pr_off, c_x1_pr_len);
996 if (inject_error == 2 && status != PSA_SUCCESS) {
997 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200998 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 } else {
1000 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001001 }
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1004 buffer1 + c_g2_off, c_g2_len);
1005 if (inject_error == 2 && status != PSA_SUCCESS) {
1006 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 } else {
1009 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001010 }
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1013 buffer1 + c_x2_pk_off, c_x2_pk_len);
1014 if (inject_error == 2 && status != PSA_SUCCESS) {
1015 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001016 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 } else {
1018 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001019 }
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1022 buffer1 + c_x2_pr_off, c_x2_pr_len);
1023 if (inject_error == 2 && status != PSA_SUCCESS) {
1024 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001025 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 } else {
1027 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001028 }
1029
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001030 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (inject_error == 2) {
1032 TEST_ASSERT(
1033 !"One of the last psa_pake_input() calls should have returned the expected error.");
1034 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001035
1036 break;
1037
1038 case 2:
1039 /* Server second round Output */
1040 buffer0_off = 0;
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1043 buffer0 + buffer0_off,
1044 512 - buffer0_off, &s_a_len));
1045 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001046 s_a_off = buffer0_off;
1047 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1049 buffer0 + buffer0_off,
1050 512 - buffer0_off, &s_x2s_pk_len));
1051 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001052 s_x2s_pk_off = buffer0_off;
1053 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1055 buffer0 + buffer0_off,
1056 512 - buffer0_off, &s_x2s_pr_len));
1057 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001058 s_x2s_pr_off = buffer0_off;
1059 buffer0_off += s_x2s_pr_len;
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001062 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001063 expected_status = PSA_ERROR_DATA_INVALID;
1064 }
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1069 buffer0 + s_a_off, s_a_len);
1070 if (inject_error == 3 && status != PSA_SUCCESS) {
1071 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001072 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 } else {
1074 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 }
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1078 buffer0 + s_x2s_pk_off,
1079 s_x2s_pk_len);
1080 if (inject_error == 3 && status != PSA_SUCCESS) {
1081 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001082 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 } else {
1084 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 }
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1088 buffer0 + s_x2s_pr_off,
1089 s_x2s_pr_len);
1090 if (inject_error == 3 && status != PSA_SUCCESS) {
1091 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001092 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 } else {
1094 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001095 }
1096
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001097 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (inject_error == 3) {
1099 TEST_ASSERT(
1100 !"One of the last psa_pake_input() calls should have returned the expected error.");
1101 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001102 }
1103
1104 /* Client second round Output */
1105 buffer1_off = 0;
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1108 buffer1 + buffer1_off,
1109 512 - buffer1_off, &c_a_len));
1110 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001111 c_a_off = buffer1_off;
1112 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1114 buffer1 + buffer1_off,
1115 512 - buffer1_off, &c_x2s_pk_len));
1116 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001117 c_x2s_pk_off = buffer1_off;
1118 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1120 buffer1 + buffer1_off,
1121 512 - buffer1_off, &c_x2s_pr_len));
1122 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001123 c_x2s_pr_off = buffer1_off;
1124 buffer1_off += c_x2s_pr_len;
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001127 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1129 buffer0 + s_a_off, s_a_len);
1130 if (inject_error == 3 && status != PSA_SUCCESS) {
1131 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001132 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else {
1134 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 }
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1138 buffer0 + s_x2s_pk_off,
1139 s_x2s_pk_len);
1140 if (inject_error == 3 && status != PSA_SUCCESS) {
1141 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001142 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 } else {
1144 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 }
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1148 buffer0 + s_x2s_pr_off,
1149 s_x2s_pr_len);
1150 if (inject_error == 3 && status != PSA_SUCCESS) {
1151 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001152 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 } else {
1154 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001155 }
1156
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001157 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (inject_error == 3) {
1159 TEST_ASSERT(
1160 !"One of the last psa_pake_input() calls should have returned the expected error.");
1161 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001165 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001166 expected_status = PSA_ERROR_DATA_INVALID;
1167 }
1168
1169 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1171 buffer1 + c_a_off, c_a_len);
1172 if (inject_error == 4 && status != PSA_SUCCESS) {
1173 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001174 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 } else {
1176 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001177 }
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1180 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1181 if (inject_error == 4 && status != PSA_SUCCESS) {
1182 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001183 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 } else {
1185 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001186 }
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1189 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1190 if (inject_error == 4 && status != PSA_SUCCESS) {
1191 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 } else {
1194 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001195 }
1196
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001197 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (inject_error == 4) {
1199 TEST_ASSERT(
1200 !"One of the last psa_pake_input() calls should have returned the expected error.");
1201 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001202
1203 break;
1204
1205 }
1206
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 mbedtls_free(buffer0);
1209 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001210}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001211#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001214 INJECT_ERR_NONE = 0,
1215 INJECT_ERR_UNINITIALIZED_ACCESS,
1216 INJECT_ERR_DUPLICATE_SETUP,
1217 INJECT_ERR_INVALID_USER,
1218 INJECT_ERR_INVALID_PEER,
1219 INJECT_ERR_SET_USER,
1220 INJECT_ERR_SET_PEER,
1221 INJECT_EMPTY_IO_BUFFER,
1222 INJECT_UNKNOWN_STEP,
1223 INJECT_INVALID_FIRST_STEP,
1224 INJECT_WRONG_BUFFER_SIZE,
1225 INJECT_VALID_OPERATION_AFTER_FAILURE,
1226 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1227 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1228} ecjpake_injected_failure_t;
1229
Paul Elliott01885fa2023-02-09 12:07:30 +00001230#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001231
Paul Elliott6f600372023-02-06 18:41:05 +00001232static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1233 psa_status_t expected_status,
1234 size_t *min_completes,
1235 size_t *max_completes)
1236{
1237
1238 /* This is slightly contrived, but we only really know that with a minimum
1239 value of max_ops that a successful operation should take more than one op
1240 to complete, and likewise that with a max_ops of
1241 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1242 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001243
Paul Elliott6f600372023-02-06 18:41:05 +00001244 if (expected_status == PSA_SUCCESS) {
1245 *min_completes = 2;
1246 } else {
1247 *min_completes = 1;
1248 }
1249
1250 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1251 } else {
1252 *min_completes = 1;
1253 *max_completes = 1;
1254 }
1255}
Paul Elliott01885fa2023-02-09 12:07:30 +00001256#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001257
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001258#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
1259static int rsa_test_e(mbedtls_svc_key_id_t key,
1260 size_t bits,
1261 const data_t *e_arg)
1262{
1263 uint8_t *exported = NULL;
1264 size_t exported_size =
1265 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
1266 size_t exported_length = SIZE_MAX;
1267 int ok = 0;
1268
1269 TEST_CALLOC(exported, exported_size);
1270 PSA_ASSERT(psa_export_public_key(key,
1271 exported, exported_size,
1272 &exported_length));
1273 uint8_t *p = exported;
1274 uint8_t *end = exported + exported_length;
1275 size_t len;
1276 /* RSAPublicKey ::= SEQUENCE {
1277 * modulus INTEGER, -- n
1278 * publicExponent INTEGER } -- e
1279 */
1280 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1281 MBEDTLS_ASN1_SEQUENCE |
1282 MBEDTLS_ASN1_CONSTRUCTED));
1283 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
1284 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1285 MBEDTLS_ASN1_INTEGER));
1286 if (len >= 1 && p[0] == 0) {
1287 ++p;
1288 --len;
1289 }
1290 if (e_arg->len == 0) {
1291 TEST_EQUAL(len, 3);
1292 TEST_EQUAL(p[0], 1);
1293 TEST_EQUAL(p[1], 0);
1294 TEST_EQUAL(p[2], 1);
1295 } else {
Gilles Peskine7a18f962024-02-12 16:48:11 +01001296 const uint8_t *expected = e_arg->x;
1297 size_t expected_len = e_arg->len;
1298 while (expected_len > 0 && *expected == 0) {
1299 ++expected;
1300 --expected_len;
1301 }
1302 TEST_MEMORY_COMPARE(p, len, expected, expected_len);
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001303 }
1304 ok = 1;
1305
1306exit:
1307 mbedtls_free(exported);
1308 return ok;
1309}
1310#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */
1311
Gilles Peskine092ce512024-02-20 12:31:24 +01001312static int setup_key_production_parameters(
1313 psa_key_production_parameters_t **params, size_t *params_data_length,
1314 int flags_arg, const data_t *params_data)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001315{
Gilles Peskine092ce512024-02-20 12:31:24 +01001316 *params_data_length = params_data->len;
Gilles Peskinec81393b2024-02-14 20:51:28 +01001317 /* If there are N bytes of padding at the end of
Gilles Peskine092ce512024-02-20 12:31:24 +01001318 * psa_key_production_parameters_t, then it's enough to allocate
1319 * MIN(sizeof(psa_key_production_parameters_t),
1320 * offsetof(psa_key_production_parameters_t, data) + params_data_length).
Gilles Peskinec81393b2024-02-14 20:51:28 +01001321 *
1322 * For simplicity, here, we allocate up to N more bytes than necessary.
Gilles Peskine092ce512024-02-20 12:31:24 +01001323 * In practice, the current layout of psa_key_production_parameters_t
Gilles Peskinec81393b2024-02-14 20:51:28 +01001324 * makes padding extremely unlikely, so we don't worry about testing
1325 * that the library code doesn't try to access these extra N bytes.
1326 */
Gilles Peskine092ce512024-02-20 12:31:24 +01001327 *params = mbedtls_calloc(1, sizeof(**params) + *params_data_length);
1328 TEST_ASSERT(*params != NULL);
1329 (*params)->flags = (uint32_t) flags_arg;
1330 memcpy((*params)->data, params_data->x, params_data->len);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001331 return 1;
1332exit:
1333 return 0;
1334}
1335
Gilles Peskinee59236f2018-01-27 23:32:46 +01001336/* END_HEADER */
1337
1338/* BEGIN_DEPENDENCIES
1339 * depends_on:MBEDTLS_PSA_CRYPTO_C
1340 * END_DEPENDENCIES
1341 */
1342
1343/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001344void psa_can_do_hash()
1345{
1346 /* We can't test that this is specific to drivers until partial init has
1347 * been implemented, but we can at least test before/after full init. */
1348 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1349 PSA_INIT();
1350 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1351 PSA_DONE();
1352}
1353/* END_CASE */
1354
1355/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001357{
1358 size_t max_truncated_mac_size =
1359 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1360
1361 /* Check that the length for a truncated MAC always fits in the algorithm
1362 * encoding. The shifted mask is the maximum truncated value. The
1363 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369void import_with_policy(int type_arg,
1370 int usage_arg, int alg_arg,
1371 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001372{
1373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1374 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001375 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001376 psa_key_type_t type = type_arg;
1377 psa_key_usage_t usage = usage_arg;
1378 psa_algorithm_t alg = alg_arg;
1379 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001381 psa_status_t status;
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 psa_set_key_type(&attributes, type);
1386 psa_set_key_usage_flags(&attributes, usage);
1387 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 status = psa_import_key(&attributes,
1390 key_material, sizeof(key_material),
1391 &key);
1392 TEST_EQUAL(status, expected_status);
1393 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001394 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1398 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1399 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1400 mbedtls_test_update_key_usage_flags(usage));
1401 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1402 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 PSA_ASSERT(psa_destroy_key(key));
1405 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001406
1407exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001408 /*
1409 * Key attributes may have been returned by psa_get_key_attributes()
1410 * thus reset them as required.
1411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 psa_destroy_key(key);
1415 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001416}
1417/* END_CASE */
1418
1419/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001420void import_with_data(data_t *data, int type_arg,
1421 int attr_bits_arg,
1422 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001423{
1424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1425 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001427 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001428 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001429 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001430 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 psa_set_key_type(&attributes, type);
1435 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001438 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1439 *
1440 * This can happen with a type supported only by a driver:
1441 * - the driver sees the invalid data (for example wrong size) and thinks
1442 * "well perhaps this is a key size I don't support" so it returns
1443 * NOT_SUPPORTED which is correct at this point;
1444 * - we fallback to built-ins, which don't support this type, so return
1445 * NOT_SUPPORTED which again is correct at this point.
1446 */
1447 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1448 status == PSA_ERROR_NOT_SUPPORTED) {
1449 ; // OK
1450 } else {
1451 TEST_EQUAL(status, expected_status);
1452 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001454 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1458 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1459 if (attr_bits != 0) {
1460 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1461 }
1462 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 PSA_ASSERT(psa_destroy_key(key));
1465 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466
1467exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001468 /*
1469 * Key attributes may have been returned by psa_get_key_attributes()
1470 * thus reset them as required.
1471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 psa_destroy_key(key);
1475 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001476}
1477/* END_CASE */
1478
1479/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001480/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481void import_large_key(int type_arg, int byte_size_arg,
1482 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001483{
1484 psa_key_type_t type = type_arg;
1485 size_t byte_size = byte_size_arg;
1486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1487 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001488 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001489 psa_status_t status;
1490 uint8_t *buffer = NULL;
1491 size_t buffer_size = byte_size + 1;
1492 size_t n;
1493
Steven Cooreman69967ce2021-01-18 18:01:08 +01001494 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001495 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001496 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001500
1501 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1503 psa_set_key_type(&attributes, type);
1504 status = psa_import_key(&attributes, buffer, byte_size, &key);
1505 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1506 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (status == PSA_SUCCESS) {
1509 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1510 TEST_EQUAL(psa_get_key_type(&attributes), type);
1511 TEST_EQUAL(psa_get_key_bits(&attributes),
1512 PSA_BYTES_TO_BITS(byte_size));
1513 ASSERT_NO_SLOT_NUMBER(&attributes);
1514 memset(buffer, 0, byte_size + 1);
1515 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1516 for (n = 0; n < byte_size; n++) {
1517 TEST_EQUAL(buffer[n], 'K');
1518 }
1519 for (n = byte_size; n < buffer_size; n++) {
1520 TEST_EQUAL(buffer[n], 0);
1521 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001522 }
1523
1524exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001525 /*
1526 * Key attributes may have been returned by psa_get_key_attributes()
1527 * thus reset them as required.
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 psa_destroy_key(key);
1532 PSA_DONE();
1533 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001534}
1535/* END_CASE */
1536
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001537/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001538/* Import an RSA key with a valid structure (but not valid numbers
1539 * inside, beyond having sensible size and parity). This is expected to
1540 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001542{
Ronald Cron5425a212020-08-04 14:58:35 +02001543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001544 size_t bits = bits_arg;
1545 psa_status_t expected_status = expected_status_arg;
1546 psa_status_t status;
1547 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001548 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001549 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001551 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001552 unsigned char *p;
1553 int ret;
1554 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001558 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1561 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001562 length = ret;
1563
1564 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 psa_set_key_type(&attributes, type);
1566 status = psa_import_key(&attributes, p, length, &key);
1567 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (status == PSA_SUCCESS) {
1570 PSA_ASSERT(psa_destroy_key(key));
1571 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001572
1573exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 mbedtls_free(buffer);
1575 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001576}
1577/* END_CASE */
1578
1579/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580void import_export(data_t *data,
1581 int type_arg,
1582 int usage_arg, int alg_arg,
1583 int lifetime_arg,
1584 int expected_bits,
1585 int export_size_delta,
1586 int expected_export_status_arg,
1587 /*whether reexport must give the original input exactly*/
1588 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001589{
Ronald Cron5425a212020-08-04 14:58:35 +02001590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001592 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001593 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301595 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596 unsigned char *exported = NULL;
1597 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001599 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001600 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001602 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603
Moran Pekercb088e72018-07-17 17:36:59 +03001604 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001605 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001607 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 }
1609 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 psa_set_key_lifetime(&attributes, lifetime);
1612 psa_set_key_usage_flags(&attributes, usage_arg);
1613 psa_set_key_algorithm(&attributes, alg);
1614 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001615
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001616 if (PSA_KEY_TYPE_IS_DH(type) &&
1617 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001618 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1619 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001620 }
1621
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001623 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001624 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625
1626 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1628 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1629 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1630 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631
1632 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 status = psa_export_key(key, exported, export_size, &exported_length);
1634 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001635
1636 /* The exported length must be set by psa_export_key() to a value between 0
1637 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1639 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1640 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1643 export_size - exported_length));
1644 if (status != PSA_SUCCESS) {
1645 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001646 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001647 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001648
Gilles Peskineea38a922021-02-13 00:05:16 +01001649 /* Run sanity checks on the exported key. For non-canonical inputs,
1650 * this validates the canonical representations. For canonical inputs,
1651 * this doesn't directly validate the implementation, but it still helps
1652 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (!psa_key_lifetime_is_external(lifetime)) {
1654 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301655 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 }
Archana4d7ae1d2021-07-07 02:50:22 +05301657 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001660 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001662 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1664 &key2));
1665 PSA_ASSERT(psa_export_key(key2,
1666 reexported,
1667 export_size,
1668 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001669 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001670 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 TEST_LE_U(exported_length,
1674 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1675 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001676 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1677 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1678 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1679 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1680 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681
1682destroy:
1683 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 PSA_ASSERT(psa_destroy_key(key));
1685 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686
1687exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001688 /*
1689 * Key attributes may have been returned by psa_get_key_attributes()
1690 * thus reset them as required.
1691 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 psa_reset_key_attributes(&got_attributes);
1693 psa_destroy_key(key);
1694 mbedtls_free(exported);
1695 mbedtls_free(reexported);
1696 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001697}
1698/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001699
Moran Pekerf709f4a2018-06-06 17:26:04 +03001700/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001701void import_export_public_key(data_t *data,
1702 int type_arg, // key pair or public key
1703 int alg_arg,
1704 int lifetime_arg,
1705 int export_size_delta,
1706 int expected_export_status_arg,
1707 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001708{
Ronald Cron5425a212020-08-04 14:58:35 +02001709 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001710 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001711 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001712 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001713 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301714 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001715 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001716 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001717 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 psa_set_key_lifetime(&attributes, lifetime);
1723 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1724 psa_set_key_algorithm(&attributes, alg);
1725 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001726
1727 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001729
Gilles Peskine49c25912018-10-29 15:15:31 +01001730 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001731 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 status = psa_export_public_key(key,
1733 exported, export_size,
1734 &exported_length);
1735 TEST_EQUAL(status, expected_export_status);
1736 if (status == PSA_SUCCESS) {
1737 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001738 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1740 bits = psa_get_key_bits(&attributes);
1741 TEST_LE_U(expected_public_key->len,
1742 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1743 TEST_LE_U(expected_public_key->len,
1744 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1745 TEST_LE_U(expected_public_key->len,
1746 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001747 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001748 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001749 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001750exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001751 /*
1752 * Key attributes may have been returned by psa_get_key_attributes()
1753 * thus reset them as required.
1754 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 mbedtls_free(exported);
1758 psa_destroy_key(key);
1759 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001760}
1761/* END_CASE */
1762
Gilles Peskine20035e32018-02-03 22:44:14 +01001763/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001764void import_and_exercise_key(data_t *data,
1765 int type_arg,
1766 int bits_arg,
1767 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001768{
Ronald Cron5425a212020-08-04 14:58:35 +02001769 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001770 psa_key_type_t type = type_arg;
1771 size_t bits = bits_arg;
1772 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001775 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 psa_set_key_usage_flags(&attributes, usage);
1780 psa_set_key_algorithm(&attributes, alg);
1781 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001782
1783 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001785
1786 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1788 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1789 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001790
1791 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001793 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 PSA_ASSERT(psa_destroy_key(key));
1797 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001798
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001799exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001800 /*
1801 * Key attributes may have been returned by psa_get_key_attributes()
1802 * thus reset them as required.
1803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 psa_reset_key_attributes(&attributes);
1807 psa_destroy_key(key);
1808 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001809}
1810/* END_CASE */
1811
1812/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001813void effective_key_attributes(int type_arg, int expected_type_arg,
1814 int bits_arg, int expected_bits_arg,
1815 int usage_arg, int expected_usage_arg,
1816 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001817{
Ronald Cron5425a212020-08-04 14:58:35 +02001818 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001819 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001820 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001821 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001822 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001823 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001824 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001825 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001826 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 psa_set_key_usage_flags(&attributes, usage);
1832 psa_set_key_algorithm(&attributes, alg);
1833 psa_set_key_type(&attributes, key_type);
1834 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 PSA_ASSERT(psa_generate_key(&attributes, &key));
1837 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1840 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1841 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1842 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1843 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001844
1845exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001846 /*
1847 * Key attributes may have been returned by psa_get_key_attributes()
1848 * thus reset them as required.
1849 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 psa_destroy_key(key);
1853 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001854}
1855/* END_CASE */
1856
1857/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858void check_key_policy(int type_arg, int bits_arg,
1859 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001860{
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1862 usage_arg,
1863 mbedtls_test_update_key_usage_flags(usage_arg),
1864 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001865 goto exit;
1866}
1867/* END_CASE */
1868
1869/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001870void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001871{
1872 /* Test each valid way of initializing the object, except for `= {0}`, as
1873 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1874 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001875 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001877 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1878 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1883 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1884 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 TEST_EQUAL(psa_get_key_type(&func), 0);
1887 TEST_EQUAL(psa_get_key_type(&init), 0);
1888 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 TEST_EQUAL(psa_get_key_bits(&func), 0);
1891 TEST_EQUAL(psa_get_key_bits(&init), 0);
1892 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1895 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1896 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1899 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1900 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001901}
1902/* END_CASE */
1903
1904/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001905void mac_key_policy(int policy_usage_arg,
1906 int policy_alg_arg,
1907 int key_type_arg,
1908 data_t *key_data,
1909 int exercise_alg_arg,
1910 int expected_status_sign_arg,
1911 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001912{
Ronald Cron5425a212020-08-04 14:58:35 +02001913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001915 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001916 psa_key_type_t key_type = key_type_arg;
1917 psa_algorithm_t policy_alg = policy_alg_arg;
1918 psa_algorithm_t exercise_alg = exercise_alg_arg;
1919 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001921 psa_status_t expected_status_sign = expected_status_sign_arg;
1922 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001923 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 psa_set_key_usage_flags(&attributes, policy_usage);
1928 psa_set_key_algorithm(&attributes, policy_alg);
1929 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1932 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1935 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1938 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001939
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001940 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001942 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1944 input, 128,
1945 mac, PSA_MAC_MAX_SIZE, &mac_len),
1946 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001947
Neil Armstrong3af9b972022-02-07 12:20:21 +01001948 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 PSA_ASSERT(psa_mac_abort(&operation));
1950 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1951 if (status == PSA_SUCCESS) {
1952 status = psa_mac_update(&operation, input, 128);
1953 if (status == PSA_SUCCESS) {
1954 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1955 &mac_len),
1956 expected_status_sign);
1957 } else {
1958 TEST_EQUAL(status, expected_status_sign);
1959 }
1960 } else {
1961 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001962 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001964
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001965 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 status = psa_mac_verify(key, exercise_alg, input, 128,
1967 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1970 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1971 } else {
1972 TEST_EQUAL(status, expected_status_verify);
1973 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001974
Neil Armstrong3af9b972022-02-07 12:20:21 +01001975 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1977 if (status == PSA_SUCCESS) {
1978 status = psa_mac_update(&operation, input, 128);
1979 if (status == PSA_SUCCESS) {
1980 status = psa_mac_verify_finish(&operation, mac, mac_len);
1981 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1982 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1983 } else {
1984 TEST_EQUAL(status, expected_status_verify);
1985 }
1986 } else {
1987 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001988 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 } else {
1990 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001991 }
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 memset(mac, 0, sizeof(mac));
1996 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1997 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998
1999exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 psa_mac_abort(&operation);
2001 psa_destroy_key(key);
2002 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003}
2004/* END_CASE */
2005
2006/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007void cipher_key_policy(int policy_usage_arg,
2008 int policy_alg,
2009 int key_type,
2010 data_t *key_data,
2011 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012{
Ronald Cron5425a212020-08-04 14:58:35 +02002013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002015 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002016 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002017 size_t output_buffer_size = 0;
2018 size_t input_buffer_size = 0;
2019 size_t output_length = 0;
2020 uint8_t *output = NULL;
2021 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022 psa_status_t status;
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
2025 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
2026 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002027
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002028 TEST_CALLOC(input, input_buffer_size);
2029 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 psa_set_key_usage_flags(&attributes, policy_usage);
2034 psa_set_key_algorithm(&attributes, policy_alg);
2035 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2038 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002040 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 TEST_EQUAL(policy_usage,
2042 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002043
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002044 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2046 output, output_buffer_size,
2047 &output_length);
2048 if (policy_alg == exercise_alg &&
2049 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2050 PSA_ASSERT(status);
2051 } else {
2052 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2053 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002054
2055 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2057 if (policy_alg == exercise_alg &&
2058 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2059 PSA_ASSERT(status);
2060 } else {
2061 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2062 }
2063 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002065 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2067 input, input_buffer_size,
2068 &output_length);
2069 if (policy_alg == exercise_alg &&
2070 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2071 PSA_ASSERT(status);
2072 } else {
2073 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2074 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002075
2076 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2078 if (policy_alg == exercise_alg &&
2079 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2080 PSA_ASSERT(status);
2081 } else {
2082 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2083 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
2085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 psa_cipher_abort(&operation);
2087 mbedtls_free(input);
2088 mbedtls_free(output);
2089 psa_destroy_key(key);
2090 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091}
2092/* END_CASE */
2093
2094/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095void aead_key_policy(int policy_usage_arg,
2096 int policy_alg,
2097 int key_type,
2098 data_t *key_data,
2099 int nonce_length_arg,
2100 int tag_length_arg,
2101 int exercise_alg,
2102 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103{
Ronald Cron5425a212020-08-04 14:58:35 +02002104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002106 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002107 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002109 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111 size_t nonce_length = nonce_length_arg;
2112 unsigned char tag[16];
2113 size_t tag_length = tag_length_arg;
2114 size_t output_length;
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 TEST_LE_U(nonce_length, sizeof(nonce));
2117 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 psa_set_key_usage_flags(&attributes, policy_usage);
2122 psa_set_key_algorithm(&attributes, policy_alg);
2123 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2126 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002128 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 TEST_EQUAL(policy_usage,
2130 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002131
Neil Armstrong752d8112022-02-07 14:51:11 +01002132 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 status = psa_aead_encrypt(key, exercise_alg,
2134 nonce, nonce_length,
2135 NULL, 0,
2136 NULL, 0,
2137 tag, tag_length,
2138 &output_length);
2139 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2140 TEST_EQUAL(status, expected_status);
2141 } else {
2142 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2143 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144
Neil Armstrong752d8112022-02-07 14:51:11 +01002145 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2147 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2148 TEST_EQUAL(status, expected_status);
2149 } else {
2150 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2151 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002152
2153 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 memset(tag, 0, sizeof(tag));
2155 status = psa_aead_decrypt(key, exercise_alg,
2156 nonce, nonce_length,
2157 NULL, 0,
2158 tag, tag_length,
2159 NULL, 0,
2160 &output_length);
2161 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2162 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2163 } else if (expected_status == PSA_SUCCESS) {
2164 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2165 } else {
2166 TEST_EQUAL(status, expected_status);
2167 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168
Neil Armstrong752d8112022-02-07 14:51:11 +01002169 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 PSA_ASSERT(psa_aead_abort(&operation));
2171 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2172 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2173 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2174 } else {
2175 TEST_EQUAL(status, expected_status);
2176 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002177
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 PSA_ASSERT(psa_aead_abort(&operation));
2180 psa_destroy_key(key);
2181 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182}
2183/* END_CASE */
2184
2185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186void asymmetric_encryption_key_policy(int policy_usage_arg,
2187 int policy_alg,
2188 int key_type,
2189 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002190 int exercise_alg,
2191 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192{
Ronald Cron5425a212020-08-04 14:58:35 +02002193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002195 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196 psa_status_t status;
2197 size_t key_bits;
2198 size_t buffer_length;
2199 unsigned char *buffer = NULL;
2200 size_t output_length;
2201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 psa_set_key_usage_flags(&attributes, policy_usage);
2205 psa_set_key_algorithm(&attributes, policy_alg);
2206 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Valerio Settif202c292024-01-15 10:42:37 +01002208 if (use_opaque_key) {
2209 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2210 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2211 }
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2214 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002215
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002216 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 TEST_EQUAL(policy_usage,
2218 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2221 key_bits = psa_get_key_bits(&attributes);
2222 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2223 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002224 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 status = psa_asymmetric_encrypt(key, exercise_alg,
2227 NULL, 0,
2228 NULL, 0,
2229 buffer, buffer_length,
2230 &output_length);
2231 if (policy_alg == exercise_alg &&
2232 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 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 if (buffer_length != 0) {
2239 memset(buffer, 0, buffer_length);
2240 }
2241 status = psa_asymmetric_decrypt(key, exercise_alg,
2242 buffer, buffer_length,
2243 NULL, 0,
2244 buffer, buffer_length,
2245 &output_length);
2246 if (policy_alg == exercise_alg &&
2247 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2248 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2249 } else {
2250 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2251 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002252
2253exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002254 /*
2255 * Key attributes may have been returned by psa_get_key_attributes()
2256 * thus reset them as required.
2257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 psa_destroy_key(key);
2261 PSA_DONE();
2262 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002263}
2264/* END_CASE */
2265
2266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002267void asymmetric_signature_key_policy(int policy_usage_arg,
2268 int policy_alg,
2269 int key_type,
2270 data_t *key_data,
2271 int exercise_alg,
2272 int payload_length_arg,
2273 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002274{
Ronald Cron5425a212020-08-04 14:58:35 +02002275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002277 psa_key_usage_t policy_usage = policy_usage_arg;
2278 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002279 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002281 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2282 * compatible with the policy and `payload_length_arg` is supposed to be
2283 * a valid input length to sign. If `payload_length_arg <= 0`,
2284 * `exercise_alg` is supposed to be forbidden by the policy. */
2285 int compatible_alg = payload_length_arg > 0;
2286 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002288 size_t signature_length;
2289
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002290 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002291 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 TEST_EQUAL(expected_usage,
2293 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 psa_set_key_usage_flags(&attributes, policy_usage);
2298 psa_set_key_algorithm(&attributes, policy_alg);
2299 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2302 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 status = psa_sign_hash(key, exercise_alg,
2307 payload, payload_length,
2308 signature, sizeof(signature),
2309 &signature_length);
2310 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2311 PSA_ASSERT(status);
2312 } else {
2313 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2314 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 memset(signature, 0, sizeof(signature));
2317 status = psa_verify_hash(key, exercise_alg,
2318 payload, payload_length,
2319 signature, sizeof(signature));
2320 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2321 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2322 } else {
2323 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2324 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2327 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2328 status = psa_sign_message(key, exercise_alg,
2329 payload, payload_length,
2330 signature, sizeof(signature),
2331 &signature_length);
2332 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2333 PSA_ASSERT(status);
2334 } else {
2335 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2336 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 memset(signature, 0, sizeof(signature));
2339 status = psa_verify_message(key, exercise_alg,
2340 payload, payload_length,
2341 signature, sizeof(signature));
2342 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2343 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2344 } else {
2345 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2346 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002347 }
2348
Gilles Peskined5b33222018-06-18 22:20:03 +02002349exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 psa_destroy_key(key);
2351 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002352}
2353/* END_CASE */
2354
Janos Follathba3fab92019-06-11 14:50:16 +01002355/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002356void derive_key_policy(int policy_usage,
2357 int policy_alg,
2358 int key_type,
2359 data_t *key_data,
2360 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002361{
Ronald Cron5425a212020-08-04 14:58:35 +02002362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002364 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002365 psa_status_t status;
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 psa_set_key_usage_flags(&attributes, policy_usage);
2370 psa_set_key_algorithm(&attributes, policy_alg);
2371 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2374 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2379 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2380 PSA_ASSERT(psa_key_derivation_input_bytes(
2381 &operation,
2382 PSA_KEY_DERIVATION_INPUT_SEED,
2383 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002384 }
Janos Follathba3fab92019-06-11 14:50:16 +01002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 status = psa_key_derivation_input_key(&operation,
2387 PSA_KEY_DERIVATION_INPUT_SECRET,
2388 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (policy_alg == exercise_alg &&
2391 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2392 PSA_ASSERT(status);
2393 } else {
2394 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2395 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002396
2397exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 psa_key_derivation_abort(&operation);
2399 psa_destroy_key(key);
2400 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002401}
2402/* END_CASE */
2403
2404/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002405void agreement_key_policy(int policy_usage,
2406 int policy_alg,
2407 int key_type_arg,
2408 data_t *key_data,
2409 int exercise_alg,
2410 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002411{
Ronald Cron5425a212020-08-04 14:58:35 +02002412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002414 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002415 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002416 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002417 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 psa_set_key_usage_flags(&attributes, policy_usage);
2422 psa_set_key_algorithm(&attributes, policy_alg);
2423 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2426 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2429 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002432
2433exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 psa_key_derivation_abort(&operation);
2435 psa_destroy_key(key);
2436 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002437}
2438/* END_CASE */
2439
2440/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441void key_policy_alg2(int key_type_arg, data_t *key_data,
2442 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002443{
Ronald Cron5425a212020-08-04 14:58:35 +02002444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002445 psa_key_type_t key_type = key_type_arg;
2446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2447 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2448 psa_key_usage_t usage = usage_arg;
2449 psa_algorithm_t alg = alg_arg;
2450 psa_algorithm_t alg2 = alg2_arg;
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 psa_set_key_usage_flags(&attributes, usage);
2455 psa_set_key_algorithm(&attributes, alg);
2456 psa_set_key_enrollment_algorithm(&attributes, alg2);
2457 psa_set_key_type(&attributes, key_type);
2458 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2459 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002460
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002461 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 usage = mbedtls_test_update_key_usage_flags(usage);
2463 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2464 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2465 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2466 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002469 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 }
2471 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002474
2475exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002476 /*
2477 * Key attributes may have been returned by psa_get_key_attributes()
2478 * thus reset them as required.
2479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 psa_destroy_key(key);
2483 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002484}
2485/* END_CASE */
2486
2487/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002488void raw_agreement_key_policy(int policy_usage,
2489 int policy_alg,
2490 int key_type_arg,
2491 data_t *key_data,
2492 int exercise_alg,
2493 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002494{
Ronald Cron5425a212020-08-04 14:58:35 +02002495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002497 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002498 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002499 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002500 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 psa_set_key_usage_flags(&attributes, policy_usage);
2505 psa_set_key_algorithm(&attributes, policy_alg);
2506 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2509 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002514
2515exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 psa_key_derivation_abort(&operation);
2517 psa_destroy_key(key);
2518 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002519}
2520/* END_CASE */
2521
2522/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002523void copy_success(int source_usage_arg,
2524 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002525 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 int type_arg, data_t *material,
2527 int copy_attributes,
2528 int target_usage_arg,
2529 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002530 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 int expected_usage_arg,
2532 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002533{
Gilles Peskineca25db92019-04-19 11:43:08 +02002534 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2535 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002536 psa_key_usage_t expected_usage = expected_usage_arg;
2537 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002538 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302539 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2540 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002541 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2542 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002543 uint8_t *export_buffer = NULL;
2544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002546
Gilles Peskineca25db92019-04-19 11:43:08 +02002547 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2549 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2550 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2551 psa_set_key_type(&source_attributes, type_arg);
2552 psa_set_key_lifetime(&source_attributes, source_lifetime);
2553 PSA_ASSERT(psa_import_key(&source_attributes,
2554 material->x, material->len,
2555 &source_key));
2556 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002557
Gilles Peskineca25db92019-04-19 11:43:08 +02002558 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002560 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002561 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (target_usage_arg != -1) {
2565 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2566 }
2567 if (target_alg_arg != -1) {
2568 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2569 }
2570 if (target_alg2_arg != -1) {
2571 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2572 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002573
Archana8a180362021-07-05 02:18:48 +05302574
Gilles Peskine57ab7212019-01-28 13:03:09 +01002575 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 PSA_ASSERT(psa_copy_key(source_key,
2577 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002578
2579 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002581
2582 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2584 TEST_EQUAL(psa_get_key_type(&source_attributes),
2585 psa_get_key_type(&target_attributes));
2586 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2587 psa_get_key_bits(&target_attributes));
2588 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2589 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2590 TEST_EQUAL(expected_alg2,
2591 psa_get_key_enrollment_algorithm(&target_attributes));
2592 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002593 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002594 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2596 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002597 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002598 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002599 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (!psa_key_lifetime_is_external(target_lifetime)) {
2602 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 }
2605 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 }
Archana8a180362021-07-05 02:18:48 +05302608 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002611
2612exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002613 /*
2614 * Source and target key attributes may have been returned by
2615 * psa_get_key_attributes() thus reset them as required.
2616 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 psa_reset_key_attributes(&source_attributes);
2618 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 PSA_DONE();
2621 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002622}
2623/* END_CASE */
2624
2625/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626void copy_fail(int source_usage_arg,
2627 int source_alg_arg, int source_alg2_arg,
2628 int source_lifetime_arg,
2629 int type_arg, data_t *material,
2630 int target_type_arg, int target_bits_arg,
2631 int target_usage_arg,
2632 int target_alg_arg, int target_alg2_arg,
2633 int target_id_arg, int target_lifetime_arg,
2634 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002635{
2636 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2637 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002638 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2639 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002643
2644 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2646 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2647 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2648 psa_set_key_type(&source_attributes, type_arg);
2649 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2650 PSA_ASSERT(psa_import_key(&source_attributes,
2651 material->x, material->len,
2652 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002653
2654 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 psa_set_key_id(&target_attributes, key_id);
2656 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2657 psa_set_key_type(&target_attributes, target_type_arg);
2658 psa_set_key_bits(&target_attributes, target_bits_arg);
2659 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2660 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2661 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002662
2663 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 TEST_EQUAL(psa_copy_key(source_key,
2665 &target_attributes, &target_key),
2666 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002669
Gilles Peskine4a644642019-05-03 17:14:08 +02002670exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 psa_reset_key_attributes(&source_attributes);
2672 psa_reset_key_attributes(&target_attributes);
2673 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002674}
2675/* END_CASE */
2676
2677/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002678void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002679{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002680 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002681 /* Test each valid way of initializing the object, except for `= {0}`, as
2682 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2683 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002684 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002686 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2687 psa_hash_operation_t zero;
2688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002690
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002691 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2693 PSA_ERROR_BAD_STATE);
2694 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2695 PSA_ERROR_BAD_STATE);
2696 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2697 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002698
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002699 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 PSA_ASSERT(psa_hash_abort(&func));
2701 PSA_ASSERT(psa_hash_abort(&init));
2702 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002703}
2704/* END_CASE */
2705
2706/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002707void hash_setup(int alg_arg,
2708 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002709{
2710 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002711 uint8_t *output = NULL;
2712 size_t output_size = 0;
2713 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002714 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002716 psa_status_t status;
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002719
Neil Armstrongedb20862022-02-07 15:47:44 +01002720 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002722 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 status = psa_hash_compute(alg, NULL, 0,
2725 output, output_size, &output_length);
2726 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002727
2728 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 status = psa_hash_setup(&operation, alg);
2730 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002731
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002732 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002734
2735 /* If setup failed, reproduce the failure, so as to
2736 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (status != PSA_SUCCESS) {
2738 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2739 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002740
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002741 /* Now the operation object should be reusable. */
2742#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2744 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002745#endif
2746
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002747exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 mbedtls_free(output);
2749 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002750}
2751/* END_CASE */
2752
2753/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002754void hash_compute_fail(int alg_arg, data_t *input,
2755 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002756{
2757 psa_algorithm_t alg = alg_arg;
2758 uint8_t *output = NULL;
2759 size_t output_size = output_size_arg;
2760 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002761 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762 psa_status_t expected_status = expected_status_arg;
2763 psa_status_t status;
2764
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002765 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002768
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002769 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 status = psa_hash_compute(alg, input->x, input->len,
2771 output, output_size, &output_length);
2772 TEST_EQUAL(status, expected_status);
2773 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002774
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002775 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 status = psa_hash_setup(&operation, alg);
2777 if (status == PSA_SUCCESS) {
2778 status = psa_hash_update(&operation, input->x, input->len);
2779 if (status == PSA_SUCCESS) {
2780 status = psa_hash_finish(&operation, output, output_size,
2781 &output_length);
2782 if (status == PSA_SUCCESS) {
2783 TEST_LE_U(output_length, output_size);
2784 } else {
2785 TEST_EQUAL(status, expected_status);
2786 }
2787 } else {
2788 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002789 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 } else {
2791 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002792 }
2793
Gilles Peskine0a749c82019-11-28 19:33:58 +01002794exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 PSA_ASSERT(psa_hash_abort(&operation));
2796 mbedtls_free(output);
2797 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002798}
2799/* END_CASE */
2800
2801/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802void hash_compare_fail(int alg_arg, data_t *input,
2803 data_t *reference_hash,
2804 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002805{
2806 psa_algorithm_t alg = alg_arg;
2807 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002808 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002809 psa_status_t status;
2810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002812
Neil Armstrong55a1be12022-02-07 11:23:20 +01002813 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 status = psa_hash_compare(alg, input->x, input->len,
2815 reference_hash->x, reference_hash->len);
2816 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002817
Neil Armstrong55a1be12022-02-07 11:23:20 +01002818 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 status = psa_hash_setup(&operation, alg);
2820 if (status == PSA_SUCCESS) {
2821 status = psa_hash_update(&operation, input->x, input->len);
2822 if (status == PSA_SUCCESS) {
2823 status = psa_hash_verify(&operation, reference_hash->x,
2824 reference_hash->len);
2825 TEST_EQUAL(status, expected_status);
2826 } else {
2827 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 } else {
2830 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002831 }
2832
Gilles Peskine88e08462020-01-28 20:43:00 +01002833exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 PSA_ASSERT(psa_hash_abort(&operation));
2835 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002836}
2837/* END_CASE */
2838
2839/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840void hash_compute_compare(int alg_arg, data_t *input,
2841 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002842{
2843 psa_algorithm_t alg = alg_arg;
2844 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2845 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002846 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002847 size_t i;
2848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002850
Neil Armstrongca30a002022-02-07 11:40:23 +01002851 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2853 output, PSA_HASH_LENGTH(alg),
2854 &output_length));
2855 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002856 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002857 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002858
Neil Armstrongca30a002022-02-07 11:40:23 +01002859 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 PSA_ASSERT(psa_hash_setup(&operation, alg));
2861 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2862 PSA_ASSERT(psa_hash_finish(&operation, output,
2863 PSA_HASH_LENGTH(alg),
2864 &output_length));
2865 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002866 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002867 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002868
2869 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2871 output, sizeof(output),
2872 &output_length));
2873 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002874 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002875 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002876
Neil Armstrongca30a002022-02-07 11:40:23 +01002877 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 PSA_ASSERT(psa_hash_setup(&operation, alg));
2879 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2880 PSA_ASSERT(psa_hash_finish(&operation, output,
2881 sizeof(output), &output_length));
2882 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002883 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002884 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002885
2886 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2888 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002889
Neil Armstrongca30a002022-02-07 11:40:23 +01002890 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 PSA_ASSERT(psa_hash_setup(&operation, alg));
2892 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2893 PSA_ASSERT(psa_hash_verify(&operation, output,
2894 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002895
2896 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2898 output, output_length + 1),
2899 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002900
Neil Armstrongca30a002022-02-07 11:40:23 +01002901 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 PSA_ASSERT(psa_hash_setup(&operation, alg));
2903 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2904 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2905 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002906
2907 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2909 output, output_length - 1),
2910 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002911
Neil Armstrongca30a002022-02-07 11:40:23 +01002912 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 PSA_ASSERT(psa_hash_setup(&operation, alg));
2914 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2915 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2916 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002917
Gilles Peskine0a749c82019-11-28 19:33:58 +01002918 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 for (i = 0; i < output_length; i++) {
2920 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002921 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002922
2923 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2925 output, output_length),
2926 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002927
2928 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 PSA_ASSERT(psa_hash_setup(&operation, alg));
2930 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2931 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2932 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002933
Gilles Peskine0a749c82019-11-28 19:33:58 +01002934 output[i] ^= 1;
2935 }
2936
2937exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 PSA_ASSERT(psa_hash_abort(&operation));
2939 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002940}
2941/* END_CASE */
2942
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002943/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002944void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002945{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002946 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002947 unsigned char input[] = "";
2948 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002949 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002950 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2951 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2953 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002954 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002955 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002956 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002959
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002960 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 PSA_ASSERT(psa_hash_setup(&operation, alg));
2962 ASSERT_OPERATION_IS_ACTIVE(operation);
2963 TEST_EQUAL(psa_hash_setup(&operation, alg),
2964 PSA_ERROR_BAD_STATE);
2965 ASSERT_OPERATION_IS_INACTIVE(operation);
2966 PSA_ASSERT(psa_hash_abort(&operation));
2967 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002968
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002969 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2971 PSA_ERROR_BAD_STATE);
2972 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002973
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002974 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002976 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 ASSERT_OPERATION_IS_ACTIVE(operation);
2978 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2979 PSA_ERROR_BAD_STATE);
2980 ASSERT_OPERATION_IS_INACTIVE(operation);
2981 PSA_ASSERT(psa_hash_abort(&operation));
2982 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002983
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002984 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 PSA_ASSERT(psa_hash_setup(&operation, alg));
2986 PSA_ASSERT(psa_hash_finish(&operation,
2987 hash, sizeof(hash), &hash_len));
2988 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2989 PSA_ERROR_BAD_STATE);
2990 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002991
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002992 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 TEST_EQUAL(psa_hash_verify(&operation,
2994 valid_hash, sizeof(valid_hash)),
2995 PSA_ERROR_BAD_STATE);
2996 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002997
2998 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 PSA_ASSERT(psa_hash_setup(&operation, alg));
3000 PSA_ASSERT(psa_hash_finish(&operation,
3001 hash, sizeof(hash), &hash_len));
3002 TEST_EQUAL(psa_hash_verify(&operation,
3003 valid_hash, sizeof(valid_hash)),
3004 PSA_ERROR_BAD_STATE);
3005 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003006
3007 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 PSA_ASSERT(psa_hash_setup(&operation, alg));
3009 ASSERT_OPERATION_IS_ACTIVE(operation);
3010 PSA_ASSERT(psa_hash_verify(&operation,
3011 valid_hash, sizeof(valid_hash)));
3012 ASSERT_OPERATION_IS_INACTIVE(operation);
3013 TEST_EQUAL(psa_hash_verify(&operation,
3014 valid_hash, sizeof(valid_hash)),
3015 PSA_ERROR_BAD_STATE);
3016 ASSERT_OPERATION_IS_INACTIVE(operation);
3017 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003018
3019 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 TEST_EQUAL(psa_hash_finish(&operation,
3021 hash, sizeof(hash), &hash_len),
3022 PSA_ERROR_BAD_STATE);
3023 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003024
3025 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 PSA_ASSERT(psa_hash_setup(&operation, alg));
3027 PSA_ASSERT(psa_hash_finish(&operation,
3028 hash, sizeof(hash), &hash_len));
3029 TEST_EQUAL(psa_hash_finish(&operation,
3030 hash, sizeof(hash), &hash_len),
3031 PSA_ERROR_BAD_STATE);
3032 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003033
3034 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 PSA_ASSERT(psa_hash_setup(&operation, alg));
3036 PSA_ASSERT(psa_hash_verify(&operation,
3037 valid_hash, sizeof(valid_hash)));
3038 TEST_EQUAL(psa_hash_finish(&operation,
3039 hash, sizeof(hash), &hash_len),
3040 PSA_ERROR_BAD_STATE);
3041 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003042
3043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003045}
3046/* END_CASE */
3047
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003048/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003050{
3051 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003052 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3053 * appended to it */
3054 unsigned char hash[] = {
3055 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3056 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3058 };
3059 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003060 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003063
itayzafrir27e69452018-11-01 14:26:34 +02003064 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 PSA_ASSERT(psa_hash_setup(&operation, alg));
3066 ASSERT_OPERATION_IS_ACTIVE(operation);
3067 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3068 PSA_ERROR_INVALID_SIGNATURE);
3069 ASSERT_OPERATION_IS_INACTIVE(operation);
3070 PSA_ASSERT(psa_hash_abort(&operation));
3071 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003072
itayzafrir27e69452018-11-01 14:26:34 +02003073 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 PSA_ASSERT(psa_hash_setup(&operation, alg));
3075 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3076 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003077
itayzafrir27e69452018-11-01 14:26:34 +02003078 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 PSA_ASSERT(psa_hash_setup(&operation, alg));
3080 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3081 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003082
itayzafrirec93d302018-10-18 18:01:10 +03003083exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003085}
3086/* END_CASE */
3087
Ronald Cronee414c72021-03-18 18:50:08 +01003088/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003089void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003090{
3091 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003092 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003094 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003095 size_t hash_len;
3096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003098
itayzafrir58028322018-10-25 10:22:01 +03003099 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 PSA_ASSERT(psa_hash_setup(&operation, alg));
3101 TEST_EQUAL(psa_hash_finish(&operation,
3102 hash, expected_size - 1, &hash_len),
3103 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003104
3105exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003107}
3108/* END_CASE */
3109
Ronald Cronee414c72021-03-18 18:50:08 +01003110/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003112{
3113 psa_algorithm_t alg = PSA_ALG_SHA_256;
3114 unsigned char hash[PSA_HASH_MAX_SIZE];
3115 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3116 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3117 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3118 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3119 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3120 size_t hash_len;
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 PSA_ASSERT(psa_crypto_init());
3123 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3126 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3127 PSA_ASSERT(psa_hash_finish(&op_finished,
3128 hash, sizeof(hash), &hash_len));
3129 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3130 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3133 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3136 PSA_ASSERT(psa_hash_finish(&op_init,
3137 hash, sizeof(hash), &hash_len));
3138 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3139 PSA_ASSERT(psa_hash_finish(&op_finished,
3140 hash, sizeof(hash), &hash_len));
3141 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3142 PSA_ASSERT(psa_hash_finish(&op_aborted,
3143 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003144
3145exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 psa_hash_abort(&op_source);
3147 psa_hash_abort(&op_init);
3148 psa_hash_abort(&op_setup);
3149 psa_hash_abort(&op_finished);
3150 psa_hash_abort(&op_aborted);
3151 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003152}
3153/* END_CASE */
3154
Ronald Cronee414c72021-03-18 18:50:08 +01003155/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003156void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003157{
3158 psa_algorithm_t alg = PSA_ALG_SHA_256;
3159 unsigned char hash[PSA_HASH_MAX_SIZE];
3160 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3161 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3162 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3163 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3164 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3165 size_t hash_len;
3166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3170 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3171 PSA_ASSERT(psa_hash_finish(&op_finished,
3172 hash, sizeof(hash), &hash_len));
3173 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3174 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003175
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3177 PSA_ASSERT(psa_hash_finish(&op_target,
3178 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3181 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3182 PSA_ERROR_BAD_STATE);
3183 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3184 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003185
3186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 psa_hash_abort(&op_target);
3188 psa_hash_abort(&op_init);
3189 psa_hash_abort(&op_setup);
3190 psa_hash_abort(&op_finished);
3191 psa_hash_abort(&op_aborted);
3192 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003193}
3194/* END_CASE */
3195
itayzafrir58028322018-10-25 10:22:01 +03003196/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003197void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003198{
Jaeden Amero252ef282019-02-15 14:05:35 +00003199 const uint8_t input[1] = { 0 };
3200
Jaeden Amero769ce272019-01-04 11:48:03 +00003201 /* Test each valid way of initializing the object, except for `= {0}`, as
3202 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3203 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003204 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003206 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3207 psa_mac_operation_t zero;
3208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003210
Jaeden Amero252ef282019-02-15 14:05:35 +00003211 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 TEST_EQUAL(psa_mac_update(&func,
3213 input, sizeof(input)),
3214 PSA_ERROR_BAD_STATE);
3215 TEST_EQUAL(psa_mac_update(&init,
3216 input, sizeof(input)),
3217 PSA_ERROR_BAD_STATE);
3218 TEST_EQUAL(psa_mac_update(&zero,
3219 input, sizeof(input)),
3220 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003221
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003222 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 PSA_ASSERT(psa_mac_abort(&func));
3224 PSA_ASSERT(psa_mac_abort(&init));
3225 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003226}
3227/* END_CASE */
3228
3229/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003230void mac_setup(int key_type_arg,
3231 data_t *key,
3232 int alg_arg,
3233 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003234{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235 psa_key_type_t key_type = key_type_arg;
3236 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003237 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003238 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003239 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3240#if defined(KNOWN_SUPPORTED_MAC_ALG)
3241 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3242#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3247 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 }
3250 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003251
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003252 /* The operation object should be reusable. */
3253#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3255 smoke_test_key_data,
3256 sizeof(smoke_test_key_data),
3257 KNOWN_SUPPORTED_MAC_ALG,
3258 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 }
3261 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003262#endif
3263
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003264exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003266}
3267/* END_CASE */
3268
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003269/* 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 +01003270void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003271{
Ronald Cron5425a212020-08-04 14:58:35 +02003272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003273 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3274 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003275 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003276 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3277 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3279 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003281 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3282 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3283 size_t sign_mac_length = 0;
3284 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3285 const uint8_t verify_mac[] = {
3286 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3287 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3289 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003290
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 PSA_ASSERT(psa_crypto_init());
3292 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3293 psa_set_key_algorithm(&attributes, alg);
3294 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3297 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298
Jaeden Amero252ef282019-02-15 14:05:35 +00003299 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3301 PSA_ERROR_BAD_STATE);
3302 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003303
3304 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3306 &sign_mac_length),
3307 PSA_ERROR_BAD_STATE);
3308 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003309
3310 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 TEST_EQUAL(psa_mac_verify_finish(&operation,
3312 verify_mac, sizeof(verify_mac)),
3313 PSA_ERROR_BAD_STATE);
3314 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003315
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003316 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3318 ASSERT_OPERATION_IS_ACTIVE(operation);
3319 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3320 PSA_ERROR_BAD_STATE);
3321 ASSERT_OPERATION_IS_INACTIVE(operation);
3322 PSA_ASSERT(psa_mac_abort(&operation));
3323 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003324
Jaeden Amero252ef282019-02-15 14:05:35 +00003325 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3327 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3328 PSA_ASSERT(psa_mac_sign_finish(&operation,
3329 sign_mac, sizeof(sign_mac),
3330 &sign_mac_length));
3331 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3332 PSA_ERROR_BAD_STATE);
3333 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003334
3335 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3337 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3338 PSA_ASSERT(psa_mac_verify_finish(&operation,
3339 verify_mac, sizeof(verify_mac)));
3340 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3341 PSA_ERROR_BAD_STATE);
3342 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003343
3344 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3346 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3347 PSA_ASSERT(psa_mac_sign_finish(&operation,
3348 sign_mac, sizeof(sign_mac),
3349 &sign_mac_length));
3350 TEST_EQUAL(psa_mac_sign_finish(&operation,
3351 sign_mac, sizeof(sign_mac),
3352 &sign_mac_length),
3353 PSA_ERROR_BAD_STATE);
3354 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003355
3356 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3358 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3359 PSA_ASSERT(psa_mac_verify_finish(&operation,
3360 verify_mac, sizeof(verify_mac)));
3361 TEST_EQUAL(psa_mac_verify_finish(&operation,
3362 verify_mac, sizeof(verify_mac)),
3363 PSA_ERROR_BAD_STATE);
3364 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003365
3366 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3368 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3369 ASSERT_OPERATION_IS_ACTIVE(operation);
3370 TEST_EQUAL(psa_mac_verify_finish(&operation,
3371 verify_mac, sizeof(verify_mac)),
3372 PSA_ERROR_BAD_STATE);
3373 ASSERT_OPERATION_IS_INACTIVE(operation);
3374 PSA_ASSERT(psa_mac_abort(&operation));
3375 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003376
3377 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3379 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3380 ASSERT_OPERATION_IS_ACTIVE(operation);
3381 TEST_EQUAL(psa_mac_sign_finish(&operation,
3382 sign_mac, sizeof(sign_mac),
3383 &sign_mac_length),
3384 PSA_ERROR_BAD_STATE);
3385 ASSERT_OPERATION_IS_INACTIVE(operation);
3386 PSA_ASSERT(psa_mac_abort(&operation));
3387 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003390
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003391exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003393}
3394/* END_CASE */
3395
3396/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003397void mac_sign_verify_multi(int key_type_arg,
3398 data_t *key_data,
3399 int alg_arg,
3400 data_t *input,
3401 int is_verify,
3402 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003403{
3404 size_t data_part_len = 0;
3405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003407 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 if (mac_multipart_internal_func(key_type_arg, key_data,
3411 alg_arg,
3412 input, data_part_len,
3413 expected_mac,
3414 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003415 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003417
3418 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003420
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 if (mac_multipart_internal_func(key_type_arg, key_data,
3422 alg_arg,
3423 input, data_part_len,
3424 expected_mac,
3425 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003426 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003428 }
3429
3430 /* Goto is required to silence warnings about unused labels, as we
3431 * don't actually do any test assertions in this function. */
3432 goto exit;
3433}
3434/* END_CASE */
3435
3436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003437void mac_sign(int key_type_arg,
3438 data_t *key_data,
3439 int alg_arg,
3440 data_t *input,
3441 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003442{
Ronald Cron5425a212020-08-04 14:58:35 +02003443 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003444 psa_key_type_t key_type = key_type_arg;
3445 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003446 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003448 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003449 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003451 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003452 const size_t output_sizes_to_test[] = {
3453 0,
3454 1,
3455 expected_mac->len - 1,
3456 expected_mac->len,
3457 expected_mac->len + 1,
3458 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003459
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003461 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3467 psa_set_key_algorithm(&attributes, alg);
3468 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003469
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3471 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003472
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003474 const size_t output_size = output_sizes_to_test[i];
3475 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 (output_size >= expected_mac->len ? PSA_SUCCESS :
3477 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003478
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003480 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003481
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003482 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 TEST_EQUAL(psa_mac_compute(key, alg,
3484 input->x, input->len,
3485 actual_mac, output_size, &mac_length),
3486 expected_status);
3487 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003488 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003489 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003490 }
3491
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 if (output_size > 0) {
3493 memset(actual_mac, 0, output_size);
3494 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003495
3496 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3498 PSA_ASSERT(psa_mac_update(&operation,
3499 input->x, input->len));
3500 TEST_EQUAL(psa_mac_sign_finish(&operation,
3501 actual_mac, output_size,
3502 &mac_length),
3503 expected_status);
3504 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003507 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003508 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003509 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003511 actual_mac = NULL;
3512 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003513
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 psa_mac_abort(&operation);
3516 psa_destroy_key(key);
3517 PSA_DONE();
3518 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003519}
3520/* END_CASE */
3521
3522/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003523void mac_verify(int key_type_arg,
3524 data_t *key_data,
3525 int alg_arg,
3526 data_t *input,
3527 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003528{
Ronald Cron5425a212020-08-04 14:58:35 +02003529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003530 psa_key_type_t key_type = key_type_arg;
3531 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003532 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003534 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003537
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3541 psa_set_key_algorithm(&attributes, alg);
3542 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3545 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003546
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003547 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3549 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003550
3551 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3553 PSA_ASSERT(psa_mac_update(&operation,
3554 input->x, input->len));
3555 PSA_ASSERT(psa_mac_verify_finish(&operation,
3556 expected_mac->x,
3557 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003558
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003559 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 TEST_EQUAL(psa_mac_verify(key, alg,
3561 input->x, input->len,
3562 expected_mac->x,
3563 expected_mac->len - 1),
3564 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003565
3566 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3568 PSA_ASSERT(psa_mac_update(&operation,
3569 input->x, input->len));
3570 TEST_EQUAL(psa_mac_verify_finish(&operation,
3571 expected_mac->x,
3572 expected_mac->len - 1),
3573 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003574
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003575 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003576 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3578 TEST_EQUAL(psa_mac_verify(key, alg,
3579 input->x, input->len,
3580 perturbed_mac, expected_mac->len + 1),
3581 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003582
3583 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3585 PSA_ASSERT(psa_mac_update(&operation,
3586 input->x, input->len));
3587 TEST_EQUAL(psa_mac_verify_finish(&operation,
3588 perturbed_mac,
3589 expected_mac->len + 1),
3590 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003591
3592 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 for (size_t i = 0; i < expected_mac->len; i++) {
3594 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003595 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 TEST_EQUAL(psa_mac_verify(key, alg,
3598 input->x, input->len,
3599 perturbed_mac, expected_mac->len),
3600 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003601
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3603 PSA_ASSERT(psa_mac_update(&operation,
3604 input->x, input->len));
3605 TEST_EQUAL(psa_mac_verify_finish(&operation,
3606 perturbed_mac,
3607 expected_mac->len),
3608 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003609 perturbed_mac[i] ^= 1;
3610 }
3611
Gilles Peskine8c9def32018-02-08 10:02:12 +01003612exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003613 psa_mac_abort(&operation);
3614 psa_destroy_key(key);
3615 PSA_DONE();
3616 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003617}
3618/* END_CASE */
3619
3620/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003621void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003622{
Jaeden Ameroab439972019-02-15 14:12:05 +00003623 const uint8_t input[1] = { 0 };
3624 unsigned char output[1] = { 0 };
3625 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003626 /* Test each valid way of initializing the object, except for `= {0}`, as
3627 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3628 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003629 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003631 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3632 psa_cipher_operation_t zero;
3633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003635
Jaeden Ameroab439972019-02-15 14:12:05 +00003636 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003637 TEST_EQUAL(psa_cipher_update(&func,
3638 input, sizeof(input),
3639 output, sizeof(output),
3640 &output_length),
3641 PSA_ERROR_BAD_STATE);
3642 TEST_EQUAL(psa_cipher_update(&init,
3643 input, sizeof(input),
3644 output, sizeof(output),
3645 &output_length),
3646 PSA_ERROR_BAD_STATE);
3647 TEST_EQUAL(psa_cipher_update(&zero,
3648 input, sizeof(input),
3649 output, sizeof(output),
3650 &output_length),
3651 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003652
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003653 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 PSA_ASSERT(psa_cipher_abort(&func));
3655 PSA_ASSERT(psa_cipher_abort(&init));
3656 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003657}
3658/* END_CASE */
3659
3660/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003661void cipher_setup(int key_type_arg,
3662 data_t *key,
3663 int alg_arg,
3664 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003665{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003666 psa_key_type_t key_type = key_type_arg;
3667 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003668 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003669 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003670 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003671#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003672 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3673#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003674
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003676
Gilles Peskine449bd832023-01-11 14:50:10 +01003677 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3678 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003679 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003680 }
3681 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003682
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003683 /* The operation object should be reusable. */
3684#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003685 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3686 smoke_test_key_data,
3687 sizeof(smoke_test_key_data),
3688 KNOWN_SUPPORTED_CIPHER_ALG,
3689 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 }
3692 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003693#endif
3694
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003696 psa_cipher_abort(&operation);
3697 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003698}
3699/* END_CASE */
3700
Ronald Cronee414c72021-03-18 18:50:08 +01003701/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003702void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003703{
Ronald Cron5425a212020-08-04 14:58:35 +02003704 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003705 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3706 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003708 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003709 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003710 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003711 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003712 0xaa, 0xaa, 0xaa, 0xaa
3713 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003714 const uint8_t text[] = {
3715 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003716 0xbb, 0xbb, 0xbb, 0xbb
3717 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003718 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003719 size_t length = 0;
3720
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 PSA_ASSERT(psa_crypto_init());
3722 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3723 psa_set_key_algorithm(&attributes, alg);
3724 psa_set_key_type(&attributes, key_type);
3725 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3726 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003727
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003728 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3730 ASSERT_OPERATION_IS_ACTIVE(operation);
3731 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3732 PSA_ERROR_BAD_STATE);
3733 ASSERT_OPERATION_IS_INACTIVE(operation);
3734 PSA_ASSERT(psa_cipher_abort(&operation));
3735 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003736
3737 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003738 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3739 ASSERT_OPERATION_IS_ACTIVE(operation);
3740 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3741 PSA_ERROR_BAD_STATE);
3742 ASSERT_OPERATION_IS_INACTIVE(operation);
3743 PSA_ASSERT(psa_cipher_abort(&operation));
3744 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003745
Jaeden Ameroab439972019-02-15 14:12:05 +00003746 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003747 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3748 buffer, sizeof(buffer),
3749 &length),
3750 PSA_ERROR_BAD_STATE);
3751 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003752
3753 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3755 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3756 buffer, sizeof(buffer),
3757 &length));
3758 ASSERT_OPERATION_IS_ACTIVE(operation);
3759 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3760 buffer, sizeof(buffer),
3761 &length),
3762 PSA_ERROR_BAD_STATE);
3763 ASSERT_OPERATION_IS_INACTIVE(operation);
3764 PSA_ASSERT(psa_cipher_abort(&operation));
3765 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003766
3767 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003768 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3769 PSA_ASSERT(psa_cipher_set_iv(&operation,
3770 iv, sizeof(iv)));
3771 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3772 buffer, sizeof(buffer),
3773 &length),
3774 PSA_ERROR_BAD_STATE);
3775 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003776
3777 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003778 TEST_EQUAL(psa_cipher_set_iv(&operation,
3779 iv, sizeof(iv)),
3780 PSA_ERROR_BAD_STATE);
3781 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003782
3783 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3785 PSA_ASSERT(psa_cipher_set_iv(&operation,
3786 iv, sizeof(iv)));
3787 ASSERT_OPERATION_IS_ACTIVE(operation);
3788 TEST_EQUAL(psa_cipher_set_iv(&operation,
3789 iv, sizeof(iv)),
3790 PSA_ERROR_BAD_STATE);
3791 ASSERT_OPERATION_IS_INACTIVE(operation);
3792 PSA_ASSERT(psa_cipher_abort(&operation));
3793 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003794
3795 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003796 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3797 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3798 buffer, sizeof(buffer),
3799 &length));
3800 TEST_EQUAL(psa_cipher_set_iv(&operation,
3801 iv, sizeof(iv)),
3802 PSA_ERROR_BAD_STATE);
3803 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003804
3805 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003806 TEST_EQUAL(psa_cipher_update(&operation,
3807 text, sizeof(text),
3808 buffer, sizeof(buffer),
3809 &length),
3810 PSA_ERROR_BAD_STATE);
3811 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003812
3813 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003814 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3815 ASSERT_OPERATION_IS_ACTIVE(operation);
3816 TEST_EQUAL(psa_cipher_update(&operation,
3817 text, sizeof(text),
3818 buffer, sizeof(buffer),
3819 &length),
3820 PSA_ERROR_BAD_STATE);
3821 ASSERT_OPERATION_IS_INACTIVE(operation);
3822 PSA_ASSERT(psa_cipher_abort(&operation));
3823 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003824
3825 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3827 PSA_ASSERT(psa_cipher_set_iv(&operation,
3828 iv, sizeof(iv)));
3829 PSA_ASSERT(psa_cipher_finish(&operation,
3830 buffer, sizeof(buffer), &length));
3831 TEST_EQUAL(psa_cipher_update(&operation,
3832 text, sizeof(text),
3833 buffer, sizeof(buffer),
3834 &length),
3835 PSA_ERROR_BAD_STATE);
3836 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003837
3838 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 TEST_EQUAL(psa_cipher_finish(&operation,
3840 buffer, sizeof(buffer), &length),
3841 PSA_ERROR_BAD_STATE);
3842 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003843
3844 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003846 /* Not calling update means we are encrypting an empty buffer, which is OK
3847 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 ASSERT_OPERATION_IS_ACTIVE(operation);
3849 TEST_EQUAL(psa_cipher_finish(&operation,
3850 buffer, sizeof(buffer), &length),
3851 PSA_ERROR_BAD_STATE);
3852 ASSERT_OPERATION_IS_INACTIVE(operation);
3853 PSA_ASSERT(psa_cipher_abort(&operation));
3854 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003855
3856 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3858 PSA_ASSERT(psa_cipher_set_iv(&operation,
3859 iv, sizeof(iv)));
3860 PSA_ASSERT(psa_cipher_finish(&operation,
3861 buffer, sizeof(buffer), &length));
3862 TEST_EQUAL(psa_cipher_finish(&operation,
3863 buffer, sizeof(buffer), &length),
3864 PSA_ERROR_BAD_STATE);
3865 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003866
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003868
Jaeden Ameroab439972019-02-15 14:12:05 +00003869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 psa_cipher_abort(&operation);
3871 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003872}
3873/* END_CASE */
3874
3875/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003876void cipher_encrypt_fail(int alg_arg,
3877 int key_type_arg,
3878 data_t *key_data,
3879 data_t *input,
3880 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003881{
Ronald Cron5425a212020-08-04 14:58:35 +02003882 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003883 psa_status_t status;
3884 psa_key_type_t key_type = key_type_arg;
3885 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003886 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003888 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3889 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003890 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003891 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003892 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003893 size_t function_output_length;
3894 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003895 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3896
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 if (PSA_ERROR_BAD_STATE != expected_status) {
3898 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003899
Gilles Peskine449bd832023-01-11 14:50:10 +01003900 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3901 psa_set_key_algorithm(&attributes, alg);
3902 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3905 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003906 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003907
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3909 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003910 }
3911
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003912 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3914 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915
Gilles Peskine449bd832023-01-11 14:50:10 +01003916 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003917
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003918 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 status = psa_cipher_encrypt_setup(&operation, key, alg);
3920 if (status == PSA_SUCCESS) {
3921 if (alg != PSA_ALG_ECB_NO_PADDING) {
3922 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3923 iv, iv_size,
3924 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003925 }
3926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 status = psa_cipher_update(&operation, input->x, input->len,
3928 output, output_buffer_size,
3929 &function_output_length);
3930 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003931 output_length += function_output_length;
3932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 status = psa_cipher_finish(&operation, output + output_length,
3934 output_buffer_size - output_length,
3935 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003936
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 TEST_EQUAL(status, expected_status);
3938 } else {
3939 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003940 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 } else {
3942 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003943 }
3944
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003945exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 psa_cipher_abort(&operation);
3947 mbedtls_free(output);
3948 psa_destroy_key(key);
3949 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003950}
3951/* END_CASE */
3952
3953/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003954void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3955 data_t *input, int iv_length,
3956 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003957{
3958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3959 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3961 size_t output_buffer_size = 0;
3962 unsigned char *output = NULL;
3963
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003965 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003966
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003968
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3970 psa_set_key_algorithm(&attributes, alg);
3971 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003972
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3974 &key));
3975 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3976 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3977 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003978
3979exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 psa_cipher_abort(&operation);
3981 mbedtls_free(output);
3982 psa_destroy_key(key);
3983 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003984}
3985/* END_CASE */
3986
3987/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003988void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3989 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003990{
3991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3992 psa_key_type_t key_type = key_type_arg;
3993 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003994 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3995 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003996 unsigned char *output = NULL;
3997 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003998 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004002
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004003 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 TEST_LE_U(ciphertext->len,
4005 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
4006 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
4007 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
4008 TEST_LE_U(plaintext->len,
4009 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
4010 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
4011 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004012
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004013
4014 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 psa_set_key_usage_flags(&attributes,
4016 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4017 psa_set_key_algorithm(&attributes, alg);
4018 psa_set_key_type(&attributes, key_type);
4019 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4020 &key));
4021 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4022 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004023 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004024
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004025 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4027 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4028 PSA_ERROR_BAD_STATE);
4029 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4030 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4031 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004032
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004033 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4035 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4036 &length),
4037 PSA_ERROR_BAD_STATE);
4038 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4039 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4040 &length),
4041 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004042
Gilles Peskine286c3142022-04-20 17:09:38 +02004043 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004045 output_length = 0;
4046 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 PSA_ASSERT(psa_cipher_update(&operation,
4048 plaintext->x, plaintext->len,
4049 output, output_buffer_size,
4050 &length));
4051 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004052 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 PSA_ASSERT(psa_cipher_finish(&operation,
4054 mbedtls_buffer_offset(output, output_length),
4055 output_buffer_size - output_length,
4056 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004057 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004058 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004059 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004060
Gilles Peskine286c3142022-04-20 17:09:38 +02004061 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004063 output_length = 0;
4064 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004065 PSA_ASSERT(psa_cipher_update(&operation,
4066 ciphertext->x, ciphertext->len,
4067 output, output_buffer_size,
4068 &length));
4069 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004070 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 PSA_ASSERT(psa_cipher_finish(&operation,
4072 mbedtls_buffer_offset(output, output_length),
4073 output_buffer_size - output_length,
4074 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004075 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004076 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004077 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004078
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004079 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004080 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4082 output, output_buffer_size,
4083 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004084 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004085 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004086
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004087 /* One-shot decryption */
4088 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4090 output, output_buffer_size,
4091 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004092 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004093 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004094
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 PSA_ASSERT(psa_cipher_abort(&operation));
4097 mbedtls_free(output);
4098 psa_cipher_abort(&operation);
4099 psa_destroy_key(key);
4100 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004101}
4102/* END_CASE */
4103
4104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004105void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004106{
4107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4108 psa_algorithm_t alg = alg_arg;
4109 psa_key_type_t key_type = key_type_arg;
4110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4111 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4112 psa_status_t status;
4113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4117 psa_set_key_algorithm(&attributes, alg);
4118 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004119
4120 /* Usage of either of these two size macros would cause divide by zero
4121 * with incorrect key types previously. Input length should be irrelevant
4122 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4124 0);
4125 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004126
4127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4129 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004130
4131 /* Should fail due to invalid alg type (to support invalid key type).
4132 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004136
4137exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 psa_cipher_abort(&operation);
4139 psa_destroy_key(key);
4140 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004141}
4142/* END_CASE */
4143
4144/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004145void cipher_encrypt_validation(int alg_arg,
4146 int key_type_arg,
4147 data_t *key_data,
4148 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004149{
4150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4151 psa_key_type_t key_type = key_type_arg;
4152 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004154 unsigned char *output1 = NULL;
4155 size_t output1_buffer_size = 0;
4156 size_t output1_length = 0;
4157 unsigned char *output2 = NULL;
4158 size_t output2_buffer_size = 0;
4159 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004161 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4167 psa_set_key_algorithm(&attributes, alg);
4168 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4171 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4172 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004173 TEST_CALLOC(output1, output1_buffer_size);
4174 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004175
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
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004179 /* The one-shot cipher encryption uses generated iv so validating
4180 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4182 output1_buffer_size, &output1_length));
4183 TEST_LE_U(output1_length,
4184 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4185 TEST_LE_U(output1_length,
4186 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4189 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 PSA_ASSERT(psa_cipher_update(&operation,
4192 input->x, input->len,
4193 output2, output2_buffer_size,
4194 &function_output_length));
4195 TEST_LE_U(function_output_length,
4196 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4197 TEST_LE_U(function_output_length,
4198 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004199 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 PSA_ASSERT(psa_cipher_finish(&operation,
4202 output2 + output2_length,
4203 output2_buffer_size - output2_length,
4204 &function_output_length));
4205 TEST_LE_U(function_output_length,
4206 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4207 TEST_LE_U(function_output_length,
4208 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004209 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004212 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004213 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004214
Gilles Peskine50e586b2018-06-08 14:28:46 +02004215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 psa_cipher_abort(&operation);
4217 mbedtls_free(output1);
4218 mbedtls_free(output2);
4219 psa_destroy_key(key);
4220 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004221}
4222/* END_CASE */
4223
4224/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004225void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4226 data_t *key_data, data_t *iv,
4227 data_t *input,
4228 int first_part_size_arg,
4229 int output1_length_arg, int output2_length_arg,
4230 data_t *expected_output,
4231 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004232{
Ronald Cron5425a212020-08-04 14:58:35 +02004233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234 psa_key_type_t key_type = key_type_arg;
4235 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004236 psa_status_t status;
4237 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004238 size_t first_part_size = first_part_size_arg;
4239 size_t output1_length = output1_length_arg;
4240 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004241 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004242 size_t output_buffer_size = 0;
4243 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004244 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004245 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4251 psa_set_key_algorithm(&attributes, alg);
4252 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4255 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 if (iv->len > 0) {
4260 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004261 }
4262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4264 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004265 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 TEST_LE_U(first_part_size, input->len);
4268 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4269 output, output_buffer_size,
4270 &function_output_length));
4271 TEST_ASSERT(function_output_length == output1_length);
4272 TEST_LE_U(function_output_length,
4273 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4274 TEST_LE_U(function_output_length,
4275 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004276 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (first_part_size < input->len) {
4279 PSA_ASSERT(psa_cipher_update(&operation,
4280 input->x + first_part_size,
4281 input->len - first_part_size,
4282 (output_buffer_size == 0 ? NULL :
4283 output + total_output_length),
4284 output_buffer_size - total_output_length,
4285 &function_output_length));
4286 TEST_ASSERT(function_output_length == output2_length);
4287 TEST_LE_U(function_output_length,
4288 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4289 alg,
4290 input->len - first_part_size));
4291 TEST_LE_U(function_output_length,
4292 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004293 total_output_length += function_output_length;
4294 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 status = psa_cipher_finish(&operation,
4297 (output_buffer_size == 0 ? NULL :
4298 output + total_output_length),
4299 output_buffer_size - total_output_length,
4300 &function_output_length);
4301 TEST_LE_U(function_output_length,
4302 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4303 TEST_LE_U(function_output_length,
4304 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004305 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 if (expected_status == PSA_SUCCESS) {
4309 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004310
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004311 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004312 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004313 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004314
4315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 psa_cipher_abort(&operation);
4317 mbedtls_free(output);
4318 psa_destroy_key(key);
4319 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320}
4321/* END_CASE */
4322
4323/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004324void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4325 data_t *key_data, data_t *iv,
4326 data_t *input,
4327 int first_part_size_arg,
4328 int output1_length_arg, int output2_length_arg,
4329 data_t *expected_output,
4330 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004331{
Ronald Cron5425a212020-08-04 14:58:35 +02004332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004333 psa_key_type_t key_type = key_type_arg;
4334 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004335 psa_status_t status;
4336 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004337 size_t first_part_size = first_part_size_arg;
4338 size_t output1_length = output1_length_arg;
4339 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004340 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004341 size_t output_buffer_size = 0;
4342 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004343 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004344 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004346
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4350 psa_set_key_algorithm(&attributes, alg);
4351 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4354 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 if (iv->len > 0) {
4359 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004360 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4363 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004364 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 TEST_LE_U(first_part_size, input->len);
4367 PSA_ASSERT(psa_cipher_update(&operation,
4368 input->x, first_part_size,
4369 output, output_buffer_size,
4370 &function_output_length));
4371 TEST_ASSERT(function_output_length == output1_length);
4372 TEST_LE_U(function_output_length,
4373 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4374 TEST_LE_U(function_output_length,
4375 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004376 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (first_part_size < input->len) {
4379 PSA_ASSERT(psa_cipher_update(&operation,
4380 input->x + first_part_size,
4381 input->len - first_part_size,
4382 (output_buffer_size == 0 ? NULL :
4383 output + total_output_length),
4384 output_buffer_size - total_output_length,
4385 &function_output_length));
4386 TEST_ASSERT(function_output_length == output2_length);
4387 TEST_LE_U(function_output_length,
4388 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4389 alg,
4390 input->len - first_part_size));
4391 TEST_LE_U(function_output_length,
4392 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004393 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004394 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 status = psa_cipher_finish(&operation,
4397 (output_buffer_size == 0 ? NULL :
4398 output + total_output_length),
4399 output_buffer_size - total_output_length,
4400 &function_output_length);
4401 TEST_LE_U(function_output_length,
4402 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4403 TEST_LE_U(function_output_length,
4404 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004405 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 if (expected_status == PSA_SUCCESS) {
4409 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004410
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004411 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004412 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004413 }
4414
Gilles Peskine50e586b2018-06-08 14:28:46 +02004415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 psa_cipher_abort(&operation);
4417 mbedtls_free(output);
4418 psa_destroy_key(key);
4419 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004420}
4421/* END_CASE */
4422
Gilles Peskine50e586b2018-06-08 14:28:46 +02004423/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004424void cipher_decrypt_fail(int alg_arg,
4425 int key_type_arg,
4426 data_t *key_data,
4427 data_t *iv,
4428 data_t *input_arg,
4429 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004430{
4431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4432 psa_status_t status;
4433 psa_key_type_t key_type = key_type_arg;
4434 psa_algorithm_t alg = alg_arg;
4435 psa_status_t expected_status = expected_status_arg;
4436 unsigned char *input = NULL;
4437 size_t input_buffer_size = 0;
4438 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004439 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004440 size_t output_buffer_size = 0;
4441 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004442 size_t function_output_length;
4443 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (PSA_ERROR_BAD_STATE != expected_status) {
4447 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004448
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4450 psa_set_key_algorithm(&attributes, alg);
4451 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4454 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004455 }
4456
4457 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4459 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004460 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 memcpy(input, iv->x, iv->len);
4462 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004463 }
4464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004466 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004467
Neil Armstrong66a479f2022-02-07 15:41:19 +01004468 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4470 output_buffer_size, &output_length);
4471 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004472
Neil Armstrong66a479f2022-02-07 15:41:19 +01004473 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 status = psa_cipher_decrypt_setup(&operation, key, alg);
4475 if (status == PSA_SUCCESS) {
4476 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4477 input_arg->len) +
4478 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004479 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 if (iv->len > 0) {
4482 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 if (status != PSA_SUCCESS) {
4485 TEST_EQUAL(status, expected_status);
4486 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004487 }
4488
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 if (status == PSA_SUCCESS) {
4490 status = psa_cipher_update(&operation,
4491 input_arg->x, input_arg->len,
4492 output_multi, output_buffer_size,
4493 &function_output_length);
4494 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004495 output_length = function_output_length;
4496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 status = psa_cipher_finish(&operation,
4498 output_multi + output_length,
4499 output_buffer_size - output_length,
4500 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004501
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 TEST_EQUAL(status, expected_status);
4503 } else {
4504 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004505 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 } else {
4507 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004508 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 } else {
4510 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004511 }
4512
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 psa_cipher_abort(&operation);
4515 mbedtls_free(input);
4516 mbedtls_free(output);
4517 mbedtls_free(output_multi);
4518 psa_destroy_key(key);
4519 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004520}
4521/* END_CASE */
4522
4523/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004524void cipher_decrypt(int alg_arg,
4525 int key_type_arg,
4526 data_t *key_data,
4527 data_t *iv,
4528 data_t *input_arg,
4529 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004530{
4531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4532 psa_key_type_t key_type = key_type_arg;
4533 psa_algorithm_t alg = alg_arg;
4534 unsigned char *input = NULL;
4535 size_t input_buffer_size = 0;
4536 unsigned char *output = NULL;
4537 size_t output_buffer_size = 0;
4538 size_t output_length = 0;
4539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4544 psa_set_key_algorithm(&attributes, alg);
4545 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004546
4547 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4549 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004550 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 memcpy(input, iv->x, iv->len);
4552 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004553 }
4554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004556 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4559 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4562 output_buffer_size, &output_length));
4563 TEST_LE_U(output_length,
4564 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4565 TEST_LE_U(output_length,
4566 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004567
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004568 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004569 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 mbedtls_free(input);
4572 mbedtls_free(output);
4573 psa_destroy_key(key);
4574 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004575}
4576/* END_CASE */
4577
4578/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004579void cipher_verify_output(int alg_arg,
4580 int key_type_arg,
4581 data_t *key_data,
4582 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004583{
Ronald Cron5425a212020-08-04 14:58:35 +02004584 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004585 psa_key_type_t key_type = key_type_arg;
4586 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004587 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004588 size_t output1_size = 0;
4589 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004590 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004591 size_t output2_size = 0;
4592 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4598 psa_set_key_algorithm(&attributes, alg);
4599 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4602 &key));
4603 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004604 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4607 output1, output1_size,
4608 &output1_length));
4609 TEST_LE_U(output1_length,
4610 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4611 TEST_LE_U(output1_length,
4612 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004613
4614 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004615 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4618 output2, output2_size,
4619 &output2_length));
4620 TEST_LE_U(output2_length,
4621 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4622 TEST_LE_U(output2_length,
4623 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004624
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004625 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004626
4627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 mbedtls_free(output1);
4629 mbedtls_free(output2);
4630 psa_destroy_key(key);
4631 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004632}
4633/* END_CASE */
4634
4635/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004636void cipher_verify_output_multipart(int alg_arg,
4637 int key_type_arg,
4638 data_t *key_data,
4639 data_t *input,
4640 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004641{
Ronald Cron5425a212020-08-04 14:58:35 +02004642 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004643 psa_key_type_t key_type = key_type_arg;
4644 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004645 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004647 size_t iv_size = 16;
4648 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004649 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004650 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004651 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004652 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004653 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004654 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004655 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004656 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4657 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4663 psa_set_key_algorithm(&attributes, alg);
4664 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4667 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4670 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if (alg != PSA_ALG_ECB_NO_PADDING) {
4673 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4674 iv, iv_size,
4675 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004676 }
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4679 TEST_LE_U(output1_buffer_size,
4680 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004681 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4686 output1, output1_buffer_size,
4687 &function_output_length));
4688 TEST_LE_U(function_output_length,
4689 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4690 TEST_LE_U(function_output_length,
4691 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004692 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 PSA_ASSERT(psa_cipher_update(&operation1,
4695 input->x + first_part_size,
4696 input->len - first_part_size,
4697 output1, output1_buffer_size,
4698 &function_output_length));
4699 TEST_LE_U(function_output_length,
4700 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4701 alg,
4702 input->len - first_part_size));
4703 TEST_LE_U(function_output_length,
4704 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004705 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 PSA_ASSERT(psa_cipher_finish(&operation1,
4708 output1 + output1_length,
4709 output1_buffer_size - output1_length,
4710 &function_output_length));
4711 TEST_LE_U(function_output_length,
4712 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4713 TEST_LE_U(function_output_length,
4714 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004715 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004718
Gilles Peskine048b7f02018-06-08 14:20:49 +02004719 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 TEST_LE_U(output2_buffer_size,
4721 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4722 TEST_LE_U(output2_buffer_size,
4723 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004724 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (iv_length > 0) {
4727 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4728 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004729 }
Moran Pekerded84402018-06-06 16:36:50 +03004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4732 output2, output2_buffer_size,
4733 &function_output_length));
4734 TEST_LE_U(function_output_length,
4735 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4736 TEST_LE_U(function_output_length,
4737 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004738 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 PSA_ASSERT(psa_cipher_update(&operation2,
4741 output1 + first_part_size,
4742 output1_length - first_part_size,
4743 output2, output2_buffer_size,
4744 &function_output_length));
4745 TEST_LE_U(function_output_length,
4746 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4747 alg,
4748 output1_length - first_part_size));
4749 TEST_LE_U(function_output_length,
4750 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004751 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 PSA_ASSERT(psa_cipher_finish(&operation2,
4754 output2 + output2_length,
4755 output2_buffer_size - output2_length,
4756 &function_output_length));
4757 TEST_LE_U(function_output_length,
4758 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4759 TEST_LE_U(function_output_length,
4760 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004761 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004764
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004765 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004766
4767exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 psa_cipher_abort(&operation1);
4769 psa_cipher_abort(&operation2);
4770 mbedtls_free(output1);
4771 mbedtls_free(output2);
4772 psa_destroy_key(key);
4773 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004774}
4775/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004776
Gilles Peskine20035e32018-02-03 22:44:14 +01004777/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004778void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4779 int alg_arg,
4780 data_t *nonce,
4781 data_t *additional_data,
4782 data_t *input_data,
4783 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004784{
Ronald Cron5425a212020-08-04 14:58:35 +02004785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786 psa_key_type_t key_type = key_type_arg;
4787 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004788 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789 unsigned char *output_data = NULL;
4790 size_t output_size = 0;
4791 size_t output_length = 0;
4792 unsigned char *output_data2 = NULL;
4793 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004794 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004795 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4801 psa_set_key_algorithm(&attributes, alg);
4802 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4805 &key));
4806 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4807 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4810 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004811 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4812 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4814 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4815 TEST_EQUAL(output_size,
4816 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4817 TEST_LE_U(output_size,
4818 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004819 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004820 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 status = psa_aead_encrypt(key, alg,
4823 nonce->x, nonce->len,
4824 additional_data->x,
4825 additional_data->len,
4826 input_data->x, input_data->len,
4827 output_data, output_size,
4828 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004829
4830 /* If the operation is not supported, just skip and not fail in case the
4831 * encryption involves a common limitation of cryptography hardwares and
4832 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (status == PSA_ERROR_NOT_SUPPORTED) {
4834 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4835 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004836 }
4837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004841 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842
Gilles Peskine003a4a92019-05-14 16:09:40 +02004843 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4844 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 TEST_EQUAL(input_data->len,
4846 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 TEST_LE_U(input_data->len,
4849 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 TEST_EQUAL(psa_aead_decrypt(key, alg,
4852 nonce->x, nonce->len,
4853 additional_data->x,
4854 additional_data->len,
4855 output_data, output_length,
4856 output_data2, output_length,
4857 &output_length2),
4858 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004859
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004860 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004861 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004862 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004863
Gilles Peskinea1cac842018-06-11 19:33:02 +02004864exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 psa_destroy_key(key);
4866 mbedtls_free(output_data);
4867 mbedtls_free(output_data2);
4868 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869}
4870/* END_CASE */
4871
4872/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004873void aead_encrypt(int key_type_arg, data_t *key_data,
4874 int alg_arg,
4875 data_t *nonce,
4876 data_t *additional_data,
4877 data_t *input_data,
4878 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004879{
Ronald Cron5425a212020-08-04 14:58:35 +02004880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004881 psa_key_type_t key_type = key_type_arg;
4882 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004883 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884 unsigned char *output_data = NULL;
4885 size_t output_size = 0;
4886 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004888 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4893 psa_set_key_algorithm(&attributes, alg);
4894 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004895
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4897 &key));
4898 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4899 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4902 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004903 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4904 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 TEST_EQUAL(output_size,
4906 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4907 TEST_LE_U(output_size,
4908 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004909 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 status = psa_aead_encrypt(key, alg,
4912 nonce->x, nonce->len,
4913 additional_data->x, additional_data->len,
4914 input_data->x, input_data->len,
4915 output_data, output_size,
4916 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004917
Ronald Cron28a45ed2021-02-09 20:35:42 +01004918 /* If the operation is not supported, just skip and not fail in case the
4919 * encryption involves a common limitation of cryptography hardwares and
4920 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (status == PSA_ERROR_NOT_SUPPORTED) {
4922 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4923 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004924 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004927 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004928 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004929
Gilles Peskinea1cac842018-06-11 19:33:02 +02004930exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 psa_destroy_key(key);
4932 mbedtls_free(output_data);
4933 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004934}
4935/* END_CASE */
4936
4937/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004938void aead_decrypt(int key_type_arg, data_t *key_data,
4939 int alg_arg,
4940 data_t *nonce,
4941 data_t *additional_data,
4942 data_t *input_data,
4943 data_t *expected_data,
4944 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004945{
Ronald Cron5425a212020-08-04 14:58:35 +02004946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004947 psa_key_type_t key_type = key_type_arg;
4948 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004949 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004950 unsigned char *output_data = NULL;
4951 size_t output_size = 0;
4952 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004954 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004955 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004956
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4960 psa_set_key_algorithm(&attributes, alg);
4961 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4964 &key));
4965 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4966 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4969 alg);
4970 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4971 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004972 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4973 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 TEST_EQUAL(output_size,
4975 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4976 TEST_LE_U(output_size,
4977 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004978 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004979 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 status = psa_aead_decrypt(key, alg,
4982 nonce->x, nonce->len,
4983 additional_data->x,
4984 additional_data->len,
4985 input_data->x, input_data->len,
4986 output_data, output_size,
4987 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004988
Ronald Cron28a45ed2021-02-09 20:35:42 +01004989 /* If the operation is not supported, just skip and not fail in case the
4990 * decryption involves a common limitation of cryptography hardwares and
4991 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 if (status == PSA_ERROR_NOT_SUPPORTED) {
4993 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4994 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004995 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005000 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005001 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02005003
Gilles Peskinea1cac842018-06-11 19:33:02 +02005004exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 psa_destroy_key(key);
5006 mbedtls_free(output_data);
5007 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005008}
5009/* END_CASE */
5010
5011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005012void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
5013 int alg_arg,
5014 data_t *nonce,
5015 data_t *additional_data,
5016 data_t *input_data,
5017 int do_set_lengths,
5018 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005019{
Paul Elliottd3f82412021-06-16 16:52:21 +01005020 size_t ad_part_len = 0;
5021 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005022 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
5025 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 if (do_set_lengths) {
5028 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005029 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005033 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005034
5035 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (!aead_multipart_internal_func(key_type_arg, key_data,
5037 alg_arg, nonce,
5038 additional_data,
5039 ad_part_len,
5040 input_data, -1,
5041 set_lengths_method,
5042 expected_output,
5043 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005045 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 /* length(0) part, length(ad_part_len) part, length(0) part... */
5048 mbedtls_test_set_step(1000 + ad_part_len);
5049
5050 if (!aead_multipart_internal_func(key_type_arg, key_data,
5051 alg_arg, nonce,
5052 additional_data,
5053 ad_part_len,
5054 input_data, -1,
5055 set_lengths_method,
5056 expected_output,
5057 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 }
5060 }
5061
5062 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5063 /* Split data into length(data_part_len) parts. */
5064 mbedtls_test_set_step(2000 + data_part_len);
5065
5066 if (do_set_lengths) {
5067 if (data_part_len & 0x01) {
5068 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5069 } else {
5070 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5071 }
5072 }
5073
5074 if (!aead_multipart_internal_func(key_type_arg, key_data,
5075 alg_arg, nonce,
5076 additional_data, -1,
5077 input_data, data_part_len,
5078 set_lengths_method,
5079 expected_output,
5080 1, 0)) {
5081 break;
5082 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005083
5084 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 if (!aead_multipart_internal_func(key_type_arg, key_data,
5088 alg_arg, nonce,
5089 additional_data, -1,
5090 input_data, data_part_len,
5091 set_lengths_method,
5092 expected_output,
5093 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005094 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005096 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005097
Paul Elliott8fc45162021-06-23 16:06:01 +01005098 /* Goto is required to silence warnings about unused labels, as we
5099 * don't actually do any test assertions in this function. */
5100 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005101}
5102/* END_CASE */
5103
5104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005105void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5106 int alg_arg,
5107 data_t *nonce,
5108 data_t *additional_data,
5109 data_t *input_data,
5110 int do_set_lengths,
5111 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005112{
Paul Elliottd3f82412021-06-16 16:52:21 +01005113 size_t ad_part_len = 0;
5114 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005115 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005118 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005120
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 if (do_set_lengths) {
5122 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005123 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005125 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005127 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (!aead_multipart_internal_func(key_type_arg, key_data,
5130 alg_arg, nonce,
5131 additional_data,
5132 ad_part_len,
5133 input_data, -1,
5134 set_lengths_method,
5135 expected_output,
5136 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005137 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005139
5140 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 if (!aead_multipart_internal_func(key_type_arg, key_data,
5144 alg_arg, nonce,
5145 additional_data,
5146 ad_part_len,
5147 input_data, -1,
5148 set_lengths_method,
5149 expected_output,
5150 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005151 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005153 }
5154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005156 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (do_set_lengths) {
5160 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005161 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005163 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005165 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 if (!aead_multipart_internal_func(key_type_arg, key_data,
5168 alg_arg, nonce,
5169 additional_data, -1,
5170 input_data, data_part_len,
5171 set_lengths_method,
5172 expected_output,
5173 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005174 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005176
5177 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (!aead_multipart_internal_func(key_type_arg, key_data,
5181 alg_arg, nonce,
5182 additional_data, -1,
5183 input_data, data_part_len,
5184 set_lengths_method,
5185 expected_output,
5186 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005187 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005189 }
5190
Paul Elliott8fc45162021-06-23 16:06:01 +01005191 /* Goto is required to silence warnings about unused labels, as we
5192 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005193 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005194}
5195/* END_CASE */
5196
5197/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5199 int alg_arg,
5200 int nonce_length,
5201 int expected_nonce_length_arg,
5202 data_t *additional_data,
5203 data_t *input_data,
5204 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005205{
5206
5207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5208 psa_key_type_t key_type = key_type_arg;
5209 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005210 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005211 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5213 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005214 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005215 size_t actual_nonce_length = 0;
5216 size_t expected_nonce_length = expected_nonce_length_arg;
5217 unsigned char *output = NULL;
5218 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005219 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005220 size_t ciphertext_size = 0;
5221 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005222 size_t tag_length = 0;
5223 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5228 psa_set_key_algorithm(&attributes, alg);
5229 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5232 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005237
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005238 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005239
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005243
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005244 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005245
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005247
5248 /* If the operation is not supported, just skip and not fail in case the
5249 * encryption involves a common limitation of cryptography hardwares and
5250 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 if (status == PSA_ERROR_NOT_SUPPORTED) {
5252 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5253 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005254 }
5255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5259 nonce_length,
5260 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 if (expected_status == PSA_SUCCESS) {
5267 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5268 alg));
5269 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005274 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5276 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5279 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5282 output, output_size,
5283 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5286 &ciphertext_length, tag_buffer,
5287 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005288 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005289
5290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 psa_destroy_key(key);
5292 mbedtls_free(output);
5293 mbedtls_free(ciphertext);
5294 psa_aead_abort(&operation);
5295 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005296}
5297/* END_CASE */
5298
5299/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005300void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5301 int alg_arg,
5302 int nonce_length_arg,
5303 int set_lengths_method_arg,
5304 data_t *additional_data,
5305 data_t *input_data,
5306 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005307{
5308
5309 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5310 psa_key_type_t key_type = key_type_arg;
5311 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005312 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005313 uint8_t *nonce_buffer = NULL;
5314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5315 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5316 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005317 unsigned char *output = NULL;
5318 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005319 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005320 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005321 size_t ciphertext_size = 0;
5322 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005323 size_t tag_length = 0;
5324 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005325 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005326 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005329
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5331 psa_set_key_algorithm(&attributes, alg);
5332 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5335 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005338
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005340
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005341 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005342
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005346
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005347 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005350
5351 /* If the operation is not supported, just skip and not fail in case the
5352 * encryption involves a common limitation of cryptography hardwares and
5353 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 if (status == PSA_ERROR_NOT_SUPPORTED) {
5355 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5356 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005357 }
5358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005360
Paul Elliott4023ffd2021-09-10 16:21:22 +01005361 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 if (nonce_length_arg == -1) {
5363 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005364 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 nonce_length = 0;
5366 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005367 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005369 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if (nonce_buffer) {
5372 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005373 nonce_buffer[index] = 'a' + index;
5374 }
Paul Elliott66696b52021-08-16 18:42:41 +01005375 }
Paul Elliott863864a2021-07-23 17:28:31 +01005376 }
5377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5379 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5380 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005381 }
5382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if (expected_status == PSA_SUCCESS) {
5388 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5389 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5390 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005391 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005393 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 }
Paul Elliott863864a2021-07-23 17:28:31 +01005395
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005396 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5398 additional_data->len),
5399 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5402 output, output_size,
5403 &ciphertext_length),
5404 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5407 &ciphertext_length, tag_buffer,
5408 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5409 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005410 }
5411
5412exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 psa_destroy_key(key);
5414 mbedtls_free(output);
5415 mbedtls_free(ciphertext);
5416 mbedtls_free(nonce_buffer);
5417 psa_aead_abort(&operation);
5418 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005419}
5420/* END_CASE */
5421
5422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005423void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005424 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005425 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005426 data_t *nonce,
5427 data_t *additional_data,
5428 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005430{
5431
5432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5433 psa_key_type_t key_type = key_type_arg;
5434 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005435 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5437 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5438 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005439 unsigned char *output = NULL;
5440 unsigned char *ciphertext = NULL;
5441 size_t output_size = output_size_arg;
5442 size_t ciphertext_size = 0;
5443 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005444 size_t tag_length = 0;
5445 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5450 psa_set_key_algorithm(&attributes, alg);
5451 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5454 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005457
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005458 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005461
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005462 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005465
5466 /* If the operation is not supported, just skip and not fail in case the
5467 * encryption involves a common limitation of cryptography hardwares and
5468 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (status == PSA_ERROR_NOT_SUPPORTED) {
5470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5471 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005472 }
5473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5477 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5482 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 status = psa_aead_update(&operation, input_data->x, input_data->len,
5485 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005488
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005490 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5492 &ciphertext_length, tag_buffer,
5493 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005494 }
5495
5496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 psa_destroy_key(key);
5498 mbedtls_free(output);
5499 mbedtls_free(ciphertext);
5500 psa_aead_abort(&operation);
5501 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005502}
5503/* END_CASE */
5504
Paul Elliott91b021e2021-07-23 18:52:31 +01005505/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005506void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5507 int alg_arg,
5508 int finish_ciphertext_size_arg,
5509 int tag_size_arg,
5510 data_t *nonce,
5511 data_t *additional_data,
5512 data_t *input_data,
5513 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005514{
5515
5516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5517 psa_key_type_t key_type = key_type_arg;
5518 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005519 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5521 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5522 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005523 unsigned char *ciphertext = NULL;
5524 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005525 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005526 size_t ciphertext_size = 0;
5527 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5529 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005530 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005531
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5535 psa_set_key_algorithm(&attributes, alg);
5536 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5539 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005544
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005545 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005546
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005547 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005548
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005549 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005552
5553 /* If the operation is not supported, just skip and not fail in case the
5554 * encryption involves a common limitation of cryptography hardwares and
5555 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 if (status == PSA_ERROR_NOT_SUPPORTED) {
5557 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5558 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005559 }
5560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5566 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5569 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5572 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005573
5574 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 status = psa_aead_finish(&operation, finish_ciphertext,
5576 finish_ciphertext_size,
5577 &ciphertext_length, tag_buffer,
5578 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005581
5582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 psa_destroy_key(key);
5584 mbedtls_free(ciphertext);
5585 mbedtls_free(finish_ciphertext);
5586 mbedtls_free(tag_buffer);
5587 psa_aead_abort(&operation);
5588 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005589}
5590/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005591
5592/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005593void aead_multipart_verify(int key_type_arg, data_t *key_data,
5594 int alg_arg,
5595 data_t *nonce,
5596 data_t *additional_data,
5597 data_t *input_data,
5598 data_t *tag,
5599 int tag_usage_arg,
5600 int expected_setup_status_arg,
5601 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005602{
5603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5604 psa_key_type_t key_type = key_type_arg;
5605 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005606 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5608 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5609 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005610 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005611 unsigned char *plaintext = NULL;
5612 unsigned char *finish_plaintext = NULL;
5613 size_t plaintext_size = 0;
5614 size_t plaintext_length = 0;
5615 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005616 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005617 unsigned char *tag_buffer = NULL;
5618 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5623 psa_set_key_algorithm(&attributes, alg);
5624 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005625
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5627 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5632 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005633
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005634 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005637
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005638 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005641
5642 /* If the operation is not supported, just skip and not fail in case the
5643 * encryption involves a common limitation of cryptography hardwares and
5644 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 if (status == PSA_ERROR_NOT_SUPPORTED) {
5646 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5647 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005652 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 }
Paul Elliott9961a662021-09-17 19:19:02 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 status = psa_aead_set_lengths(&operation, additional_data->len,
5660 input_data->len);
5661 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5664 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5667 input_data->len,
5668 plaintext, plaintext_size,
5669 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005672 tag_buffer = tag->x;
5673 tag_size = tag->len;
5674 }
5675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 status = psa_aead_verify(&operation, finish_plaintext,
5677 verify_plaintext_size,
5678 &plaintext_length,
5679 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005682
5683exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 psa_destroy_key(key);
5685 mbedtls_free(plaintext);
5686 mbedtls_free(finish_plaintext);
5687 psa_aead_abort(&operation);
5688 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005689}
5690/* END_CASE */
5691
Paul Elliott9961a662021-09-17 19:19:02 +01005692/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005693void aead_multipart_setup(int key_type_arg, data_t *key_data,
5694 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005695{
5696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5697 psa_key_type_t key_type = key_type_arg;
5698 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005699 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5701 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5702 psa_status_t expected_status = expected_status_arg;
5703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 psa_set_key_usage_flags(&attributes,
5707 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5708 psa_set_key_algorithm(&attributes, alg);
5709 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5712 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005723
5724exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 psa_destroy_key(key);
5726 psa_aead_abort(&operation);
5727 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005728}
5729/* END_CASE */
5730
5731/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005732void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5733 int alg_arg,
5734 data_t *nonce,
5735 data_t *additional_data,
5736 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737{
5738 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5739 psa_key_type_t key_type = key_type_arg;
5740 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005741 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005742 unsigned char *output_data = NULL;
5743 unsigned char *final_data = NULL;
5744 size_t output_size = 0;
5745 size_t finish_output_size = 0;
5746 size_t output_length = 0;
5747 size_t key_bits = 0;
5748 size_t tag_length = 0;
5749 size_t tag_size = 0;
5750 size_t nonce_length = 0;
5751 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5752 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5753 size_t output_part_length = 0;
5754 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 psa_set_key_usage_flags(&attributes,
5759 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5760 psa_set_key_algorithm(&attributes, alg);
5761 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005762
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5764 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5767 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005775 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005781 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005782
5783 /* Test all operations error without calling setup first. */
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5786 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5791 PSA_AEAD_NONCE_MAX_SIZE,
5792 &nonce_length),
5793 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005796
Paul Elliott481be342021-07-16 17:38:47 +01005797 /* ------------------------------------------------------- */
5798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5800 input_data->len),
5801 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005804
Paul Elliott481be342021-07-16 17:38:47 +01005805 /* ------------------------------------------------------- */
5806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5808 additional_data->len),
5809 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005812
Paul Elliott481be342021-07-16 17:38:47 +01005813 /* ------------------------------------------------------- */
5814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5816 input_data->len, output_data,
5817 output_size, &output_length),
5818 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005821
Paul Elliott481be342021-07-16 17:38:47 +01005822 /* ------------------------------------------------------- */
5823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5825 finish_output_size,
5826 &output_part_length,
5827 tag_buffer, tag_length,
5828 &tag_size),
5829 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005832
Paul Elliott481be342021-07-16 17:38:47 +01005833 /* ------------------------------------------------------- */
5834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5836 finish_output_size,
5837 &output_part_length,
5838 tag_buffer,
5839 tag_length),
5840 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843
5844 /* Test for double setups. */
5845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5849 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005852
Paul Elliott481be342021-07-16 17:38:47 +01005853 /* ------------------------------------------------------- */
5854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5858 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005861
Paul Elliott374a2be2021-07-16 17:53:40 +01005862 /* ------------------------------------------------------- */
5863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5867 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005870
5871 /* ------------------------------------------------------- */
5872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5876 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005879
Paul Elliottc23a9a02021-06-21 18:32:46 +01005880 /* Test for not setting a nonce. */
5881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5885 additional_data->len),
5886 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005889
Paul Elliott7f628422021-09-01 12:08:29 +01005890 /* ------------------------------------------------------- */
5891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5895 input_data->len, output_data,
5896 output_size, &output_length),
5897 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005900
Paul Elliottbdc2c682021-09-21 18:37:10 +01005901 /* ------------------------------------------------------- */
5902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5906 finish_output_size,
5907 &output_part_length,
5908 tag_buffer, tag_length,
5909 &tag_size),
5910 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005913
5914 /* ------------------------------------------------------- */
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5919 finish_output_size,
5920 &output_part_length,
5921 tag_buffer,
5922 tag_length),
5923 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005926
Paul Elliottc23a9a02021-06-21 18:32:46 +01005927 /* Test for double setting nonce. */
5928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5934 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005937
Paul Elliott374a2be2021-07-16 17:53:40 +01005938 /* Test for double generating nonce. */
5939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5943 PSA_AEAD_NONCE_MAX_SIZE,
5944 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005945
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5947 PSA_AEAD_NONCE_MAX_SIZE,
5948 &nonce_length),
5949 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005950
5951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005953
5954 /* Test for generate nonce then set and vice versa */
5955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5959 PSA_AEAD_NONCE_MAX_SIZE,
5960 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5963 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005966
Andrzej Kurekad837522021-12-15 15:28:49 +01005967 /* Test for generating nonce after calling set lengths */
5968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5972 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5975 PSA_AEAD_NONCE_MAX_SIZE,
5976 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005979
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005980 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (operation.alg == PSA_ALG_CCM) {
5985 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5986 input_data->len),
5987 PSA_ERROR_INVALID_ARGUMENT);
5988 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5989 PSA_AEAD_NONCE_MAX_SIZE,
5990 &nonce_length),
5991 PSA_ERROR_BAD_STATE);
5992 } else {
5993 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5994 input_data->len));
5995 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5996 PSA_AEAD_NONCE_MAX_SIZE,
5997 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005998 }
5999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006001
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006002 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00006003#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6007 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6008 input_data->len),
6009 PSA_ERROR_INVALID_ARGUMENT);
6010 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6011 PSA_AEAD_NONCE_MAX_SIZE,
6012 &nonce_length),
6013 PSA_ERROR_BAD_STATE);
6014 } else {
6015 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6016 input_data->len));
6017 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6018 PSA_AEAD_NONCE_MAX_SIZE,
6019 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006020 }
6021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00006023#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01006024
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006025 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6030 PSA_AEAD_NONCE_MAX_SIZE,
6031 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (operation.alg == PSA_ALG_CCM) {
6034 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6035 input_data->len),
6036 PSA_ERROR_INVALID_ARGUMENT);
6037 } else {
6038 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6039 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006040 }
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006043
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006044 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006045 /* Test for setting nonce after calling set lengths */
6046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6050 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006055
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006056 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 if (operation.alg == PSA_ALG_CCM) {
6061 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6062 input_data->len),
6063 PSA_ERROR_INVALID_ARGUMENT);
6064 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6065 PSA_ERROR_BAD_STATE);
6066 } else {
6067 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6068 input_data->len));
6069 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006070 }
6071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006073
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006074 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006075#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6079 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6080 input_data->len),
6081 PSA_ERROR_INVALID_ARGUMENT);
6082 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6083 PSA_ERROR_BAD_STATE);
6084 } else {
6085 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6086 input_data->len));
6087 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006088 }
6089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006091#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006092
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006093 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (operation.alg == PSA_ALG_CCM) {
6100 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6101 input_data->len),
6102 PSA_ERROR_INVALID_ARGUMENT);
6103 } else {
6104 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6105 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006106 }
6107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006109
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006110 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006111#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (operation.alg == PSA_ALG_GCM) {
6115 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6116 SIZE_MAX),
6117 PSA_ERROR_INVALID_ARGUMENT);
6118 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6119 PSA_ERROR_BAD_STATE);
6120 } else if (operation.alg != PSA_ALG_CCM) {
6121 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6122 SIZE_MAX));
6123 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006124 }
6125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006127#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006128
Tom Cosgrove1797b052022-12-04 17:19:59 +00006129 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006130#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 if (operation.alg == PSA_ALG_GCM) {
6136 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6137 SIZE_MAX),
6138 PSA_ERROR_INVALID_ARGUMENT);
6139 } else if (operation.alg != PSA_ALG_CCM) {
6140 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6141 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006142 }
6143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006145#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006146
6147 /* ------------------------------------------------------- */
6148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6154 PSA_AEAD_NONCE_MAX_SIZE,
6155 &nonce_length),
6156 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006159
Paul Elliott7220cae2021-06-22 17:25:57 +01006160 /* Test for generating nonce in decrypt setup. */
6161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6165 PSA_AEAD_NONCE_MAX_SIZE,
6166 &nonce_length),
6167 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006170
Paul Elliottc23a9a02021-06-21 18:32:46 +01006171 /* Test for setting lengths twice. */
6172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6178 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6181 input_data->len),
6182 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006185
Andrzej Kurekad837522021-12-15 15:28:49 +01006186 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6195 additional_data->len),
6196 PSA_ERROR_BAD_STATE);
6197 } else {
6198 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6199 additional_data->len));
6200
6201 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6202 input_data->len),
6203 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006204 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006206
6207 /* ------------------------------------------------------- */
6208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 if (operation.alg == PSA_ALG_CCM) {
6214 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6215 input_data->len, output_data,
6216 output_size, &output_length),
6217 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 } else {
6220 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6221 input_data->len, output_data,
6222 output_size, &output_length));
6223
6224 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6225 input_data->len),
6226 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006227 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006229
6230 /* ------------------------------------------------------- */
6231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 if (operation.alg == PSA_ALG_CCM) {
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));
6242 } else {
6243 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6244 finish_output_size,
6245 &output_part_length,
6246 tag_buffer, tag_length,
6247 &tag_size));
6248
6249 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6250 input_data->len),
6251 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006252 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006254
6255 /* Test for setting lengths after generating nonce + already starting data. */
6256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6260 PSA_AEAD_NONCE_MAX_SIZE,
6261 &nonce_length));
6262 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6265 additional_data->len),
6266 PSA_ERROR_BAD_STATE);
6267 } else {
6268 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6269 additional_data->len));
6270
6271 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6272 input_data->len),
6273 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006274 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006276
6277 /* ------------------------------------------------------- */
6278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6282 PSA_AEAD_NONCE_MAX_SIZE,
6283 &nonce_length));
6284 if (operation.alg == PSA_ALG_CCM) {
6285 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6286 input_data->len, output_data,
6287 output_size, &output_length),
6288 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 } else {
6291 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6292 input_data->len, output_data,
6293 output_size, &output_length));
6294
6295 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6296 input_data->len),
6297 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006298 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006300
6301 /* ------------------------------------------------------- */
6302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6306 PSA_AEAD_NONCE_MAX_SIZE,
6307 &nonce_length));
6308 if (operation.alg == PSA_ALG_CCM) {
6309 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6310 finish_output_size,
6311 &output_part_length,
6312 tag_buffer, tag_length,
6313 &tag_size));
6314 } else {
6315 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6316 finish_output_size,
6317 &output_part_length,
6318 tag_buffer, tag_length,
6319 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6322 input_data->len),
6323 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006324 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006326
Paul Elliott243080c2021-07-21 19:01:17 +01006327 /* Test for not sending any additional data or data after setting non zero
6328 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6335 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6338 finish_output_size,
6339 &output_part_length,
6340 tag_buffer, tag_length,
6341 &tag_size),
6342 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006345
Paul Elliott243080c2021-07-21 19:01:17 +01006346 /* Test for not sending any additional data or data after setting non-zero
6347 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6354 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6357 finish_output_size,
6358 &output_part_length,
6359 tag_buffer,
6360 tag_length),
6361 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006364
Paul Elliott243080c2021-07-21 19:01:17 +01006365 /* Test for not sending any additional data after setting a non-zero length
6366 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6373 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6376 input_data->len, output_data,
6377 output_size, &output_length),
6378 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006381
Paul Elliottf94bd992021-09-19 18:15:59 +01006382 /* Test for not sending any data after setting a non-zero length for it.*/
6383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6389 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6392 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6395 finish_output_size,
6396 &output_part_length,
6397 tag_buffer, tag_length,
6398 &tag_size),
6399 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006402
Paul Elliottb0450fe2021-09-01 15:06:26 +01006403 /* Test for sending too much additional data after setting lengths. */
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006410
6411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6413 additional_data->len),
6414 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006417
Paul Elliotta2a09b02021-09-22 14:56:40 +01006418 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6425 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6428 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6431 1),
6432 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006435
Paul Elliottb0450fe2021-09-01 15:06:26 +01006436 /* Test for sending too much data after setting lengths. */
6437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6445 input_data->len, output_data,
6446 output_size, &output_length),
6447 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006450
Paul Elliotta2a09b02021-09-22 14:56:40 +01006451 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006452
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6458 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006459
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6461 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6464 input_data->len, output_data,
6465 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6468 1, output_data,
6469 output_size, &output_length),
6470 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006473
Paul Elliottc23a9a02021-06-21 18:32:46 +01006474 /* Test sending additional data after data. */
6475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 if (operation.alg != PSA_ALG_CCM) {
6481 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6482 input_data->len, output_data,
6483 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006484
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6486 additional_data->len),
6487 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006488 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006490
Paul Elliott534d0b42021-06-22 19:15:20 +01006491 /* Test calling finish on decryption. */
6492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6498 finish_output_size,
6499 &output_part_length,
6500 tag_buffer, tag_length,
6501 &tag_size),
6502 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006505
6506 /* Test calling verify on encryption. */
6507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006511
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6513 finish_output_size,
6514 &output_part_length,
6515 tag_buffer,
6516 tag_length),
6517 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006518
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006520
6521
Paul Elliottc23a9a02021-06-21 18:32:46 +01006522exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 psa_destroy_key(key);
6524 psa_aead_abort(&operation);
6525 mbedtls_free(output_data);
6526 mbedtls_free(final_data);
6527 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006528}
6529/* END_CASE */
6530
6531/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006532void signature_size(int type_arg,
6533 int bits,
6534 int alg_arg,
6535 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006536{
6537 psa_key_type_t type = type_arg;
6538 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006542
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543exit:
6544 ;
6545}
6546/* END_CASE */
6547
6548/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006549void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6550 int alg_arg, data_t *input_data,
6551 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006552{
Ronald Cron5425a212020-08-04 14:58:35 +02006553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006554 psa_key_type_t key_type = key_type_arg;
6555 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006556 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006557 unsigned char *signature = NULL;
6558 size_t signature_size;
6559 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6565 psa_set_key_algorithm(&attributes, alg);
6566 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006567
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6569 &key));
6570 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6571 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006572
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006573 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006574 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6576 key_bits, alg);
6577 TEST_ASSERT(signature_size != 0);
6578 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006579 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006580
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006581 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 PSA_ASSERT(psa_sign_hash(key, alg,
6583 input_data->x, input_data->len,
6584 signature, signature_size,
6585 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006586 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006587 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006588 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006589
6590exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006591 /*
6592 * Key attributes may have been returned by psa_get_key_attributes()
6593 * thus reset them as required.
6594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 psa_destroy_key(key);
6598 mbedtls_free(signature);
6599 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006600}
6601/* END_CASE */
6602
Paul Elliott712d5122022-12-07 14:03:10 +00006603/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006604/**
6605 * sign_hash_interruptible() test intentions:
6606 *
6607 * Note: This test can currently only handle ECDSA.
6608 *
6609 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006610 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006611 *
6612 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6613 * expected for different max_ops values.
6614 *
6615 * 3. Test that the number of ops done prior to start and after abort is zero
6616 * and that each successful stage completes some ops (this is not mandated by
6617 * the PSA specification, but is currently the case).
6618 *
6619 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6620 * complete() calls does not alter the number of ops returned.
6621 */
Paul Elliott712d5122022-12-07 14:03:10 +00006622void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6623 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006624 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006625{
6626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6627 psa_key_type_t key_type = key_type_arg;
6628 psa_algorithm_t alg = alg_arg;
6629 size_t key_bits;
6630 unsigned char *signature = NULL;
6631 size_t signature_size;
6632 size_t signature_length = 0xdeadbeef;
6633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6634 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006635 uint32_t num_ops = 0;
6636 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006637 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006638 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006639 size_t min_completes = 0;
6640 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006641
Paul Elliott712d5122022-12-07 14:03:10 +00006642 psa_sign_hash_interruptible_operation_t operation =
6643 psa_sign_hash_interruptible_operation_init();
6644
6645 PSA_ASSERT(psa_crypto_init());
6646
6647 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6648 psa_set_key_algorithm(&attributes, alg);
6649 psa_set_key_type(&attributes, key_type);
6650
6651 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6652 &key));
6653 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6654 key_bits = psa_get_key_bits(&attributes);
6655
6656 /* Allocate a buffer which has the size advertised by the
6657 * library. */
6658 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6659 key_bits, alg);
6660 TEST_ASSERT(signature_size != 0);
6661 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006662 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006663
Paul Elliott0c683352022-12-16 19:16:56 +00006664 psa_interruptible_set_max_ops(max_ops);
6665
Paul Elliott6f600372023-02-06 18:41:05 +00006666 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6667 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006668
Paul Elliott712d5122022-12-07 14:03:10 +00006669 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6670 TEST_ASSERT(num_ops_prior == 0);
6671
6672 /* Start performing the signature. */
6673 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6674 input_data->x, input_data->len));
6675
6676 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6677 TEST_ASSERT(num_ops_prior == 0);
6678
6679 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006680 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006681 status = psa_sign_hash_complete(&operation, signature, signature_size,
6682 &signature_length);
6683
Paul Elliott0c683352022-12-16 19:16:56 +00006684 num_completes++;
6685
Paul Elliott712d5122022-12-07 14:03:10 +00006686 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6687 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006688 /* We are asserting here that every complete makes progress
6689 * (completes some ops), which is true of the internal
6690 * implementation and probably any implementation, however this is
6691 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006692 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006693
Paul Elliott712d5122022-12-07 14:03:10 +00006694 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006695
6696 /* Ensure calling get_num_ops() twice still returns the same
6697 * number of ops as previously reported. */
6698 num_ops = psa_sign_hash_get_num_ops(&operation);
6699
6700 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006701 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006702 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006703
6704 TEST_ASSERT(status == PSA_SUCCESS);
6705
Paul Elliott0c683352022-12-16 19:16:56 +00006706 TEST_LE_U(min_completes, num_completes);
6707 TEST_LE_U(num_completes, max_completes);
6708
Paul Elliott712d5122022-12-07 14:03:10 +00006709 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006710 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006711 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006712
6713 PSA_ASSERT(psa_sign_hash_abort(&operation));
6714
Paul Elliott59ad9452022-12-18 15:09:02 +00006715 num_ops = psa_sign_hash_get_num_ops(&operation);
6716 TEST_ASSERT(num_ops == 0);
6717
Paul Elliott712d5122022-12-07 14:03:10 +00006718exit:
6719
6720 /*
6721 * Key attributes may have been returned by psa_get_key_attributes()
6722 * thus reset them as required.
6723 */
6724 psa_reset_key_attributes(&attributes);
6725
6726 psa_destroy_key(key);
6727 mbedtls_free(signature);
6728 PSA_DONE();
6729}
6730/* END_CASE */
6731
Gilles Peskine20035e32018-02-03 22:44:14 +01006732/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006733void sign_hash_fail(int key_type_arg, data_t *key_data,
6734 int alg_arg, data_t *input_data,
6735 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006736{
Ronald Cron5425a212020-08-04 14:58:35 +02006737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006738 psa_key_type_t key_type = key_type_arg;
6739 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006740 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006741 psa_status_t actual_status;
6742 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006743 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006744 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006746
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006747 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6752 psa_set_key_algorithm(&attributes, alg);
6753 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006754
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6756 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 actual_status = psa_sign_hash(key, alg,
6759 input_data->x, input_data->len,
6760 signature, signature_size,
6761 &signature_length);
6762 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006763 /* The value of *signature_length is unspecified on error, but
6764 * whatever it is, it should be less than signature_size, so that
6765 * if the caller tries to read *signature_length bytes without
6766 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006768
6769exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 psa_reset_key_attributes(&attributes);
6771 psa_destroy_key(key);
6772 mbedtls_free(signature);
6773 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006774}
6775/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006776
Paul Elliott91007972022-12-16 12:21:24 +00006777/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006778/**
6779 * sign_hash_fail_interruptible() test intentions:
6780 *
6781 * Note: This test can currently only handle ECDSA.
6782 *
6783 * 1. Test that various failure cases for interruptible sign hash fail with the
6784 * correct error codes, and at the correct point (at start or during
6785 * complete).
6786 *
6787 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6788 * expected for different max_ops values.
6789 *
6790 * 3. Test that the number of ops done prior to start and after abort is zero
6791 * and that each successful stage completes some ops (this is not mandated by
6792 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006793 *
6794 * 4. Check that calling complete() when start() fails and complete()
6795 * after completion results in a BAD_STATE error.
6796 *
6797 * 5. Check that calling start() again after start fails results in a BAD_STATE
6798 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006799 */
Paul Elliott91007972022-12-16 12:21:24 +00006800void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6801 int alg_arg, data_t *input_data,
6802 int signature_size_arg,
6803 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006804 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006805 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006806{
6807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6808 psa_key_type_t key_type = key_type_arg;
6809 psa_algorithm_t alg = alg_arg;
6810 size_t signature_size = signature_size_arg;
6811 psa_status_t actual_status;
6812 psa_status_t expected_start_status = expected_start_status_arg;
6813 psa_status_t expected_complete_status = expected_complete_status_arg;
6814 unsigned char *signature = NULL;
6815 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006816 uint32_t num_ops = 0;
6817 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006818 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006819 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006820 size_t min_completes = 0;
6821 size_t max_completes = 0;
6822
Paul Elliott91007972022-12-16 12:21:24 +00006823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6824 psa_sign_hash_interruptible_operation_t operation =
6825 psa_sign_hash_interruptible_operation_init();
6826
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006827 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006828
6829 PSA_ASSERT(psa_crypto_init());
6830
6831 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6832 psa_set_key_algorithm(&attributes, alg);
6833 psa_set_key_type(&attributes, key_type);
6834
6835 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6836 &key));
6837
Paul Elliott0c683352022-12-16 19:16:56 +00006838 psa_interruptible_set_max_ops(max_ops);
6839
Paul Elliott6f600372023-02-06 18:41:05 +00006840 interruptible_signverify_get_minmax_completes(max_ops,
6841 expected_complete_status,
6842 &min_completes,
6843 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006844
Paul Elliott91007972022-12-16 12:21:24 +00006845 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6846 TEST_ASSERT(num_ops_prior == 0);
6847
6848 /* Start performing the signature. */
6849 actual_status = psa_sign_hash_start(&operation, key, alg,
6850 input_data->x, input_data->len);
6851
6852 TEST_EQUAL(actual_status, expected_start_status);
6853
Paul Elliottc9774412023-02-06 15:14:07 +00006854 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006855 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006856 * start failed. */
6857 actual_status = psa_sign_hash_complete(&operation, signature,
6858 signature_size,
6859 &signature_length);
6860
6861 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6862
6863 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006864 actual_status = psa_sign_hash_start(&operation, key, alg,
6865 input_data->x, input_data->len);
6866
6867 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6868 }
6869
Paul Elliott91007972022-12-16 12:21:24 +00006870 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6871 TEST_ASSERT(num_ops_prior == 0);
6872
Paul Elliott91007972022-12-16 12:21:24 +00006873 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006874 do {
Paul Elliott91007972022-12-16 12:21:24 +00006875 actual_status = psa_sign_hash_complete(&operation, signature,
6876 signature_size,
6877 &signature_length);
6878
Paul Elliott0c683352022-12-16 19:16:56 +00006879 num_completes++;
6880
Paul Elliott334d7262023-01-20 17:29:41 +00006881 if (actual_status == PSA_SUCCESS ||
6882 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006883 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006884 /* We are asserting here that every complete makes progress
6885 * (completes some ops), which is true of the internal
6886 * implementation and probably any implementation, however this is
6887 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006888 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006889
Paul Elliott91007972022-12-16 12:21:24 +00006890 num_ops_prior = num_ops;
6891 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006892 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006893
Paul Elliottc9774412023-02-06 15:14:07 +00006894 TEST_EQUAL(actual_status, expected_complete_status);
6895
Paul Elliottefebad02023-02-15 16:56:45 +00006896 /* Check that another complete returns BAD_STATE. */
6897 actual_status = psa_sign_hash_complete(&operation, signature,
6898 signature_size,
6899 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006900
Paul Elliottefebad02023-02-15 16:56:45 +00006901 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006902
Paul Elliott91007972022-12-16 12:21:24 +00006903 PSA_ASSERT(psa_sign_hash_abort(&operation));
6904
Paul Elliott59ad9452022-12-18 15:09:02 +00006905 num_ops = psa_sign_hash_get_num_ops(&operation);
6906 TEST_ASSERT(num_ops == 0);
6907
Paul Elliott91007972022-12-16 12:21:24 +00006908 /* The value of *signature_length is unspecified on error, but
6909 * whatever it is, it should be less than signature_size, so that
6910 * if the caller tries to read *signature_length bytes without
6911 * checking the error code then they don't overflow a buffer. */
6912 TEST_LE_U(signature_length, signature_size);
6913
Paul Elliott0c683352022-12-16 19:16:56 +00006914 TEST_LE_U(min_completes, num_completes);
6915 TEST_LE_U(num_completes, max_completes);
6916
Paul Elliott91007972022-12-16 12:21:24 +00006917exit:
6918 psa_reset_key_attributes(&attributes);
6919 psa_destroy_key(key);
6920 mbedtls_free(signature);
6921 PSA_DONE();
6922}
6923/* END_CASE */
6924
mohammad16038cc1cee2018-03-28 01:21:33 +03006925/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006926void sign_verify_hash(int key_type_arg, data_t *key_data,
6927 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006928{
Ronald Cron5425a212020-08-04 14:58:35 +02006929 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006930 psa_key_type_t key_type = key_type_arg;
6931 psa_algorithm_t alg = alg_arg;
6932 size_t key_bits;
6933 unsigned char *signature = NULL;
6934 size_t signature_size;
6935 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6941 psa_set_key_algorithm(&attributes, alg);
6942 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006943
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6945 &key));
6946 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6947 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006948
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006949 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006950 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6952 key_bits, alg);
6953 TEST_ASSERT(signature_size != 0);
6954 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006955 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006956
6957 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 PSA_ASSERT(psa_sign_hash(key, alg,
6959 input_data->x, input_data->len,
6960 signature, signature_size,
6961 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006962 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 TEST_LE_U(signature_length, signature_size);
6964 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006965
6966 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 PSA_ASSERT(psa_verify_hash(key, alg,
6968 input_data->x, input_data->len,
6969 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006970
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006972 /* Flip a bit in the input and verify that the signature is now
6973 * detected as invalid. Flip a bit at the beginning, not at the end,
6974 * because ECDSA may ignore the last few bits of the input. */
6975 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006976 TEST_EQUAL(psa_verify_hash(key, alg,
6977 input_data->x, input_data->len,
6978 signature, signature_length),
6979 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006980 }
6981
6982exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006983 /*
6984 * Key attributes may have been returned by psa_get_key_attributes()
6985 * thus reset them as required.
6986 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 psa_destroy_key(key);
6990 mbedtls_free(signature);
6991 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006992}
6993/* END_CASE */
6994
Paul Elliott712d5122022-12-07 14:03:10 +00006995/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006996/**
6997 * sign_verify_hash_interruptible() test intentions:
6998 *
6999 * Note: This test can currently only handle ECDSA.
7000 *
Paul Elliott8c092052023-03-06 17:49:14 +00007001 * 1. Test that we can sign an input hash with the given keypair and then
7002 * afterwards verify that signature. This is currently the only way to test
7003 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00007004 *
7005 * 2. Test that after corrupting the hash, the verification detects an invalid
7006 * signature.
7007 *
7008 * 3. Test the number of calls to psa_sign_hash_complete() required are as
7009 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00007010 *
7011 * 4. Test that the number of ops done prior to starting signing and after abort
7012 * is zero and that each successful signing stage completes some ops (this is
7013 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00007014 */
Paul Elliott712d5122022-12-07 14:03:10 +00007015void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00007016 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007017 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007018{
7019 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7020 psa_key_type_t key_type = key_type_arg;
7021 psa_algorithm_t alg = alg_arg;
7022 size_t key_bits;
7023 unsigned char *signature = NULL;
7024 size_t signature_size;
7025 size_t signature_length = 0xdeadbeef;
7026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7027 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007028 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00007029 uint32_t num_ops = 0;
7030 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007031 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007032 size_t min_completes = 0;
7033 size_t max_completes = 0;
7034
Paul Elliott712d5122022-12-07 14:03:10 +00007035 psa_sign_hash_interruptible_operation_t sign_operation =
7036 psa_sign_hash_interruptible_operation_init();
7037 psa_verify_hash_interruptible_operation_t verify_operation =
7038 psa_verify_hash_interruptible_operation_init();
7039
7040 PSA_ASSERT(psa_crypto_init());
7041
Paul Elliott0c683352022-12-16 19:16:56 +00007042 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7043 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007044 psa_set_key_algorithm(&attributes, alg);
7045 psa_set_key_type(&attributes, key_type);
7046
7047 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7048 &key));
7049 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7050 key_bits = psa_get_key_bits(&attributes);
7051
7052 /* Allocate a buffer which has the size advertised by the
7053 * library. */
7054 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7055 key_bits, alg);
7056 TEST_ASSERT(signature_size != 0);
7057 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007058 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007059
Paul Elliott0c683352022-12-16 19:16:56 +00007060 psa_interruptible_set_max_ops(max_ops);
7061
Paul Elliott6f600372023-02-06 18:41:05 +00007062 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7063 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007064
Paul Elliott7c173082023-02-26 18:44:45 +00007065 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7066 TEST_ASSERT(num_ops_prior == 0);
7067
Paul Elliott712d5122022-12-07 14:03:10 +00007068 /* Start performing the signature. */
7069 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7070 input_data->x, input_data->len));
7071
Paul Elliott7c173082023-02-26 18:44:45 +00007072 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7073 TEST_ASSERT(num_ops_prior == 0);
7074
Paul Elliott712d5122022-12-07 14:03:10 +00007075 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007076 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007077
Paul Elliott0c683352022-12-16 19:16:56 +00007078 status = psa_sign_hash_complete(&sign_operation, signature,
7079 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007080 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007081
7082 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007083
7084 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7085 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7086 /* We are asserting here that every complete makes progress
7087 * (completes some ops), which is true of the internal
7088 * implementation and probably any implementation, however this is
7089 * not mandated by the PSA specification. */
7090 TEST_ASSERT(num_ops > num_ops_prior);
7091
7092 num_ops_prior = num_ops;
7093 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007094 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007095
7096 TEST_ASSERT(status == PSA_SUCCESS);
7097
Paul Elliott0c683352022-12-16 19:16:56 +00007098 TEST_LE_U(min_completes, num_completes);
7099 TEST_LE_U(num_completes, max_completes);
7100
Paul Elliott712d5122022-12-07 14:03:10 +00007101 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7102
Paul Elliott7c173082023-02-26 18:44:45 +00007103 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7104 TEST_ASSERT(num_ops == 0);
7105
Paul Elliott712d5122022-12-07 14:03:10 +00007106 /* Check that the signature length looks sensible. */
7107 TEST_LE_U(signature_length, signature_size);
7108 TEST_ASSERT(signature_length > 0);
7109
Paul Elliott0c683352022-12-16 19:16:56 +00007110 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007111
7112 /* Start verification. */
7113 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7114 input_data->x, input_data->len,
7115 signature, signature_length));
7116
7117 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007118 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007119 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007120
7121 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007122 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007123
7124 TEST_ASSERT(status == PSA_SUCCESS);
7125
Paul Elliott0c683352022-12-16 19:16:56 +00007126 TEST_LE_U(min_completes, num_completes);
7127 TEST_LE_U(num_completes, max_completes);
7128
Paul Elliott712d5122022-12-07 14:03:10 +00007129 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7130
7131 verify_operation = psa_verify_hash_interruptible_operation_init();
7132
7133 if (input_data->len != 0) {
7134 /* Flip a bit in the input and verify that the signature is now
7135 * detected as invalid. Flip a bit at the beginning, not at the end,
7136 * because ECDSA may ignore the last few bits of the input. */
7137 input_data->x[0] ^= 1;
7138
Paul Elliott712d5122022-12-07 14:03:10 +00007139 /* Start verification. */
7140 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7141 input_data->x, input_data->len,
7142 signature, signature_length));
7143
7144 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007145 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007146 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007147 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007148
7149 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7150 }
7151
7152 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7153
7154exit:
7155 /*
7156 * Key attributes may have been returned by psa_get_key_attributes()
7157 * thus reset them as required.
7158 */
7159 psa_reset_key_attributes(&attributes);
7160
7161 psa_destroy_key(key);
7162 mbedtls_free(signature);
7163 PSA_DONE();
7164}
7165/* END_CASE */
7166
Gilles Peskine9911b022018-06-29 17:30:48 +02007167/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168void verify_hash(int key_type_arg, data_t *key_data,
7169 int alg_arg, data_t *hash_data,
7170 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007171{
Ronald Cron5425a212020-08-04 14:58:35 +02007172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007173 psa_key_type_t key_type = key_type_arg;
7174 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007178
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007180
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7182 psa_set_key_algorithm(&attributes, alg);
7183 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007184
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7186 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007187
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 PSA_ASSERT(psa_verify_hash(key, alg,
7189 hash_data->x, hash_data->len,
7190 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007191
itayzafrir5c753392018-05-08 11:18:38 +03007192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 psa_reset_key_attributes(&attributes);
7194 psa_destroy_key(key);
7195 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007196}
7197/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007198
Paul Elliott712d5122022-12-07 14:03:10 +00007199/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007200/**
7201 * verify_hash_interruptible() test intentions:
7202 *
7203 * Note: This test can currently only handle ECDSA.
7204 *
7205 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007206 * only). Given this test only does verification it can accept public keys as
7207 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007208 *
7209 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7210 * expected for different max_ops values.
7211 *
7212 * 3. Test that the number of ops done prior to start and after abort is zero
7213 * and that each successful stage completes some ops (this is not mandated by
7214 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007215 *
7216 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7217 * complete() calls does not alter the number of ops returned.
7218 *
7219 * 5. Test that after corrupting the hash, the verification detects an invalid
7220 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007221 */
Paul Elliott712d5122022-12-07 14:03:10 +00007222void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7223 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007224 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007225{
7226 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7227 psa_key_type_t key_type = key_type_arg;
7228 psa_algorithm_t alg = alg_arg;
7229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7230 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007231 uint32_t num_ops = 0;
7232 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007233 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007234 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007235 size_t min_completes = 0;
7236 size_t max_completes = 0;
7237
Paul Elliott712d5122022-12-07 14:03:10 +00007238 psa_verify_hash_interruptible_operation_t operation =
7239 psa_verify_hash_interruptible_operation_init();
7240
7241 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7242
7243 PSA_ASSERT(psa_crypto_init());
7244
7245 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7246 psa_set_key_algorithm(&attributes, alg);
7247 psa_set_key_type(&attributes, key_type);
7248
7249 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7250 &key));
7251
Paul Elliott0c683352022-12-16 19:16:56 +00007252 psa_interruptible_set_max_ops(max_ops);
7253
Paul Elliott6f600372023-02-06 18:41:05 +00007254 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7255 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007256
Paul Elliott712d5122022-12-07 14:03:10 +00007257 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7258
7259 TEST_ASSERT(num_ops_prior == 0);
7260
7261 /* Start verification. */
7262 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7263 hash_data->x, hash_data->len,
7264 signature_data->x, signature_data->len)
7265 );
7266
7267 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7268
7269 TEST_ASSERT(num_ops_prior == 0);
7270
7271 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007272 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007273 status = psa_verify_hash_complete(&operation);
7274
Paul Elliott0c683352022-12-16 19:16:56 +00007275 num_completes++;
7276
Paul Elliott712d5122022-12-07 14:03:10 +00007277 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7278 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007279 /* We are asserting here that every complete makes progress
7280 * (completes some ops), which is true of the internal
7281 * implementation and probably any implementation, however this is
7282 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007283 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007284
Paul Elliott712d5122022-12-07 14:03:10 +00007285 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007286
7287 /* Ensure calling get_num_ops() twice still returns the same
7288 * number of ops as previously reported. */
7289 num_ops = psa_verify_hash_get_num_ops(&operation);
7290
7291 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007292 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007293 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007294
7295 TEST_ASSERT(status == PSA_SUCCESS);
7296
Paul Elliott0c683352022-12-16 19:16:56 +00007297 TEST_LE_U(min_completes, num_completes);
7298 TEST_LE_U(num_completes, max_completes);
7299
Paul Elliott712d5122022-12-07 14:03:10 +00007300 PSA_ASSERT(psa_verify_hash_abort(&operation));
7301
Paul Elliott59ad9452022-12-18 15:09:02 +00007302 num_ops = psa_verify_hash_get_num_ops(&operation);
7303 TEST_ASSERT(num_ops == 0);
7304
Paul Elliott8359c142023-02-24 18:40:10 +00007305 if (hash_data->len != 0) {
7306 /* Flip a bit in the hash and verify that the signature is now detected
7307 * as invalid. Flip a bit at the beginning, not at the end, because
7308 * ECDSA may ignore the last few bits of the input. */
7309 hash_data->x[0] ^= 1;
7310
7311 /* Start verification. */
7312 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7313 hash_data->x, hash_data->len,
7314 signature_data->x, signature_data->len));
7315
7316 /* Continue performing the signature until complete. */
7317 do {
7318 status = psa_verify_hash_complete(&operation);
7319 } while (status == PSA_OPERATION_INCOMPLETE);
7320
7321 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7322 }
7323
Paul Elliott712d5122022-12-07 14:03:10 +00007324exit:
7325 psa_reset_key_attributes(&attributes);
7326 psa_destroy_key(key);
7327 PSA_DONE();
7328}
7329/* END_CASE */
7330
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007331/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007332void verify_hash_fail(int key_type_arg, data_t *key_data,
7333 int alg_arg, data_t *hash_data,
7334 data_t *signature_data,
7335 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007336{
Ronald Cron5425a212020-08-04 14:58:35 +02007337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007338 psa_key_type_t key_type = key_type_arg;
7339 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007340 psa_status_t actual_status;
7341 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007345
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7347 psa_set_key_algorithm(&attributes, alg);
7348 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007349
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7351 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007352
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 actual_status = psa_verify_hash(key, alg,
7354 hash_data->x, hash_data->len,
7355 signature_data->x, signature_data->len);
7356 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007357
7358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 psa_reset_key_attributes(&attributes);
7360 psa_destroy_key(key);
7361 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007362}
7363/* END_CASE */
7364
Paul Elliott91007972022-12-16 12:21:24 +00007365/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007366/**
7367 * verify_hash_fail_interruptible() test intentions:
7368 *
7369 * Note: This test can currently only handle ECDSA.
7370 *
7371 * 1. Test that various failure cases for interruptible verify hash fail with
7372 * the correct error codes, and at the correct point (at start or during
7373 * complete).
7374 *
7375 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7376 * expected for different max_ops values.
7377 *
7378 * 3. Test that the number of ops done prior to start and after abort is zero
7379 * and that each successful stage completes some ops (this is not mandated by
7380 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007381 *
7382 * 4. Check that calling complete() when start() fails and complete()
7383 * after completion results in a BAD_STATE error.
7384 *
7385 * 5. Check that calling start() again after start fails results in a BAD_STATE
7386 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007387 */
Paul Elliott91007972022-12-16 12:21:24 +00007388void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7389 int alg_arg, data_t *hash_data,
7390 data_t *signature_data,
7391 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007392 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007393 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007394{
7395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7396 psa_key_type_t key_type = key_type_arg;
7397 psa_algorithm_t alg = alg_arg;
7398 psa_status_t actual_status;
7399 psa_status_t expected_start_status = expected_start_status_arg;
7400 psa_status_t expected_complete_status = expected_complete_status_arg;
7401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007402 uint32_t num_ops = 0;
7403 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007404 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007405 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007406 size_t min_completes = 0;
7407 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007408 psa_verify_hash_interruptible_operation_t operation =
7409 psa_verify_hash_interruptible_operation_init();
7410
7411 PSA_ASSERT(psa_crypto_init());
7412
7413 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7414 psa_set_key_algorithm(&attributes, alg);
7415 psa_set_key_type(&attributes, key_type);
7416
7417 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7418 &key));
7419
Paul Elliott0c683352022-12-16 19:16:56 +00007420 psa_interruptible_set_max_ops(max_ops);
7421
Paul Elliott6f600372023-02-06 18:41:05 +00007422 interruptible_signverify_get_minmax_completes(max_ops,
7423 expected_complete_status,
7424 &min_completes,
7425 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007426
Paul Elliott91007972022-12-16 12:21:24 +00007427 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7428 TEST_ASSERT(num_ops_prior == 0);
7429
7430 /* Start verification. */
7431 actual_status = psa_verify_hash_start(&operation, key, alg,
7432 hash_data->x, hash_data->len,
7433 signature_data->x,
7434 signature_data->len);
7435
7436 TEST_EQUAL(actual_status, expected_start_status);
7437
Paul Elliottc9774412023-02-06 15:14:07 +00007438 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007439 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007440 * start failed. */
7441 actual_status = psa_verify_hash_complete(&operation);
7442
7443 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7444
7445 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007446 actual_status = psa_verify_hash_start(&operation, key, alg,
7447 hash_data->x, hash_data->len,
7448 signature_data->x,
7449 signature_data->len);
7450
7451 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7452 }
7453
Paul Elliott91007972022-12-16 12:21:24 +00007454 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7455 TEST_ASSERT(num_ops_prior == 0);
7456
Paul Elliott91007972022-12-16 12:21:24 +00007457 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007458 do {
Paul Elliott91007972022-12-16 12:21:24 +00007459 actual_status = psa_verify_hash_complete(&operation);
7460
Paul Elliott0c683352022-12-16 19:16:56 +00007461 num_completes++;
7462
Paul Elliott334d7262023-01-20 17:29:41 +00007463 if (actual_status == PSA_SUCCESS ||
7464 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007465 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007466 /* We are asserting here that every complete makes progress
7467 * (completes some ops), which is true of the internal
7468 * implementation and probably any implementation, however this is
7469 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007470 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007471
Paul Elliott91007972022-12-16 12:21:24 +00007472 num_ops_prior = num_ops;
7473 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007474 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007475
Paul Elliottc9774412023-02-06 15:14:07 +00007476 TEST_EQUAL(actual_status, expected_complete_status);
7477
Paul Elliottefebad02023-02-15 16:56:45 +00007478 /* Check that another complete returns BAD_STATE. */
7479 actual_status = psa_verify_hash_complete(&operation);
7480 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007481
Paul Elliott0c683352022-12-16 19:16:56 +00007482 TEST_LE_U(min_completes, num_completes);
7483 TEST_LE_U(num_completes, max_completes);
7484
Paul Elliott91007972022-12-16 12:21:24 +00007485 PSA_ASSERT(psa_verify_hash_abort(&operation));
7486
Paul Elliott59ad9452022-12-18 15:09:02 +00007487 num_ops = psa_verify_hash_get_num_ops(&operation);
7488 TEST_ASSERT(num_ops == 0);
7489
Paul Elliott91007972022-12-16 12:21:24 +00007490exit:
7491 psa_reset_key_attributes(&attributes);
7492 psa_destroy_key(key);
7493 PSA_DONE();
7494}
7495/* END_CASE */
7496
Paul Elliott20a36062022-12-18 13:21:25 +00007497/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007498/**
7499 * interruptible_signverify_hash_state_test() test intentions:
7500 *
7501 * Note: This test can currently only handle ECDSA.
7502 *
7503 * 1. Test that calling the various interruptible sign and verify hash functions
7504 * in incorrect orders returns BAD_STATE errors.
7505 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007506void interruptible_signverify_hash_state_test(int key_type_arg,
7507 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007508{
7509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7510 psa_key_type_t key_type = key_type_arg;
7511 psa_algorithm_t alg = alg_arg;
7512 size_t key_bits;
7513 unsigned char *signature = NULL;
7514 size_t signature_size;
7515 size_t signature_length = 0xdeadbeef;
7516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7517 psa_sign_hash_interruptible_operation_t sign_operation =
7518 psa_sign_hash_interruptible_operation_init();
7519 psa_verify_hash_interruptible_operation_t verify_operation =
7520 psa_verify_hash_interruptible_operation_init();
7521
7522 PSA_ASSERT(psa_crypto_init());
7523
7524 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7525 PSA_KEY_USAGE_VERIFY_HASH);
7526 psa_set_key_algorithm(&attributes, alg);
7527 psa_set_key_type(&attributes, key_type);
7528
7529 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7530 &key));
7531 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7532 key_bits = psa_get_key_bits(&attributes);
7533
7534 /* Allocate a buffer which has the size advertised by the
7535 * library. */
7536 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7537 key_bits, alg);
7538 TEST_ASSERT(signature_size != 0);
7539 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007540 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007541
7542 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7543
7544 /* --- Attempt completes prior to starts --- */
7545 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7546 signature_size,
7547 &signature_length),
7548 PSA_ERROR_BAD_STATE);
7549
7550 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7551
7552 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7553 PSA_ERROR_BAD_STATE);
7554
7555 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7556
7557 /* --- Aborts in all other places. --- */
7558 psa_sign_hash_abort(&sign_operation);
7559
7560 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7561 input_data->x, input_data->len));
7562
7563 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7564
7565 psa_interruptible_set_max_ops(1);
7566
7567 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7568 input_data->x, input_data->len));
7569
7570 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7571 signature_size,
7572 &signature_length),
7573 PSA_OPERATION_INCOMPLETE);
7574
7575 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7576
7577 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7578
7579 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7580 input_data->x, input_data->len));
7581
7582 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7583 signature_size,
7584 &signature_length));
7585
7586 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7587
7588 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7589
7590 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7591 input_data->x, input_data->len,
7592 signature, signature_length));
7593
7594 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7595
7596 psa_interruptible_set_max_ops(1);
7597
7598 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7599 input_data->x, input_data->len,
7600 signature, signature_length));
7601
7602 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7603 PSA_OPERATION_INCOMPLETE);
7604
7605 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7606
7607 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7608
7609 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7610 input_data->x, input_data->len,
7611 signature, signature_length));
7612
7613 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7614
7615 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7616
7617 /* --- Attempt double starts. --- */
7618
7619 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7620 input_data->x, input_data->len));
7621
7622 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7623 input_data->x, input_data->len),
7624 PSA_ERROR_BAD_STATE);
7625
7626 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7627
7628 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7629 input_data->x, input_data->len,
7630 signature, signature_length));
7631
7632 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7633 input_data->x, input_data->len,
7634 signature, signature_length),
7635 PSA_ERROR_BAD_STATE);
7636
7637 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7638
Paul Elliott76d671a2023-02-07 17:45:18 +00007639exit:
7640 /*
7641 * Key attributes may have been returned by psa_get_key_attributes()
7642 * thus reset them as required.
7643 */
7644 psa_reset_key_attributes(&attributes);
7645
7646 psa_destroy_key(key);
7647 mbedtls_free(signature);
7648 PSA_DONE();
7649}
7650/* END_CASE */
7651
7652/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007653/**
Paul Elliottc2033502023-02-26 17:09:14 +00007654 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007655 *
7656 * Note: This test can currently only handle ECDSA.
7657 *
7658 * 1. Test various edge cases in the interruptible sign and verify hash
7659 * interfaces.
7660 */
Paul Elliottc2033502023-02-26 17:09:14 +00007661void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007662 data_t *key_data, int alg_arg, data_t *input_data)
7663{
7664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7665 psa_key_type_t key_type = key_type_arg;
7666 psa_algorithm_t alg = alg_arg;
7667 size_t key_bits;
7668 unsigned char *signature = NULL;
7669 size_t signature_size;
7670 size_t signature_length = 0xdeadbeef;
7671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7672 uint8_t *input_buffer = NULL;
7673 psa_sign_hash_interruptible_operation_t sign_operation =
7674 psa_sign_hash_interruptible_operation_init();
7675 psa_verify_hash_interruptible_operation_t verify_operation =
7676 psa_verify_hash_interruptible_operation_init();
7677
7678 PSA_ASSERT(psa_crypto_init());
7679
7680 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7681 PSA_KEY_USAGE_VERIFY_HASH);
7682 psa_set_key_algorithm(&attributes, alg);
7683 psa_set_key_type(&attributes, key_type);
7684
7685 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7686 &key));
7687 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7688 key_bits = psa_get_key_bits(&attributes);
7689
7690 /* Allocate a buffer which has the size advertised by the
7691 * library. */
7692 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7693 key_bits, alg);
7694 TEST_ASSERT(signature_size != 0);
7695 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007696 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007697
Paul Elliott20a36062022-12-18 13:21:25 +00007698 /* --- Change function inputs mid run, to cause an error (sign only,
7699 * verify passes all inputs to start. --- */
7700
7701 psa_interruptible_set_max_ops(1);
7702
7703 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7704 input_data->x, input_data->len));
7705
7706 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7707 signature_size,
7708 &signature_length),
7709 PSA_OPERATION_INCOMPLETE);
7710
7711 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7712 0,
7713 &signature_length),
7714 PSA_ERROR_BUFFER_TOO_SMALL);
7715
Paul Elliottc9774412023-02-06 15:14:07 +00007716 /* And test that this invalidates the operation. */
7717 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7718 0,
7719 &signature_length),
7720 PSA_ERROR_BAD_STATE);
7721
Paul Elliott20a36062022-12-18 13:21:25 +00007722 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7723
Paul Elliottf9c91a72023-02-05 18:06:38 +00007724 /* Trash the hash buffer in between start and complete, to ensure
7725 * no reliance on external buffers. */
7726 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7727
Paul Elliott6c68df42023-10-23 15:33:37 +01007728 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007729
7730 memcpy(input_buffer, input_data->x, input_data->len);
7731
7732 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7733 input_buffer, input_data->len));
7734
7735 memset(input_buffer, '!', input_data->len);
7736 mbedtls_free(input_buffer);
7737 input_buffer = NULL;
7738
7739 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7740 signature_size,
7741 &signature_length));
7742
7743 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7744
Paul Elliott6c68df42023-10-23 15:33:37 +01007745 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007746
7747 memcpy(input_buffer, input_data->x, input_data->len);
7748
7749 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7750 input_buffer, input_data->len,
7751 signature, signature_length));
7752
7753 memset(input_buffer, '!', input_data->len);
7754 mbedtls_free(input_buffer);
7755 input_buffer = NULL;
7756
7757 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7758
7759 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7760
Paul Elliott20a36062022-12-18 13:21:25 +00007761exit:
7762 /*
7763 * Key attributes may have been returned by psa_get_key_attributes()
7764 * thus reset them as required.
7765 */
7766 psa_reset_key_attributes(&attributes);
7767
7768 psa_destroy_key(key);
7769 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007770 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007771 PSA_DONE();
7772}
7773/* END_CASE */
7774
Paul Elliotta4cb9092023-02-07 18:01:55 +00007775/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007776/**
Paul Elliott57702242023-02-26 20:36:10 +00007777 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007778 *
7779 * Note: This test can currently only handle ECDSA.
7780 *
7781 * 1. Test that setting max ops is reflected in both interruptible sign and
7782 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007783 * 2. Test that changing the value of max_ops to unlimited during an operation
7784 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007785 *
7786 * 3. Test that calling get_num_ops() between complete calls gives the same
7787 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007788 */
Paul Elliott57702242023-02-26 20:36:10 +00007789void interruptible_signverify_hash_ops_tests(int key_type_arg,
7790 data_t *key_data, int alg_arg,
7791 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007792{
7793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7794 psa_key_type_t key_type = key_type_arg;
7795 psa_algorithm_t alg = alg_arg;
7796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007797 size_t key_bits;
7798 unsigned char *signature = NULL;
7799 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007800 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007801 uint32_t num_ops = 0;
7802 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7803
Paul Elliotta4cb9092023-02-07 18:01:55 +00007804 psa_sign_hash_interruptible_operation_t sign_operation =
7805 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007806 psa_verify_hash_interruptible_operation_t verify_operation =
7807 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007808
7809 PSA_ASSERT(psa_crypto_init());
7810
7811 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7812 PSA_KEY_USAGE_VERIFY_HASH);
7813 psa_set_key_algorithm(&attributes, alg);
7814 psa_set_key_type(&attributes, key_type);
7815
Paul Elliottf1743e22023-02-15 18:44:16 +00007816 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7817 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7818 key_bits = psa_get_key_bits(&attributes);
7819
7820 /* Allocate a buffer which has the size advertised by the
7821 * library. */
7822 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7823
7824 TEST_ASSERT(signature_size != 0);
7825 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007826 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007827
7828 /* Check that default max ops gets set if we don't set it. */
7829 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7830 input_data->x, input_data->len));
7831
7832 TEST_EQUAL(psa_interruptible_get_max_ops(),
7833 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7834
7835 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7836
Paul Elliottf1743e22023-02-15 18:44:16 +00007837 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7838 input_data->x, input_data->len,
7839 signature, signature_size));
7840
7841 TEST_EQUAL(psa_interruptible_get_max_ops(),
7842 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7843
7844 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7845
Paul Elliotta4cb9092023-02-07 18:01:55 +00007846 /* Check that max ops gets set properly. */
7847
7848 psa_interruptible_set_max_ops(0xbeef);
7849
Paul Elliottf1743e22023-02-15 18:44:16 +00007850 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007851
Paul Elliott9e8819f2023-02-26 19:01:35 +00007852 /* --- Ensure changing the max ops mid operation works (operation should
7853 * complete successfully after setting max ops to unlimited --- */
7854 psa_interruptible_set_max_ops(1);
7855
7856 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7857 input_data->x, input_data->len));
7858
7859 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7860 signature_size,
7861 &signature_length),
7862 PSA_OPERATION_INCOMPLETE);
7863
7864 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7865
7866 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7867 signature_size,
7868 &signature_length));
7869
7870 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7871
7872 psa_interruptible_set_max_ops(1);
7873
7874 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7875 input_data->x, input_data->len,
7876 signature, signature_length));
7877
7878 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7879 PSA_OPERATION_INCOMPLETE);
7880
7881 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7882
7883 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7884
7885 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7886
Paul Elliottc1e04002023-02-26 20:27:23 +00007887 /* --- Test that not calling get_num_ops inbetween complete calls does not
7888 * result in lost ops. ---*/
7889
7890 psa_interruptible_set_max_ops(1);
7891
7892 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7893 input_data->x, input_data->len));
7894
7895 /* Continue performing the signature until complete. */
7896 do {
7897 status = psa_sign_hash_complete(&sign_operation, signature,
7898 signature_size,
7899 &signature_length);
7900
7901 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7902
7903 } while (status == PSA_OPERATION_INCOMPLETE);
7904
7905 PSA_ASSERT(status);
7906
7907 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7908
7909 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7910 input_data->x, input_data->len));
7911
7912 /* Continue performing the signature until complete. */
7913 do {
7914 status = psa_sign_hash_complete(&sign_operation, signature,
7915 signature_size,
7916 &signature_length);
7917 } while (status == PSA_OPERATION_INCOMPLETE);
7918
7919 PSA_ASSERT(status);
7920
7921 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7922
7923 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7924
7925 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7926 input_data->x, input_data->len,
7927 signature, signature_length));
7928
7929 /* Continue performing the verification until complete. */
7930 do {
7931 status = psa_verify_hash_complete(&verify_operation);
7932
7933 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7934
7935 } while (status == PSA_OPERATION_INCOMPLETE);
7936
7937 PSA_ASSERT(status);
7938
7939 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7940
7941 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7942 input_data->x, input_data->len,
7943 signature, signature_length));
7944
7945 /* Continue performing the verification until complete. */
7946 do {
7947 status = psa_verify_hash_complete(&verify_operation);
7948
7949 } while (status == PSA_OPERATION_INCOMPLETE);
7950
7951 PSA_ASSERT(status);
7952
7953 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7954
7955 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7956
Paul Elliotta4cb9092023-02-07 18:01:55 +00007957exit:
7958 /*
7959 * Key attributes may have been returned by psa_get_key_attributes()
7960 * thus reset them as required.
7961 */
7962 psa_reset_key_attributes(&attributes);
7963
7964 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007965 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007966 PSA_DONE();
7967}
7968/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007969
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007970/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007971void sign_message_deterministic(int key_type_arg,
7972 data_t *key_data,
7973 int alg_arg,
7974 data_t *input_data,
7975 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007976{
7977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7978 psa_key_type_t key_type = key_type_arg;
7979 psa_algorithm_t alg = alg_arg;
7980 size_t key_bits;
7981 unsigned char *signature = NULL;
7982 size_t signature_size;
7983 size_t signature_length = 0xdeadbeef;
7984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7985
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007987
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7989 psa_set_key_algorithm(&attributes, alg);
7990 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007991
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7993 &key));
7994 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7995 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007996
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7998 TEST_ASSERT(signature_size != 0);
7999 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008000 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008001
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 PSA_ASSERT(psa_sign_message(key, alg,
8003 input_data->x, input_data->len,
8004 signature, signature_size,
8005 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02008006
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008007 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008008 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008009
8010exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008012
Gilles Peskine449bd832023-01-11 14:50:10 +01008013 psa_destroy_key(key);
8014 mbedtls_free(signature);
8015 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02008016
8017}
8018/* END_CASE */
8019
8020/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008021void sign_message_fail(int key_type_arg,
8022 data_t *key_data,
8023 int alg_arg,
8024 data_t *input_data,
8025 int signature_size_arg,
8026 int expected_status_arg)
8027{
8028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8029 psa_key_type_t key_type = key_type_arg;
8030 psa_algorithm_t alg = alg_arg;
8031 size_t signature_size = signature_size_arg;
8032 psa_status_t actual_status;
8033 psa_status_t expected_status = expected_status_arg;
8034 unsigned char *signature = NULL;
8035 size_t signature_length = 0xdeadbeef;
8036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8037
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008038 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008039
8040 PSA_ASSERT(psa_crypto_init());
8041
8042 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8043 psa_set_key_algorithm(&attributes, alg);
8044 psa_set_key_type(&attributes, key_type);
8045
8046 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8047 &key));
8048
8049 actual_status = psa_sign_message(key, alg,
8050 input_data->x, input_data->len,
8051 signature, signature_size,
8052 &signature_length);
8053 TEST_EQUAL(actual_status, expected_status);
8054 /* The value of *signature_length is unspecified on error, but
8055 * whatever it is, it should be less than signature_size, so that
8056 * if the caller tries to read *signature_length bytes without
8057 * checking the error code then they don't overflow a buffer. */
8058 TEST_LE_U(signature_length, signature_size);
8059
8060exit:
8061 psa_reset_key_attributes(&attributes);
8062 psa_destroy_key(key);
8063 mbedtls_free(signature);
8064 PSA_DONE();
8065}
8066/* END_CASE */
8067
8068/* BEGIN_CASE */
8069void sign_verify_message(int key_type_arg,
8070 data_t *key_data,
8071 int alg_arg,
8072 data_t *input_data)
8073{
8074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8075 psa_key_type_t key_type = key_type_arg;
8076 psa_algorithm_t alg = alg_arg;
8077 size_t key_bits;
8078 unsigned char *signature = NULL;
8079 size_t signature_size;
8080 size_t signature_length = 0xdeadbeef;
8081 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8082
8083 PSA_ASSERT(psa_crypto_init());
8084
8085 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8086 PSA_KEY_USAGE_VERIFY_MESSAGE);
8087 psa_set_key_algorithm(&attributes, alg);
8088 psa_set_key_type(&attributes, key_type);
8089
8090 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8091 &key));
8092 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8093 key_bits = psa_get_key_bits(&attributes);
8094
8095 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8096 TEST_ASSERT(signature_size != 0);
8097 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008098 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008099
8100 PSA_ASSERT(psa_sign_message(key, alg,
8101 input_data->x, input_data->len,
8102 signature, signature_size,
8103 &signature_length));
8104 TEST_LE_U(signature_length, signature_size);
8105 TEST_ASSERT(signature_length > 0);
8106
8107 PSA_ASSERT(psa_verify_message(key, alg,
8108 input_data->x, input_data->len,
8109 signature, signature_length));
8110
8111 if (input_data->len != 0) {
8112 /* Flip a bit in the input and verify that the signature is now
8113 * detected as invalid. Flip a bit at the beginning, not at the end,
8114 * because ECDSA may ignore the last few bits of the input. */
8115 input_data->x[0] ^= 1;
8116 TEST_EQUAL(psa_verify_message(key, alg,
8117 input_data->x, input_data->len,
8118 signature, signature_length),
8119 PSA_ERROR_INVALID_SIGNATURE);
8120 }
8121
8122exit:
8123 psa_reset_key_attributes(&attributes);
8124
8125 psa_destroy_key(key);
8126 mbedtls_free(signature);
8127 PSA_DONE();
8128}
8129/* END_CASE */
8130
8131/* BEGIN_CASE */
8132void verify_message(int key_type_arg,
8133 data_t *key_data,
8134 int alg_arg,
8135 data_t *input_data,
8136 data_t *signature_data)
8137{
8138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8139 psa_key_type_t key_type = key_type_arg;
8140 psa_algorithm_t alg = alg_arg;
8141 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8142
8143 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8144
8145 PSA_ASSERT(psa_crypto_init());
8146
8147 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8148 psa_set_key_algorithm(&attributes, alg);
8149 psa_set_key_type(&attributes, key_type);
8150
8151 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8152 &key));
8153
8154 PSA_ASSERT(psa_verify_message(key, alg,
8155 input_data->x, input_data->len,
8156 signature_data->x, signature_data->len));
8157
8158exit:
8159 psa_reset_key_attributes(&attributes);
8160 psa_destroy_key(key);
8161 PSA_DONE();
8162}
8163/* END_CASE */
8164
8165/* BEGIN_CASE */
8166void verify_message_fail(int key_type_arg,
8167 data_t *key_data,
8168 int alg_arg,
8169 data_t *hash_data,
8170 data_t *signature_data,
8171 int expected_status_arg)
8172{
8173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8174 psa_key_type_t key_type = key_type_arg;
8175 psa_algorithm_t alg = alg_arg;
8176 psa_status_t actual_status;
8177 psa_status_t expected_status = expected_status_arg;
8178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8179
8180 PSA_ASSERT(psa_crypto_init());
8181
8182 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8183 psa_set_key_algorithm(&attributes, alg);
8184 psa_set_key_type(&attributes, key_type);
8185
8186 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8187 &key));
8188
8189 actual_status = psa_verify_message(key, alg,
8190 hash_data->x, hash_data->len,
8191 signature_data->x,
8192 signature_data->len);
8193 TEST_EQUAL(actual_status, expected_status);
8194
8195exit:
8196 psa_reset_key_attributes(&attributes);
8197 psa_destroy_key(key);
8198 PSA_DONE();
8199}
8200/* END_CASE */
8201
8202/* BEGIN_CASE */
8203void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008204 data_t *key_data,
8205 int alg_arg,
8206 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008207 data_t *label,
8208 int expected_output_length_arg,
8209 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008210{
Ronald Cron5425a212020-08-04 14:58:35 +02008211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008212 psa_key_type_t key_type = key_type_arg;
8213 psa_algorithm_t alg = alg_arg;
8214 size_t expected_output_length = expected_output_length_arg;
8215 size_t key_bits;
8216 unsigned char *output = NULL;
8217 size_t output_size;
8218 size_t output_length = ~0;
8219 psa_status_t actual_status;
8220 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008224
Gilles Peskine656896e2018-06-29 19:12:28 +02008225 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8227 psa_set_key_algorithm(&attributes, alg);
8228 psa_set_key_type(&attributes, key_type);
8229 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8230 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008231
8232 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8234 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008235
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8237 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008238 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008239
8240 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 actual_status = psa_asymmetric_encrypt(key, alg,
8242 input_data->x, input_data->len,
8243 label->x, label->len,
8244 output, output_size,
8245 &output_length);
8246 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008247 if (actual_status == PSA_SUCCESS) {
8248 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008249 } else {
8250 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008251 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008252
Gilles Peskine68428122018-06-30 18:42:41 +02008253 /* If the label is empty, the test framework puts a non-null pointer
8254 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008256 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 if (output_size != 0) {
8258 memset(output, 0, output_size);
8259 }
8260 actual_status = psa_asymmetric_encrypt(key, alg,
8261 input_data->x, input_data->len,
8262 NULL, label->len,
8263 output, output_size,
8264 &output_length);
8265 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008266 if (actual_status == PSA_SUCCESS) {
8267 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008268 } else {
8269 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008270 }
Gilles Peskine68428122018-06-30 18:42:41 +02008271 }
8272
Gilles Peskine656896e2018-06-29 19:12:28 +02008273exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008274 /*
8275 * Key attributes may have been returned by psa_get_key_attributes()
8276 * thus reset them as required.
8277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008279
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 psa_destroy_key(key);
8281 mbedtls_free(output);
8282 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008283}
8284/* END_CASE */
8285
8286/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008287void asymmetric_encrypt_decrypt(int key_type_arg,
8288 data_t *key_data,
8289 int alg_arg,
8290 data_t *input_data,
8291 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008292{
Ronald Cron5425a212020-08-04 14:58:35 +02008293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008294 psa_key_type_t key_type = key_type_arg;
8295 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008296 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008297 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008298 size_t output_size;
8299 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008300 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008301 size_t output2_size;
8302 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008304
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008306
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8308 psa_set_key_algorithm(&attributes, alg);
8309 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008310
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8312 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008313
8314 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8316 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008317
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8319 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008320 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008321
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008322 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 TEST_LE_U(output2_size,
8324 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8325 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008326 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008327
Gilles Peskineeebd7382018-06-08 18:11:54 +02008328 /* We test encryption by checking that encrypt-then-decrypt gives back
8329 * the original plaintext because of the non-optional random
8330 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8332 input_data->x, input_data->len,
8333 label->x, label->len,
8334 output, output_size,
8335 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008336 /* We don't know what ciphertext length to expect, but check that
8337 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008339
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8341 output, output_length,
8342 label->x, label->len,
8343 output2, output2_size,
8344 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008345 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008346 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008347
8348exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008349 /*
8350 * Key attributes may have been returned by psa_get_key_attributes()
8351 * thus reset them as required.
8352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008354
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 psa_destroy_key(key);
8356 mbedtls_free(output);
8357 mbedtls_free(output2);
8358 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008359}
8360/* END_CASE */
8361
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008362/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008363void asymmetric_decrypt(int key_type_arg,
8364 data_t *key_data,
8365 int alg_arg,
8366 data_t *input_data,
8367 data_t *label,
8368 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008369{
Ronald Cron5425a212020-08-04 14:58:35 +02008370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371 psa_key_type_t key_type = key_type_arg;
8372 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008373 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008374 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008375 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008376 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008378
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008380
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8382 psa_set_key_algorithm(&attributes, alg);
8383 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008384
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8386 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008387
Gilles Peskine449bd832023-01-11 14:50:10 +01008388 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8389 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008390
8391 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008392 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8393 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008394 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008395
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8397 input_data->x, input_data->len,
8398 label->x, label->len,
8399 output,
8400 output_size,
8401 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008402 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008403 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008404
Gilles Peskine68428122018-06-30 18:42:41 +02008405 /* If the label is empty, the test framework puts a non-null pointer
8406 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008408 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 if (output_size != 0) {
8410 memset(output, 0, output_size);
8411 }
8412 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8413 input_data->x, input_data->len,
8414 NULL, label->len,
8415 output,
8416 output_size,
8417 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008418 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008419 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008420 }
8421
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 psa_reset_key_attributes(&attributes);
8424 psa_destroy_key(key);
8425 mbedtls_free(output);
8426 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008427}
8428/* END_CASE */
8429
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008430/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008431void asymmetric_decrypt_fail(int key_type_arg,
8432 data_t *key_data,
8433 int alg_arg,
8434 data_t *input_data,
8435 data_t *label,
8436 int output_size_arg,
8437 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008438{
Ronald Cron5425a212020-08-04 14:58:35 +02008439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008440 psa_key_type_t key_type = key_type_arg;
8441 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008442 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008443 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008444 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008445 psa_status_t actual_status;
8446 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008448
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008449 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008452
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8454 psa_set_key_algorithm(&attributes, alg);
8455 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008456
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8458 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 actual_status = psa_asymmetric_decrypt(key, alg,
8461 input_data->x, input_data->len,
8462 label->x, label->len,
8463 output, output_size,
8464 &output_length);
8465 TEST_EQUAL(actual_status, expected_status);
8466 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008467
Gilles Peskine68428122018-06-30 18:42:41 +02008468 /* If the label is empty, the test framework puts a non-null pointer
8469 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008471 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 if (output_size != 0) {
8473 memset(output, 0, output_size);
8474 }
8475 actual_status = psa_asymmetric_decrypt(key, alg,
8476 input_data->x, input_data->len,
8477 NULL, label->len,
8478 output, output_size,
8479 &output_length);
8480 TEST_EQUAL(actual_status, expected_status);
8481 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008482 }
8483
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 psa_reset_key_attributes(&attributes);
8486 psa_destroy_key(key);
8487 mbedtls_free(output);
8488 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008489}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008490/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008491
8492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008493void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008494{
8495 /* Test each valid way of initializing the object, except for `= {0}`, as
8496 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8497 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008498 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008499 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008501 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8502 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008503
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008505
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008506 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8508 PSA_ERROR_BAD_STATE);
8509 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8510 PSA_ERROR_BAD_STATE);
8511 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8512 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008513
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008514 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008515 PSA_ASSERT(psa_key_derivation_abort(&func));
8516 PSA_ASSERT(psa_key_derivation_abort(&init));
8517 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008518}
8519/* END_CASE */
8520
Janos Follath16de4a42019-06-13 16:32:24 +01008521/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008522void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008523{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008524 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008525 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008526 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008527
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008529
Gilles Peskine449bd832023-01-11 14:50:10 +01008530 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8531 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008532
8533exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 psa_key_derivation_abort(&operation);
8535 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008536}
8537/* END_CASE */
8538
Janos Follathaf3c2a02019-06-12 12:34:34 +01008539/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308540void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008542{
8543 psa_algorithm_t alg = alg_arg;
8544 size_t capacity = capacity_arg;
8545 psa_status_t expected_status = expected_status_arg;
8546 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8547
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008549
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008551
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8553 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008554
8555exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 psa_key_derivation_abort(&operation);
8557 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008558}
8559/* END_CASE */
8560
8561/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308562void parse_binary_string_test(data_t *input, int output)
8563{
8564 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308565 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308566 TEST_EQUAL(value, output);
8567}
8568/* END_CASE */
8569
8570/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008571void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308572 int step_arg1, int key_type_arg1, data_t *input1,
8573 int expected_status_arg1,
8574 int step_arg2, int key_type_arg2, data_t *input2,
8575 int expected_status_arg2,
8576 int step_arg3, int key_type_arg3, data_t *input3,
8577 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008579{
8580 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308582 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 psa_status_t expected_statuses[] = { expected_status_arg1,
8584 expected_status_arg2,
8585 expected_status_arg3 };
8586 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008587 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8588 MBEDTLS_SVC_KEY_ID_INIT,
8589 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008590 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8592 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008593 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008594 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008595 psa_status_t expected_output_status = expected_output_status_arg;
8596 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008599
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8601 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008602
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008604
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8606 mbedtls_test_set_step(i);
8607 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008608 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308609 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8610 key_types[i] != INPUT_INTEGER) {
8611 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 PSA_ASSERT(psa_import_key(&attributes,
8613 inputs[i]->x, inputs[i]->len,
8614 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308615 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008617 // When taking a private key as secret input, use key agreement
8618 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8620 &operation, keys[i]),
8621 expected_statuses[i]);
8622 } else {
8623 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8624 keys[i]),
8625 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008626 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308628 if (key_types[i] == INPUT_INTEGER) {
8629 TEST_EQUAL(psa_key_derivation_input_integer(
8630 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308631 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308632 expected_statuses[i]);
8633 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308634 TEST_EQUAL(psa_key_derivation_input_bytes(
8635 &operation, steps[i],
8636 inputs[i]->x, inputs[i]->len),
8637 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308638 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008639 }
8640 }
8641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 if (output_key_type != PSA_KEY_TYPE_NONE) {
8643 psa_reset_key_attributes(&attributes);
8644 psa_set_key_type(&attributes, output_key_type);
8645 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008646 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 psa_key_derivation_output_key(&attributes, &operation,
8648 &output_key);
8649 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008650 uint8_t buffer[1];
8651 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 psa_key_derivation_output_bytes(&operation,
8653 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008654 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008656
Janos Follathaf3c2a02019-06-12 12:34:34 +01008657exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 psa_key_derivation_abort(&operation);
8659 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8660 psa_destroy_key(keys[i]);
8661 }
8662 psa_destroy_key(output_key);
8663 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008664}
8665/* END_CASE */
8666
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308667/* BEGIN_CASE*/
8668void derive_input_invalid_cost(int alg_arg, int64_t cost)
8669{
8670 psa_algorithm_t alg = alg_arg;
8671 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8672
8673 PSA_ASSERT(psa_crypto_init());
8674 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8675
8676 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8677 PSA_KEY_DERIVATION_INPUT_COST,
8678 cost),
8679 PSA_ERROR_NOT_SUPPORTED);
8680
8681exit:
8682 psa_key_derivation_abort(&operation);
8683 PSA_DONE();
8684}
8685/* END_CASE*/
8686
Janos Follathd958bb72019-07-03 15:02:16 +01008687/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008688void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008689{
Janos Follathd958bb72019-07-03 15:02:16 +01008690 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008692 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008693 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008694 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008696 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008698 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008700 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8701 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008704
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008706
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8708 psa_set_key_algorithm(&attributes, alg);
8709 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008710
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 PSA_ASSERT(psa_import_key(&attributes,
8712 key_data, sizeof(key_data),
8713 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008714
8715 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8717 input1, input1_length,
8718 input2, input2_length,
8719 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008722
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008723 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8725 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008726
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008728
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8730 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008731
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008732exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 psa_key_derivation_abort(&operation);
8734 psa_destroy_key(key);
8735 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008736}
8737/* END_CASE */
8738
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008739/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008740void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008741{
8742 uint8_t output_buffer[16];
8743 size_t buffer_size = 16;
8744 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008745 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008746
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8748 output_buffer, buffer_size)
8749 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008750
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8752 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008753
Gilles Peskine449bd832023-01-11 14:50:10 +01008754 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008755
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8757 output_buffer, buffer_size)
8758 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8761 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008762
8763exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008765}
8766/* END_CASE */
8767
8768/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008769void derive_output(int alg_arg,
8770 int step1_arg, data_t *input1, int expected_status_arg1,
8771 int step2_arg, data_t *input2, int expected_status_arg2,
8772 int step3_arg, data_t *input3, int expected_status_arg3,
8773 int step4_arg, data_t *input4, int expected_status_arg4,
8774 data_t *key_agreement_peer_key,
8775 int requested_capacity_arg,
8776 data_t *expected_output1,
8777 data_t *expected_output2,
8778 int other_key_input_type,
8779 int key_input_type,
8780 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008781{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008782 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8784 data_t *inputs[] = { input1, input2, input3, input4 };
8785 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8786 MBEDTLS_SVC_KEY_ID_INIT,
8787 MBEDTLS_SVC_KEY_ID_INIT,
8788 MBEDTLS_SVC_KEY_ID_INIT };
8789 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8790 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008791 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008792 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008793 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008795 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008797 size_t output_buffer_size = 0;
8798 uint8_t *output_buffer = NULL;
8799 size_t expected_capacity;
8800 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008801 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8802 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8803 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8804 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008805 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008806 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008807 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008808
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8810 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008811 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 }
8813 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008814 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008816 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008817 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008819
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008820 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8822 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8823 requested_capacity));
8824 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8825 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008826 case 0:
8827 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308828 case PSA_KEY_DERIVATION_INPUT_COST:
8829 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308830 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308831 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308832 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308833 if (statuses[i] != PSA_SUCCESS) {
8834 goto exit;
8835 }
8836 break;
8837 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008838 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008840 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 TEST_EQUAL(psa_key_derivation_input_bytes(
8842 &operation, steps[i],
8843 inputs[i]->x, inputs[i]->len),
8844 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008845
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008847 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008849 break;
8850 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8852 psa_set_key_algorithm(&attributes1, alg);
8853 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 PSA_ASSERT(psa_import_key(&attributes1,
8856 inputs[i]->x, inputs[i]->len,
8857 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8860 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8861 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8862 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008863 }
8864
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308865 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308867 keys[i]),
8868 statuses[i]);
8869
8870 if (statuses[i] != PSA_SUCCESS) {
8871 goto exit;
8872 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008873 break;
8874 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008875 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008876 break;
8877 }
8878 break;
8879 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008881 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8883 steps[i],
8884 inputs[i]->x,
8885 inputs[i]->len),
8886 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008887 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008888 case 1: // input key, type DERIVE
8889 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8891 psa_set_key_algorithm(&attributes2, alg);
8892 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008893
8894 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 if (other_key_input_type == 11) {
8896 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8897 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 PSA_ASSERT(psa_import_key(&attributes2,
8900 inputs[i]->x, inputs[i]->len,
8901 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008902
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8904 steps[i],
8905 keys[i]),
8906 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008907 break;
8908 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8910 psa_set_key_algorithm(&attributes3, alg);
8911 psa_set_key_type(&attributes3,
8912 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008913
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 PSA_ASSERT(psa_import_key(&attributes3,
8915 inputs[i]->x, inputs[i]->len,
8916 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008917
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 TEST_EQUAL(psa_key_derivation_key_agreement(
8919 &operation,
8920 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8921 keys[i], key_agreement_peer_key->x,
8922 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008923 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008924 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008925 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008926 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008927 }
8928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008930 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008932 break;
8933 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 TEST_EQUAL(psa_key_derivation_input_bytes(
8935 &operation, steps[i],
8936 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008937
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008939 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008941 break;
8942 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008943 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008944
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8946 &current_capacity));
8947 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008948 expected_capacity = requested_capacity;
8949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008951 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8952
8953 /* For output key derivation secret must be provided using
8954 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008956 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008958
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8960 psa_set_key_algorithm(&attributes4, alg);
8961 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8962 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008963
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8965 &derived_key), expected_status);
8966 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008967 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008969 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 status = psa_key_derivation_output_bytes(&operation,
8971 output_buffer, output_sizes[i]);
8972 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008973 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 TEST_ASSERT(status == PSA_SUCCESS ||
8975 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008976 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 } else if (expected_capacity == 0 ||
8978 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008979 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008981 expected_capacity = 0;
8982 continue;
8983 }
8984 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 PSA_ASSERT(status);
8986 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008987 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008988 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008990 /* Check the operation status. */
8991 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8993 &current_capacity));
8994 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008995 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008996 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008998
8999exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 mbedtls_free(output_buffer);
9001 psa_key_derivation_abort(&operation);
9002 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
9003 psa_destroy_key(keys[i]);
9004 }
9005 psa_destroy_key(derived_key);
9006 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009007}
9008/* END_CASE */
9009
9010/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009011void derive_full(int alg_arg,
9012 data_t *key_data,
9013 data_t *input1,
9014 data_t *input2,
9015 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02009016{
Ronald Cron5425a212020-08-04 14:58:35 +02009017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009018 psa_algorithm_t alg = alg_arg;
9019 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009020 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05309021 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02009022 size_t expected_capacity = requested_capacity;
9023 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009025
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02009027
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9029 psa_set_key_algorithm(&attributes, alg);
9030 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009031
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9033 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9036 input1->x, input1->len,
9037 input2->x, input2->len,
9038 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009040 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009041
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9043 &current_capacity));
9044 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009045
9046 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 while (current_capacity > 0) {
9048 size_t read_size = sizeof(output_buffer);
9049 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009050 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 }
9052 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9053 output_buffer,
9054 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009055 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9057 &current_capacity));
9058 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009059 }
9060
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009061 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009062 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9063 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009064
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009066
9067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 psa_key_derivation_abort(&operation);
9069 psa_destroy_key(key);
9070 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009071}
9072/* END_CASE */
9073
Stephan Koch78109f52023-04-12 14:19:36 +02009074/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009075void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9076 int derivation_step,
9077 int capacity, int expected_capacity_status_arg,
9078 data_t *expected_output,
9079 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009080{
9081 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9082 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009083 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009084 uint8_t *output_buffer = NULL;
9085 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009086 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9087 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9088 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009089
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009090 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009091 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009092
Gilles Peskine449bd832023-01-11 14:50:10 +01009093 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9094 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9095 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009096
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9098 step, input->x, input->len),
9099 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009100
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009104
Gilles Peskine449bd832023-01-11 14:50:10 +01009105 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9106 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009107
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 TEST_EQUAL(status, expected_output_status);
9109 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009110 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009111 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009113
9114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 mbedtls_free(output_buffer);
9116 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009117 PSA_DONE();
9118}
9119/* END_CASE */
9120
Janos Follathe60c9052019-07-03 13:51:30 +01009121/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009122void derive_key_exercise(int alg_arg,
9123 data_t *key_data,
9124 data_t *input1,
9125 data_t *input2,
9126 int derived_type_arg,
9127 int derived_bits_arg,
9128 int derived_usage_arg,
9129 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009130{
Ronald Cron5425a212020-08-04 14:58:35 +02009131 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9132 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009133 psa_algorithm_t alg = alg_arg;
9134 psa_key_type_t derived_type = derived_type_arg;
9135 size_t derived_bits = derived_bits_arg;
9136 psa_key_usage_t derived_usage = derived_usage_arg;
9137 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009139 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009140 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009141 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009142
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009144
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9146 psa_set_key_algorithm(&attributes, alg);
9147 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9148 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9149 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009150
9151 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9153 input1->x, input1->len,
9154 input2->x, input2->len,
9155 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 }
Janos Follathe60c9052019-07-03 13:51:30 +01009158
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 psa_set_key_usage_flags(&attributes, derived_usage);
9160 psa_set_key_algorithm(&attributes, derived_alg);
9161 psa_set_key_type(&attributes, derived_type);
9162 psa_set_key_bits(&attributes, derived_bits);
9163 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9164 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009165
9166 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9168 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9169 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009170
9171 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009175
9176exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009177 /*
9178 * Key attributes may have been returned by psa_get_key_attributes()
9179 * thus reset them as required.
9180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009181 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009182
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 psa_key_derivation_abort(&operation);
9184 psa_destroy_key(base_key);
9185 psa_destroy_key(derived_key);
9186 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009187}
9188/* END_CASE */
9189
Janos Follath42fd8882019-07-03 14:17:09 +01009190/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009191void derive_key_export(int alg_arg,
9192 data_t *key_data,
9193 data_t *input1,
9194 data_t *input2,
9195 int bytes1_arg,
9196 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009197{
Ronald Cron5425a212020-08-04 14:58:35 +02009198 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9199 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009200 psa_algorithm_t alg = alg_arg;
9201 size_t bytes1 = bytes1_arg;
9202 size_t bytes2 = bytes2_arg;
9203 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009204 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009205 uint8_t *output_buffer = NULL;
9206 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009207 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9208 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009209 size_t length;
9210
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009211 TEST_CALLOC(output_buffer, capacity);
9212 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009214
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9216 psa_set_key_algorithm(&base_attributes, alg);
9217 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9218 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9219 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009220
9221 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9223 input1->x, input1->len,
9224 input2->x, input2->len,
9225 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 }
Janos Follath42fd8882019-07-03 14:17:09 +01009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9230 output_buffer,
9231 capacity));
9232 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009233
9234 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9236 input1->x, input1->len,
9237 input2->x, input2->len,
9238 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 }
Janos Follath42fd8882019-07-03 14:17:09 +01009241
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9243 psa_set_key_algorithm(&derived_attributes, 0);
9244 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9245 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9246 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9247 &derived_key));
9248 PSA_ASSERT(psa_export_key(derived_key,
9249 export_buffer, bytes1,
9250 &length));
9251 TEST_EQUAL(length, bytes1);
9252 PSA_ASSERT(psa_destroy_key(derived_key));
9253 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9254 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9255 &derived_key));
9256 PSA_ASSERT(psa_export_key(derived_key,
9257 export_buffer + bytes1, bytes2,
9258 &length));
9259 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009260
9261 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009262 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009263 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009264
9265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 mbedtls_free(output_buffer);
9267 mbedtls_free(export_buffer);
9268 psa_key_derivation_abort(&operation);
9269 psa_destroy_key(base_key);
9270 psa_destroy_key(derived_key);
9271 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009272}
9273/* END_CASE */
9274
9275/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009276void derive_key_type(int alg_arg,
9277 data_t *key_data,
9278 data_t *input1,
9279 data_t *input2,
9280 int key_type_arg, int bits_arg,
9281 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009282{
9283 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9284 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9285 const psa_algorithm_t alg = alg_arg;
9286 const psa_key_type_t key_type = key_type_arg;
9287 const size_t bits = bits_arg;
9288 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9289 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009291 uint8_t *export_buffer = NULL;
9292 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9293 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9294 size_t export_length;
9295
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009296 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009298
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9300 psa_set_key_algorithm(&base_attributes, alg);
9301 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9302 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9303 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009304
Gilles Peskine449bd832023-01-11 14:50:10 +01009305 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009306 &operation, base_key, alg,
9307 input1->x, input1->len,
9308 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009309 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009310 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009312
Gilles Peskine449bd832023-01-11 14:50:10 +01009313 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9314 psa_set_key_algorithm(&derived_attributes, 0);
9315 psa_set_key_type(&derived_attributes, key_type);
9316 psa_set_key_bits(&derived_attributes, bits);
9317 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9318 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009319
Gilles Peskine449bd832023-01-11 14:50:10 +01009320 PSA_ASSERT(psa_export_key(derived_key,
9321 export_buffer, export_buffer_size,
9322 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009323 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009324 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009325
9326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009327 mbedtls_free(export_buffer);
9328 psa_key_derivation_abort(&operation);
9329 psa_destroy_key(base_key);
9330 psa_destroy_key(derived_key);
9331 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009332}
9333/* END_CASE */
9334
9335/* BEGIN_CASE */
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009336void derive_key_ext(int alg_arg,
9337 data_t *key_data,
9338 data_t *input1,
9339 data_t *input2,
9340 int key_type_arg, int bits_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009341 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +01009342 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009343 psa_status_t expected_status,
9344 data_t *expected_export)
9345{
9346 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9347 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9348 const psa_algorithm_t alg = alg_arg;
9349 const psa_key_type_t key_type = key_type_arg;
9350 const size_t bits = bits_arg;
Gilles Peskine092ce512024-02-20 12:31:24 +01009351 psa_key_production_parameters_t *params = NULL;
9352 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009353 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9354 const size_t export_buffer_size =
9355 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
9356 uint8_t *export_buffer = NULL;
9357 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9358 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9359 size_t export_length;
9360
9361 TEST_CALLOC(export_buffer, export_buffer_size);
9362 PSA_ASSERT(psa_crypto_init());
9363
9364 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9365 psa_set_key_algorithm(&base_attributes, alg);
9366 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9367 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9368 &base_key));
9369
9370 if (mbedtls_test_psa_setup_key_derivation_wrap(
9371 &operation, base_key, alg,
9372 input1->x, input1->len,
9373 input2->x, input2->len,
9374 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
9375 goto exit;
9376 }
9377
9378 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9379 psa_set_key_algorithm(&derived_attributes, 0);
9380 psa_set_key_type(&derived_attributes, key_type);
9381 psa_set_key_bits(&derived_attributes, bits);
Gilles Peskine092ce512024-02-20 12:31:24 +01009382 if (!setup_key_production_parameters(&params, &params_data_length,
9383 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009384 goto exit;
9385 }
9386
9387 TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01009388 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009389 &derived_key),
9390 expected_status);
9391
9392 if (expected_status == PSA_SUCCESS) {
9393 PSA_ASSERT(psa_export_key(derived_key,
9394 export_buffer, export_buffer_size,
9395 &export_length));
9396 TEST_MEMORY_COMPARE(export_buffer, export_length,
9397 expected_export->x, expected_export->len);
9398 }
9399
9400exit:
9401 mbedtls_free(export_buffer);
Gilles Peskine092ce512024-02-20 12:31:24 +01009402 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009403 psa_key_derivation_abort(&operation);
9404 psa_destroy_key(base_key);
9405 psa_destroy_key(derived_key);
9406 PSA_DONE();
9407}
9408/* END_CASE */
9409
9410/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009411void derive_key(int alg_arg,
9412 data_t *key_data, data_t *input1, data_t *input2,
9413 int type_arg, int bits_arg,
9414 int expected_status_arg,
9415 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009416{
Ronald Cron5425a212020-08-04 14:58:35 +02009417 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9418 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009419 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009420 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009421 size_t bits = bits_arg;
9422 psa_status_t expected_status = expected_status_arg;
9423 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9424 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9425 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9426
Gilles Peskine449bd832023-01-11 14:50:10 +01009427 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009428
Gilles Peskine449bd832023-01-11 14:50:10 +01009429 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9430 psa_set_key_algorithm(&base_attributes, alg);
9431 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9432 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9433 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009434
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9436 input1->x, input1->len,
9437 input2->x, input2->len,
9438 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009441
Gilles Peskine449bd832023-01-11 14:50:10 +01009442 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9443 psa_set_key_algorithm(&derived_attributes, 0);
9444 psa_set_key_type(&derived_attributes, type);
9445 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009446
9447 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009448 psa_key_derivation_output_key(&derived_attributes,
9449 &operation,
9450 &derived_key);
9451 if (is_large_output > 0) {
9452 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9453 }
9454 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009455
9456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 psa_key_derivation_abort(&operation);
9458 psa_destroy_key(base_key);
9459 psa_destroy_key(derived_key);
9460 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009461}
9462/* END_CASE */
9463
9464/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009465void key_agreement_setup(int alg_arg,
9466 int our_key_type_arg, int our_key_alg_arg,
9467 data_t *our_key_data, data_t *peer_key_data,
9468 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009469{
Ronald Cron5425a212020-08-04 14:58:35 +02009470 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009471 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009472 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009473 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009474 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009476 psa_status_t expected_status = expected_status_arg;
9477 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009478
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009480
Gilles Peskine449bd832023-01-11 14:50:10 +01009481 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9482 psa_set_key_algorithm(&attributes, our_key_alg);
9483 psa_set_key_type(&attributes, our_key_type);
9484 PSA_ASSERT(psa_import_key(&attributes,
9485 our_key_data->x, our_key_data->len,
9486 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009487
Gilles Peskine77f40d82019-04-11 21:27:06 +02009488 /* The tests currently include inputs that should fail at either step.
9489 * Test cases that fail at the setup step should be changed to call
9490 * key_derivation_setup instead, and this function should be renamed
9491 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 status = psa_key_derivation_setup(&operation, alg);
9493 if (status == PSA_SUCCESS) {
9494 TEST_EQUAL(psa_key_derivation_key_agreement(
9495 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9496 our_key,
9497 peer_key_data->x, peer_key_data->len),
9498 expected_status);
9499 } else {
9500 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009501 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009502
9503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 psa_key_derivation_abort(&operation);
9505 psa_destroy_key(our_key);
9506 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009507}
9508/* END_CASE */
9509
9510/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009511void raw_key_agreement(int alg_arg,
9512 int our_key_type_arg, data_t *our_key_data,
9513 data_t *peer_key_data,
9514 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009515{
Ronald Cron5425a212020-08-04 14:58:35 +02009516 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009517 psa_algorithm_t alg = alg_arg;
9518 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009520 unsigned char *output = NULL;
9521 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009522 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009523
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9527 psa_set_key_algorithm(&attributes, alg);
9528 psa_set_key_type(&attributes, our_key_type);
9529 PSA_ASSERT(psa_import_key(&attributes,
9530 our_key_data->x, our_key_data->len,
9531 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009532
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9534 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009535
Gilles Peskine992bee82022-04-13 23:25:52 +02009536 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009537 TEST_LE_U(expected_output->len,
9538 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9539 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9540 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009541
9542 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009543 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9545 peer_key_data->x, peer_key_data->len,
9546 output, expected_output->len,
9547 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009548 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009549 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009551 output = NULL;
9552 output_length = ~0;
9553
9554 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009555 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9557 peer_key_data->x, peer_key_data->len,
9558 output, expected_output->len + 1,
9559 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009560 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009561 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009563 output = NULL;
9564 output_length = ~0;
9565
9566 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009567 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9569 peer_key_data->x, peer_key_data->len,
9570 output, expected_output->len - 1,
9571 &output_length),
9572 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009573 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 TEST_LE_U(output_length, expected_output->len - 1);
9575 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009576 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009577
9578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 mbedtls_free(output);
9580 psa_destroy_key(our_key);
9581 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009582}
9583/* END_CASE */
9584
9585/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009586void key_agreement_capacity(int alg_arg,
9587 int our_key_type_arg, data_t *our_key_data,
9588 data_t *peer_key_data,
9589 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009590{
Ronald Cron5425a212020-08-04 14:58:35 +02009591 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009592 psa_algorithm_t alg = alg_arg;
9593 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009594 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009596 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009597 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009600
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9602 psa_set_key_algorithm(&attributes, alg);
9603 psa_set_key_type(&attributes, our_key_type);
9604 PSA_ASSERT(psa_import_key(&attributes,
9605 our_key_data->x, our_key_data->len,
9606 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009607
Gilles Peskine449bd832023-01-11 14:50:10 +01009608 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9609 PSA_ASSERT(psa_key_derivation_key_agreement(
9610 &operation,
9611 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9612 peer_key_data->x, peer_key_data->len));
9613 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009614 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9616 PSA_KEY_DERIVATION_INPUT_INFO,
9617 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009618 }
Gilles Peskine59685592018-09-18 12:11:34 +02009619
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009620 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 PSA_ASSERT(psa_key_derivation_get_capacity(
9622 &operation, &actual_capacity));
9623 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009624
Gilles Peskinebf491972018-10-25 22:36:12 +02009625 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 while (actual_capacity > sizeof(output)) {
9627 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9628 output, sizeof(output)));
9629 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009630 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9632 output, actual_capacity));
9633 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9634 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009635
Gilles Peskine59685592018-09-18 12:11:34 +02009636exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009637 psa_key_derivation_abort(&operation);
9638 psa_destroy_key(our_key);
9639 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009640}
9641/* END_CASE */
9642
Valerio Settiad819672023-12-29 12:14:41 +01009643/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9644void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009645{
9646 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009647 psa_ecc_family_t ecc_family = psa_family_arg;
9648 size_t bits = bits_arg;
9649 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009650
Valerio Settiad819672023-12-29 12:14:41 +01009651 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9652 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009653 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009654}
9655/* END_CASE */
9656
Valerio Settiac739522024-01-04 10:22:01 +01009657/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9658void ecc_conversion_functions_fail()
9659{
9660 size_t bits;
9661
Valerio Settidb6e0292024-01-05 10:15:45 +01009662 /* Invalid legacy curve identifiers. */
9663 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9664 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009665 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9666 TEST_EQUAL(0, bits);
9667
9668 /* Invalid PSA EC family. */
9669 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9670 /* Invalid bit-size for a valid EC family. */
9671 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9672
9673 /* Twisted-Edward curves are not supported yet. */
9674 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9675 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9676 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9677 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9678}
9679/* END_CASE */
9680
9681
Valerio Settibf999cb2023-12-28 17:48:13 +01009682/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009683void key_agreement_output(int alg_arg,
9684 int our_key_type_arg, data_t *our_key_data,
9685 data_t *peer_key_data,
9686 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009687{
Ronald Cron5425a212020-08-04 14:58:35 +02009688 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009689 psa_algorithm_t alg = alg_arg;
9690 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009691 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009693 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009694
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009695 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009696 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009697
Gilles Peskine449bd832023-01-11 14:50:10 +01009698 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009699
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9701 psa_set_key_algorithm(&attributes, alg);
9702 psa_set_key_type(&attributes, our_key_type);
9703 PSA_ASSERT(psa_import_key(&attributes,
9704 our_key_data->x, our_key_data->len,
9705 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009706
Gilles Peskine449bd832023-01-11 14:50:10 +01009707 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9708 PSA_ASSERT(psa_key_derivation_key_agreement(
9709 &operation,
9710 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9711 peer_key_data->x, peer_key_data->len));
9712 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009713 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009714 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9715 PSA_KEY_DERIVATION_INPUT_INFO,
9716 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009717 }
Gilles Peskine59685592018-09-18 12:11:34 +02009718
Gilles Peskine449bd832023-01-11 14:50:10 +01009719 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9720 actual_output,
9721 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009722 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009723 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009724 if (expected_output2->len != 0) {
9725 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9726 actual_output,
9727 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009728 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009729 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009730 }
Gilles Peskine59685592018-09-18 12:11:34 +02009731
9732exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009733 psa_key_derivation_abort(&operation);
9734 psa_destroy_key(our_key);
9735 PSA_DONE();
9736 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009737}
9738/* END_CASE */
9739
9740/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009741void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009742{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009743 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009744 unsigned char *output = NULL;
9745 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009746 size_t i;
9747 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009748
Gilles Peskine449bd832023-01-11 14:50:10 +01009749 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009750
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009751 TEST_CALLOC(output, bytes);
9752 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009753
Gilles Peskine449bd832023-01-11 14:50:10 +01009754 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009755
Gilles Peskinea50d7392018-06-21 10:22:13 +02009756 /* Run several times, to ensure that every output byte will be
9757 * nonzero at least once with overwhelming probability
9758 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009759 for (run = 0; run < 10; run++) {
9760 if (bytes != 0) {
9761 memset(output, 0, bytes);
9762 }
9763 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009764
Gilles Peskine449bd832023-01-11 14:50:10 +01009765 for (i = 0; i < bytes; i++) {
9766 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009767 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009768 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009769 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009770 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009771
9772 /* Check that every byte was changed to nonzero at least once. This
9773 * validates that psa_generate_random is overwriting every byte of
9774 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 for (i = 0; i < bytes; i++) {
9776 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009777 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009778
9779exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 PSA_DONE();
9781 mbedtls_free(output);
9782 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009783}
9784/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009785
9786/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009787void generate_key(int type_arg,
9788 int bits_arg,
9789 int usage_arg,
9790 int alg_arg,
9791 int expected_status_arg,
9792 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009793{
Ronald Cron5425a212020-08-04 14:58:35 +02009794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009795 psa_key_type_t type = type_arg;
9796 psa_key_usage_t usage = usage_arg;
9797 size_t bits = bits_arg;
9798 psa_algorithm_t alg = alg_arg;
9799 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009801 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009802
Gilles Peskine449bd832023-01-11 14:50:10 +01009803 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009804
Gilles Peskine449bd832023-01-11 14:50:10 +01009805 psa_set_key_usage_flags(&attributes, usage);
9806 psa_set_key_algorithm(&attributes, alg);
9807 psa_set_key_type(&attributes, type);
9808 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009809
9810 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009811 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009812
Gilles Peskine449bd832023-01-11 14:50:10 +01009813 if (is_large_key > 0) {
9814 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9815 }
9816 TEST_EQUAL(status, expected_status);
9817 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009818 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009819 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009820
9821 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009822 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9823 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9824 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009825
Gilles Peskine818ca122018-06-20 18:16:48 +02009826 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009827 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009828 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009829 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009830
9831exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009832 /*
9833 * Key attributes may have been returned by psa_get_key_attributes()
9834 * thus reset them as required.
9835 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009836 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009837
Gilles Peskine449bd832023-01-11 14:50:10 +01009838 psa_destroy_key(key);
9839 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009840}
9841/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009842
Valerio Setti19fec542023-07-25 12:31:50 +02009843/* 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 +01009844void generate_key_rsa(int bits_arg,
9845 data_t *e_arg,
9846 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009847{
Ronald Cron5425a212020-08-04 14:58:35 +02009848 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009849 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009850 size_t bits = bits_arg;
9851 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9852 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9853 psa_status_t expected_status = expected_status_arg;
9854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009855 uint8_t *e_read_buffer = NULL;
9856 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009857 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009858 size_t e_read_length = SIZE_MAX;
9859
Gilles Peskine449bd832023-01-11 14:50:10 +01009860 if (e_arg->len == 0 ||
9861 (e_arg->len == 3 &&
9862 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009863 is_default_public_exponent = 1;
9864 e_read_size = 0;
9865 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009866 TEST_CALLOC(e_read_buffer, e_read_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009867
Gilles Peskine449bd832023-01-11 14:50:10 +01009868 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009869
Gilles Peskine449bd832023-01-11 14:50:10 +01009870 psa_set_key_usage_flags(&attributes, usage);
9871 psa_set_key_algorithm(&attributes, alg);
9872 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9873 e_arg->x, e_arg->len));
9874 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009875
9876 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009877 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9878 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009879 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009880 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009881
9882 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009883 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9884 TEST_EQUAL(psa_get_key_type(&attributes), type);
9885 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009886 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9887 e_read_buffer, e_read_size,
9888 &e_read_length);
9889
9890
9891#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9892 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9893 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009894 if (is_default_public_exponent) {
9895 TEST_EQUAL(e_read_length, 0);
9896 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009897 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009898 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009899 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009900#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009901 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009902 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009903#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009904
9905 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009906 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009907 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009908 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009909
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01009910 TEST_ASSERT(rsa_test_e(key, bits, e_arg));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009911
9912exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009913 /*
9914 * Key attributes may have been returned by psa_get_key_attributes() or
9915 * set by psa_set_key_domain_parameters() thus reset them as required.
9916 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009917 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009918
Gilles Peskine449bd832023-01-11 14:50:10 +01009919 psa_destroy_key(key);
9920 PSA_DONE();
9921 mbedtls_free(e_read_buffer);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009922}
9923/* END_CASE */
9924
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009925/* BEGIN_CASE */
9926void generate_key_ext(int type_arg,
9927 int bits_arg,
9928 int usage_arg,
9929 int alg_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009930 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +01009931 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009932 int expected_status_arg)
9933{
9934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9935 psa_key_type_t type = type_arg;
9936 psa_key_usage_t usage = usage_arg;
9937 size_t bits = bits_arg;
9938 psa_algorithm_t alg = alg_arg;
9939 psa_status_t expected_status = expected_status_arg;
9940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine092ce512024-02-20 12:31:24 +01009941 psa_key_production_parameters_t *params = NULL;
9942 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009943 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
9944
9945 PSA_ASSERT(psa_crypto_init());
9946
9947 psa_set_key_usage_flags(&attributes, usage);
9948 psa_set_key_algorithm(&attributes, alg);
9949 psa_set_key_type(&attributes, type);
9950 psa_set_key_bits(&attributes, bits);
9951
Gilles Peskine092ce512024-02-20 12:31:24 +01009952 if (!setup_key_production_parameters(&params, &params_data_length,
9953 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009954 goto exit;
9955 }
9956
9957 /* Generate a key */
9958 psa_status_t status = psa_generate_key_ext(&attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01009959 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009960 &key);
9961
9962 TEST_EQUAL(status, expected_status);
9963 if (expected_status != PSA_SUCCESS) {
9964 goto exit;
9965 }
9966
9967 /* Test the key information */
9968 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9969 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9970 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
9971
Gilles Peskine7a18f962024-02-12 16:48:11 +01009972#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
9973 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01009974 TEST_ASSERT(rsa_test_e(key, bits, params_data));
Gilles Peskine7a18f962024-02-12 16:48:11 +01009975 }
9976#endif
9977
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009978 /* Do something with the key according to its type and permitted usage. */
9979 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
9980 goto exit;
9981 }
9982
9983exit:
9984 /*
9985 * Key attributes may have been returned by psa_get_key_attributes()
9986 * thus reset them as required.
9987 */
9988 psa_reset_key_attributes(&got_attributes);
Gilles Peskine092ce512024-02-20 12:31:24 +01009989 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009990 psa_destroy_key(key);
9991 PSA_DONE();
9992}
9993/* END_CASE */
9994
9995/* BEGIN_CASE */
Gilles Peskine092ce512024-02-20 12:31:24 +01009996void key_production_parameters_init()
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009997{
Gilles Peskine092ce512024-02-20 12:31:24 +01009998 psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT;
9999 psa_key_production_parameters_t zero;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010000 memset(&zero, 0, sizeof(zero));
10001
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010002 TEST_EQUAL(init.flags, 0);
10003 TEST_EQUAL(zero.flags, 0);
10004}
10005/* END_CASE */
10006
Darryl Greend49a4992018-06-18 17:27:26 +010010007/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010010008void persistent_key_load_key_from_storage(data_t *data,
10009 int type_arg, int bits_arg,
10010 int usage_flags_arg, int alg_arg,
10011 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +010010012{
Gilles Peskine449bd832023-01-11 14:50:10 +010010013 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +020010015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10016 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010017 psa_key_type_t type = type_arg;
10018 size_t bits = bits_arg;
10019 psa_key_usage_t usage_flags = usage_flags_arg;
10020 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +020010021 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +010010022 unsigned char *first_export = NULL;
10023 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010010024 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -070010025 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +010010026 size_t second_exported_length;
10027
Gilles Peskine449bd832023-01-11 14:50:10 +010010028 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010029 TEST_CALLOC(first_export, export_size);
10030 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010031 }
Darryl Greend49a4992018-06-18 17:27:26 +010010032
Gilles Peskine449bd832023-01-11 14:50:10 +010010033 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010034
Gilles Peskine449bd832023-01-11 14:50:10 +010010035 psa_set_key_id(&attributes, key_id);
10036 psa_set_key_usage_flags(&attributes, usage_flags);
10037 psa_set_key_algorithm(&attributes, alg);
10038 psa_set_key_type(&attributes, type);
10039 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +010010040
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010042 case IMPORT_KEY:
10043 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010044 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
10045 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010046 break;
Darryl Greend49a4992018-06-18 17:27:26 +010010047
Darryl Green0c6575a2018-11-07 16:05:30 +000010048 case GENERATE_KEY:
10049 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010050 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010051 break;
10052
10053 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +010010054#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +010010055 {
10056 /* Create base key */
10057 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
10058 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
10059 psa_set_key_usage_flags(&base_attributes,
10060 PSA_KEY_USAGE_DERIVE);
10061 psa_set_key_algorithm(&base_attributes, derive_alg);
10062 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
10063 PSA_ASSERT(psa_import_key(&base_attributes,
10064 data->x, data->len,
10065 &base_key));
10066 /* Derive a key. */
10067 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
10068 PSA_ASSERT(psa_key_derivation_input_key(
10069 &operation,
10070 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
10071 PSA_ASSERT(psa_key_derivation_input_bytes(
10072 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
10073 NULL, 0));
10074 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
10075 &operation,
10076 &key));
10077 PSA_ASSERT(psa_key_derivation_abort(&operation));
10078 PSA_ASSERT(psa_destroy_key(base_key));
10079 base_key = MBEDTLS_SVC_KEY_ID_INIT;
10080 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010081#else
Gilles Peskine449bd832023-01-11 14:50:10 +010010082 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010083#endif
10084 break;
10085
10086 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010010087 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010088 break;
Darryl Green0c6575a2018-11-07 16:05:30 +000010089 }
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +010010091
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010092 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10094 PSA_ASSERT(psa_export_key(key,
10095 first_export, export_size,
10096 &first_exported_length));
10097 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010098 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010099 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +010010100 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010101 }
Darryl Greend49a4992018-06-18 17:27:26 +010010102
10103 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +010010104 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010105 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +010010106 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010107
Darryl Greend49a4992018-06-18 17:27:26 +010010108 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
10110 TEST_ASSERT(mbedtls_svc_key_id_equal(
10111 psa_get_key_id(&attributes), key_id));
10112 TEST_EQUAL(psa_get_key_lifetime(&attributes),
10113 PSA_KEY_LIFETIME_PERSISTENT);
10114 TEST_EQUAL(psa_get_key_type(&attributes), type);
10115 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
10116 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
10117 mbedtls_test_update_key_usage_flags(usage_flags));
10118 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +010010119
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010120 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010121 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10122 PSA_ASSERT(psa_export_key(key,
10123 second_export, export_size,
10124 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010125 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010126 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +000010127 }
10128
10129 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010130 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010132 }
Darryl Greend49a4992018-06-18 17:27:26 +010010133
10134exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010135 /*
10136 * Key attributes may have been returned by psa_get_key_attributes()
10137 * thus reset them as required.
10138 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010139 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010140
Gilles Peskine449bd832023-01-11 14:50:10 +010010141 mbedtls_free(first_export);
10142 mbedtls_free(second_export);
10143 psa_key_derivation_abort(&operation);
10144 psa_destroy_key(base_key);
10145 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010146 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +010010147}
10148/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +020010149
Neil Armstronga557cb82022-06-10 08:58:32 +020010150/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010151void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
10152 int primitive_arg, int hash_arg, int role_arg,
10153 int test_input, data_t *pw_data,
10154 int inj_err_type_arg,
10155 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010156{
10157 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10158 psa_pake_operation_t operation = psa_pake_operation_init();
10159 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010160 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +020010161 psa_key_type_t key_type_pw = key_type_pw_arg;
10162 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010163 psa_algorithm_t hash_alg = hash_arg;
10164 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010167 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
10168 psa_status_t expected_error = expected_error_arg;
10169 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010170 unsigned char *output_buffer = NULL;
10171 size_t output_len = 0;
10172
Gilles Peskine449bd832023-01-11 14:50:10 +010010173 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010174
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010175 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +010010176 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010177 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010178
Gilles Peskine449bd832023-01-11 14:50:10 +010010179 if (pw_data->len > 0) {
10180 psa_set_key_usage_flags(&attributes, key_usage_pw);
10181 psa_set_key_algorithm(&attributes, alg);
10182 psa_set_key_type(&attributes, key_type_pw);
10183 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10184 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010185 }
10186
Gilles Peskine449bd832023-01-11 14:50:10 +010010187 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10188 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10189 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010190
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010192
Gilles Peskine449bd832023-01-11 14:50:10 +010010193 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10194 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10195 expected_error);
10196 PSA_ASSERT(psa_pake_abort(&operation));
10197 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10198 expected_error);
10199 PSA_ASSERT(psa_pake_abort(&operation));
10200 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10201 expected_error);
10202 PSA_ASSERT(psa_pake_abort(&operation));
10203 TEST_EQUAL(psa_pake_set_role(&operation, role),
10204 expected_error);
10205 PSA_ASSERT(psa_pake_abort(&operation));
10206 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10207 NULL, 0, NULL),
10208 expected_error);
10209 PSA_ASSERT(psa_pake_abort(&operation));
10210 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10211 expected_error);
10212 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010213 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010214 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010215
Gilles Peskine449bd832023-01-11 14:50:10 +010010216 status = psa_pake_setup(&operation, &cipher_suite);
10217 if (status != PSA_SUCCESS) {
10218 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010219 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010220 }
10221
Gilles Peskine449bd832023-01-11 14:50:10 +010010222 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10223 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10224 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010225 goto exit;
10226 }
10227
Gilles Peskine449bd832023-01-11 14:50:10 +010010228 status = psa_pake_set_role(&operation, role);
10229 if (status != PSA_SUCCESS) {
10230 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010231 goto exit;
10232 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010233
Gilles Peskine449bd832023-01-11 14:50:10 +010010234 if (pw_data->len > 0) {
10235 status = psa_pake_set_password_key(&operation, key);
10236 if (status != PSA_SUCCESS) {
10237 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010238 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010239 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010240 }
10241
Gilles Peskine449bd832023-01-11 14:50:10 +010010242 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10243 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10244 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010245 goto exit;
10246 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010247
Gilles Peskine449bd832023-01-11 14:50:10 +010010248 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10249 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10250 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010251 goto exit;
10252 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010253
Gilles Peskine449bd832023-01-11 14:50:10 +010010254 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010255 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010256 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10257 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010258 goto exit;
10259 }
10260
Gilles Peskine449bd832023-01-11 14:50:10 +010010261 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010262 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010263 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10264 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010265 goto exit;
10266 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010267
Gilles Peskine449bd832023-01-11 14:50:10 +010010268 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10269 PSA_PAKE_STEP_KEY_SHARE);
10270 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10271 PSA_PAKE_STEP_ZK_PUBLIC);
10272 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10273 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010274
Gilles Peskine449bd832023-01-11 14:50:10 +010010275 if (test_input) {
10276 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10277 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10278 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010279 goto exit;
10280 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010281
Gilles Peskine449bd832023-01-11 14:50:10 +010010282 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10283 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10284 output_buffer, size_zk_proof),
10285 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010286 goto exit;
10287 }
10288
Gilles Peskine449bd832023-01-11 14:50:10 +010010289 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10290 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10291 output_buffer, size_zk_proof),
10292 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010293 goto exit;
10294 }
10295
Gilles Peskine449bd832023-01-11 14:50:10 +010010296 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10297 output_buffer, size_key_share);
10298 if (status != PSA_SUCCESS) {
10299 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010300 goto exit;
10301 }
10302
Gilles Peskine449bd832023-01-11 14:50:10 +010010303 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10304 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10305 output_buffer, size_zk_public + 1),
10306 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010307 goto exit;
10308 }
10309
Gilles Peskine449bd832023-01-11 14:50:10 +010010310 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010311 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010312 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10313 output_buffer, size_zk_public + 1);
10314 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10315 output_buffer, size_zk_public),
10316 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010317 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010318 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010319 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010320 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10321 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10322 NULL, 0, NULL),
10323 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010324 goto exit;
10325 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010326
Gilles Peskine449bd832023-01-11 14:50:10 +010010327 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10328 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10329 output_buffer, buf_size, &output_len),
10330 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010331 goto exit;
10332 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010333
Gilles Peskine449bd832023-01-11 14:50:10 +010010334 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10335 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10336 output_buffer, buf_size, &output_len),
10337 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010338 goto exit;
10339 }
10340
Gilles Peskine449bd832023-01-11 14:50:10 +010010341 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10342 output_buffer, buf_size, &output_len);
10343 if (status != PSA_SUCCESS) {
10344 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010345 goto exit;
10346 }
10347
Gilles Peskine449bd832023-01-11 14:50:10 +010010348 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010349
Gilles Peskine449bd832023-01-11 14:50:10 +010010350 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10351 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10352 output_buffer, size_zk_public - 1, &output_len),
10353 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010354 goto exit;
10355 }
10356
Gilles Peskine449bd832023-01-11 14:50:10 +010010357 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010358 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010359 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10360 output_buffer, size_zk_public - 1, &output_len);
10361 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10362 output_buffer, buf_size, &output_len),
10363 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010364 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010365 }
10366 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010367
10368exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010369 PSA_ASSERT(psa_destroy_key(key));
10370 PSA_ASSERT(psa_pake_abort(&operation));
10371 mbedtls_free(output_buffer);
10372 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010373}
10374/* END_CASE */
10375
Neil Armstronga557cb82022-06-10 08:58:32 +020010376/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010377void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10378 int client_input_first, int inject_error,
10379 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010380{
10381 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10382 psa_pake_operation_t server = psa_pake_operation_init();
10383 psa_pake_operation_t client = psa_pake_operation_init();
10384 psa_algorithm_t alg = alg_arg;
10385 psa_algorithm_t hash_alg = hash_arg;
10386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10388
Gilles Peskine449bd832023-01-11 14:50:10 +010010389 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010390
Gilles Peskine449bd832023-01-11 14:50:10 +010010391 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10392 psa_set_key_algorithm(&attributes, alg);
10393 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10394 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10395 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010396
Gilles Peskine449bd832023-01-11 14:50:10 +010010397 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10398 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10399 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010400
10401
Gilles Peskine449bd832023-01-11 14:50:10 +010010402 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10403 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010404
Gilles Peskine449bd832023-01-11 14:50:10 +010010405 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10406 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010407
Gilles Peskine449bd832023-01-11 14:50:10 +010010408 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10409 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010410
Gilles Peskine449bd832023-01-11 14:50:10 +010010411 ecjpake_do_round(alg, primitive_arg, &server, &client,
10412 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010413
Gilles Peskine449bd832023-01-11 14:50:10 +010010414 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010415 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010416 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010417
Gilles Peskine449bd832023-01-11 14:50:10 +010010418 ecjpake_do_round(alg, primitive_arg, &server, &client,
10419 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010420
10421exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010422 psa_destroy_key(key);
10423 psa_pake_abort(&server);
10424 psa_pake_abort(&client);
10425 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010426}
10427/* END_CASE */
10428
10429/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010430void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10431 int derive_alg_arg, data_t *pw_data,
10432 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010433{
10434 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10435 psa_pake_operation_t server = psa_pake_operation_init();
10436 psa_pake_operation_t client = psa_pake_operation_init();
10437 psa_algorithm_t alg = alg_arg;
10438 psa_algorithm_t hash_alg = hash_arg;
10439 psa_algorithm_t derive_alg = derive_alg_arg;
10440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10442 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010443 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010444 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010445 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010446 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010447
Gilles Peskine449bd832023-01-11 14:50:10 +010010448 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010449
Gilles Peskine449bd832023-01-11 14:50:10 +010010450 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10451 psa_set_key_algorithm(&attributes, alg);
10452 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10453 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10454 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010455
Gilles Peskine449bd832023-01-11 14:50:10 +010010456 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10457 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10458 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010459
Neil Armstrong1e855602022-06-15 11:32:11 +020010460 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010461 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10462 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010463
Gilles Peskine449bd832023-01-11 14:50:10 +010010464 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10465 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10466 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10467 PSA_KEY_DERIVATION_INPUT_SEED,
10468 (const uint8_t *) "", 0));
10469 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10470 PSA_KEY_DERIVATION_INPUT_SEED,
10471 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010472 }
10473
Gilles Peskine449bd832023-01-11 14:50:10 +010010474 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10475 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010476
Gilles Peskine449bd832023-01-11 14:50:10 +010010477 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10478 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010479
Gilles Peskine449bd832023-01-11 14:50:10 +010010480 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10481 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010482
Gilles Peskine449bd832023-01-11 14:50:10 +010010483 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10484 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10485 PSA_ERROR_BAD_STATE);
10486 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10487 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010488 goto exit;
10489 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010490
Neil Armstrongf983caf2022-06-15 15:27:48 +020010491 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010492 ecjpake_do_round(alg, primitive_arg, &server, &client,
10493 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010494
Gilles Peskine449bd832023-01-11 14:50:10 +010010495 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10496 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10497 PSA_ERROR_BAD_STATE);
10498 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10499 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010500 goto exit;
10501 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010502
Neil Armstrongf983caf2022-06-15 15:27:48 +020010503 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010504 ecjpake_do_round(alg, primitive_arg, &server, &client,
10505 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010506
Gilles Peskine449bd832023-01-11 14:50:10 +010010507 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10508 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010509
10510exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010511 psa_key_derivation_abort(&server_derive);
10512 psa_key_derivation_abort(&client_derive);
10513 psa_destroy_key(key);
10514 psa_pake_abort(&server);
10515 psa_pake_abort(&client);
10516 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010517}
10518/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010519
10520/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010521void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010522{
10523 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10524 const size_t bits = 256;
10525 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010526 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010527 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010528 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010529
10530 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10531 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010532 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10533 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10534 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10535 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010536 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010537 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10538 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010539
10540 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010541 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10542 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10543 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10544 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10545 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10546 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010547
10548 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010549 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10550 PSA_PAKE_OUTPUT_MAX_SIZE);
10551 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10552 PSA_PAKE_OUTPUT_MAX_SIZE);
10553 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10554 PSA_PAKE_OUTPUT_MAX_SIZE);
10555 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10556 PSA_PAKE_INPUT_MAX_SIZE);
10557 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10558 PSA_PAKE_INPUT_MAX_SIZE);
10559 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10560 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010561}
10562/* END_CASE */