blob: 9390958378096d0310106ee6a32529be45e8322d [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
Ryan3a1b7862024-03-01 17:24:04 +000031#if defined(MBEDTLS_THREADING_PTHREAD)
32#include "mbedtls/threading.h"
33#endif
34
Gilles Peskine4023c012021-05-27 13:21:20 +020035/* If this comes up, it's a bug in the test code or in the test data. */
36#define UNUSED 0xdeadbeef
37
Dave Rodgman647791d2021-06-23 12:49:59 +010038/* Assert that an operation is (not) active.
39 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010040#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
41#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010042
Przemek Stekiel7c795482022-11-15 22:26:12 +010043#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010044int ecjpake_operation_setup(psa_pake_operation_t *operation,
45 psa_pake_cipher_suite_t *cipher_suite,
46 psa_pake_role_t role,
47 mbedtls_svc_key_id_t key,
48 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010049{
Gilles Peskine449bd832023-01-11 14:50:10 +010050 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 if (key_available) {
57 PSA_ASSERT(psa_pake_set_password_key(operation, key));
58 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010059 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010060exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010061 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010062}
63#endif
64
Jaeden Amerof24c7f82018-06-27 17:20:43 +010065/** An invalid export length that will never be set by psa_export_key(). */
66static const size_t INVALID_EXPORT_LENGTH = ~0U;
67
Gilles Peskinea7aa4422018-08-14 15:17:54 +020068/** Test if a buffer contains a constant byte value.
69 *
70 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020071 *
72 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020073 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074 * \param size Size of the buffer in bytes.
75 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020076 * \return 1 if the buffer is all-bits-zero.
77 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020078 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080{
81 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 for (i = 0; i < size; i++) {
83 if (((unsigned char *) buffer)[i] != c) {
84 return 0;
85 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020086 }
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020088}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010089#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020090/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010091static int asn1_write_10x(unsigned char **p,
92 unsigned char *start,
93 size_t bits,
94 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020095{
96 int ret;
97 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (bits == 0) {
99 return MBEDTLS_ERR_ASN1_INVALID_DATA;
100 }
101 if (bits <= 8 && x >= 1 << (bits - 1)) {
102 return MBEDTLS_ERR_ASN1_INVALID_DATA;
103 }
104 if (*p < start || *p - start < (ptrdiff_t) len) {
105 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
106 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200107 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 (*p)[len-1] = x;
109 if (bits % 8 == 0) {
110 (*p)[1] |= 1;
111 } else {
112 (*p)[0] |= 1 << (bits % 8);
113 }
114 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
115 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
116 MBEDTLS_ASN1_INTEGER));
117 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200118}
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120static int construct_fake_rsa_key(unsigned char *buffer,
121 size_t buffer_size,
122 unsigned char **p,
123 size_t bits,
124 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200125{
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200127 int ret;
128 int len = 0;
129 /* Construct something that looks like a DER encoding of
130 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
131 * RSAPrivateKey ::= SEQUENCE {
132 * version Version,
133 * modulus INTEGER, -- n
134 * publicExponent INTEGER, -- e
135 * privateExponent INTEGER, -- d
136 * prime1 INTEGER, -- p
137 * prime2 INTEGER, -- q
138 * exponent1 INTEGER, -- d mod (p-1)
139 * exponent2 INTEGER, -- d mod (q-1)
140 * coefficient INTEGER, -- (inverse of q) mod p
141 * otherPrimeInfos OtherPrimeInfos OPTIONAL
142 * }
143 * Or, for a public key, the same structure with only
144 * version, modulus and publicExponent.
145 */
146 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (keypair) {
148 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
151 asn1_write_10x(p, buffer, half_bits, 1));
152 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
153 asn1_write_10x(p, buffer, half_bits, 1));
154 MBEDTLS_ASN1_CHK_ADD(len, /* q */
155 asn1_write_10x(p, buffer, half_bits, 1));
156 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
157 asn1_write_10x(p, buffer, half_bits, 3));
158 MBEDTLS_ASN1_CHK_ADD(len, /* d */
159 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200160 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
162 asn1_write_10x(p, buffer, 17, 1));
163 MBEDTLS_ASN1_CHK_ADD(len, /* n */
164 asn1_write_10x(p, buffer, bits, 1));
165 if (keypair) {
166 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
167 mbedtls_asn1_write_int(p, buffer, 0));
168 }
169 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170 {
171 const unsigned char tag =
172 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200174 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200176}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100177#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179int exercise_mac_setup(psa_key_type_t key_type,
180 const unsigned char *key_bytes,
181 size_t key_length,
182 psa_algorithm_t alg,
183 psa_mac_operation_t *operation,
184 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185{
Ronald Cron5425a212020-08-04 14:58:35 +0200186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
190 psa_set_key_algorithm(&attributes, alg);
191 psa_set_key_type(&attributes, key_type);
192 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100195 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100197 /* If setup failed, reproduce the failure, so that the caller can
198 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (*status != PSA_SUCCESS) {
200 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201 }
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 psa_destroy_key(key);
204 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100205
206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 psa_destroy_key(key);
208 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100209}
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211int exercise_cipher_setup(psa_key_type_t key_type,
212 const unsigned char *key_bytes,
213 size_t key_length,
214 psa_algorithm_t alg,
215 psa_cipher_operation_t *operation,
216 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100217{
Ronald Cron5425a212020-08-04 14:58:35 +0200218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
222 psa_set_key_algorithm(&attributes, alg);
223 psa_set_key_type(&attributes, key_type);
224 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100227 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100229 /* If setup failed, reproduce the failure, so that the caller can
230 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (*status != PSA_SUCCESS) {
232 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
233 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234 }
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 psa_destroy_key(key);
237 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100238
239exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 psa_destroy_key(key);
241 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100242}
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200245{
246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200248 uint8_t buffer[1];
249 size_t length;
250 int ok = 0;
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 psa_set_key_id(&attributes, key_id);
253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
254 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
255 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
256 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
257 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200258 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200260 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
262 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
263 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
264 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
265 TEST_EQUAL(psa_get_key_type(&attributes), 0);
266 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
269 PSA_ERROR_INVALID_HANDLE);
270 TEST_EQUAL(psa_export_public_key(key,
271 buffer, sizeof(buffer), &length),
272 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200273
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200274 ok = 1;
275
276exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100277 /*
278 * Key attributes may have been returned by psa_get_key_attributes()
279 * thus reset them as required.
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200284}
285
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200286/* Assert that a key isn't reported as having a slot number. */
287#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100288#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200289 do \
290 { \
291 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 TEST_EQUAL(psa_get_key_slot_number( \
293 attributes, \
294 &ASSERT_NO_SLOT_NUMBER_slot_number), \
295 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200296 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200298#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299#define ASSERT_NO_SLOT_NUMBER(attributes) \
300 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
302
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530303#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
304
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100305/* An overapproximation of the amount of storage needed for a key of the
306 * given type and with the given content. The API doesn't make it easy
307 * to find a good value for the size. The current implementation doesn't
308 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309#define KEY_BITS_FROM_DATA(type, data) \
310 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100311
Darryl Green0c6575a2018-11-07 16:05:30 +0000312typedef enum {
313 IMPORT_KEY = 0,
314 GENERATE_KEY = 1,
315 DERIVE_KEY = 2
316} generate_method;
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100319 DO_NOT_SET_LENGTHS = 0,
320 SET_LENGTHS_BEFORE_NONCE = 1,
321 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100322} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100325 USE_NULL_TAG = 0,
326 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100327} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100328
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530329
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330/*!
331 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100332 * \param key_type_arg Type of key passed in
333 * \param key_data The encryption / decryption key data
334 * \param alg_arg The type of algorithm used
335 * \param nonce Nonce data
336 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100337 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 * feed additional data in to be encrypted /
339 * decrypted. If -1, no chunking.
340 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100341 * \param data_part_len_arg If not -1, the length of chunks to feed
342 * the data in to be encrypted / decrypted. If
343 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100344 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100345 * expected here, this controls whether or not
346 * to set lengths, and in what order with
347 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100350 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100351 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100352 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
355 int alg_arg,
356 data_t *nonce,
357 data_t *additional_data,
358 int ad_part_len_arg,
359 data_t *input_data,
360 int data_part_len_arg,
361 set_lengths_method_t set_lengths_method,
362 data_t *expected_output,
363 int is_encrypt,
364 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100365{
366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
367 psa_key_type_t key_type = key_type_arg;
368 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100369 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 unsigned char *output_data = NULL;
371 unsigned char *part_data = NULL;
372 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100373 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100374 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 size_t output_size = 0;
376 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100377 size_t output_length = 0;
378 size_t key_bits = 0;
379 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100381 size_t part_length = 0;
382 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100384 size_t ad_part_len = 0;
385 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
388 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100391 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (is_encrypt) {
396 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
397 } else {
398 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100399 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100400
401 psa_set_key_algorithm(&attributes, alg);
402 psa_set_key_type(&attributes, key_type);
403
404 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
405 &key));
406
407 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
408 key_bits = psa_get_key_bits(&attributes);
409
410 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
411
412 if (is_encrypt) {
413 /* Tag gets written at end of buffer. */
414 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
415 (input_data->len +
416 tag_length));
417 data_true_size = input_data->len;
418 } else {
419 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
420 (input_data->len -
421 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100422
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100423 /* Do not want to attempt to decrypt tag. */
424 data_true_size = input_data->len - tag_length;
425 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100426
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100427 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (is_encrypt) {
430 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
431 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
432 } else {
433 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
434 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100435 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100436
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100437 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (is_encrypt) {
440 status = psa_aead_encrypt_setup(&operation, key, alg);
441 } else {
442 status = psa_aead_decrypt_setup(&operation, key, alg);
443 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100444
445 /* If the operation is not supported, just skip and not fail in case the
446 * encryption involves a common limitation of cryptography hardwares and
447 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (status == PSA_ERROR_NOT_SUPPORTED) {
449 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
450 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 }
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
457 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
458 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
459 data_true_size));
460 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
461 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
462 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
465 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100466 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100469 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100470 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 part_offset += part_length, part_count++) {
475 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100476 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100478 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100480 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 PSA_ASSERT(psa_aead_update_ad(&operation,
484 additional_data->x + part_offset,
485 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100486
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100489 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
491 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100495 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 data_part_len = (size_t) data_part_len_arg;
497 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
498 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100499
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100500 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100503 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 part_offset += part_length, part_count++) {
505 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100506 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 } else if ((data_true_size - part_offset) < data_part_len) {
508 part_length = (data_true_size - part_offset);
509 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100510 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 }
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 PSA_ASSERT(psa_aead_update(&operation,
514 (input_data->x + part_offset),
515 part_length, part_data,
516 part_data_size,
517 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (output_data && output_part_length) {
520 memcpy((output_data + output_length), part_data,
521 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 }
523
Paul Elliottd3f82412021-06-16 16:52:21 +0100524 output_length += output_part_length;
525 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
529 data_true_size, output_data,
530 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100531 }
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (is_encrypt) {
534 PSA_ASSERT(psa_aead_finish(&operation, final_data,
535 final_output_size,
536 &output_part_length,
537 tag_buffer, tag_length,
538 &tag_size));
539 } else {
540 PSA_ASSERT(psa_aead_verify(&operation, final_data,
541 final_output_size,
542 &output_part_length,
543 (input_data->x + data_true_size),
544 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100545 }
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (output_data && output_part_length) {
548 memcpy((output_data + output_length), final_data,
549 output_part_length);
550 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100551
552 output_length += output_part_length;
553
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100554
555 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
556 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (is_encrypt) {
558 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (output_data && tag_length) {
561 memcpy((output_data + output_length), tag_buffer,
562 tag_length);
563 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564
565 output_length += tag_length;
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 TEST_EQUAL(output_length,
568 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
569 input_data->len));
570 TEST_LE_U(output_length,
571 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
572 } else {
573 TEST_EQUAL(output_length,
574 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
575 input_data->len));
576 TEST_LE_U(output_length,
577 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100578 }
579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100581 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100582 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100583
Paul Elliottd3f82412021-06-16 16:52:21 +0100584
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100585 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100586
587exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 psa_destroy_key(key);
589 psa_aead_abort(&operation);
590 mbedtls_free(output_data);
591 mbedtls_free(part_data);
592 mbedtls_free(final_data);
593 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100596}
597
Neil Armstrong4766f992022-02-28 16:23:59 +0100598/*!
599 * \brief Internal Function for MAC multipart tests.
600 * \param key_type_arg Type of key passed in
601 * \param key_data The encryption / decryption key data
602 * \param alg_arg The type of algorithm used
603 * \param input_data Data to encrypt / decrypt
604 * \param data_part_len_arg If not -1, the length of chunks to feed
605 * the data in to be encrypted / decrypted. If
606 * -1, no chunking
607 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000608 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100609 * \param do_zero_parts If non-zero, interleave zero length chunks
610 * with normal length chunks.
611 * \return int Zero on failure, non-zero on success.
612 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
614 int alg_arg,
615 data_t *input_data,
616 int data_part_len_arg,
617 data_t *expected_output,
618 int is_verify,
619 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100620{
621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
622 psa_key_type_t key_type = key_type_arg;
623 psa_algorithm_t alg = alg_arg;
624 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
625 unsigned char mac[PSA_MAC_MAX_SIZE];
626 size_t part_offset = 0;
627 size_t part_length = 0;
628 size_t data_part_len = 0;
629 size_t mac_len = 0;
630 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
631 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
632
633 int test_ok = 0;
634 size_t part_count = 0;
635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (is_verify) {
639 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
640 } else {
641 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
642 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 psa_set_key_algorithm(&attributes, alg);
645 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
648 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (is_verify) {
651 status = psa_mac_verify_setup(&operation, key, alg);
652 } else {
653 status = psa_mac_sign_setup(&operation, key, alg);
654 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100663 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 part_offset += part_length, part_count++) {
665 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100666 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 } else if ((input_data->len - part_offset) < data_part_len) {
668 part_length = (input_data->len - part_offset);
669 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100670 part_length = data_part_len;
671 }
672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 PSA_ASSERT(psa_mac_update(&operation,
674 (input_data->x + part_offset),
675 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100676 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100678 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
680 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100681 }
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (is_verify) {
684 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
685 expected_output->len));
686 } else {
687 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
688 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100689
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100690 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100691 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100692 }
693
694 test_ok = 1;
695
696exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 psa_destroy_key(key);
698 psa_mac_abort(&operation);
699 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100702}
703
Neil Armstrong75673ab2022-06-15 17:39:01 +0200704#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100705static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
706 psa_pake_operation_t *server,
707 psa_pake_operation_t *client,
708 int client_input_first,
709 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200710{
711 unsigned char *buffer0 = NULL, *buffer1 = NULL;
712 size_t buffer_length = (
713 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
714 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
715 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200716 /* The output should be exactly this size according to the spec */
717 const size_t expected_size_key_share =
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
719 /* The output should be exactly this size according to the spec */
720 const size_t expected_size_zk_public =
721 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
722 /* The output can be smaller: the spec allows stripping leading zeroes */
723 const size_t max_expected_size_zk_proof =
724 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200725 size_t buffer0_off = 0;
726 size_t buffer1_off = 0;
727 size_t s_g1_len, s_g2_len, s_a_len;
728 size_t s_g1_off, s_g2_off, s_a_off;
729 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
730 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
731 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
732 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
733 size_t c_g1_len, c_g2_len, c_a_len;
734 size_t c_g1_off, c_g2_off, c_a_off;
735 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
736 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
737 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
738 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
739 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200740 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200741
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100742 TEST_CALLOC(buffer0, buffer_length);
743 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200746 case 1:
747 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
749 buffer0 + buffer0_off,
750 512 - buffer0_off, &s_g1_len));
751 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200752 s_g1_off = buffer0_off;
753 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
755 buffer0 + buffer0_off,
756 512 - buffer0_off, &s_x1_pk_len));
757 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200758 s_x1_pk_off = buffer0_off;
759 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
761 buffer0 + buffer0_off,
762 512 - buffer0_off, &s_x1_pr_len));
763 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200764 s_x1_pr_off = buffer0_off;
765 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
767 buffer0 + buffer0_off,
768 512 - buffer0_off, &s_g2_len));
769 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200770 s_g2_off = buffer0_off;
771 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
773 buffer0 + buffer0_off,
774 512 - buffer0_off, &s_x2_pk_len));
775 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200776 s_x2_pk_off = buffer0_off;
777 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
779 buffer0 + buffer0_off,
780 512 - buffer0_off, &s_x2_pr_len));
781 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200782 s_x2_pr_off = buffer0_off;
783 buffer0_off += s_x2_pr_len;
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500786 buffer0[s_x1_pr_off + 8] ^= 1;
787 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200788 expected_status = PSA_ERROR_DATA_INVALID;
789 }
790
Neil Armstrong51009d72022-09-05 17:59:54 +0200791 /*
792 * When injecting errors in inputs, the implementation is
793 * free to detect it right away of with a delay.
794 * This permits delaying the error until the end of the input
795 * sequence, if no error appears then, this will be treated
796 * as an error.
797 */
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200800 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
802 buffer0 + s_g1_off, s_g1_len);
803 if (inject_error == 1 && status != PSA_SUCCESS) {
804 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200805 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 } else {
807 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200808 }
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
811 buffer0 + s_x1_pk_off,
812 s_x1_pk_len);
813 if (inject_error == 1 && status != PSA_SUCCESS) {
814 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200815 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 } else {
817 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
821 buffer0 + s_x1_pr_off,
822 s_x1_pr_len);
823 if (inject_error == 1 && status != PSA_SUCCESS) {
824 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200825 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 } else {
827 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
831 buffer0 + s_g2_off,
832 s_g2_len);
833 if (inject_error == 1 && status != PSA_SUCCESS) {
834 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200835 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 } else {
837 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200838 }
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
841 buffer0 + s_x2_pk_off,
842 s_x2_pk_len);
843 if (inject_error == 1 && status != PSA_SUCCESS) {
844 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200845 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 } else {
847 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
851 buffer0 + s_x2_pr_off,
852 s_x2_pr_len);
853 if (inject_error == 1 && status != PSA_SUCCESS) {
854 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200855 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 } else {
857 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200858 }
859
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200860 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (inject_error == 1) {
862 TEST_ASSERT(
863 !"One of the last psa_pake_input() calls should have returned the expected error.");
864 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200865 }
866
867 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
869 buffer1 + buffer1_off,
870 512 - buffer1_off, &c_g1_len));
871 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200872 c_g1_off = buffer1_off;
873 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
875 buffer1 + buffer1_off,
876 512 - buffer1_off, &c_x1_pk_len));
877 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200878 c_x1_pk_off = buffer1_off;
879 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
881 buffer1 + buffer1_off,
882 512 - buffer1_off, &c_x1_pr_len));
883 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200884 c_x1_pr_off = buffer1_off;
885 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
887 buffer1 + buffer1_off,
888 512 - buffer1_off, &c_g2_len));
889 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200890 c_g2_off = buffer1_off;
891 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
893 buffer1 + buffer1_off,
894 512 - buffer1_off, &c_x2_pk_len));
895 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200896 c_x2_pk_off = buffer1_off;
897 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
899 buffer1 + buffer1_off,
900 512 - buffer1_off, &c_x2_pr_len));
901 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 c_x2_pr_off = buffer1_off;
903 buffer1_off += c_x2_pr_len;
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200906 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
908 buffer0 + s_g1_off, s_g1_len);
909 if (inject_error == 1 && status != PSA_SUCCESS) {
910 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200911 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 } else {
913 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200914 }
915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
917 buffer0 + s_x1_pk_off,
918 s_x1_pk_len);
919 if (inject_error == 1 && status != PSA_SUCCESS) {
920 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200921 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 } else {
923 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200924 }
925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
927 buffer0 + s_x1_pr_off,
928 s_x1_pr_len);
929 if (inject_error == 1 && status != PSA_SUCCESS) {
930 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200931 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 } else {
933 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200934 }
935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
937 buffer0 + s_g2_off,
938 s_g2_len);
939 if (inject_error == 1 && status != PSA_SUCCESS) {
940 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200941 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 } else {
943 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200944 }
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
947 buffer0 + s_x2_pk_off,
948 s_x2_pk_len);
949 if (inject_error == 1 && status != PSA_SUCCESS) {
950 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200951 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 } else {
953 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200954 }
955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
957 buffer0 + s_x2_pr_off,
958 s_x2_pr_len);
959 if (inject_error == 1 && status != PSA_SUCCESS) {
960 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200961 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 } else {
963 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200964 }
965
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200966 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (inject_error == 1) {
968 TEST_ASSERT(
969 !"One of the last psa_pake_input() calls should have returned the expected error.");
970 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200971 }
972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500974 buffer1[c_x1_pr_off + 12] ^= 1;
975 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200976 expected_status = PSA_ERROR_DATA_INVALID;
977 }
978
979 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
981 buffer1 + c_g1_off, c_g1_len);
982 if (inject_error == 2 && status != PSA_SUCCESS) {
983 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200984 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 } else {
986 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200987 }
988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
990 buffer1 + c_x1_pk_off, c_x1_pk_len);
991 if (inject_error == 2 && status != PSA_SUCCESS) {
992 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200993 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 } else {
995 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200996 }
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
999 buffer1 + c_x1_pr_off, c_x1_pr_len);
1000 if (inject_error == 2 && status != PSA_SUCCESS) {
1001 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001002 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 } else {
1004 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001005 }
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1008 buffer1 + c_g2_off, c_g2_len);
1009 if (inject_error == 2 && status != PSA_SUCCESS) {
1010 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001011 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 } else {
1013 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001014 }
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1017 buffer1 + c_x2_pk_off, c_x2_pk_len);
1018 if (inject_error == 2 && status != PSA_SUCCESS) {
1019 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001020 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 } else {
1022 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001023 }
1024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1026 buffer1 + c_x2_pr_off, c_x2_pr_len);
1027 if (inject_error == 2 && status != PSA_SUCCESS) {
1028 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001029 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 } else {
1031 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001032 }
1033
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001034 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (inject_error == 2) {
1036 TEST_ASSERT(
1037 !"One of the last psa_pake_input() calls should have returned the expected error.");
1038 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001039
1040 break;
1041
1042 case 2:
1043 /* Server second round Output */
1044 buffer0_off = 0;
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1047 buffer0 + buffer0_off,
1048 512 - buffer0_off, &s_a_len));
1049 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001050 s_a_off = buffer0_off;
1051 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1053 buffer0 + buffer0_off,
1054 512 - buffer0_off, &s_x2s_pk_len));
1055 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001056 s_x2s_pk_off = buffer0_off;
1057 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1059 buffer0 + buffer0_off,
1060 512 - buffer0_off, &s_x2s_pr_len));
1061 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001062 s_x2s_pr_off = buffer0_off;
1063 buffer0_off += s_x2s_pr_len;
1064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001066 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 expected_status = PSA_ERROR_DATA_INVALID;
1068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001071 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1073 buffer0 + s_a_off, s_a_len);
1074 if (inject_error == 3 && status != PSA_SUCCESS) {
1075 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001076 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 } else {
1078 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001079 }
1080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1082 buffer0 + s_x2s_pk_off,
1083 s_x2s_pk_len);
1084 if (inject_error == 3 && status != PSA_SUCCESS) {
1085 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001086 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 } else {
1088 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001089 }
1090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1092 buffer0 + s_x2s_pr_off,
1093 s_x2s_pr_len);
1094 if (inject_error == 3 && status != PSA_SUCCESS) {
1095 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001096 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 } else {
1098 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001099 }
1100
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001101 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (inject_error == 3) {
1103 TEST_ASSERT(
1104 !"One of the last psa_pake_input() calls should have returned the expected error.");
1105 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001106 }
1107
1108 /* Client second round Output */
1109 buffer1_off = 0;
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1112 buffer1 + buffer1_off,
1113 512 - buffer1_off, &c_a_len));
1114 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001115 c_a_off = buffer1_off;
1116 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1118 buffer1 + buffer1_off,
1119 512 - buffer1_off, &c_x2s_pk_len));
1120 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001121 c_x2s_pk_off = buffer1_off;
1122 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1124 buffer1 + buffer1_off,
1125 512 - buffer1_off, &c_x2s_pr_len));
1126 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001127 c_x2s_pr_off = buffer1_off;
1128 buffer1_off += c_x2s_pr_len;
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001131 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1133 buffer0 + s_a_off, s_a_len);
1134 if (inject_error == 3 && status != PSA_SUCCESS) {
1135 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001136 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 } else {
1138 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001139 }
1140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1142 buffer0 + s_x2s_pk_off,
1143 s_x2s_pk_len);
1144 if (inject_error == 3 && status != PSA_SUCCESS) {
1145 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001146 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 } else {
1148 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001149 }
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1152 buffer0 + s_x2s_pr_off,
1153 s_x2s_pr_len);
1154 if (inject_error == 3 && status != PSA_SUCCESS) {
1155 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001156 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 } else {
1158 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001159 }
1160
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001161 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (inject_error == 3) {
1163 TEST_ASSERT(
1164 !"One of the last psa_pake_input() calls should have returned the expected error.");
1165 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001166 }
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001169 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001170 expected_status = PSA_ERROR_DATA_INVALID;
1171 }
1172
1173 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1175 buffer1 + c_a_off, c_a_len);
1176 if (inject_error == 4 && status != PSA_SUCCESS) {
1177 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001178 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 } else {
1180 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001181 }
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1184 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1185 if (inject_error == 4 && status != PSA_SUCCESS) {
1186 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001187 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 } else {
1189 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001190 }
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1193 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1194 if (inject_error == 4 && status != PSA_SUCCESS) {
1195 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001196 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 } else {
1198 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001199 }
1200
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001201 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (inject_error == 4) {
1203 TEST_ASSERT(
1204 !"One of the last psa_pake_input() calls should have returned the expected error.");
1205 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001206
1207 break;
1208
1209 }
1210
Neil Armstrongf983caf2022-06-15 15:27:48 +02001211exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_free(buffer0);
1213 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001214}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001215#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001218 INJECT_ERR_NONE = 0,
1219 INJECT_ERR_UNINITIALIZED_ACCESS,
1220 INJECT_ERR_DUPLICATE_SETUP,
1221 INJECT_ERR_INVALID_USER,
1222 INJECT_ERR_INVALID_PEER,
1223 INJECT_ERR_SET_USER,
1224 INJECT_ERR_SET_PEER,
1225 INJECT_EMPTY_IO_BUFFER,
1226 INJECT_UNKNOWN_STEP,
1227 INJECT_INVALID_FIRST_STEP,
1228 INJECT_WRONG_BUFFER_SIZE,
1229 INJECT_VALID_OPERATION_AFTER_FAILURE,
1230 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1231 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1232} ecjpake_injected_failure_t;
1233
Paul Elliott01885fa2023-02-09 12:07:30 +00001234#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001235
Paul Elliott6f600372023-02-06 18:41:05 +00001236static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1237 psa_status_t expected_status,
1238 size_t *min_completes,
1239 size_t *max_completes)
1240{
1241
1242 /* This is slightly contrived, but we only really know that with a minimum
1243 value of max_ops that a successful operation should take more than one op
1244 to complete, and likewise that with a max_ops of
1245 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1246 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001247
Paul Elliott6f600372023-02-06 18:41:05 +00001248 if (expected_status == PSA_SUCCESS) {
1249 *min_completes = 2;
1250 } else {
1251 *min_completes = 1;
1252 }
1253
1254 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1255 } else {
1256 *min_completes = 1;
1257 *max_completes = 1;
1258 }
1259}
Paul Elliott01885fa2023-02-09 12:07:30 +00001260#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001261
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001262#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
1263static int rsa_test_e(mbedtls_svc_key_id_t key,
1264 size_t bits,
1265 const data_t *e_arg)
1266{
1267 uint8_t *exported = NULL;
1268 size_t exported_size =
1269 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
1270 size_t exported_length = SIZE_MAX;
1271 int ok = 0;
1272
1273 TEST_CALLOC(exported, exported_size);
1274 PSA_ASSERT(psa_export_public_key(key,
1275 exported, exported_size,
1276 &exported_length));
1277 uint8_t *p = exported;
1278 uint8_t *end = exported + exported_length;
1279 size_t len;
1280 /* RSAPublicKey ::= SEQUENCE {
1281 * modulus INTEGER, -- n
1282 * publicExponent INTEGER } -- e
1283 */
1284 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1285 MBEDTLS_ASN1_SEQUENCE |
1286 MBEDTLS_ASN1_CONSTRUCTED));
1287 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
1288 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1289 MBEDTLS_ASN1_INTEGER));
1290 if (len >= 1 && p[0] == 0) {
1291 ++p;
1292 --len;
1293 }
1294 if (e_arg->len == 0) {
1295 TEST_EQUAL(len, 3);
1296 TEST_EQUAL(p[0], 1);
1297 TEST_EQUAL(p[1], 0);
1298 TEST_EQUAL(p[2], 1);
1299 } else {
Gilles Peskine7a18f962024-02-12 16:48:11 +01001300 const uint8_t *expected = e_arg->x;
1301 size_t expected_len = e_arg->len;
1302 while (expected_len > 0 && *expected == 0) {
1303 ++expected;
1304 --expected_len;
1305 }
1306 TEST_MEMORY_COMPARE(p, len, expected, expected_len);
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001307 }
1308 ok = 1;
1309
1310exit:
1311 mbedtls_free(exported);
1312 return ok;
1313}
1314#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */
1315
Gilles Peskine092ce512024-02-20 12:31:24 +01001316static int setup_key_production_parameters(
1317 psa_key_production_parameters_t **params, size_t *params_data_length,
1318 int flags_arg, const data_t *params_data)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001319{
Gilles Peskine092ce512024-02-20 12:31:24 +01001320 *params_data_length = params_data->len;
Gilles Peskinec81393b2024-02-14 20:51:28 +01001321 /* If there are N bytes of padding at the end of
Gilles Peskine092ce512024-02-20 12:31:24 +01001322 * psa_key_production_parameters_t, then it's enough to allocate
1323 * MIN(sizeof(psa_key_production_parameters_t),
1324 * offsetof(psa_key_production_parameters_t, data) + params_data_length).
Gilles Peskinec81393b2024-02-14 20:51:28 +01001325 *
1326 * For simplicity, here, we allocate up to N more bytes than necessary.
Gilles Peskine092ce512024-02-20 12:31:24 +01001327 * In practice, the current layout of psa_key_production_parameters_t
Gilles Peskinec81393b2024-02-14 20:51:28 +01001328 * makes padding extremely unlikely, so we don't worry about testing
1329 * that the library code doesn't try to access these extra N bytes.
1330 */
Gilles Peskine092ce512024-02-20 12:31:24 +01001331 *params = mbedtls_calloc(1, sizeof(**params) + *params_data_length);
1332 TEST_ASSERT(*params != NULL);
1333 (*params)->flags = (uint32_t) flags_arg;
1334 memcpy((*params)->data, params_data->x, params_data->len);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001335 return 1;
1336exit:
1337 return 0;
1338}
1339
Ryan3a1b7862024-03-01 17:24:04 +00001340#if defined(MBEDTLS_THREADING_PTHREAD)
1341typedef struct generate_key_context {
1342 psa_key_type_t type;
1343 psa_key_usage_t usage;
1344 size_t bits;
1345 psa_algorithm_t alg;
1346 psa_status_t expected_status;
1347 psa_key_attributes_t *attributes;
1348 int is_large_key;
1349 int reps;
1350}
1351generate_key_context;
1352void *thread_generate_key(void *ctx)
1353{
1354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1355 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1356 generate_key_context *gkc = (struct generate_key_context *) ctx;
1357
1358 /* If there are race conditions, it is likely the case that they do not
1359 * arise every time the code runs. We repeat the code to increase the
1360 * chance that any race conditions will be hit. */
1361 for (int n = 0; n < gkc->reps; n++) {
1362 /* Generate a key */
1363 psa_status_t status = psa_generate_key(gkc->attributes, &key);
1364
1365 if (gkc->is_large_key > 0) {
1366 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1367 }
1368
1369 TEST_EQUAL(status, gkc->expected_status);
1370 if (gkc->expected_status != PSA_SUCCESS) {
1371 PSA_ASSERT(psa_destroy_key(key));
1372 goto exit;
1373 }
1374
1375 /* Test the key information */
1376 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1377 TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type);
1378 TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits);
1379
1380 /* Do something with the key according
1381 * to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00001382 if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) {
Ryan3a1b7862024-03-01 17:24:04 +00001383 psa_destroy_key(key);
1384 goto exit;
1385 }
1386 psa_reset_key_attributes(&got_attributes);
1387
1388 PSA_ASSERT(psa_destroy_key(key));
1389 }
1390exit:
1391 /*
1392 * Key attributes may have been returned by psa_get_key_attributes()
1393 * thus reset them as required.
1394 */
1395 psa_reset_key_attributes(&got_attributes);
1396 return NULL;
1397}
1398#endif /* MBEDTLS_THREADING_PTHREAD */
1399
Gilles Peskinee59236f2018-01-27 23:32:46 +01001400/* END_HEADER */
1401
1402/* BEGIN_DEPENDENCIES
1403 * depends_on:MBEDTLS_PSA_CRYPTO_C
1404 * END_DEPENDENCIES
1405 */
1406
1407/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001408void psa_can_do_hash()
1409{
1410 /* We can't test that this is specific to drivers until partial init has
1411 * been implemented, but we can at least test before/after full init. */
1412 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1413 PSA_INIT();
1414 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1415 PSA_DONE();
1416}
1417/* END_CASE */
1418
1419/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001420void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001421{
1422 size_t max_truncated_mac_size =
1423 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1424
1425 /* Check that the length for a truncated MAC always fits in the algorithm
1426 * encoding. The shifted mask is the maximum truncated value. The
1427 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001429}
1430/* END_CASE */
1431
1432/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433void import_with_policy(int type_arg,
1434 int usage_arg, int alg_arg,
1435 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001436{
1437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1438 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001440 psa_key_type_t type = type_arg;
1441 psa_key_usage_t usage = usage_arg;
1442 psa_algorithm_t alg = alg_arg;
1443 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001445 psa_status_t status;
1446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 psa_set_key_type(&attributes, type);
1450 psa_set_key_usage_flags(&attributes, usage);
1451 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 status = psa_import_key(&attributes,
1454 key_material, sizeof(key_material),
1455 &key);
1456 TEST_EQUAL(status, expected_status);
1457 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001458 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1462 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1463 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1464 mbedtls_test_update_key_usage_flags(usage));
1465 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1466 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 PSA_ASSERT(psa_destroy_key(key));
1469 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001470
1471exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001472 /*
1473 * Key attributes may have been returned by psa_get_key_attributes()
1474 * thus reset them as required.
1475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 psa_destroy_key(key);
1479 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484void import_with_data(data_t *data, int type_arg,
1485 int attr_bits_arg,
1486 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001487{
1488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1489 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001491 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001492 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001493 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001494 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 psa_set_key_type(&attributes, type);
1499 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001502 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1503 *
1504 * This can happen with a type supported only by a driver:
1505 * - the driver sees the invalid data (for example wrong size) and thinks
1506 * "well perhaps this is a key size I don't support" so it returns
1507 * NOT_SUPPORTED which is correct at this point;
1508 * - we fallback to built-ins, which don't support this type, so return
1509 * NOT_SUPPORTED which again is correct at this point.
1510 */
1511 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1512 status == PSA_ERROR_NOT_SUPPORTED) {
1513 ; // OK
1514 } else {
1515 TEST_EQUAL(status, expected_status);
1516 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001518 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1522 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1523 if (attr_bits != 0) {
1524 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1525 }
1526 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 PSA_ASSERT(psa_destroy_key(key));
1529 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001530
1531exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001532 /*
1533 * Key attributes may have been returned by psa_get_key_attributes()
1534 * thus reset them as required.
1535 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 psa_destroy_key(key);
1539 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540}
1541/* END_CASE */
1542
1543/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001544/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545void import_large_key(int type_arg, int byte_size_arg,
1546 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001547{
1548 psa_key_type_t type = type_arg;
1549 size_t byte_size = byte_size_arg;
1550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1551 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001553 psa_status_t status;
1554 uint8_t *buffer = NULL;
1555 size_t buffer_size = byte_size + 1;
1556 size_t n;
1557
Steven Cooreman69967ce2021-01-18 18:01:08 +01001558 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001559 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001560 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001564
1565 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1567 psa_set_key_type(&attributes, type);
1568 status = psa_import_key(&attributes, buffer, byte_size, &key);
1569 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1570 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (status == PSA_SUCCESS) {
1573 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1574 TEST_EQUAL(psa_get_key_type(&attributes), type);
1575 TEST_EQUAL(psa_get_key_bits(&attributes),
1576 PSA_BYTES_TO_BITS(byte_size));
1577 ASSERT_NO_SLOT_NUMBER(&attributes);
1578 memset(buffer, 0, byte_size + 1);
1579 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1580 for (n = 0; n < byte_size; n++) {
1581 TEST_EQUAL(buffer[n], 'K');
1582 }
1583 for (n = byte_size; n < buffer_size; n++) {
1584 TEST_EQUAL(buffer[n], 0);
1585 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001586 }
1587
1588exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001589 /*
1590 * Key attributes may have been returned by psa_get_key_attributes()
1591 * thus reset them as required.
1592 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 psa_destroy_key(key);
1596 PSA_DONE();
1597 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001598}
1599/* END_CASE */
1600
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001601/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001602/* Import an RSA key with a valid structure (but not valid numbers
1603 * inside, beyond having sensible size and parity). This is expected to
1604 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001606{
Ronald Cron5425a212020-08-04 14:58:35 +02001607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001608 size_t bits = bits_arg;
1609 psa_status_t expected_status = expected_status_arg;
1610 psa_status_t status;
1611 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001612 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001613 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001615 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001616 unsigned char *p;
1617 int ret;
1618 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001622 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1625 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001626 length = ret;
1627
1628 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 psa_set_key_type(&attributes, type);
1630 status = psa_import_key(&attributes, p, length, &key);
1631 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (status == PSA_SUCCESS) {
1634 PSA_ASSERT(psa_destroy_key(key));
1635 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001636
1637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_free(buffer);
1639 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001640}
1641/* END_CASE */
1642
1643/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644void import_export(data_t *data,
1645 int type_arg,
1646 int usage_arg, int alg_arg,
1647 int lifetime_arg,
1648 int expected_bits,
1649 int export_size_delta,
1650 int expected_export_status_arg,
1651 /*whether reexport must give the original input exactly*/
1652 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001653{
Ronald Cron5425a212020-08-04 14:58:35 +02001654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001655 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001656 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001657 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301659 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660 unsigned char *exported = NULL;
1661 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001662 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001663 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001664 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001666 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667
Moran Pekercb088e72018-07-17 17:36:59 +03001668 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001669 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001671 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 }
1673 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 psa_set_key_lifetime(&attributes, lifetime);
1676 psa_set_key_usage_flags(&attributes, usage_arg);
1677 psa_set_key_algorithm(&attributes, alg);
1678 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001679
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001680 if (PSA_KEY_TYPE_IS_DH(type) &&
1681 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001682 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1683 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001684 }
1685
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001687 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001688 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001689
1690 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1692 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1693 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1694 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001695
1696 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 status = psa_export_key(key, exported, export_size, &exported_length);
1698 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001699
1700 /* The exported length must be set by psa_export_key() to a value between 0
1701 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1703 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1704 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1707 export_size - exported_length));
1708 if (status != PSA_SUCCESS) {
1709 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001710 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001711 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001712
Gilles Peskineea38a922021-02-13 00:05:16 +01001713 /* Run sanity checks on the exported key. For non-canonical inputs,
1714 * this validates the canonical representations. For canonical inputs,
1715 * this doesn't directly validate the implementation, but it still helps
1716 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 if (!psa_key_lifetime_is_external(lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00001718 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 }
Archana4d7ae1d2021-07-07 02:50:22 +05301721 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001724 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001726 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1728 &key2));
1729 PSA_ASSERT(psa_export_key(key2,
1730 reexported,
1731 export_size,
1732 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001733 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001734 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001736 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 TEST_LE_U(exported_length,
1738 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1739 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001740 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1741 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1742 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1743 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1744 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001745
1746destroy:
1747 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 PSA_ASSERT(psa_destroy_key(key));
1749 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001750
1751exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001752 /*
1753 * Key attributes may have been returned by psa_get_key_attributes()
1754 * thus reset them as required.
1755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 psa_reset_key_attributes(&got_attributes);
1757 psa_destroy_key(key);
1758 mbedtls_free(exported);
1759 mbedtls_free(reexported);
1760 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001761}
1762/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001763
Moran Pekerf709f4a2018-06-06 17:26:04 +03001764/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765void import_export_public_key(data_t *data,
1766 int type_arg, // key pair or public key
1767 int alg_arg,
1768 int lifetime_arg,
1769 int export_size_delta,
1770 int expected_export_status_arg,
1771 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001772{
Ronald Cron5425a212020-08-04 14:58:35 +02001773 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001774 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001775 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001776 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001777 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301778 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001779 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001780 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001781 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 psa_set_key_lifetime(&attributes, lifetime);
1787 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1788 psa_set_key_algorithm(&attributes, alg);
1789 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001790
1791 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001793
Gilles Peskine49c25912018-10-29 15:15:31 +01001794 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001795 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 status = psa_export_public_key(key,
1797 exported, export_size,
1798 &exported_length);
1799 TEST_EQUAL(status, expected_export_status);
1800 if (status == PSA_SUCCESS) {
1801 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001802 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1804 bits = psa_get_key_bits(&attributes);
1805 TEST_LE_U(expected_public_key->len,
1806 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1807 TEST_LE_U(expected_public_key->len,
1808 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1809 TEST_LE_U(expected_public_key->len,
1810 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001811 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001812 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001813 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001814exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001815 /*
1816 * Key attributes may have been returned by psa_get_key_attributes()
1817 * thus reset them as required.
1818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 mbedtls_free(exported);
1822 psa_destroy_key(key);
1823 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001824}
1825/* END_CASE */
1826
Gilles Peskine20035e32018-02-03 22:44:14 +01001827/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828void import_and_exercise_key(data_t *data,
1829 int type_arg,
1830 int bits_arg,
1831 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001832{
Ronald Cron5425a212020-08-04 14:58:35 +02001833 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001834 psa_key_type_t type = type_arg;
1835 size_t bits = bits_arg;
1836 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001838 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001839 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 psa_set_key_usage_flags(&attributes, usage);
1844 psa_set_key_algorithm(&attributes, alg);
1845 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001846
1847 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001849
1850 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1852 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1853 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001854
1855 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00001856 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001857 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 PSA_ASSERT(psa_destroy_key(key));
1861 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001862
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001863exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001864 /*
1865 * Key attributes may have been returned by psa_get_key_attributes()
1866 * thus reset them as required.
1867 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 psa_reset_key_attributes(&attributes);
1871 psa_destroy_key(key);
1872 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001873}
1874/* END_CASE */
1875
1876/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877void effective_key_attributes(int type_arg, int expected_type_arg,
1878 int bits_arg, int expected_bits_arg,
1879 int usage_arg, int expected_usage_arg,
1880 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001881{
Ronald Cron5425a212020-08-04 14:58:35 +02001882 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001883 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001884 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001885 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001886 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001887 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001888 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001889 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001890 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 psa_set_key_usage_flags(&attributes, usage);
1896 psa_set_key_algorithm(&attributes, alg);
1897 psa_set_key_type(&attributes, key_type);
1898 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 PSA_ASSERT(psa_generate_key(&attributes, &key));
1901 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1904 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1905 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1906 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1907 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001908
1909exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001910 /*
1911 * Key attributes may have been returned by psa_get_key_attributes()
1912 * thus reset them as required.
1913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 psa_destroy_key(key);
1917 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001918}
1919/* END_CASE */
1920
1921/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001922void check_key_policy(int type_arg, int bits_arg,
1923 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001924{
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1926 usage_arg,
1927 mbedtls_test_update_key_usage_flags(usage_arg),
1928 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001929 goto exit;
1930}
1931/* END_CASE */
1932
1933/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001935{
1936 /* Test each valid way of initializing the object, except for `= {0}`, as
1937 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1938 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001939 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001941 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1942 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1947 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1948 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 TEST_EQUAL(psa_get_key_type(&func), 0);
1951 TEST_EQUAL(psa_get_key_type(&init), 0);
1952 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 TEST_EQUAL(psa_get_key_bits(&func), 0);
1955 TEST_EQUAL(psa_get_key_bits(&init), 0);
1956 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1959 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1960 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1963 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1964 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001965}
1966/* END_CASE */
1967
1968/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969void mac_key_policy(int policy_usage_arg,
1970 int policy_alg_arg,
1971 int key_type_arg,
1972 data_t *key_data,
1973 int exercise_alg_arg,
1974 int expected_status_sign_arg,
1975 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001976{
Ronald Cron5425a212020-08-04 14:58:35 +02001977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001979 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001980 psa_key_type_t key_type = key_type_arg;
1981 psa_algorithm_t policy_alg = policy_alg_arg;
1982 psa_algorithm_t exercise_alg = exercise_alg_arg;
1983 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001985 psa_status_t expected_status_sign = expected_status_sign_arg;
1986 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001987 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 psa_set_key_usage_flags(&attributes, policy_usage);
1992 psa_set_key_algorithm(&attributes, policy_alg);
1993 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1996 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1999 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2002 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002003
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002004 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002006 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
2008 input, 128,
2009 mac, PSA_MAC_MAX_SIZE, &mac_len),
2010 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002011
Neil Armstrong3af9b972022-02-07 12:20:21 +01002012 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 PSA_ASSERT(psa_mac_abort(&operation));
2014 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2015 if (status == PSA_SUCCESS) {
2016 status = psa_mac_update(&operation, input, 128);
2017 if (status == PSA_SUCCESS) {
2018 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
2019 &mac_len),
2020 expected_status_sign);
2021 } else {
2022 TEST_EQUAL(status, expected_status_sign);
2023 }
2024 } else {
2025 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002026 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01002028
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002029 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 status = psa_mac_verify(key, exercise_alg, input, 128,
2031 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2034 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2035 } else {
2036 TEST_EQUAL(status, expected_status_verify);
2037 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01002038
Neil Armstrong3af9b972022-02-07 12:20:21 +01002039 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2041 if (status == PSA_SUCCESS) {
2042 status = psa_mac_update(&operation, input, 128);
2043 if (status == PSA_SUCCESS) {
2044 status = psa_mac_verify_finish(&operation, mac, mac_len);
2045 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2046 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2047 } else {
2048 TEST_EQUAL(status, expected_status_verify);
2049 }
2050 } else {
2051 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002052 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 } else {
2054 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002055 }
2056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 memset(mac, 0, sizeof(mac));
2060 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2061 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062
2063exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 psa_mac_abort(&operation);
2065 psa_destroy_key(key);
2066 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067}
2068/* END_CASE */
2069
2070/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002071void cipher_key_policy(int policy_usage_arg,
2072 int policy_alg,
2073 int key_type,
2074 data_t *key_data,
2075 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002076{
Ronald Cron5425a212020-08-04 14:58:35 +02002077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002079 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002080 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002081 size_t output_buffer_size = 0;
2082 size_t input_buffer_size = 0;
2083 size_t output_length = 0;
2084 uint8_t *output = NULL;
2085 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086 psa_status_t status;
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
2089 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
2090 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002091
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002092 TEST_CALLOC(input, input_buffer_size);
2093 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 psa_set_key_usage_flags(&attributes, policy_usage);
2098 psa_set_key_algorithm(&attributes, policy_alg);
2099 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2102 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002104 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 TEST_EQUAL(policy_usage,
2106 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002107
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002108 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2110 output, output_buffer_size,
2111 &output_length);
2112 if (policy_alg == exercise_alg &&
2113 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2114 PSA_ASSERT(status);
2115 } else {
2116 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2117 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002118
2119 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2121 if (policy_alg == exercise_alg &&
2122 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2123 PSA_ASSERT(status);
2124 } else {
2125 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2126 }
2127 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002129 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2131 input, input_buffer_size,
2132 &output_length);
2133 if (policy_alg == exercise_alg &&
2134 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2135 PSA_ASSERT(status);
2136 } else {
2137 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2138 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002139
2140 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2142 if (policy_alg == exercise_alg &&
2143 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2144 PSA_ASSERT(status);
2145 } else {
2146 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2147 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148
2149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 psa_cipher_abort(&operation);
2151 mbedtls_free(input);
2152 mbedtls_free(output);
2153 psa_destroy_key(key);
2154 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002155}
2156/* END_CASE */
2157
2158/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002159void aead_key_policy(int policy_usage_arg,
2160 int policy_alg,
2161 int key_type,
2162 data_t *key_data,
2163 int nonce_length_arg,
2164 int tag_length_arg,
2165 int exercise_alg,
2166 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167{
Ronald Cron5425a212020-08-04 14:58:35 +02002168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002170 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002171 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002172 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002173 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175 size_t nonce_length = nonce_length_arg;
2176 unsigned char tag[16];
2177 size_t tag_length = tag_length_arg;
2178 size_t output_length;
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 TEST_LE_U(nonce_length, sizeof(nonce));
2181 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 psa_set_key_usage_flags(&attributes, policy_usage);
2186 psa_set_key_algorithm(&attributes, policy_alg);
2187 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2190 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002192 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 TEST_EQUAL(policy_usage,
2194 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002195
Neil Armstrong752d8112022-02-07 14:51:11 +01002196 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 status = psa_aead_encrypt(key, exercise_alg,
2198 nonce, nonce_length,
2199 NULL, 0,
2200 NULL, 0,
2201 tag, tag_length,
2202 &output_length);
2203 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2204 TEST_EQUAL(status, expected_status);
2205 } else {
2206 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2207 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208
Neil Armstrong752d8112022-02-07 14:51:11 +01002209 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2211 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2212 TEST_EQUAL(status, expected_status);
2213 } else {
2214 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2215 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002216
2217 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 memset(tag, 0, sizeof(tag));
2219 status = psa_aead_decrypt(key, exercise_alg,
2220 nonce, nonce_length,
2221 NULL, 0,
2222 tag, tag_length,
2223 NULL, 0,
2224 &output_length);
2225 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2226 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2227 } else if (expected_status == PSA_SUCCESS) {
2228 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2229 } else {
2230 TEST_EQUAL(status, expected_status);
2231 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002232
Neil Armstrong752d8112022-02-07 14:51:11 +01002233 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 PSA_ASSERT(psa_aead_abort(&operation));
2235 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2236 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2237 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2238 } else {
2239 TEST_EQUAL(status, expected_status);
2240 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002241
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 PSA_ASSERT(psa_aead_abort(&operation));
2244 psa_destroy_key(key);
2245 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002246}
2247/* END_CASE */
2248
2249/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002250void asymmetric_encryption_key_policy(int policy_usage_arg,
2251 int policy_alg,
2252 int key_type,
2253 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002254 int exercise_alg,
2255 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002256{
Ronald Cron5425a212020-08-04 14:58:35 +02002257 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002259 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002260 psa_status_t status;
2261 size_t key_bits;
2262 size_t buffer_length;
2263 unsigned char *buffer = NULL;
2264 size_t output_length;
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 psa_set_key_usage_flags(&attributes, policy_usage);
2269 psa_set_key_algorithm(&attributes, policy_alg);
2270 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002271
Valerio Settif202c292024-01-15 10:42:37 +01002272 if (use_opaque_key) {
2273 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2274 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2275 }
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2278 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002279
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002280 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 TEST_EQUAL(policy_usage,
2282 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2285 key_bits = psa_get_key_bits(&attributes);
2286 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2287 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002288 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 status = psa_asymmetric_encrypt(key, exercise_alg,
2291 NULL, 0,
2292 NULL, 0,
2293 buffer, buffer_length,
2294 &output_length);
2295 if (policy_alg == exercise_alg &&
2296 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2297 PSA_ASSERT(status);
2298 } else {
2299 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2300 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (buffer_length != 0) {
2303 memset(buffer, 0, buffer_length);
2304 }
2305 status = psa_asymmetric_decrypt(key, exercise_alg,
2306 buffer, buffer_length,
2307 NULL, 0,
2308 buffer, buffer_length,
2309 &output_length);
2310 if (policy_alg == exercise_alg &&
2311 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2312 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2313 } else {
2314 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2315 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002316
2317exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002318 /*
2319 * Key attributes may have been returned by psa_get_key_attributes()
2320 * thus reset them as required.
2321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_destroy_key(key);
2325 PSA_DONE();
2326 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002327}
2328/* END_CASE */
2329
2330/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331void asymmetric_signature_key_policy(int policy_usage_arg,
2332 int policy_alg,
2333 int key_type,
2334 data_t *key_data,
2335 int exercise_alg,
2336 int payload_length_arg,
2337 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002341 psa_key_usage_t policy_usage = policy_usage_arg;
2342 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002343 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002345 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2346 * compatible with the policy and `payload_length_arg` is supposed to be
2347 * a valid input length to sign. If `payload_length_arg <= 0`,
2348 * `exercise_alg` is supposed to be forbidden by the policy. */
2349 int compatible_alg = payload_length_arg > 0;
2350 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002352 size_t signature_length;
2353
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002354 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002355 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 TEST_EQUAL(expected_usage,
2357 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 psa_set_key_usage_flags(&attributes, policy_usage);
2362 psa_set_key_algorithm(&attributes, policy_alg);
2363 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2366 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 status = psa_sign_hash(key, exercise_alg,
2371 payload, payload_length,
2372 signature, sizeof(signature),
2373 &signature_length);
2374 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2375 PSA_ASSERT(status);
2376 } else {
2377 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2378 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 memset(signature, 0, sizeof(signature));
2381 status = psa_verify_hash(key, exercise_alg,
2382 payload, payload_length,
2383 signature, sizeof(signature));
2384 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2385 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2386 } else {
2387 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2388 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2391 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2392 status = psa_sign_message(key, exercise_alg,
2393 payload, payload_length,
2394 signature, sizeof(signature),
2395 &signature_length);
2396 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2397 PSA_ASSERT(status);
2398 } else {
2399 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2400 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 memset(signature, 0, sizeof(signature));
2403 status = psa_verify_message(key, exercise_alg,
2404 payload, payload_length,
2405 signature, sizeof(signature));
2406 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2407 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2408 } else {
2409 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2410 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002411 }
2412
Gilles Peskined5b33222018-06-18 22:20:03 +02002413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 psa_destroy_key(key);
2415 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002416}
2417/* END_CASE */
2418
Janos Follathba3fab92019-06-11 14:50:16 +01002419/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420void derive_key_policy(int policy_usage,
2421 int policy_alg,
2422 int key_type,
2423 data_t *key_data,
2424 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002425{
Ronald Cron5425a212020-08-04 14:58:35 +02002426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002428 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002429 psa_status_t status;
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 psa_set_key_usage_flags(&attributes, policy_usage);
2434 psa_set_key_algorithm(&attributes, policy_alg);
2435 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2438 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2443 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2444 PSA_ASSERT(psa_key_derivation_input_bytes(
2445 &operation,
2446 PSA_KEY_DERIVATION_INPUT_SEED,
2447 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002448 }
Janos Follathba3fab92019-06-11 14:50:16 +01002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 status = psa_key_derivation_input_key(&operation,
2451 PSA_KEY_DERIVATION_INPUT_SECRET,
2452 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (policy_alg == exercise_alg &&
2455 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2456 PSA_ASSERT(status);
2457 } else {
2458 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2459 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002460
2461exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 psa_key_derivation_abort(&operation);
2463 psa_destroy_key(key);
2464 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002465}
2466/* END_CASE */
2467
2468/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002469void agreement_key_policy(int policy_usage,
2470 int policy_alg,
2471 int key_type_arg,
2472 data_t *key_data,
2473 int exercise_alg,
2474 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002475{
Ronald Cron5425a212020-08-04 14:58:35 +02002476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002478 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002479 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002480 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002481 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 psa_set_key_usage_flags(&attributes, policy_usage);
2486 psa_set_key_algorithm(&attributes, policy_alg);
2487 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2490 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2493 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002496
2497exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 psa_key_derivation_abort(&operation);
2499 psa_destroy_key(key);
2500 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002501}
2502/* END_CASE */
2503
2504/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505void key_policy_alg2(int key_type_arg, data_t *key_data,
2506 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002507{
Ronald Cron5425a212020-08-04 14:58:35 +02002508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002509 psa_key_type_t key_type = key_type_arg;
2510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2511 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2512 psa_key_usage_t usage = usage_arg;
2513 psa_algorithm_t alg = alg_arg;
2514 psa_algorithm_t alg2 = alg2_arg;
2515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 psa_set_key_usage_flags(&attributes, usage);
2519 psa_set_key_algorithm(&attributes, alg);
2520 psa_set_key_enrollment_algorithm(&attributes, alg2);
2521 psa_set_key_type(&attributes, key_type);
2522 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2523 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002524
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002525 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 usage = mbedtls_test_update_key_usage_flags(usage);
2527 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2528 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2529 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2530 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002531
Ryan Everett0a271fd2024-03-12 16:34:02 +00002532 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002533 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002535 if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002536 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002538
2539exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002540 /*
2541 * Key attributes may have been returned by psa_get_key_attributes()
2542 * thus reset them as required.
2543 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 psa_destroy_key(key);
2547 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002548}
2549/* END_CASE */
2550
2551/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552void raw_agreement_key_policy(int policy_usage,
2553 int policy_alg,
2554 int key_type_arg,
2555 data_t *key_data,
2556 int exercise_alg,
2557 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002558{
Ronald Cron5425a212020-08-04 14:58:35 +02002559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002561 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002562 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002563 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002564 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 psa_set_key_usage_flags(&attributes, policy_usage);
2569 psa_set_key_algorithm(&attributes, policy_alg);
2570 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2573 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002574
Ryan Everett81630282024-03-12 16:21:12 +00002575 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key, 0);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002578
2579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 psa_key_derivation_abort(&operation);
2581 psa_destroy_key(key);
2582 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002583}
2584/* END_CASE */
2585
2586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587void copy_success(int source_usage_arg,
2588 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002589 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 int type_arg, data_t *material,
2591 int copy_attributes,
2592 int target_usage_arg,
2593 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002594 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 int expected_usage_arg,
2596 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002597{
Gilles Peskineca25db92019-04-19 11:43:08 +02002598 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2599 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002600 psa_key_usage_t expected_usage = expected_usage_arg;
2601 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002602 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302603 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2604 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002605 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2606 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002607 uint8_t *export_buffer = NULL;
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002610
Gilles Peskineca25db92019-04-19 11:43:08 +02002611 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2613 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2614 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2615 psa_set_key_type(&source_attributes, type_arg);
2616 psa_set_key_lifetime(&source_attributes, source_lifetime);
2617 PSA_ASSERT(psa_import_key(&source_attributes,
2618 material->x, material->len,
2619 &source_key));
2620 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002621
Gilles Peskineca25db92019-04-19 11:43:08 +02002622 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002624 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002625 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (target_usage_arg != -1) {
2629 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2630 }
2631 if (target_alg_arg != -1) {
2632 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2633 }
2634 if (target_alg2_arg != -1) {
2635 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2636 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002637
Archana8a180362021-07-05 02:18:48 +05302638
Gilles Peskine57ab7212019-01-28 13:03:09 +01002639 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 PSA_ASSERT(psa_copy_key(source_key,
2641 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002642
2643 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002645
2646 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2648 TEST_EQUAL(psa_get_key_type(&source_attributes),
2649 psa_get_key_type(&target_attributes));
2650 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2651 psa_get_key_bits(&target_attributes));
2652 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2653 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2654 TEST_EQUAL(expected_alg2,
2655 psa_get_key_enrollment_algorithm(&target_attributes));
2656 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002657 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002658 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2660 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002661 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002662 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002663 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if (!psa_key_lifetime_is_external(target_lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00002666 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) {
Archana8a180362021-07-05 02:18:48 +05302667 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002669 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) {
Archana8a180362021-07-05 02:18:48 +05302670 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 }
Archana8a180362021-07-05 02:18:48 +05302672 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002675
2676exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002677 /*
2678 * Source and target key attributes may have been returned by
2679 * psa_get_key_attributes() thus reset them as required.
2680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 psa_reset_key_attributes(&source_attributes);
2682 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 PSA_DONE();
2685 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002686}
2687/* END_CASE */
2688
2689/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690void copy_fail(int source_usage_arg,
2691 int source_alg_arg, int source_alg2_arg,
2692 int source_lifetime_arg,
2693 int type_arg, data_t *material,
2694 int target_type_arg, int target_bits_arg,
2695 int target_usage_arg,
2696 int target_alg_arg, int target_alg2_arg,
2697 int target_id_arg, int target_lifetime_arg,
2698 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002699{
2700 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2701 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002702 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2703 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002707
2708 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2710 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2711 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2712 psa_set_key_type(&source_attributes, type_arg);
2713 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2714 PSA_ASSERT(psa_import_key(&source_attributes,
2715 material->x, material->len,
2716 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002717
2718 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 psa_set_key_id(&target_attributes, key_id);
2720 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2721 psa_set_key_type(&target_attributes, target_type_arg);
2722 psa_set_key_bits(&target_attributes, target_bits_arg);
2723 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2724 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2725 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002726
2727 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 TEST_EQUAL(psa_copy_key(source_key,
2729 &target_attributes, &target_key),
2730 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002733
Gilles Peskine4a644642019-05-03 17:14:08 +02002734exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 psa_reset_key_attributes(&source_attributes);
2736 psa_reset_key_attributes(&target_attributes);
2737 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002738}
2739/* END_CASE */
2740
2741/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002742void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002743{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002744 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002745 /* Test each valid way of initializing the object, except for `= {0}`, as
2746 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2747 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002748 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002750 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2751 psa_hash_operation_t zero;
2752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002754
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002755 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2757 PSA_ERROR_BAD_STATE);
2758 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2759 PSA_ERROR_BAD_STATE);
2760 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2761 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002762
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002763 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 PSA_ASSERT(psa_hash_abort(&func));
2765 PSA_ASSERT(psa_hash_abort(&init));
2766 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002767}
2768/* END_CASE */
2769
2770/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002771void hash_setup(int alg_arg,
2772 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002773{
2774 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002775 uint8_t *output = NULL;
2776 size_t output_size = 0;
2777 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002778 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002779 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002780 psa_status_t status;
2781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002783
Neil Armstrongedb20862022-02-07 15:47:44 +01002784 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002786 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 status = psa_hash_compute(alg, NULL, 0,
2789 output, output_size, &output_length);
2790 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002791
2792 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 status = psa_hash_setup(&operation, alg);
2794 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002795
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002796 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002798
2799 /* If setup failed, reproduce the failure, so as to
2800 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 if (status != PSA_SUCCESS) {
2802 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2803 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002804
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002805 /* Now the operation object should be reusable. */
2806#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2808 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002809#endif
2810
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002811exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 mbedtls_free(output);
2813 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002814}
2815/* END_CASE */
2816
2817/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002818void hash_compute_fail(int alg_arg, data_t *input,
2819 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002820{
2821 psa_algorithm_t alg = alg_arg;
2822 uint8_t *output = NULL;
2823 size_t output_size = output_size_arg;
2824 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002825 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002826 psa_status_t expected_status = expected_status_arg;
2827 psa_status_t status;
2828
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002829 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002832
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002833 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 status = psa_hash_compute(alg, input->x, input->len,
2835 output, output_size, &output_length);
2836 TEST_EQUAL(status, expected_status);
2837 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002838
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002839 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 status = psa_hash_setup(&operation, alg);
2841 if (status == PSA_SUCCESS) {
2842 status = psa_hash_update(&operation, input->x, input->len);
2843 if (status == PSA_SUCCESS) {
2844 status = psa_hash_finish(&operation, output, output_size,
2845 &output_length);
2846 if (status == PSA_SUCCESS) {
2847 TEST_LE_U(output_length, output_size);
2848 } else {
2849 TEST_EQUAL(status, expected_status);
2850 }
2851 } else {
2852 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 } else {
2855 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002856 }
2857
Gilles Peskine0a749c82019-11-28 19:33:58 +01002858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 PSA_ASSERT(psa_hash_abort(&operation));
2860 mbedtls_free(output);
2861 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002862}
2863/* END_CASE */
2864
2865/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002866void hash_compare_fail(int alg_arg, data_t *input,
2867 data_t *reference_hash,
2868 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002869{
2870 psa_algorithm_t alg = alg_arg;
2871 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002872 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002873 psa_status_t status;
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002876
Neil Armstrong55a1be12022-02-07 11:23:20 +01002877 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 status = psa_hash_compare(alg, input->x, input->len,
2879 reference_hash->x, reference_hash->len);
2880 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002881
Neil Armstrong55a1be12022-02-07 11:23:20 +01002882 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 status = psa_hash_setup(&operation, alg);
2884 if (status == PSA_SUCCESS) {
2885 status = psa_hash_update(&operation, input->x, input->len);
2886 if (status == PSA_SUCCESS) {
2887 status = psa_hash_verify(&operation, reference_hash->x,
2888 reference_hash->len);
2889 TEST_EQUAL(status, expected_status);
2890 } else {
2891 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002892 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 } else {
2894 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002895 }
2896
Gilles Peskine88e08462020-01-28 20:43:00 +01002897exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 PSA_ASSERT(psa_hash_abort(&operation));
2899 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002900}
2901/* END_CASE */
2902
2903/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002904void hash_compute_compare(int alg_arg, data_t *input,
2905 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002906{
2907 psa_algorithm_t alg = alg_arg;
2908 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2909 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002910 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002911 size_t i;
2912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002914
Neil Armstrongca30a002022-02-07 11:40:23 +01002915 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2917 output, PSA_HASH_LENGTH(alg),
2918 &output_length));
2919 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002920 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002921 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002922
Neil Armstrongca30a002022-02-07 11:40:23 +01002923 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 PSA_ASSERT(psa_hash_setup(&operation, alg));
2925 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2926 PSA_ASSERT(psa_hash_finish(&operation, output,
2927 PSA_HASH_LENGTH(alg),
2928 &output_length));
2929 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002930 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002931 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002932
2933 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2935 output, sizeof(output),
2936 &output_length));
2937 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002938 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002939 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002940
Neil Armstrongca30a002022-02-07 11:40:23 +01002941 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 PSA_ASSERT(psa_hash_setup(&operation, alg));
2943 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2944 PSA_ASSERT(psa_hash_finish(&operation, output,
2945 sizeof(output), &output_length));
2946 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002947 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002948 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002949
2950 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2952 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002953
Neil Armstrongca30a002022-02-07 11:40:23 +01002954 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 PSA_ASSERT(psa_hash_setup(&operation, alg));
2956 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2957 PSA_ASSERT(psa_hash_verify(&operation, output,
2958 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002959
2960 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2962 output, output_length + 1),
2963 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002964
Neil Armstrongca30a002022-02-07 11:40:23 +01002965 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_hash_setup(&operation, alg));
2967 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2968 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2969 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002970
2971 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2973 output, output_length - 1),
2974 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002975
Neil Armstrongca30a002022-02-07 11:40:23 +01002976 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 PSA_ASSERT(psa_hash_setup(&operation, alg));
2978 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2979 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2980 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002981
Gilles Peskine0a749c82019-11-28 19:33:58 +01002982 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 for (i = 0; i < output_length; i++) {
2984 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002985 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002986
2987 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2989 output, output_length),
2990 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002991
2992 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 PSA_ASSERT(psa_hash_setup(&operation, alg));
2994 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2995 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2996 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002997
Gilles Peskine0a749c82019-11-28 19:33:58 +01002998 output[i] ^= 1;
2999 }
3000
3001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 PSA_ASSERT(psa_hash_abort(&operation));
3003 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01003004}
3005/* END_CASE */
3006
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003007/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003008void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02003009{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003010 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02003011 unsigned char input[] = "";
3012 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003013 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02003014 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3015 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
3017 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003018 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02003019 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00003020 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02003023
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003024 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 PSA_ASSERT(psa_hash_setup(&operation, alg));
3026 ASSERT_OPERATION_IS_ACTIVE(operation);
3027 TEST_EQUAL(psa_hash_setup(&operation, alg),
3028 PSA_ERROR_BAD_STATE);
3029 ASSERT_OPERATION_IS_INACTIVE(operation);
3030 PSA_ASSERT(psa_hash_abort(&operation));
3031 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003032
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003033 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3035 PSA_ERROR_BAD_STATE);
3036 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003037
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003038 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01003040 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 ASSERT_OPERATION_IS_ACTIVE(operation);
3042 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3043 PSA_ERROR_BAD_STATE);
3044 ASSERT_OPERATION_IS_INACTIVE(operation);
3045 PSA_ASSERT(psa_hash_abort(&operation));
3046 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003047
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003048 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 PSA_ASSERT(psa_hash_setup(&operation, alg));
3050 PSA_ASSERT(psa_hash_finish(&operation,
3051 hash, sizeof(hash), &hash_len));
3052 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3053 PSA_ERROR_BAD_STATE);
3054 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003055
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003056 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 TEST_EQUAL(psa_hash_verify(&operation,
3058 valid_hash, sizeof(valid_hash)),
3059 PSA_ERROR_BAD_STATE);
3060 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003061
3062 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 PSA_ASSERT(psa_hash_setup(&operation, alg));
3064 PSA_ASSERT(psa_hash_finish(&operation,
3065 hash, sizeof(hash), &hash_len));
3066 TEST_EQUAL(psa_hash_verify(&operation,
3067 valid_hash, sizeof(valid_hash)),
3068 PSA_ERROR_BAD_STATE);
3069 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003070
3071 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 PSA_ASSERT(psa_hash_setup(&operation, alg));
3073 ASSERT_OPERATION_IS_ACTIVE(operation);
3074 PSA_ASSERT(psa_hash_verify(&operation,
3075 valid_hash, sizeof(valid_hash)));
3076 ASSERT_OPERATION_IS_INACTIVE(operation);
3077 TEST_EQUAL(psa_hash_verify(&operation,
3078 valid_hash, sizeof(valid_hash)),
3079 PSA_ERROR_BAD_STATE);
3080 ASSERT_OPERATION_IS_INACTIVE(operation);
3081 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003082
3083 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 TEST_EQUAL(psa_hash_finish(&operation,
3085 hash, sizeof(hash), &hash_len),
3086 PSA_ERROR_BAD_STATE);
3087 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003088
3089 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 PSA_ASSERT(psa_hash_setup(&operation, alg));
3091 PSA_ASSERT(psa_hash_finish(&operation,
3092 hash, sizeof(hash), &hash_len));
3093 TEST_EQUAL(psa_hash_finish(&operation,
3094 hash, sizeof(hash), &hash_len),
3095 PSA_ERROR_BAD_STATE);
3096 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003097
3098 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 PSA_ASSERT(psa_hash_setup(&operation, alg));
3100 PSA_ASSERT(psa_hash_verify(&operation,
3101 valid_hash, sizeof(valid_hash)));
3102 TEST_EQUAL(psa_hash_finish(&operation,
3103 hash, sizeof(hash), &hash_len),
3104 PSA_ERROR_BAD_STATE);
3105 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003106
3107exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003109}
3110/* END_CASE */
3111
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003112/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003114{
3115 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003116 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3117 * appended to it */
3118 unsigned char hash[] = {
3119 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3120 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3122 };
3123 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003124 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003127
itayzafrir27e69452018-11-01 14:26:34 +02003128 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 PSA_ASSERT(psa_hash_setup(&operation, alg));
3130 ASSERT_OPERATION_IS_ACTIVE(operation);
3131 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3132 PSA_ERROR_INVALID_SIGNATURE);
3133 ASSERT_OPERATION_IS_INACTIVE(operation);
3134 PSA_ASSERT(psa_hash_abort(&operation));
3135 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003136
itayzafrir27e69452018-11-01 14:26:34 +02003137 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 PSA_ASSERT(psa_hash_setup(&operation, alg));
3139 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3140 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003141
itayzafrir27e69452018-11-01 14:26:34 +02003142 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 PSA_ASSERT(psa_hash_setup(&operation, alg));
3144 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3145 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003146
itayzafrirec93d302018-10-18 18:01:10 +03003147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003149}
3150/* END_CASE */
3151
Ronald Cronee414c72021-03-18 18:50:08 +01003152/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003153void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003154{
3155 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003156 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003158 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003159 size_t hash_len;
3160
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003162
itayzafrir58028322018-10-25 10:22:01 +03003163 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 PSA_ASSERT(psa_hash_setup(&operation, alg));
3165 TEST_EQUAL(psa_hash_finish(&operation,
3166 hash, expected_size - 1, &hash_len),
3167 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003168
3169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003171}
3172/* END_CASE */
3173
Ronald Cronee414c72021-03-18 18:50:08 +01003174/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003176{
3177 psa_algorithm_t alg = PSA_ALG_SHA_256;
3178 unsigned char hash[PSA_HASH_MAX_SIZE];
3179 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3180 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3181 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3182 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3183 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3184 size_t hash_len;
3185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 PSA_ASSERT(psa_crypto_init());
3187 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003188
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3190 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3191 PSA_ASSERT(psa_hash_finish(&op_finished,
3192 hash, sizeof(hash), &hash_len));
3193 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3194 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3197 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3200 PSA_ASSERT(psa_hash_finish(&op_init,
3201 hash, sizeof(hash), &hash_len));
3202 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3203 PSA_ASSERT(psa_hash_finish(&op_finished,
3204 hash, sizeof(hash), &hash_len));
3205 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3206 PSA_ASSERT(psa_hash_finish(&op_aborted,
3207 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003208
3209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 psa_hash_abort(&op_source);
3211 psa_hash_abort(&op_init);
3212 psa_hash_abort(&op_setup);
3213 psa_hash_abort(&op_finished);
3214 psa_hash_abort(&op_aborted);
3215 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003216}
3217/* END_CASE */
3218
Ronald Cronee414c72021-03-18 18:50:08 +01003219/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003220void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003221{
3222 psa_algorithm_t alg = PSA_ALG_SHA_256;
3223 unsigned char hash[PSA_HASH_MAX_SIZE];
3224 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3225 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3226 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3227 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3228 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3229 size_t hash_len;
3230
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3234 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3235 PSA_ASSERT(psa_hash_finish(&op_finished,
3236 hash, sizeof(hash), &hash_len));
3237 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3238 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3241 PSA_ASSERT(psa_hash_finish(&op_target,
3242 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3245 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3246 PSA_ERROR_BAD_STATE);
3247 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3248 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003249
3250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 psa_hash_abort(&op_target);
3252 psa_hash_abort(&op_init);
3253 psa_hash_abort(&op_setup);
3254 psa_hash_abort(&op_finished);
3255 psa_hash_abort(&op_aborted);
3256 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003257}
3258/* END_CASE */
3259
itayzafrir58028322018-10-25 10:22:01 +03003260/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003261void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003262{
Jaeden Amero252ef282019-02-15 14:05:35 +00003263 const uint8_t input[1] = { 0 };
3264
Jaeden Amero769ce272019-01-04 11:48:03 +00003265 /* Test each valid way of initializing the object, except for `= {0}`, as
3266 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3267 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003268 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003270 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3271 psa_mac_operation_t zero;
3272
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003274
Jaeden Amero252ef282019-02-15 14:05:35 +00003275 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 TEST_EQUAL(psa_mac_update(&func,
3277 input, sizeof(input)),
3278 PSA_ERROR_BAD_STATE);
3279 TEST_EQUAL(psa_mac_update(&init,
3280 input, sizeof(input)),
3281 PSA_ERROR_BAD_STATE);
3282 TEST_EQUAL(psa_mac_update(&zero,
3283 input, sizeof(input)),
3284 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003285
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003286 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 PSA_ASSERT(psa_mac_abort(&func));
3288 PSA_ASSERT(psa_mac_abort(&init));
3289 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003290}
3291/* END_CASE */
3292
3293/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003294void mac_setup(int key_type_arg,
3295 data_t *key,
3296 int alg_arg,
3297 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003299 psa_key_type_t key_type = key_type_arg;
3300 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003301 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003302 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003303 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3304#if defined(KNOWN_SUPPORTED_MAC_ALG)
3305 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3306#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3311 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003312 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 }
3314 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003315
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003316 /* The operation object should be reusable. */
3317#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3319 smoke_test_key_data,
3320 sizeof(smoke_test_key_data),
3321 KNOWN_SUPPORTED_MAC_ALG,
3322 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003323 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 }
3325 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003326#endif
3327
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003330}
3331/* END_CASE */
3332
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003333/* 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 +01003334void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003335{
Ronald Cron5425a212020-08-04 14:58:35 +02003336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003337 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3338 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003339 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003340 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3341 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3343 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003345 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3346 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3347 size_t sign_mac_length = 0;
3348 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3349 const uint8_t verify_mac[] = {
3350 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3351 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3353 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 PSA_ASSERT(psa_crypto_init());
3356 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3357 psa_set_key_algorithm(&attributes, alg);
3358 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003359
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3361 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003362
Jaeden Amero252ef282019-02-15 14:05:35 +00003363 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3365 PSA_ERROR_BAD_STATE);
3366 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003367
3368 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3370 &sign_mac_length),
3371 PSA_ERROR_BAD_STATE);
3372 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003373
3374 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 TEST_EQUAL(psa_mac_verify_finish(&operation,
3376 verify_mac, sizeof(verify_mac)),
3377 PSA_ERROR_BAD_STATE);
3378 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003379
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003380 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3382 ASSERT_OPERATION_IS_ACTIVE(operation);
3383 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3384 PSA_ERROR_BAD_STATE);
3385 ASSERT_OPERATION_IS_INACTIVE(operation);
3386 PSA_ASSERT(psa_mac_abort(&operation));
3387 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003388
Jaeden Amero252ef282019-02-15 14:05:35 +00003389 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3391 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3392 PSA_ASSERT(psa_mac_sign_finish(&operation,
3393 sign_mac, sizeof(sign_mac),
3394 &sign_mac_length));
3395 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3396 PSA_ERROR_BAD_STATE);
3397 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003398
3399 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3401 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3402 PSA_ASSERT(psa_mac_verify_finish(&operation,
3403 verify_mac, sizeof(verify_mac)));
3404 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3405 PSA_ERROR_BAD_STATE);
3406 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003407
3408 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3410 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3411 PSA_ASSERT(psa_mac_sign_finish(&operation,
3412 sign_mac, sizeof(sign_mac),
3413 &sign_mac_length));
3414 TEST_EQUAL(psa_mac_sign_finish(&operation,
3415 sign_mac, sizeof(sign_mac),
3416 &sign_mac_length),
3417 PSA_ERROR_BAD_STATE);
3418 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003419
3420 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3422 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3423 PSA_ASSERT(psa_mac_verify_finish(&operation,
3424 verify_mac, sizeof(verify_mac)));
3425 TEST_EQUAL(psa_mac_verify_finish(&operation,
3426 verify_mac, sizeof(verify_mac)),
3427 PSA_ERROR_BAD_STATE);
3428 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003429
3430 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3432 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3433 ASSERT_OPERATION_IS_ACTIVE(operation);
3434 TEST_EQUAL(psa_mac_verify_finish(&operation,
3435 verify_mac, sizeof(verify_mac)),
3436 PSA_ERROR_BAD_STATE);
3437 ASSERT_OPERATION_IS_INACTIVE(operation);
3438 PSA_ASSERT(psa_mac_abort(&operation));
3439 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003440
3441 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3443 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3444 ASSERT_OPERATION_IS_ACTIVE(operation);
3445 TEST_EQUAL(psa_mac_sign_finish(&operation,
3446 sign_mac, sizeof(sign_mac),
3447 &sign_mac_length),
3448 PSA_ERROR_BAD_STATE);
3449 ASSERT_OPERATION_IS_INACTIVE(operation);
3450 PSA_ASSERT(psa_mac_abort(&operation));
3451 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003452
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003454
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003455exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003457}
3458/* END_CASE */
3459
3460/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461void mac_sign_verify_multi(int key_type_arg,
3462 data_t *key_data,
3463 int alg_arg,
3464 data_t *input,
3465 int is_verify,
3466 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003467{
3468 size_t data_part_len = 0;
3469
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003471 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003473
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 if (mac_multipart_internal_func(key_type_arg, key_data,
3475 alg_arg,
3476 input, data_part_len,
3477 expected_mac,
3478 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003479 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003481
3482 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003484
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 if (mac_multipart_internal_func(key_type_arg, key_data,
3486 alg_arg,
3487 input, data_part_len,
3488 expected_mac,
3489 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003490 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003492 }
3493
3494 /* Goto is required to silence warnings about unused labels, as we
3495 * don't actually do any test assertions in this function. */
3496 goto exit;
3497}
3498/* END_CASE */
3499
3500/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003501void mac_sign(int key_type_arg,
3502 data_t *key_data,
3503 int alg_arg,
3504 data_t *input,
3505 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003506{
Ronald Cron5425a212020-08-04 14:58:35 +02003507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003508 psa_key_type_t key_type = key_type_arg;
3509 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003510 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003512 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003513 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003515 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003516 const size_t output_sizes_to_test[] = {
3517 0,
3518 1,
3519 expected_mac->len - 1,
3520 expected_mac->len,
3521 expected_mac->len + 1,
3522 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003523
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003525 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003527
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003529
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3531 psa_set_key_algorithm(&attributes, alg);
3532 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003533
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3535 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003536
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003538 const size_t output_size = output_sizes_to_test[i];
3539 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 (output_size >= expected_mac->len ? PSA_SUCCESS :
3541 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003542
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003544 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003545
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003546 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 TEST_EQUAL(psa_mac_compute(key, alg,
3548 input->x, input->len,
3549 actual_mac, output_size, &mac_length),
3550 expected_status);
3551 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003552 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003553 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003554 }
3555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 if (output_size > 0) {
3557 memset(actual_mac, 0, output_size);
3558 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003559
3560 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3562 PSA_ASSERT(psa_mac_update(&operation,
3563 input->x, input->len));
3564 TEST_EQUAL(psa_mac_sign_finish(&operation,
3565 actual_mac, output_size,
3566 &mac_length),
3567 expected_status);
3568 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003569
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003571 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003572 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003573 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003574 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003575 actual_mac = NULL;
3576 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003577
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 psa_mac_abort(&operation);
3580 psa_destroy_key(key);
3581 PSA_DONE();
3582 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003583}
3584/* END_CASE */
3585
3586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003587void mac_verify(int key_type_arg,
3588 data_t *key_data,
3589 int alg_arg,
3590 data_t *input,
3591 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003592{
Ronald Cron5425a212020-08-04 14:58:35 +02003593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003594 psa_key_type_t key_type = key_type_arg;
3595 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003596 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003598 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003599
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003601
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3605 psa_set_key_algorithm(&attributes, alg);
3606 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003607
Gilles Peskine449bd832023-01-11 14:50:10 +01003608 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3609 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003610
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003611 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3613 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003614
3615 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3617 PSA_ASSERT(psa_mac_update(&operation,
3618 input->x, input->len));
3619 PSA_ASSERT(psa_mac_verify_finish(&operation,
3620 expected_mac->x,
3621 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003622
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003623 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003624 TEST_EQUAL(psa_mac_verify(key, alg,
3625 input->x, input->len,
3626 expected_mac->x,
3627 expected_mac->len - 1),
3628 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003629
3630 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003631 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3632 PSA_ASSERT(psa_mac_update(&operation,
3633 input->x, input->len));
3634 TEST_EQUAL(psa_mac_verify_finish(&operation,
3635 expected_mac->x,
3636 expected_mac->len - 1),
3637 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003638
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003639 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003640 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3642 TEST_EQUAL(psa_mac_verify(key, alg,
3643 input->x, input->len,
3644 perturbed_mac, expected_mac->len + 1),
3645 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003646
3647 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003648 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3649 PSA_ASSERT(psa_mac_update(&operation,
3650 input->x, input->len));
3651 TEST_EQUAL(psa_mac_verify_finish(&operation,
3652 perturbed_mac,
3653 expected_mac->len + 1),
3654 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003655
3656 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 for (size_t i = 0; i < expected_mac->len; i++) {
3658 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003659 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003660
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 TEST_EQUAL(psa_mac_verify(key, alg,
3662 input->x, input->len,
3663 perturbed_mac, expected_mac->len),
3664 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003665
Gilles Peskine449bd832023-01-11 14:50:10 +01003666 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3667 PSA_ASSERT(psa_mac_update(&operation,
3668 input->x, input->len));
3669 TEST_EQUAL(psa_mac_verify_finish(&operation,
3670 perturbed_mac,
3671 expected_mac->len),
3672 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003673 perturbed_mac[i] ^= 1;
3674 }
3675
Gilles Peskine8c9def32018-02-08 10:02:12 +01003676exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003677 psa_mac_abort(&operation);
3678 psa_destroy_key(key);
3679 PSA_DONE();
3680 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003681}
3682/* END_CASE */
3683
3684/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003685void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003686{
Jaeden Ameroab439972019-02-15 14:12:05 +00003687 const uint8_t input[1] = { 0 };
3688 unsigned char output[1] = { 0 };
3689 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003690 /* Test each valid way of initializing the object, except for `= {0}`, as
3691 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3692 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003693 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003695 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3696 psa_cipher_operation_t zero;
3697
Gilles Peskine449bd832023-01-11 14:50:10 +01003698 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003699
Jaeden Ameroab439972019-02-15 14:12:05 +00003700 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 TEST_EQUAL(psa_cipher_update(&func,
3702 input, sizeof(input),
3703 output, sizeof(output),
3704 &output_length),
3705 PSA_ERROR_BAD_STATE);
3706 TEST_EQUAL(psa_cipher_update(&init,
3707 input, sizeof(input),
3708 output, sizeof(output),
3709 &output_length),
3710 PSA_ERROR_BAD_STATE);
3711 TEST_EQUAL(psa_cipher_update(&zero,
3712 input, sizeof(input),
3713 output, sizeof(output),
3714 &output_length),
3715 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003716
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003717 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003718 PSA_ASSERT(psa_cipher_abort(&func));
3719 PSA_ASSERT(psa_cipher_abort(&init));
3720 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003721}
3722/* END_CASE */
3723
3724/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003725void cipher_setup(int key_type_arg,
3726 data_t *key,
3727 int alg_arg,
3728 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003729{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003730 psa_key_type_t key_type = key_type_arg;
3731 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003732 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003733 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003734 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003735#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003736 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3737#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003738
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003740
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3742 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 }
3745 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003746
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003747 /* The operation object should be reusable. */
3748#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003749 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3750 smoke_test_key_data,
3751 sizeof(smoke_test_key_data),
3752 KNOWN_SUPPORTED_CIPHER_ALG,
3753 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 }
3756 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003757#endif
3758
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003759exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 psa_cipher_abort(&operation);
3761 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003762}
3763/* END_CASE */
3764
Ronald Cronee414c72021-03-18 18:50:08 +01003765/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003766void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003767{
Ronald Cron5425a212020-08-04 14:58:35 +02003768 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003769 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3770 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003772 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003773 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003774 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003775 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003776 0xaa, 0xaa, 0xaa, 0xaa
3777 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003778 const uint8_t text[] = {
3779 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 0xbb, 0xbb, 0xbb, 0xbb
3781 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003782 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003783 size_t length = 0;
3784
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 PSA_ASSERT(psa_crypto_init());
3786 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3787 psa_set_key_algorithm(&attributes, alg);
3788 psa_set_key_type(&attributes, key_type);
3789 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3790 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003791
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003792 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3794 ASSERT_OPERATION_IS_ACTIVE(operation);
3795 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3796 PSA_ERROR_BAD_STATE);
3797 ASSERT_OPERATION_IS_INACTIVE(operation);
3798 PSA_ASSERT(psa_cipher_abort(&operation));
3799 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003800
3801 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003802 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3803 ASSERT_OPERATION_IS_ACTIVE(operation);
3804 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3805 PSA_ERROR_BAD_STATE);
3806 ASSERT_OPERATION_IS_INACTIVE(operation);
3807 PSA_ASSERT(psa_cipher_abort(&operation));
3808 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003809
Jaeden Ameroab439972019-02-15 14:12:05 +00003810 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3812 buffer, sizeof(buffer),
3813 &length),
3814 PSA_ERROR_BAD_STATE);
3815 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003816
3817 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3819 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3820 buffer, sizeof(buffer),
3821 &length));
3822 ASSERT_OPERATION_IS_ACTIVE(operation);
3823 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3824 buffer, sizeof(buffer),
3825 &length),
3826 PSA_ERROR_BAD_STATE);
3827 ASSERT_OPERATION_IS_INACTIVE(operation);
3828 PSA_ASSERT(psa_cipher_abort(&operation));
3829 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003830
3831 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3833 PSA_ASSERT(psa_cipher_set_iv(&operation,
3834 iv, sizeof(iv)));
3835 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3836 buffer, sizeof(buffer),
3837 &length),
3838 PSA_ERROR_BAD_STATE);
3839 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003840
3841 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 TEST_EQUAL(psa_cipher_set_iv(&operation,
3843 iv, sizeof(iv)),
3844 PSA_ERROR_BAD_STATE);
3845 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003846
3847 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3849 PSA_ASSERT(psa_cipher_set_iv(&operation,
3850 iv, sizeof(iv)));
3851 ASSERT_OPERATION_IS_ACTIVE(operation);
3852 TEST_EQUAL(psa_cipher_set_iv(&operation,
3853 iv, sizeof(iv)),
3854 PSA_ERROR_BAD_STATE);
3855 ASSERT_OPERATION_IS_INACTIVE(operation);
3856 PSA_ASSERT(psa_cipher_abort(&operation));
3857 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003858
3859 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3861 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3862 buffer, sizeof(buffer),
3863 &length));
3864 TEST_EQUAL(psa_cipher_set_iv(&operation,
3865 iv, sizeof(iv)),
3866 PSA_ERROR_BAD_STATE);
3867 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003868
3869 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 TEST_EQUAL(psa_cipher_update(&operation,
3871 text, sizeof(text),
3872 buffer, sizeof(buffer),
3873 &length),
3874 PSA_ERROR_BAD_STATE);
3875 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003876
3877 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3879 ASSERT_OPERATION_IS_ACTIVE(operation);
3880 TEST_EQUAL(psa_cipher_update(&operation,
3881 text, sizeof(text),
3882 buffer, sizeof(buffer),
3883 &length),
3884 PSA_ERROR_BAD_STATE);
3885 ASSERT_OPERATION_IS_INACTIVE(operation);
3886 PSA_ASSERT(psa_cipher_abort(&operation));
3887 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003888
3889 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3891 PSA_ASSERT(psa_cipher_set_iv(&operation,
3892 iv, sizeof(iv)));
3893 PSA_ASSERT(psa_cipher_finish(&operation,
3894 buffer, sizeof(buffer), &length));
3895 TEST_EQUAL(psa_cipher_update(&operation,
3896 text, sizeof(text),
3897 buffer, sizeof(buffer),
3898 &length),
3899 PSA_ERROR_BAD_STATE);
3900 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003901
3902 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 TEST_EQUAL(psa_cipher_finish(&operation,
3904 buffer, sizeof(buffer), &length),
3905 PSA_ERROR_BAD_STATE);
3906 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003907
3908 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003910 /* Not calling update means we are encrypting an empty buffer, which is OK
3911 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 ASSERT_OPERATION_IS_ACTIVE(operation);
3913 TEST_EQUAL(psa_cipher_finish(&operation,
3914 buffer, sizeof(buffer), &length),
3915 PSA_ERROR_BAD_STATE);
3916 ASSERT_OPERATION_IS_INACTIVE(operation);
3917 PSA_ASSERT(psa_cipher_abort(&operation));
3918 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003919
3920 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3922 PSA_ASSERT(psa_cipher_set_iv(&operation,
3923 iv, sizeof(iv)));
3924 PSA_ASSERT(psa_cipher_finish(&operation,
3925 buffer, sizeof(buffer), &length));
3926 TEST_EQUAL(psa_cipher_finish(&operation,
3927 buffer, sizeof(buffer), &length),
3928 PSA_ERROR_BAD_STATE);
3929 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003930
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003932
Jaeden Ameroab439972019-02-15 14:12:05 +00003933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 psa_cipher_abort(&operation);
3935 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003936}
3937/* END_CASE */
3938
3939/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003940void cipher_encrypt_fail(int alg_arg,
3941 int key_type_arg,
3942 data_t *key_data,
3943 data_t *input,
3944 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003945{
Ronald Cron5425a212020-08-04 14:58:35 +02003946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003947 psa_status_t status;
3948 psa_key_type_t key_type = key_type_arg;
3949 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003950 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003952 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3953 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003954 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003955 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003956 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003957 size_t function_output_length;
3958 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3960
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 if (PSA_ERROR_BAD_STATE != expected_status) {
3962 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003963
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3965 psa_set_key_algorithm(&attributes, alg);
3966 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003967
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3969 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003970 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3973 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003974 }
3975
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003976 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3978 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003979
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003981
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003982 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 status = psa_cipher_encrypt_setup(&operation, key, alg);
3984 if (status == PSA_SUCCESS) {
3985 if (alg != PSA_ALG_ECB_NO_PADDING) {
3986 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3987 iv, iv_size,
3988 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003989 }
3990
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 status = psa_cipher_update(&operation, input->x, input->len,
3992 output, output_buffer_size,
3993 &function_output_length);
3994 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003995 output_length += function_output_length;
3996
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 status = psa_cipher_finish(&operation, output + output_length,
3998 output_buffer_size - output_length,
3999 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 TEST_EQUAL(status, expected_status);
4002 } else {
4003 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 } else {
4006 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004007 }
4008
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 psa_cipher_abort(&operation);
4011 mbedtls_free(output);
4012 psa_destroy_key(key);
4013 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004014}
4015/* END_CASE */
4016
4017/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004018void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
4019 data_t *input, int iv_length,
4020 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02004021{
4022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4023 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4025 size_t output_buffer_size = 0;
4026 unsigned char *output = NULL;
4027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004029 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02004032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4034 psa_set_key_algorithm(&attributes, alg);
4035 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4038 &key));
4039 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4040 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
4041 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02004042
4043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 psa_cipher_abort(&operation);
4045 mbedtls_free(output);
4046 psa_destroy_key(key);
4047 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02004048}
4049/* END_CASE */
4050
4051/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004052void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
4053 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004054{
4055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4056 psa_key_type_t key_type = key_type_arg;
4057 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004058 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4059 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004060 unsigned char *output = NULL;
4061 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02004062 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4064
Gilles Peskine449bd832023-01-11 14:50:10 +01004065 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004066
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004067 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 TEST_LE_U(ciphertext->len,
4069 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
4070 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
4071 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
4072 TEST_LE_U(plaintext->len,
4073 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
4074 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
4075 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004076
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004077
4078 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 psa_set_key_usage_flags(&attributes,
4080 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4081 psa_set_key_algorithm(&attributes, alg);
4082 psa_set_key_type(&attributes, key_type);
4083 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4084 &key));
4085 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4086 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004087 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004088
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004089 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4091 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4092 PSA_ERROR_BAD_STATE);
4093 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4094 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4095 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004096
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004097 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4099 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4100 &length),
4101 PSA_ERROR_BAD_STATE);
4102 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4103 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4104 &length),
4105 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004106
Gilles Peskine286c3142022-04-20 17:09:38 +02004107 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004109 output_length = 0;
4110 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 PSA_ASSERT(psa_cipher_update(&operation,
4112 plaintext->x, plaintext->len,
4113 output, output_buffer_size,
4114 &length));
4115 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004116 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 PSA_ASSERT(psa_cipher_finish(&operation,
4118 mbedtls_buffer_offset(output, output_length),
4119 output_buffer_size - output_length,
4120 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004121 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004122 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004123 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004124
Gilles Peskine286c3142022-04-20 17:09:38 +02004125 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004127 output_length = 0;
4128 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 PSA_ASSERT(psa_cipher_update(&operation,
4130 ciphertext->x, ciphertext->len,
4131 output, output_buffer_size,
4132 &length));
4133 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004134 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 PSA_ASSERT(psa_cipher_finish(&operation,
4136 mbedtls_buffer_offset(output, output_length),
4137 output_buffer_size - output_length,
4138 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004139 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004140 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004141 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004142
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004143 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004144 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4146 output, output_buffer_size,
4147 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004148 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004149 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004150
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004151 /* One-shot decryption */
4152 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4154 output, output_buffer_size,
4155 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004156 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004157 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004158
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 PSA_ASSERT(psa_cipher_abort(&operation));
4161 mbedtls_free(output);
4162 psa_cipher_abort(&operation);
4163 psa_destroy_key(key);
4164 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004165}
4166/* END_CASE */
4167
4168/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004169void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004170{
4171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4172 psa_algorithm_t alg = alg_arg;
4173 psa_key_type_t key_type = key_type_arg;
4174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4175 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4176 psa_status_t status;
4177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4181 psa_set_key_algorithm(&attributes, alg);
4182 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004183
4184 /* Usage of either of these two size macros would cause divide by zero
4185 * with incorrect key types previously. Input length should be irrelevant
4186 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4188 0);
4189 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004190
4191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4193 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004194
4195 /* Should fail due to invalid alg type (to support invalid key type).
4196 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004200
4201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 psa_cipher_abort(&operation);
4203 psa_destroy_key(key);
4204 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004205}
4206/* END_CASE */
4207
4208/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004209void cipher_encrypt_validation(int alg_arg,
4210 int key_type_arg,
4211 data_t *key_data,
4212 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004213{
4214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4215 psa_key_type_t key_type = key_type_arg;
4216 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004218 unsigned char *output1 = NULL;
4219 size_t output1_buffer_size = 0;
4220 size_t output1_length = 0;
4221 unsigned char *output2 = NULL;
4222 size_t output2_buffer_size = 0;
4223 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004224 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004225 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4231 psa_set_key_algorithm(&attributes, alg);
4232 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4235 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4236 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004237 TEST_CALLOC(output1, output1_buffer_size);
4238 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4241 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004242
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004243 /* The one-shot cipher encryption uses generated iv so validating
4244 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4246 output1_buffer_size, &output1_length));
4247 TEST_LE_U(output1_length,
4248 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4249 TEST_LE_U(output1_length,
4250 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4253 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 PSA_ASSERT(psa_cipher_update(&operation,
4256 input->x, input->len,
4257 output2, output2_buffer_size,
4258 &function_output_length));
4259 TEST_LE_U(function_output_length,
4260 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4261 TEST_LE_U(function_output_length,
4262 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004263 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 PSA_ASSERT(psa_cipher_finish(&operation,
4266 output2 + output2_length,
4267 output2_buffer_size - output2_length,
4268 &function_output_length));
4269 TEST_LE_U(function_output_length,
4270 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4271 TEST_LE_U(function_output_length,
4272 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004273 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004276 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004277 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004278
Gilles Peskine50e586b2018-06-08 14:28:46 +02004279exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 psa_cipher_abort(&operation);
4281 mbedtls_free(output1);
4282 mbedtls_free(output2);
4283 psa_destroy_key(key);
4284 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004285}
4286/* END_CASE */
4287
4288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004289void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4290 data_t *key_data, data_t *iv,
4291 data_t *input,
4292 int first_part_size_arg,
4293 int output1_length_arg, int output2_length_arg,
4294 data_t *expected_output,
4295 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004296{
Ronald Cron5425a212020-08-04 14:58:35 +02004297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004298 psa_key_type_t key_type = key_type_arg;
4299 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004300 psa_status_t status;
4301 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004302 size_t first_part_size = first_part_size_arg;
4303 size_t output1_length = output1_length_arg;
4304 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004305 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004306 size_t output_buffer_size = 0;
4307 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004308 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004309 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004310 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004313
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4315 psa_set_key_algorithm(&attributes, alg);
4316 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4319 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 if (iv->len > 0) {
4324 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004325 }
4326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4328 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004329 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 TEST_LE_U(first_part_size, input->len);
4332 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4333 output, output_buffer_size,
4334 &function_output_length));
4335 TEST_ASSERT(function_output_length == output1_length);
4336 TEST_LE_U(function_output_length,
4337 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4338 TEST_LE_U(function_output_length,
4339 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004340 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 if (first_part_size < input->len) {
4343 PSA_ASSERT(psa_cipher_update(&operation,
4344 input->x + first_part_size,
4345 input->len - first_part_size,
4346 (output_buffer_size == 0 ? NULL :
4347 output + total_output_length),
4348 output_buffer_size - total_output_length,
4349 &function_output_length));
4350 TEST_ASSERT(function_output_length == output2_length);
4351 TEST_LE_U(function_output_length,
4352 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4353 alg,
4354 input->len - first_part_size));
4355 TEST_LE_U(function_output_length,
4356 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004357 total_output_length += function_output_length;
4358 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 status = psa_cipher_finish(&operation,
4361 (output_buffer_size == 0 ? NULL :
4362 output + total_output_length),
4363 output_buffer_size - total_output_length,
4364 &function_output_length);
4365 TEST_LE_U(function_output_length,
4366 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4367 TEST_LE_U(function_output_length,
4368 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004369 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (expected_status == PSA_SUCCESS) {
4373 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004374
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004375 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004376 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004377 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004378
4379exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 psa_cipher_abort(&operation);
4381 mbedtls_free(output);
4382 psa_destroy_key(key);
4383 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004384}
4385/* END_CASE */
4386
4387/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004388void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4389 data_t *key_data, data_t *iv,
4390 data_t *input,
4391 int first_part_size_arg,
4392 int output1_length_arg, int output2_length_arg,
4393 data_t *expected_output,
4394 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004395{
Ronald Cron5425a212020-08-04 14:58:35 +02004396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004397 psa_key_type_t key_type = key_type_arg;
4398 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004399 psa_status_t status;
4400 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004401 size_t first_part_size = first_part_size_arg;
4402 size_t output1_length = output1_length_arg;
4403 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004404 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004405 size_t output_buffer_size = 0;
4406 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004407 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004408 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4414 psa_set_key_algorithm(&attributes, alg);
4415 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4418 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (iv->len > 0) {
4423 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004424 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004425
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4427 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004428 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 TEST_LE_U(first_part_size, input->len);
4431 PSA_ASSERT(psa_cipher_update(&operation,
4432 input->x, first_part_size,
4433 output, output_buffer_size,
4434 &function_output_length));
4435 TEST_ASSERT(function_output_length == output1_length);
4436 TEST_LE_U(function_output_length,
4437 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4438 TEST_LE_U(function_output_length,
4439 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004440 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 if (first_part_size < input->len) {
4443 PSA_ASSERT(psa_cipher_update(&operation,
4444 input->x + first_part_size,
4445 input->len - first_part_size,
4446 (output_buffer_size == 0 ? NULL :
4447 output + total_output_length),
4448 output_buffer_size - total_output_length,
4449 &function_output_length));
4450 TEST_ASSERT(function_output_length == output2_length);
4451 TEST_LE_U(function_output_length,
4452 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4453 alg,
4454 input->len - first_part_size));
4455 TEST_LE_U(function_output_length,
4456 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004457 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004458 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 status = psa_cipher_finish(&operation,
4461 (output_buffer_size == 0 ? NULL :
4462 output + total_output_length),
4463 output_buffer_size - total_output_length,
4464 &function_output_length);
4465 TEST_LE_U(function_output_length,
4466 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4467 TEST_LE_U(function_output_length,
4468 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004469 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 if (expected_status == PSA_SUCCESS) {
4473 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004474
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004475 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004476 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004477 }
4478
Gilles Peskine50e586b2018-06-08 14:28:46 +02004479exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 psa_cipher_abort(&operation);
4481 mbedtls_free(output);
4482 psa_destroy_key(key);
4483 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004484}
4485/* END_CASE */
4486
Gilles Peskine50e586b2018-06-08 14:28:46 +02004487/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004488void cipher_decrypt_fail(int alg_arg,
4489 int key_type_arg,
4490 data_t *key_data,
4491 data_t *iv,
4492 data_t *input_arg,
4493 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004494{
4495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4496 psa_status_t status;
4497 psa_key_type_t key_type = key_type_arg;
4498 psa_algorithm_t alg = alg_arg;
4499 psa_status_t expected_status = expected_status_arg;
4500 unsigned char *input = NULL;
4501 size_t input_buffer_size = 0;
4502 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004503 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004504 size_t output_buffer_size = 0;
4505 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004506 size_t function_output_length;
4507 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 if (PSA_ERROR_BAD_STATE != expected_status) {
4511 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4514 psa_set_key_algorithm(&attributes, alg);
4515 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4518 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004519 }
4520
4521 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4523 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004524 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 memcpy(input, iv->x, iv->len);
4526 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004527 }
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004530 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004531
Neil Armstrong66a479f2022-02-07 15:41:19 +01004532 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4534 output_buffer_size, &output_length);
4535 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004536
Neil Armstrong66a479f2022-02-07 15:41:19 +01004537 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 status = psa_cipher_decrypt_setup(&operation, key, alg);
4539 if (status == PSA_SUCCESS) {
4540 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4541 input_arg->len) +
4542 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004543 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 if (iv->len > 0) {
4546 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004547
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 if (status != PSA_SUCCESS) {
4549 TEST_EQUAL(status, expected_status);
4550 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004551 }
4552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if (status == PSA_SUCCESS) {
4554 status = psa_cipher_update(&operation,
4555 input_arg->x, input_arg->len,
4556 output_multi, output_buffer_size,
4557 &function_output_length);
4558 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004559 output_length = function_output_length;
4560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 status = psa_cipher_finish(&operation,
4562 output_multi + output_length,
4563 output_buffer_size - output_length,
4564 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 TEST_EQUAL(status, expected_status);
4567 } else {
4568 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004569 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 } else {
4571 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004572 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 } else {
4574 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004575 }
4576
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004577exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 psa_cipher_abort(&operation);
4579 mbedtls_free(input);
4580 mbedtls_free(output);
4581 mbedtls_free(output_multi);
4582 psa_destroy_key(key);
4583 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004584}
4585/* END_CASE */
4586
4587/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004588void cipher_decrypt(int alg_arg,
4589 int key_type_arg,
4590 data_t *key_data,
4591 data_t *iv,
4592 data_t *input_arg,
4593 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004594{
4595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4596 psa_key_type_t key_type = key_type_arg;
4597 psa_algorithm_t alg = alg_arg;
4598 unsigned char *input = NULL;
4599 size_t input_buffer_size = 0;
4600 unsigned char *output = NULL;
4601 size_t output_buffer_size = 0;
4602 size_t output_length = 0;
4603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4608 psa_set_key_algorithm(&attributes, alg);
4609 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004610
4611 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4613 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004614 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 memcpy(input, iv->x, iv->len);
4616 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004617 }
4618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004620 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4623 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4626 output_buffer_size, &output_length));
4627 TEST_LE_U(output_length,
4628 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4629 TEST_LE_U(output_length,
4630 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004631
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004632 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004633 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004634exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 mbedtls_free(input);
4636 mbedtls_free(output);
4637 psa_destroy_key(key);
4638 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004639}
4640/* END_CASE */
4641
4642/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643void cipher_verify_output(int alg_arg,
4644 int key_type_arg,
4645 data_t *key_data,
4646 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004647{
Ronald Cron5425a212020-08-04 14:58:35 +02004648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004649 psa_key_type_t key_type = key_type_arg;
4650 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004651 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004652 size_t output1_size = 0;
4653 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004654 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004655 size_t output2_size = 0;
4656 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004658
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4662 psa_set_key_algorithm(&attributes, alg);
4663 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4666 &key));
4667 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004668 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4671 output1, output1_size,
4672 &output1_length));
4673 TEST_LE_U(output1_length,
4674 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4675 TEST_LE_U(output1_length,
4676 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004677
4678 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004679 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004680
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4682 output2, output2_size,
4683 &output2_length));
4684 TEST_LE_U(output2_length,
4685 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4686 TEST_LE_U(output2_length,
4687 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004688
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004689 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004690
4691exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 mbedtls_free(output1);
4693 mbedtls_free(output2);
4694 psa_destroy_key(key);
4695 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004696}
4697/* END_CASE */
4698
4699/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004700void cipher_verify_output_multipart(int alg_arg,
4701 int key_type_arg,
4702 data_t *key_data,
4703 data_t *input,
4704 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004705{
Ronald Cron5425a212020-08-04 14:58:35 +02004706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004707 psa_key_type_t key_type = key_type_arg;
4708 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004709 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004711 size_t iv_size = 16;
4712 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004713 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004714 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004715 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004716 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004717 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004718 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004719 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004720 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4721 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4727 psa_set_key_algorithm(&attributes, alg);
4728 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4731 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4734 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (alg != PSA_ALG_ECB_NO_PADDING) {
4737 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4738 iv, iv_size,
4739 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004740 }
4741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4743 TEST_LE_U(output1_buffer_size,
4744 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004745 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4750 output1, output1_buffer_size,
4751 &function_output_length));
4752 TEST_LE_U(function_output_length,
4753 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4754 TEST_LE_U(function_output_length,
4755 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004756 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 PSA_ASSERT(psa_cipher_update(&operation1,
4759 input->x + first_part_size,
4760 input->len - first_part_size,
4761 output1, output1_buffer_size,
4762 &function_output_length));
4763 TEST_LE_U(function_output_length,
4764 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4765 alg,
4766 input->len - first_part_size));
4767 TEST_LE_U(function_output_length,
4768 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004769 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 PSA_ASSERT(psa_cipher_finish(&operation1,
4772 output1 + output1_length,
4773 output1_buffer_size - output1_length,
4774 &function_output_length));
4775 TEST_LE_U(function_output_length,
4776 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4777 TEST_LE_U(function_output_length,
4778 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004779 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004782
Gilles Peskine048b7f02018-06-08 14:20:49 +02004783 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 TEST_LE_U(output2_buffer_size,
4785 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4786 TEST_LE_U(output2_buffer_size,
4787 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004788 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (iv_length > 0) {
4791 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4792 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004793 }
Moran Pekerded84402018-06-06 16:36:50 +03004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4796 output2, output2_buffer_size,
4797 &function_output_length));
4798 TEST_LE_U(function_output_length,
4799 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4800 TEST_LE_U(function_output_length,
4801 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004802 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 PSA_ASSERT(psa_cipher_update(&operation2,
4805 output1 + first_part_size,
4806 output1_length - first_part_size,
4807 output2, output2_buffer_size,
4808 &function_output_length));
4809 TEST_LE_U(function_output_length,
4810 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4811 alg,
4812 output1_length - first_part_size));
4813 TEST_LE_U(function_output_length,
4814 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004815 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 PSA_ASSERT(psa_cipher_finish(&operation2,
4818 output2 + output2_length,
4819 output2_buffer_size - output2_length,
4820 &function_output_length));
4821 TEST_LE_U(function_output_length,
4822 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4823 TEST_LE_U(function_output_length,
4824 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004825 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004828
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004829 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004830
4831exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 psa_cipher_abort(&operation1);
4833 psa_cipher_abort(&operation2);
4834 mbedtls_free(output1);
4835 mbedtls_free(output2);
4836 psa_destroy_key(key);
4837 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004838}
4839/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004840
Gilles Peskine20035e32018-02-03 22:44:14 +01004841/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004842void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4843 int alg_arg,
4844 data_t *nonce,
4845 data_t *additional_data,
4846 data_t *input_data,
4847 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848{
Ronald Cron5425a212020-08-04 14:58:35 +02004849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850 psa_key_type_t key_type = key_type_arg;
4851 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004852 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004853 unsigned char *output_data = NULL;
4854 size_t output_size = 0;
4855 size_t output_length = 0;
4856 unsigned char *output_data2 = NULL;
4857 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004858 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004859 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4865 psa_set_key_algorithm(&attributes, alg);
4866 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4869 &key));
4870 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4871 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4874 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004875 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4876 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4878 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4879 TEST_EQUAL(output_size,
4880 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4881 TEST_LE_U(output_size,
4882 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004883 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004884 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 status = psa_aead_encrypt(key, alg,
4887 nonce->x, nonce->len,
4888 additional_data->x,
4889 additional_data->len,
4890 input_data->x, input_data->len,
4891 output_data, output_size,
4892 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004893
4894 /* If the operation is not supported, just skip and not fail in case the
4895 * encryption involves a common limitation of cryptography hardwares and
4896 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if (status == PSA_ERROR_NOT_SUPPORTED) {
4898 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4899 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004900 }
4901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004905 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004906
Gilles Peskine003a4a92019-05-14 16:09:40 +02004907 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4908 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 TEST_EQUAL(input_data->len,
4910 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 TEST_LE_U(input_data->len,
4913 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 TEST_EQUAL(psa_aead_decrypt(key, alg,
4916 nonce->x, nonce->len,
4917 additional_data->x,
4918 additional_data->len,
4919 output_data, output_length,
4920 output_data2, output_length,
4921 &output_length2),
4922 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004923
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004924 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004925 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004926 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004927
Gilles Peskinea1cac842018-06-11 19:33:02 +02004928exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 psa_destroy_key(key);
4930 mbedtls_free(output_data);
4931 mbedtls_free(output_data2);
4932 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004933}
4934/* END_CASE */
4935
4936/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004937void aead_encrypt(int key_type_arg, data_t *key_data,
4938 int alg_arg,
4939 data_t *nonce,
4940 data_t *additional_data,
4941 data_t *input_data,
4942 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004943{
Ronald Cron5425a212020-08-04 14:58:35 +02004944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004945 psa_key_type_t key_type = key_type_arg;
4946 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004947 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004948 unsigned char *output_data = NULL;
4949 size_t output_size = 0;
4950 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004952 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4957 psa_set_key_algorithm(&attributes, alg);
4958 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4961 &key));
4962 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4963 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4966 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004967 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4968 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 TEST_EQUAL(output_size,
4970 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4971 TEST_LE_U(output_size,
4972 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004973 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 status = psa_aead_encrypt(key, alg,
4976 nonce->x, nonce->len,
4977 additional_data->x, additional_data->len,
4978 input_data->x, input_data->len,
4979 output_data, output_size,
4980 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004981
Ronald Cron28a45ed2021-02-09 20:35:42 +01004982 /* If the operation is not supported, just skip and not fail in case the
4983 * encryption involves a common limitation of cryptography hardwares and
4984 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 if (status == PSA_ERROR_NOT_SUPPORTED) {
4986 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4987 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004988 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004991 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004992 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004993
Gilles Peskinea1cac842018-06-11 19:33:02 +02004994exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 psa_destroy_key(key);
4996 mbedtls_free(output_data);
4997 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004998}
4999/* END_CASE */
5000
5001/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005002void aead_decrypt(int key_type_arg, data_t *key_data,
5003 int alg_arg,
5004 data_t *nonce,
5005 data_t *additional_data,
5006 data_t *input_data,
5007 data_t *expected_data,
5008 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005009{
Ronald Cron5425a212020-08-04 14:58:35 +02005010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005011 psa_key_type_t key_type = key_type_arg;
5012 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005013 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005014 unsigned char *output_data = NULL;
5015 size_t output_size = 0;
5016 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02005018 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01005019 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5024 psa_set_key_algorithm(&attributes, alg);
5025 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5028 &key));
5029 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5030 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5033 alg);
5034 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
5035 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01005036 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
5037 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 TEST_EQUAL(output_size,
5039 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5040 TEST_LE_U(output_size,
5041 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01005042 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005043 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005044
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 status = psa_aead_decrypt(key, alg,
5046 nonce->x, nonce->len,
5047 additional_data->x,
5048 additional_data->len,
5049 input_data->x, input_data->len,
5050 output_data, output_size,
5051 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01005052
Ronald Cron28a45ed2021-02-09 20:35:42 +01005053 /* If the operation is not supported, just skip and not fail in case the
5054 * decryption involves a common limitation of cryptography hardwares and
5055 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (status == PSA_ERROR_NOT_SUPPORTED) {
5057 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5058 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01005059 }
Steven Cooremand588ea12021-01-11 19:36:04 +01005060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005064 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005065 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02005067
Gilles Peskinea1cac842018-06-11 19:33:02 +02005068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 psa_destroy_key(key);
5070 mbedtls_free(output_data);
5071 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005072}
5073/* END_CASE */
5074
5075/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005076void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
5077 int alg_arg,
5078 data_t *nonce,
5079 data_t *additional_data,
5080 data_t *input_data,
5081 int do_set_lengths,
5082 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005083{
Paul Elliottd3f82412021-06-16 16:52:21 +01005084 size_t ad_part_len = 0;
5085 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005086 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
5089 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (do_set_lengths) {
5092 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005093 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005095 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005097 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005098
5099 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 if (!aead_multipart_internal_func(key_type_arg, key_data,
5101 alg_arg, nonce,
5102 additional_data,
5103 ad_part_len,
5104 input_data, -1,
5105 set_lengths_method,
5106 expected_output,
5107 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005108 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005109 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 /* length(0) part, length(ad_part_len) part, length(0) part... */
5112 mbedtls_test_set_step(1000 + ad_part_len);
5113
5114 if (!aead_multipart_internal_func(key_type_arg, key_data,
5115 alg_arg, nonce,
5116 additional_data,
5117 ad_part_len,
5118 input_data, -1,
5119 set_lengths_method,
5120 expected_output,
5121 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005122 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 }
5124 }
5125
5126 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5127 /* Split data into length(data_part_len) parts. */
5128 mbedtls_test_set_step(2000 + data_part_len);
5129
5130 if (do_set_lengths) {
5131 if (data_part_len & 0x01) {
5132 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5133 } else {
5134 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5135 }
5136 }
5137
5138 if (!aead_multipart_internal_func(key_type_arg, key_data,
5139 alg_arg, nonce,
5140 additional_data, -1,
5141 input_data, data_part_len,
5142 set_lengths_method,
5143 expected_output,
5144 1, 0)) {
5145 break;
5146 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005147
5148 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 if (!aead_multipart_internal_func(key_type_arg, key_data,
5152 alg_arg, nonce,
5153 additional_data, -1,
5154 input_data, data_part_len,
5155 set_lengths_method,
5156 expected_output,
5157 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005158 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005160 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005161
Paul Elliott8fc45162021-06-23 16:06:01 +01005162 /* Goto is required to silence warnings about unused labels, as we
5163 * don't actually do any test assertions in this function. */
5164 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005165}
5166/* END_CASE */
5167
5168/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005169void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5170 int alg_arg,
5171 data_t *nonce,
5172 data_t *additional_data,
5173 data_t *input_data,
5174 int do_set_lengths,
5175 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005176{
Paul Elliottd3f82412021-06-16 16:52:21 +01005177 size_t ad_part_len = 0;
5178 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005179 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005182 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (do_set_lengths) {
5186 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005187 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005189 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005191 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 if (!aead_multipart_internal_func(key_type_arg, key_data,
5194 alg_arg, nonce,
5195 additional_data,
5196 ad_part_len,
5197 input_data, -1,
5198 set_lengths_method,
5199 expected_output,
5200 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005201 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005203
5204 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 if (!aead_multipart_internal_func(key_type_arg, key_data,
5208 alg_arg, nonce,
5209 additional_data,
5210 ad_part_len,
5211 input_data, -1,
5212 set_lengths_method,
5213 expected_output,
5214 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005215 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005217 }
5218
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005220 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 if (do_set_lengths) {
5224 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005225 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005227 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005229 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 if (!aead_multipart_internal_func(key_type_arg, key_data,
5232 alg_arg, nonce,
5233 additional_data, -1,
5234 input_data, data_part_len,
5235 set_lengths_method,
5236 expected_output,
5237 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005238 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005240
5241 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 if (!aead_multipart_internal_func(key_type_arg, key_data,
5245 alg_arg, nonce,
5246 additional_data, -1,
5247 input_data, data_part_len,
5248 set_lengths_method,
5249 expected_output,
5250 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005251 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005253 }
5254
Paul Elliott8fc45162021-06-23 16:06:01 +01005255 /* Goto is required to silence warnings about unused labels, as we
5256 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005257 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005258}
5259/* END_CASE */
5260
5261/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005262void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5263 int alg_arg,
5264 int nonce_length,
5265 int expected_nonce_length_arg,
5266 data_t *additional_data,
5267 data_t *input_data,
5268 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005269{
5270
5271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5272 psa_key_type_t key_type = key_type_arg;
5273 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005274 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005275 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5277 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005278 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005279 size_t actual_nonce_length = 0;
5280 size_t expected_nonce_length = expected_nonce_length_arg;
5281 unsigned char *output = NULL;
5282 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005283 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005284 size_t ciphertext_size = 0;
5285 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005286 size_t tag_length = 0;
5287 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5292 psa_set_key_algorithm(&attributes, alg);
5293 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005294
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5296 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005301
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005302 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005307
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005308 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005311
5312 /* If the operation is not supported, just skip and not fail in case the
5313 * encryption involves a common limitation of cryptography hardwares and
5314 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 if (status == PSA_ERROR_NOT_SUPPORTED) {
5316 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5317 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005318 }
5319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5323 nonce_length,
5324 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005325
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005329
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 if (expected_status == PSA_SUCCESS) {
5331 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5332 alg));
5333 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005338 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5340 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5343 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5346 output, output_size,
5347 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5350 &ciphertext_length, tag_buffer,
5351 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005352 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005353
5354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 psa_destroy_key(key);
5356 mbedtls_free(output);
5357 mbedtls_free(ciphertext);
5358 psa_aead_abort(&operation);
5359 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005360}
5361/* END_CASE */
5362
5363/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005364void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5365 int alg_arg,
5366 int nonce_length_arg,
5367 int set_lengths_method_arg,
5368 data_t *additional_data,
5369 data_t *input_data,
5370 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005371{
5372
5373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5374 psa_key_type_t key_type = key_type_arg;
5375 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005376 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005377 uint8_t *nonce_buffer = NULL;
5378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5379 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5380 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005381 unsigned char *output = NULL;
5382 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005383 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005384 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005385 size_t ciphertext_size = 0;
5386 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005387 size_t tag_length = 0;
5388 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005389 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005390 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5395 psa_set_key_algorithm(&attributes, alg);
5396 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5399 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005402
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005404
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005405 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005410
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005411 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005414
5415 /* If the operation is not supported, just skip and not fail in case the
5416 * encryption involves a common limitation of cryptography hardwares and
5417 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if (status == PSA_ERROR_NOT_SUPPORTED) {
5419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005421 }
5422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005424
Paul Elliott4023ffd2021-09-10 16:21:22 +01005425 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 if (nonce_length_arg == -1) {
5427 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005428 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 nonce_length = 0;
5430 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005431 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005433 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 if (nonce_buffer) {
5436 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005437 nonce_buffer[index] = 'a' + index;
5438 }
Paul Elliott66696b52021-08-16 18:42:41 +01005439 }
Paul Elliott863864a2021-07-23 17:28:31 +01005440 }
5441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5443 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5444 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005445 }
5446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (expected_status == PSA_SUCCESS) {
5452 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5453 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5454 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005455 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005457 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 }
Paul Elliott863864a2021-07-23 17:28:31 +01005459
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005460 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5462 additional_data->len),
5463 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5466 output, output_size,
5467 &ciphertext_length),
5468 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5471 &ciphertext_length, tag_buffer,
5472 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5473 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005474 }
5475
5476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 psa_destroy_key(key);
5478 mbedtls_free(output);
5479 mbedtls_free(ciphertext);
5480 mbedtls_free(nonce_buffer);
5481 psa_aead_abort(&operation);
5482 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005483}
5484/* END_CASE */
5485
5486/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005487void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005488 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005489 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005490 data_t *nonce,
5491 data_t *additional_data,
5492 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005494{
5495
5496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5497 psa_key_type_t key_type = key_type_arg;
5498 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005499 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5501 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5502 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005503 unsigned char *output = NULL;
5504 unsigned char *ciphertext = NULL;
5505 size_t output_size = output_size_arg;
5506 size_t ciphertext_size = 0;
5507 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005508 size_t tag_length = 0;
5509 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5514 psa_set_key_algorithm(&attributes, alg);
5515 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5518 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005521
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005522 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005525
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005526 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005529
5530 /* If the operation is not supported, just skip and not fail in case the
5531 * encryption involves a common limitation of cryptography hardwares and
5532 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 if (status == PSA_ERROR_NOT_SUPPORTED) {
5534 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5535 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005536 }
5537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5541 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5546 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 status = psa_aead_update(&operation, input_data->x, input_data->len,
5549 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005554 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5556 &ciphertext_length, tag_buffer,
5557 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005558 }
5559
5560exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 psa_destroy_key(key);
5562 mbedtls_free(output);
5563 mbedtls_free(ciphertext);
5564 psa_aead_abort(&operation);
5565 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005566}
5567/* END_CASE */
5568
Paul Elliott91b021e2021-07-23 18:52:31 +01005569/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005570void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5571 int alg_arg,
5572 int finish_ciphertext_size_arg,
5573 int tag_size_arg,
5574 data_t *nonce,
5575 data_t *additional_data,
5576 data_t *input_data,
5577 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005578{
5579
5580 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5581 psa_key_type_t key_type = key_type_arg;
5582 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005583 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5585 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5586 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005587 unsigned char *ciphertext = NULL;
5588 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005589 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005590 size_t ciphertext_size = 0;
5591 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5593 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005594 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5599 psa_set_key_algorithm(&attributes, alg);
5600 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5603 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005608
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005609 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005610
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005611 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005612
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005613 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005616
5617 /* If the operation is not supported, just skip and not fail in case the
5618 * encryption involves a common limitation of cryptography hardwares and
5619 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 if (status == PSA_ERROR_NOT_SUPPORTED) {
5621 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5622 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005623 }
5624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5630 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5633 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5636 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005637
5638 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 status = psa_aead_finish(&operation, finish_ciphertext,
5640 finish_ciphertext_size,
5641 &ciphertext_length, tag_buffer,
5642 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005645
5646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 psa_destroy_key(key);
5648 mbedtls_free(ciphertext);
5649 mbedtls_free(finish_ciphertext);
5650 mbedtls_free(tag_buffer);
5651 psa_aead_abort(&operation);
5652 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005653}
5654/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005655
5656/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005657void aead_multipart_verify(int key_type_arg, data_t *key_data,
5658 int alg_arg,
5659 data_t *nonce,
5660 data_t *additional_data,
5661 data_t *input_data,
5662 data_t *tag,
5663 int tag_usage_arg,
5664 int expected_setup_status_arg,
5665 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005666{
5667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5668 psa_key_type_t key_type = key_type_arg;
5669 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005670 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5672 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5673 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005674 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005675 unsigned char *plaintext = NULL;
5676 unsigned char *finish_plaintext = NULL;
5677 size_t plaintext_size = 0;
5678 size_t plaintext_length = 0;
5679 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005680 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005681 unsigned char *tag_buffer = NULL;
5682 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5687 psa_set_key_algorithm(&attributes, alg);
5688 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5691 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5696 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005697
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005698 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005701
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005702 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005705
5706 /* If the operation is not supported, just skip and not fail in case the
5707 * encryption involves a common limitation of cryptography hardwares and
5708 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 if (status == PSA_ERROR_NOT_SUPPORTED) {
5710 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5711 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005712 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005716 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 }
Paul Elliott9961a662021-09-17 19:19:02 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 status = psa_aead_set_lengths(&operation, additional_data->len,
5724 input_data->len);
5725 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5728 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005729
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5731 input_data->len,
5732 plaintext, plaintext_size,
5733 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005736 tag_buffer = tag->x;
5737 tag_size = tag->len;
5738 }
5739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 status = psa_aead_verify(&operation, finish_plaintext,
5741 verify_plaintext_size,
5742 &plaintext_length,
5743 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005746
5747exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 psa_destroy_key(key);
5749 mbedtls_free(plaintext);
5750 mbedtls_free(finish_plaintext);
5751 psa_aead_abort(&operation);
5752 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005753}
5754/* END_CASE */
5755
Paul Elliott9961a662021-09-17 19:19:02 +01005756/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005757void aead_multipart_setup(int key_type_arg, data_t *key_data,
5758 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005759{
5760 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5761 psa_key_type_t key_type = key_type_arg;
5762 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005763 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5765 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5766 psa_status_t expected_status = expected_status_arg;
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 psa_set_key_usage_flags(&attributes,
5771 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5772 psa_set_key_algorithm(&attributes, alg);
5773 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5776 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005787
5788exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 psa_destroy_key(key);
5790 psa_aead_abort(&operation);
5791 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005792}
5793/* END_CASE */
5794
5795/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005796void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5797 int alg_arg,
5798 data_t *nonce,
5799 data_t *additional_data,
5800 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005801{
5802 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5803 psa_key_type_t key_type = key_type_arg;
5804 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005805 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005806 unsigned char *output_data = NULL;
5807 unsigned char *final_data = NULL;
5808 size_t output_size = 0;
5809 size_t finish_output_size = 0;
5810 size_t output_length = 0;
5811 size_t key_bits = 0;
5812 size_t tag_length = 0;
5813 size_t tag_size = 0;
5814 size_t nonce_length = 0;
5815 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5816 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5817 size_t output_part_length = 0;
5818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 psa_set_key_usage_flags(&attributes,
5823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5824 psa_set_key_algorithm(&attributes, alg);
5825 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5828 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5831 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005832
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005839 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005844
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005845 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005846
5847 /* Test all operations error without calling setup first. */
5848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5850 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5855 PSA_AEAD_NONCE_MAX_SIZE,
5856 &nonce_length),
5857 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005860
Paul Elliott481be342021-07-16 17:38:47 +01005861 /* ------------------------------------------------------- */
5862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5864 input_data->len),
5865 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005868
Paul Elliott481be342021-07-16 17:38:47 +01005869 /* ------------------------------------------------------- */
5870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5872 additional_data->len),
5873 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005876
Paul Elliott481be342021-07-16 17:38:47 +01005877 /* ------------------------------------------------------- */
5878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5880 input_data->len, output_data,
5881 output_size, &output_length),
5882 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005885
Paul Elliott481be342021-07-16 17:38:47 +01005886 /* ------------------------------------------------------- */
5887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5889 finish_output_size,
5890 &output_part_length,
5891 tag_buffer, tag_length,
5892 &tag_size),
5893 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005896
Paul Elliott481be342021-07-16 17:38:47 +01005897 /* ------------------------------------------------------- */
5898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5900 finish_output_size,
5901 &output_part_length,
5902 tag_buffer,
5903 tag_length),
5904 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005907
5908 /* Test for double setups. */
5909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5913 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005916
Paul Elliott481be342021-07-16 17:38:47 +01005917 /* ------------------------------------------------------- */
5918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005920
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5922 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005925
Paul Elliott374a2be2021-07-16 17:53:40 +01005926 /* ------------------------------------------------------- */
5927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5931 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005934
5935 /* ------------------------------------------------------- */
5936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5940 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005943
Paul Elliottc23a9a02021-06-21 18:32:46 +01005944 /* Test for not setting a nonce. */
5945
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5949 additional_data->len),
5950 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005953
Paul Elliott7f628422021-09-01 12:08:29 +01005954 /* ------------------------------------------------------- */
5955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5959 input_data->len, output_data,
5960 output_size, &output_length),
5961 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005964
Paul Elliottbdc2c682021-09-21 18:37:10 +01005965 /* ------------------------------------------------------- */
5966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5970 finish_output_size,
5971 &output_part_length,
5972 tag_buffer, tag_length,
5973 &tag_size),
5974 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005977
5978 /* ------------------------------------------------------- */
5979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5983 finish_output_size,
5984 &output_part_length,
5985 tag_buffer,
5986 tag_length),
5987 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005990
Paul Elliottc23a9a02021-06-21 18:32:46 +01005991 /* Test for double setting nonce. */
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5998 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006001
Paul Elliott374a2be2021-07-16 17:53:40 +01006002 /* Test for double generating nonce. */
6003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6007 PSA_AEAD_NONCE_MAX_SIZE,
6008 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6011 PSA_AEAD_NONCE_MAX_SIZE,
6012 &nonce_length),
6013 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006014
6015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006017
6018 /* Test for generate nonce then set and vice versa */
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6023 PSA_AEAD_NONCE_MAX_SIZE,
6024 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6027 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006030
Andrzej Kurekad837522021-12-15 15:28:49 +01006031 /* Test for generating nonce after calling set lengths */
6032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006034
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6036 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6039 PSA_AEAD_NONCE_MAX_SIZE,
6040 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006043
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006044 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (operation.alg == PSA_ALG_CCM) {
6049 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6050 input_data->len),
6051 PSA_ERROR_INVALID_ARGUMENT);
6052 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6053 PSA_AEAD_NONCE_MAX_SIZE,
6054 &nonce_length),
6055 PSA_ERROR_BAD_STATE);
6056 } else {
6057 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6058 input_data->len));
6059 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6060 PSA_AEAD_NONCE_MAX_SIZE,
6061 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006062 }
6063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006065
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006066 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00006067#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6071 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6072 input_data->len),
6073 PSA_ERROR_INVALID_ARGUMENT);
6074 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6075 PSA_AEAD_NONCE_MAX_SIZE,
6076 &nonce_length),
6077 PSA_ERROR_BAD_STATE);
6078 } else {
6079 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6080 input_data->len));
6081 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6082 PSA_AEAD_NONCE_MAX_SIZE,
6083 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006084 }
6085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00006087#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01006088
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006089 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6094 PSA_AEAD_NONCE_MAX_SIZE,
6095 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (operation.alg == PSA_ALG_CCM) {
6098 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6099 input_data->len),
6100 PSA_ERROR_INVALID_ARGUMENT);
6101 } else {
6102 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6103 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006104 }
6105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006107
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006108 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006109 /* Test for setting nonce after calling set lengths */
6110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6114 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006119
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006120 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 if (operation.alg == PSA_ALG_CCM) {
6125 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6126 input_data->len),
6127 PSA_ERROR_INVALID_ARGUMENT);
6128 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6129 PSA_ERROR_BAD_STATE);
6130 } else {
6131 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6132 input_data->len));
6133 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006134 }
6135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006137
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006138 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006139#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6143 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6144 input_data->len),
6145 PSA_ERROR_INVALID_ARGUMENT);
6146 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6147 PSA_ERROR_BAD_STATE);
6148 } else {
6149 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6150 input_data->len));
6151 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006152 }
6153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006155#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006156
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006157 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (operation.alg == PSA_ALG_CCM) {
6164 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6165 input_data->len),
6166 PSA_ERROR_INVALID_ARGUMENT);
6167 } else {
6168 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6169 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006170 }
6171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006173
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006174 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006175#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 if (operation.alg == PSA_ALG_GCM) {
6179 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6180 SIZE_MAX),
6181 PSA_ERROR_INVALID_ARGUMENT);
6182 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6183 PSA_ERROR_BAD_STATE);
6184 } else if (operation.alg != PSA_ALG_CCM) {
6185 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6186 SIZE_MAX));
6187 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006188 }
6189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006191#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006192
Tom Cosgrove1797b052022-12-04 17:19:59 +00006193 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006194#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (operation.alg == PSA_ALG_GCM) {
6200 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6201 SIZE_MAX),
6202 PSA_ERROR_INVALID_ARGUMENT);
6203 } else if (operation.alg != PSA_ALG_CCM) {
6204 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6205 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006206 }
6207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006209#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006210
6211 /* ------------------------------------------------------- */
6212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6218 PSA_AEAD_NONCE_MAX_SIZE,
6219 &nonce_length),
6220 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006223
Paul Elliott7220cae2021-06-22 17:25:57 +01006224 /* Test for generating nonce in decrypt setup. */
6225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6229 PSA_AEAD_NONCE_MAX_SIZE,
6230 &nonce_length),
6231 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006234
Paul Elliottc23a9a02021-06-21 18:32:46 +01006235 /* Test for setting lengths twice. */
6236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6242 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6245 input_data->len),
6246 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006249
Andrzej Kurekad837522021-12-15 15:28:49 +01006250 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006251
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6259 additional_data->len),
6260 PSA_ERROR_BAD_STATE);
6261 } else {
6262 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6263 additional_data->len));
6264
6265 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6266 input_data->len),
6267 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006268 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006270
6271 /* ------------------------------------------------------- */
6272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 if (operation.alg == PSA_ALG_CCM) {
6278 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6279 input_data->len, output_data,
6280 output_size, &output_length),
6281 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 } else {
6284 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6285 input_data->len, output_data,
6286 output_size, &output_length));
6287
6288 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6289 input_data->len),
6290 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006293
6294 /* ------------------------------------------------------- */
6295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 if (operation.alg == PSA_ALG_CCM) {
6301 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6302 finish_output_size,
6303 &output_part_length,
6304 tag_buffer, tag_length,
6305 &tag_size));
6306 } else {
6307 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6308 finish_output_size,
6309 &output_part_length,
6310 tag_buffer, tag_length,
6311 &tag_size));
6312
6313 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6314 input_data->len),
6315 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006316 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006318
6319 /* Test for setting lengths after generating nonce + already starting data. */
6320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6324 PSA_AEAD_NONCE_MAX_SIZE,
6325 &nonce_length));
6326 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6329 additional_data->len),
6330 PSA_ERROR_BAD_STATE);
6331 } else {
6332 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6333 additional_data->len));
6334
6335 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6336 input_data->len),
6337 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006338 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006340
6341 /* ------------------------------------------------------- */
6342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6346 PSA_AEAD_NONCE_MAX_SIZE,
6347 &nonce_length));
6348 if (operation.alg == PSA_ALG_CCM) {
6349 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6350 input_data->len, output_data,
6351 output_size, &output_length),
6352 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 } else {
6355 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6356 input_data->len, output_data,
6357 output_size, &output_length));
6358
6359 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6360 input_data->len),
6361 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006362 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006364
6365 /* ------------------------------------------------------- */
6366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6370 PSA_AEAD_NONCE_MAX_SIZE,
6371 &nonce_length));
6372 if (operation.alg == PSA_ALG_CCM) {
6373 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6374 finish_output_size,
6375 &output_part_length,
6376 tag_buffer, tag_length,
6377 &tag_size));
6378 } else {
6379 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6380 finish_output_size,
6381 &output_part_length,
6382 tag_buffer, tag_length,
6383 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6386 input_data->len),
6387 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006388 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006390
Paul Elliott243080c2021-07-21 19:01:17 +01006391 /* Test for not sending any additional data or data after setting non zero
6392 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6399 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6402 finish_output_size,
6403 &output_part_length,
6404 tag_buffer, tag_length,
6405 &tag_size),
6406 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006409
Paul Elliott243080c2021-07-21 19:01:17 +01006410 /* Test for not sending any additional data or data after setting non-zero
6411 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6418 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6421 finish_output_size,
6422 &output_part_length,
6423 tag_buffer,
6424 tag_length),
6425 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006428
Paul Elliott243080c2021-07-21 19:01:17 +01006429 /* Test for not sending any additional data after setting a non-zero length
6430 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6437 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6440 input_data->len, output_data,
6441 output_size, &output_length),
6442 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006445
Paul Elliottf94bd992021-09-19 18:15:59 +01006446 /* Test for not sending any data after setting a non-zero length for it.*/
6447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6453 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6456 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006457
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6459 finish_output_size,
6460 &output_part_length,
6461 tag_buffer, tag_length,
6462 &tag_size),
6463 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006466
Paul Elliottb0450fe2021-09-01 15:06:26 +01006467 /* Test for sending too much additional data after setting lengths. */
6468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006474
6475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6477 additional_data->len),
6478 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006481
Paul Elliotta2a09b02021-09-22 14:56:40 +01006482 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6489 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6492 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6495 1),
6496 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006499
Paul Elliottb0450fe2021-09-01 15:06:26 +01006500 /* Test for sending too much data after setting lengths. */
6501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6509 input_data->len, output_data,
6510 output_size, &output_length),
6511 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006514
Paul Elliotta2a09b02021-09-22 14:56:40 +01006515 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006518
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6522 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6525 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6528 input_data->len, output_data,
6529 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006530
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6532 1, output_data,
6533 output_size, &output_length),
6534 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006537
Paul Elliottc23a9a02021-06-21 18:32:46 +01006538 /* Test sending additional data after data. */
6539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006543
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 if (operation.alg != PSA_ALG_CCM) {
6545 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6546 input_data->len, output_data,
6547 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6550 additional_data->len),
6551 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006554
Paul Elliott534d0b42021-06-22 19:15:20 +01006555 /* Test calling finish on decryption. */
6556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6562 finish_output_size,
6563 &output_part_length,
6564 tag_buffer, tag_length,
6565 &tag_size),
6566 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006567
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006569
6570 /* Test calling verify on encryption. */
6571
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006573
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006575
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6577 finish_output_size,
6578 &output_part_length,
6579 tag_buffer,
6580 tag_length),
6581 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006582
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006584
6585
Paul Elliottc23a9a02021-06-21 18:32:46 +01006586exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 psa_destroy_key(key);
6588 psa_aead_abort(&operation);
6589 mbedtls_free(output_data);
6590 mbedtls_free(final_data);
6591 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006592}
6593/* END_CASE */
6594
6595/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006596void signature_size(int type_arg,
6597 int bits,
6598 int alg_arg,
6599 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006600{
6601 psa_key_type_t type = type_arg;
6602 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006606
Gilles Peskinee59236f2018-01-27 23:32:46 +01006607exit:
6608 ;
6609}
6610/* END_CASE */
6611
6612/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006613void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6614 int alg_arg, data_t *input_data,
6615 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006616{
Ronald Cron5425a212020-08-04 14:58:35 +02006617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006618 psa_key_type_t key_type = key_type_arg;
6619 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006620 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006621 unsigned char *signature = NULL;
6622 size_t signature_size;
6623 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006625
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6629 psa_set_key_algorithm(&attributes, alg);
6630 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006631
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6633 &key));
6634 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6635 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006636
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006637 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006638 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6640 key_bits, alg);
6641 TEST_ASSERT(signature_size != 0);
6642 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006643 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006644
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006645 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 PSA_ASSERT(psa_sign_hash(key, alg,
6647 input_data->x, input_data->len,
6648 signature, signature_size,
6649 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006650 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006651 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006652 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006653
6654exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006655 /*
6656 * Key attributes may have been returned by psa_get_key_attributes()
6657 * thus reset them as required.
6658 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 psa_destroy_key(key);
6662 mbedtls_free(signature);
6663 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006664}
6665/* END_CASE */
6666
Paul Elliott712d5122022-12-07 14:03:10 +00006667/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006668/**
6669 * sign_hash_interruptible() test intentions:
6670 *
6671 * Note: This test can currently only handle ECDSA.
6672 *
6673 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006674 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006675 *
6676 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6677 * expected for different max_ops values.
6678 *
6679 * 3. Test that the number of ops done prior to start and after abort is zero
6680 * and that each successful stage completes some ops (this is not mandated by
6681 * the PSA specification, but is currently the case).
6682 *
6683 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6684 * complete() calls does not alter the number of ops returned.
6685 */
Paul Elliott712d5122022-12-07 14:03:10 +00006686void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6687 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006688 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006689{
6690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6691 psa_key_type_t key_type = key_type_arg;
6692 psa_algorithm_t alg = alg_arg;
6693 size_t key_bits;
6694 unsigned char *signature = NULL;
6695 size_t signature_size;
6696 size_t signature_length = 0xdeadbeef;
6697 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6698 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006699 uint32_t num_ops = 0;
6700 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006701 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006702 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006703 size_t min_completes = 0;
6704 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006705
Paul Elliott712d5122022-12-07 14:03:10 +00006706 psa_sign_hash_interruptible_operation_t operation =
6707 psa_sign_hash_interruptible_operation_init();
6708
6709 PSA_ASSERT(psa_crypto_init());
6710
6711 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6712 psa_set_key_algorithm(&attributes, alg);
6713 psa_set_key_type(&attributes, key_type);
6714
6715 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6716 &key));
6717 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6718 key_bits = psa_get_key_bits(&attributes);
6719
6720 /* Allocate a buffer which has the size advertised by the
6721 * library. */
6722 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6723 key_bits, alg);
6724 TEST_ASSERT(signature_size != 0);
6725 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006726 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006727
Paul Elliott0c683352022-12-16 19:16:56 +00006728 psa_interruptible_set_max_ops(max_ops);
6729
Paul Elliott6f600372023-02-06 18:41:05 +00006730 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6731 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006732
Paul Elliott712d5122022-12-07 14:03:10 +00006733 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6734 TEST_ASSERT(num_ops_prior == 0);
6735
6736 /* Start performing the signature. */
6737 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6738 input_data->x, input_data->len));
6739
6740 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6741 TEST_ASSERT(num_ops_prior == 0);
6742
6743 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006744 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006745 status = psa_sign_hash_complete(&operation, signature, signature_size,
6746 &signature_length);
6747
Paul Elliott0c683352022-12-16 19:16:56 +00006748 num_completes++;
6749
Paul Elliott712d5122022-12-07 14:03:10 +00006750 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6751 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006752 /* We are asserting here that every complete makes progress
6753 * (completes some ops), which is true of the internal
6754 * implementation and probably any implementation, however this is
6755 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006756 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006757
Paul Elliott712d5122022-12-07 14:03:10 +00006758 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006759
6760 /* Ensure calling get_num_ops() twice still returns the same
6761 * number of ops as previously reported. */
6762 num_ops = psa_sign_hash_get_num_ops(&operation);
6763
6764 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006765 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006766 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006767
6768 TEST_ASSERT(status == PSA_SUCCESS);
6769
Paul Elliott0c683352022-12-16 19:16:56 +00006770 TEST_LE_U(min_completes, num_completes);
6771 TEST_LE_U(num_completes, max_completes);
6772
Paul Elliott712d5122022-12-07 14:03:10 +00006773 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006774 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006775 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006776
6777 PSA_ASSERT(psa_sign_hash_abort(&operation));
6778
Paul Elliott59ad9452022-12-18 15:09:02 +00006779 num_ops = psa_sign_hash_get_num_ops(&operation);
6780 TEST_ASSERT(num_ops == 0);
6781
Paul Elliott712d5122022-12-07 14:03:10 +00006782exit:
6783
6784 /*
6785 * Key attributes may have been returned by psa_get_key_attributes()
6786 * thus reset them as required.
6787 */
6788 psa_reset_key_attributes(&attributes);
6789
6790 psa_destroy_key(key);
6791 mbedtls_free(signature);
6792 PSA_DONE();
6793}
6794/* END_CASE */
6795
Gilles Peskine20035e32018-02-03 22:44:14 +01006796/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006797void sign_hash_fail(int key_type_arg, data_t *key_data,
6798 int alg_arg, data_t *input_data,
6799 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006800{
Ronald Cron5425a212020-08-04 14:58:35 +02006801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006802 psa_key_type_t key_type = key_type_arg;
6803 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006804 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006805 psa_status_t actual_status;
6806 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006807 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006808 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006810
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006811 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006812
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006814
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6816 psa_set_key_algorithm(&attributes, alg);
6817 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6820 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006821
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 actual_status = psa_sign_hash(key, alg,
6823 input_data->x, input_data->len,
6824 signature, signature_size,
6825 &signature_length);
6826 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006827 /* The value of *signature_length is unspecified on error, but
6828 * whatever it is, it should be less than signature_size, so that
6829 * if the caller tries to read *signature_length bytes without
6830 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006832
6833exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 psa_reset_key_attributes(&attributes);
6835 psa_destroy_key(key);
6836 mbedtls_free(signature);
6837 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006838}
6839/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006840
Paul Elliott91007972022-12-16 12:21:24 +00006841/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006842/**
6843 * sign_hash_fail_interruptible() test intentions:
6844 *
6845 * Note: This test can currently only handle ECDSA.
6846 *
6847 * 1. Test that various failure cases for interruptible sign hash fail with the
6848 * correct error codes, and at the correct point (at start or during
6849 * complete).
6850 *
6851 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6852 * expected for different max_ops values.
6853 *
6854 * 3. Test that the number of ops done prior to start and after abort is zero
6855 * and that each successful stage completes some ops (this is not mandated by
6856 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006857 *
6858 * 4. Check that calling complete() when start() fails and complete()
6859 * after completion results in a BAD_STATE error.
6860 *
6861 * 5. Check that calling start() again after start fails results in a BAD_STATE
6862 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006863 */
Paul Elliott91007972022-12-16 12:21:24 +00006864void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6865 int alg_arg, data_t *input_data,
6866 int signature_size_arg,
6867 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006868 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006869 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006870{
6871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6872 psa_key_type_t key_type = key_type_arg;
6873 psa_algorithm_t alg = alg_arg;
6874 size_t signature_size = signature_size_arg;
6875 psa_status_t actual_status;
6876 psa_status_t expected_start_status = expected_start_status_arg;
6877 psa_status_t expected_complete_status = expected_complete_status_arg;
6878 unsigned char *signature = NULL;
6879 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006880 uint32_t num_ops = 0;
6881 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006882 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006883 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006884 size_t min_completes = 0;
6885 size_t max_completes = 0;
6886
Paul Elliott91007972022-12-16 12:21:24 +00006887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6888 psa_sign_hash_interruptible_operation_t operation =
6889 psa_sign_hash_interruptible_operation_init();
6890
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006891 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006892
6893 PSA_ASSERT(psa_crypto_init());
6894
6895 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6896 psa_set_key_algorithm(&attributes, alg);
6897 psa_set_key_type(&attributes, key_type);
6898
6899 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6900 &key));
6901
Paul Elliott0c683352022-12-16 19:16:56 +00006902 psa_interruptible_set_max_ops(max_ops);
6903
Paul Elliott6f600372023-02-06 18:41:05 +00006904 interruptible_signverify_get_minmax_completes(max_ops,
6905 expected_complete_status,
6906 &min_completes,
6907 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006908
Paul Elliott91007972022-12-16 12:21:24 +00006909 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6910 TEST_ASSERT(num_ops_prior == 0);
6911
6912 /* Start performing the signature. */
6913 actual_status = psa_sign_hash_start(&operation, key, alg,
6914 input_data->x, input_data->len);
6915
6916 TEST_EQUAL(actual_status, expected_start_status);
6917
Paul Elliottc9774412023-02-06 15:14:07 +00006918 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006919 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006920 * start failed. */
6921 actual_status = psa_sign_hash_complete(&operation, signature,
6922 signature_size,
6923 &signature_length);
6924
6925 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6926
6927 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006928 actual_status = psa_sign_hash_start(&operation, key, alg,
6929 input_data->x, input_data->len);
6930
6931 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6932 }
6933
Paul Elliott91007972022-12-16 12:21:24 +00006934 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6935 TEST_ASSERT(num_ops_prior == 0);
6936
Paul Elliott91007972022-12-16 12:21:24 +00006937 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006938 do {
Paul Elliott91007972022-12-16 12:21:24 +00006939 actual_status = psa_sign_hash_complete(&operation, signature,
6940 signature_size,
6941 &signature_length);
6942
Paul Elliott0c683352022-12-16 19:16:56 +00006943 num_completes++;
6944
Paul Elliott334d7262023-01-20 17:29:41 +00006945 if (actual_status == PSA_SUCCESS ||
6946 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006947 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006948 /* We are asserting here that every complete makes progress
6949 * (completes some ops), which is true of the internal
6950 * implementation and probably any implementation, however this is
6951 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006952 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006953
Paul Elliott91007972022-12-16 12:21:24 +00006954 num_ops_prior = num_ops;
6955 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006956 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006957
Paul Elliottc9774412023-02-06 15:14:07 +00006958 TEST_EQUAL(actual_status, expected_complete_status);
6959
Paul Elliottefebad02023-02-15 16:56:45 +00006960 /* Check that another complete returns BAD_STATE. */
6961 actual_status = psa_sign_hash_complete(&operation, signature,
6962 signature_size,
6963 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006964
Paul Elliottefebad02023-02-15 16:56:45 +00006965 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006966
Paul Elliott91007972022-12-16 12:21:24 +00006967 PSA_ASSERT(psa_sign_hash_abort(&operation));
6968
Paul Elliott59ad9452022-12-18 15:09:02 +00006969 num_ops = psa_sign_hash_get_num_ops(&operation);
6970 TEST_ASSERT(num_ops == 0);
6971
Paul Elliott91007972022-12-16 12:21:24 +00006972 /* The value of *signature_length is unspecified on error, but
6973 * whatever it is, it should be less than signature_size, so that
6974 * if the caller tries to read *signature_length bytes without
6975 * checking the error code then they don't overflow a buffer. */
6976 TEST_LE_U(signature_length, signature_size);
6977
Paul Elliott0c683352022-12-16 19:16:56 +00006978 TEST_LE_U(min_completes, num_completes);
6979 TEST_LE_U(num_completes, max_completes);
6980
Paul Elliott91007972022-12-16 12:21:24 +00006981exit:
6982 psa_reset_key_attributes(&attributes);
6983 psa_destroy_key(key);
6984 mbedtls_free(signature);
6985 PSA_DONE();
6986}
6987/* END_CASE */
6988
mohammad16038cc1cee2018-03-28 01:21:33 +03006989/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006990void sign_verify_hash(int key_type_arg, data_t *key_data,
6991 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006992{
Ronald Cron5425a212020-08-04 14:58:35 +02006993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006994 psa_key_type_t key_type = key_type_arg;
6995 psa_algorithm_t alg = alg_arg;
6996 size_t key_bits;
6997 unsigned char *signature = NULL;
6998 size_t signature_size;
6999 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007000 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02007001
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02007003
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
7005 psa_set_key_algorithm(&attributes, alg);
7006 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02007007
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7009 &key));
7010 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7011 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02007012
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007013 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02007014 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7016 key_bits, alg);
7017 TEST_ASSERT(signature_size != 0);
7018 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007019 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02007020
7021 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 PSA_ASSERT(psa_sign_hash(key, alg,
7023 input_data->x, input_data->len,
7024 signature, signature_size,
7025 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007026 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 TEST_LE_U(signature_length, signature_size);
7028 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02007029
7030 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 PSA_ASSERT(psa_verify_hash(key, alg,
7032 input_data->x, input_data->len,
7033 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007034
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02007036 /* Flip a bit in the input and verify that the signature is now
7037 * detected as invalid. Flip a bit at the beginning, not at the end,
7038 * because ECDSA may ignore the last few bits of the input. */
7039 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 TEST_EQUAL(psa_verify_hash(key, alg,
7041 input_data->x, input_data->len,
7042 signature, signature_length),
7043 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02007044 }
7045
7046exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007047 /*
7048 * Key attributes may have been returned by psa_get_key_attributes()
7049 * thus reset them as required.
7050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 psa_destroy_key(key);
7054 mbedtls_free(signature);
7055 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02007056}
7057/* END_CASE */
7058
Paul Elliott712d5122022-12-07 14:03:10 +00007059/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007060/**
7061 * sign_verify_hash_interruptible() test intentions:
7062 *
7063 * Note: This test can currently only handle ECDSA.
7064 *
Paul Elliott8c092052023-03-06 17:49:14 +00007065 * 1. Test that we can sign an input hash with the given keypair and then
7066 * afterwards verify that signature. This is currently the only way to test
7067 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00007068 *
7069 * 2. Test that after corrupting the hash, the verification detects an invalid
7070 * signature.
7071 *
7072 * 3. Test the number of calls to psa_sign_hash_complete() required are as
7073 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00007074 *
7075 * 4. Test that the number of ops done prior to starting signing and after abort
7076 * is zero and that each successful signing stage completes some ops (this is
7077 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00007078 */
Paul Elliott712d5122022-12-07 14:03:10 +00007079void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00007080 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007081 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007082{
7083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7084 psa_key_type_t key_type = key_type_arg;
7085 psa_algorithm_t alg = alg_arg;
7086 size_t key_bits;
7087 unsigned char *signature = NULL;
7088 size_t signature_size;
7089 size_t signature_length = 0xdeadbeef;
7090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7091 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007092 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00007093 uint32_t num_ops = 0;
7094 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007095 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007096 size_t min_completes = 0;
7097 size_t max_completes = 0;
7098
Paul Elliott712d5122022-12-07 14:03:10 +00007099 psa_sign_hash_interruptible_operation_t sign_operation =
7100 psa_sign_hash_interruptible_operation_init();
7101 psa_verify_hash_interruptible_operation_t verify_operation =
7102 psa_verify_hash_interruptible_operation_init();
7103
7104 PSA_ASSERT(psa_crypto_init());
7105
Paul Elliott0c683352022-12-16 19:16:56 +00007106 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7107 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007108 psa_set_key_algorithm(&attributes, alg);
7109 psa_set_key_type(&attributes, key_type);
7110
7111 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7112 &key));
7113 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7114 key_bits = psa_get_key_bits(&attributes);
7115
7116 /* Allocate a buffer which has the size advertised by the
7117 * library. */
7118 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7119 key_bits, alg);
7120 TEST_ASSERT(signature_size != 0);
7121 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007122 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007123
Paul Elliott0c683352022-12-16 19:16:56 +00007124 psa_interruptible_set_max_ops(max_ops);
7125
Paul Elliott6f600372023-02-06 18:41:05 +00007126 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7127 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007128
Paul Elliott7c173082023-02-26 18:44:45 +00007129 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7130 TEST_ASSERT(num_ops_prior == 0);
7131
Paul Elliott712d5122022-12-07 14:03:10 +00007132 /* Start performing the signature. */
7133 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7134 input_data->x, input_data->len));
7135
Paul Elliott7c173082023-02-26 18:44:45 +00007136 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7137 TEST_ASSERT(num_ops_prior == 0);
7138
Paul Elliott712d5122022-12-07 14:03:10 +00007139 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007140 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007141
Paul Elliott0c683352022-12-16 19:16:56 +00007142 status = psa_sign_hash_complete(&sign_operation, signature,
7143 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007144 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007145
7146 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007147
7148 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7149 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7150 /* We are asserting here that every complete makes progress
7151 * (completes some ops), which is true of the internal
7152 * implementation and probably any implementation, however this is
7153 * not mandated by the PSA specification. */
7154 TEST_ASSERT(num_ops > num_ops_prior);
7155
7156 num_ops_prior = num_ops;
7157 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007158 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007159
7160 TEST_ASSERT(status == PSA_SUCCESS);
7161
Paul Elliott0c683352022-12-16 19:16:56 +00007162 TEST_LE_U(min_completes, num_completes);
7163 TEST_LE_U(num_completes, max_completes);
7164
Paul Elliott712d5122022-12-07 14:03:10 +00007165 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7166
Paul Elliott7c173082023-02-26 18:44:45 +00007167 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7168 TEST_ASSERT(num_ops == 0);
7169
Paul Elliott712d5122022-12-07 14:03:10 +00007170 /* Check that the signature length looks sensible. */
7171 TEST_LE_U(signature_length, signature_size);
7172 TEST_ASSERT(signature_length > 0);
7173
Paul Elliott0c683352022-12-16 19:16:56 +00007174 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007175
7176 /* Start verification. */
7177 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7178 input_data->x, input_data->len,
7179 signature, signature_length));
7180
7181 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007182 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007183 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007184
7185 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007186 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007187
7188 TEST_ASSERT(status == PSA_SUCCESS);
7189
Paul Elliott0c683352022-12-16 19:16:56 +00007190 TEST_LE_U(min_completes, num_completes);
7191 TEST_LE_U(num_completes, max_completes);
7192
Paul Elliott712d5122022-12-07 14:03:10 +00007193 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7194
7195 verify_operation = psa_verify_hash_interruptible_operation_init();
7196
7197 if (input_data->len != 0) {
7198 /* Flip a bit in the input and verify that the signature is now
7199 * detected as invalid. Flip a bit at the beginning, not at the end,
7200 * because ECDSA may ignore the last few bits of the input. */
7201 input_data->x[0] ^= 1;
7202
Paul Elliott712d5122022-12-07 14:03:10 +00007203 /* Start verification. */
7204 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7205 input_data->x, input_data->len,
7206 signature, signature_length));
7207
7208 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007209 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007210 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007211 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007212
7213 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7214 }
7215
7216 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7217
7218exit:
7219 /*
7220 * Key attributes may have been returned by psa_get_key_attributes()
7221 * thus reset them as required.
7222 */
7223 psa_reset_key_attributes(&attributes);
7224
7225 psa_destroy_key(key);
7226 mbedtls_free(signature);
7227 PSA_DONE();
7228}
7229/* END_CASE */
7230
Gilles Peskine9911b022018-06-29 17:30:48 +02007231/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232void verify_hash(int key_type_arg, data_t *key_data,
7233 int alg_arg, data_t *hash_data,
7234 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007235{
Ronald Cron5425a212020-08-04 14:58:35 +02007236 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007237 psa_key_type_t key_type = key_type_arg;
7238 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007240
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007244
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 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);
itayzafrir5c753392018-05-08 11:18:38 +03007248
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7250 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007251
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 PSA_ASSERT(psa_verify_hash(key, alg,
7253 hash_data->x, hash_data->len,
7254 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007255
itayzafrir5c753392018-05-08 11:18:38 +03007256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 psa_reset_key_attributes(&attributes);
7258 psa_destroy_key(key);
7259 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007260}
7261/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007262
Paul Elliott712d5122022-12-07 14:03:10 +00007263/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007264/**
7265 * verify_hash_interruptible() test intentions:
7266 *
7267 * Note: This test can currently only handle ECDSA.
7268 *
7269 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007270 * only). Given this test only does verification it can accept public keys as
7271 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007272 *
7273 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7274 * expected for different max_ops values.
7275 *
7276 * 3. Test that the number of ops done prior to start and after abort is zero
7277 * and that each successful stage completes some ops (this is not mandated by
7278 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007279 *
7280 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7281 * complete() calls does not alter the number of ops returned.
7282 *
7283 * 5. Test that after corrupting the hash, the verification detects an invalid
7284 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007285 */
Paul Elliott712d5122022-12-07 14:03:10 +00007286void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7287 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007288 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007289{
7290 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7291 psa_key_type_t key_type = key_type_arg;
7292 psa_algorithm_t alg = alg_arg;
7293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7294 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007295 uint32_t num_ops = 0;
7296 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007297 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007298 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007299 size_t min_completes = 0;
7300 size_t max_completes = 0;
7301
Paul Elliott712d5122022-12-07 14:03:10 +00007302 psa_verify_hash_interruptible_operation_t operation =
7303 psa_verify_hash_interruptible_operation_init();
7304
7305 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7306
7307 PSA_ASSERT(psa_crypto_init());
7308
7309 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7310 psa_set_key_algorithm(&attributes, alg);
7311 psa_set_key_type(&attributes, key_type);
7312
7313 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7314 &key));
7315
Paul Elliott0c683352022-12-16 19:16:56 +00007316 psa_interruptible_set_max_ops(max_ops);
7317
Paul Elliott6f600372023-02-06 18:41:05 +00007318 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7319 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007320
Paul Elliott712d5122022-12-07 14:03:10 +00007321 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7322
7323 TEST_ASSERT(num_ops_prior == 0);
7324
7325 /* Start verification. */
7326 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7327 hash_data->x, hash_data->len,
7328 signature_data->x, signature_data->len)
7329 );
7330
7331 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7332
7333 TEST_ASSERT(num_ops_prior == 0);
7334
7335 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007336 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007337 status = psa_verify_hash_complete(&operation);
7338
Paul Elliott0c683352022-12-16 19:16:56 +00007339 num_completes++;
7340
Paul Elliott712d5122022-12-07 14:03:10 +00007341 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7342 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007343 /* We are asserting here that every complete makes progress
7344 * (completes some ops), which is true of the internal
7345 * implementation and probably any implementation, however this is
7346 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007347 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007348
Paul Elliott712d5122022-12-07 14:03:10 +00007349 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007350
7351 /* Ensure calling get_num_ops() twice still returns the same
7352 * number of ops as previously reported. */
7353 num_ops = psa_verify_hash_get_num_ops(&operation);
7354
7355 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007356 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007357 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007358
7359 TEST_ASSERT(status == PSA_SUCCESS);
7360
Paul Elliott0c683352022-12-16 19:16:56 +00007361 TEST_LE_U(min_completes, num_completes);
7362 TEST_LE_U(num_completes, max_completes);
7363
Paul Elliott712d5122022-12-07 14:03:10 +00007364 PSA_ASSERT(psa_verify_hash_abort(&operation));
7365
Paul Elliott59ad9452022-12-18 15:09:02 +00007366 num_ops = psa_verify_hash_get_num_ops(&operation);
7367 TEST_ASSERT(num_ops == 0);
7368
Paul Elliott8359c142023-02-24 18:40:10 +00007369 if (hash_data->len != 0) {
7370 /* Flip a bit in the hash and verify that the signature is now detected
7371 * as invalid. Flip a bit at the beginning, not at the end, because
7372 * ECDSA may ignore the last few bits of the input. */
7373 hash_data->x[0] ^= 1;
7374
7375 /* Start verification. */
7376 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7377 hash_data->x, hash_data->len,
7378 signature_data->x, signature_data->len));
7379
7380 /* Continue performing the signature until complete. */
7381 do {
7382 status = psa_verify_hash_complete(&operation);
7383 } while (status == PSA_OPERATION_INCOMPLETE);
7384
7385 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7386 }
7387
Paul Elliott712d5122022-12-07 14:03:10 +00007388exit:
7389 psa_reset_key_attributes(&attributes);
7390 psa_destroy_key(key);
7391 PSA_DONE();
7392}
7393/* END_CASE */
7394
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007395/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007396void verify_hash_fail(int key_type_arg, data_t *key_data,
7397 int alg_arg, data_t *hash_data,
7398 data_t *signature_data,
7399 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007400{
Ronald Cron5425a212020-08-04 14:58:35 +02007401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007402 psa_key_type_t key_type = key_type_arg;
7403 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007404 psa_status_t actual_status;
7405 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007407
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007409
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7411 psa_set_key_algorithm(&attributes, alg);
7412 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7415 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007416
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 actual_status = psa_verify_hash(key, alg,
7418 hash_data->x, hash_data->len,
7419 signature_data->x, signature_data->len);
7420 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007421
7422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 psa_reset_key_attributes(&attributes);
7424 psa_destroy_key(key);
7425 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007426}
7427/* END_CASE */
7428
Paul Elliott91007972022-12-16 12:21:24 +00007429/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007430/**
7431 * verify_hash_fail_interruptible() test intentions:
7432 *
7433 * Note: This test can currently only handle ECDSA.
7434 *
7435 * 1. Test that various failure cases for interruptible verify hash fail with
7436 * the correct error codes, and at the correct point (at start or during
7437 * complete).
7438 *
7439 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7440 * expected for different max_ops values.
7441 *
7442 * 3. Test that the number of ops done prior to start and after abort is zero
7443 * and that each successful stage completes some ops (this is not mandated by
7444 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007445 *
7446 * 4. Check that calling complete() when start() fails and complete()
7447 * after completion results in a BAD_STATE error.
7448 *
7449 * 5. Check that calling start() again after start fails results in a BAD_STATE
7450 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007451 */
Paul Elliott91007972022-12-16 12:21:24 +00007452void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7453 int alg_arg, data_t *hash_data,
7454 data_t *signature_data,
7455 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007456 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007457 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007458{
7459 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7460 psa_key_type_t key_type = key_type_arg;
7461 psa_algorithm_t alg = alg_arg;
7462 psa_status_t actual_status;
7463 psa_status_t expected_start_status = expected_start_status_arg;
7464 psa_status_t expected_complete_status = expected_complete_status_arg;
7465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007466 uint32_t num_ops = 0;
7467 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007468 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007469 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007470 size_t min_completes = 0;
7471 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007472 psa_verify_hash_interruptible_operation_t operation =
7473 psa_verify_hash_interruptible_operation_init();
7474
7475 PSA_ASSERT(psa_crypto_init());
7476
7477 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7478 psa_set_key_algorithm(&attributes, alg);
7479 psa_set_key_type(&attributes, key_type);
7480
7481 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7482 &key));
7483
Paul Elliott0c683352022-12-16 19:16:56 +00007484 psa_interruptible_set_max_ops(max_ops);
7485
Paul Elliott6f600372023-02-06 18:41:05 +00007486 interruptible_signverify_get_minmax_completes(max_ops,
7487 expected_complete_status,
7488 &min_completes,
7489 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007490
Paul Elliott91007972022-12-16 12:21:24 +00007491 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7492 TEST_ASSERT(num_ops_prior == 0);
7493
7494 /* Start verification. */
7495 actual_status = psa_verify_hash_start(&operation, key, alg,
7496 hash_data->x, hash_data->len,
7497 signature_data->x,
7498 signature_data->len);
7499
7500 TEST_EQUAL(actual_status, expected_start_status);
7501
Paul Elliottc9774412023-02-06 15:14:07 +00007502 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007503 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007504 * start failed. */
7505 actual_status = psa_verify_hash_complete(&operation);
7506
7507 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7508
7509 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007510 actual_status = psa_verify_hash_start(&operation, key, alg,
7511 hash_data->x, hash_data->len,
7512 signature_data->x,
7513 signature_data->len);
7514
7515 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7516 }
7517
Paul Elliott91007972022-12-16 12:21:24 +00007518 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7519 TEST_ASSERT(num_ops_prior == 0);
7520
Paul Elliott91007972022-12-16 12:21:24 +00007521 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007522 do {
Paul Elliott91007972022-12-16 12:21:24 +00007523 actual_status = psa_verify_hash_complete(&operation);
7524
Paul Elliott0c683352022-12-16 19:16:56 +00007525 num_completes++;
7526
Paul Elliott334d7262023-01-20 17:29:41 +00007527 if (actual_status == PSA_SUCCESS ||
7528 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007529 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007530 /* We are asserting here that every complete makes progress
7531 * (completes some ops), which is true of the internal
7532 * implementation and probably any implementation, however this is
7533 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007534 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007535
Paul Elliott91007972022-12-16 12:21:24 +00007536 num_ops_prior = num_ops;
7537 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007538 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007539
Paul Elliottc9774412023-02-06 15:14:07 +00007540 TEST_EQUAL(actual_status, expected_complete_status);
7541
Paul Elliottefebad02023-02-15 16:56:45 +00007542 /* Check that another complete returns BAD_STATE. */
7543 actual_status = psa_verify_hash_complete(&operation);
7544 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007545
Paul Elliott0c683352022-12-16 19:16:56 +00007546 TEST_LE_U(min_completes, num_completes);
7547 TEST_LE_U(num_completes, max_completes);
7548
Paul Elliott91007972022-12-16 12:21:24 +00007549 PSA_ASSERT(psa_verify_hash_abort(&operation));
7550
Paul Elliott59ad9452022-12-18 15:09:02 +00007551 num_ops = psa_verify_hash_get_num_ops(&operation);
7552 TEST_ASSERT(num_ops == 0);
7553
Paul Elliott91007972022-12-16 12:21:24 +00007554exit:
7555 psa_reset_key_attributes(&attributes);
7556 psa_destroy_key(key);
7557 PSA_DONE();
7558}
7559/* END_CASE */
7560
Paul Elliott20a36062022-12-18 13:21:25 +00007561/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007562/**
7563 * interruptible_signverify_hash_state_test() test intentions:
7564 *
7565 * Note: This test can currently only handle ECDSA.
7566 *
7567 * 1. Test that calling the various interruptible sign and verify hash functions
7568 * in incorrect orders returns BAD_STATE errors.
7569 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007570void interruptible_signverify_hash_state_test(int key_type_arg,
7571 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007572{
7573 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7574 psa_key_type_t key_type = key_type_arg;
7575 psa_algorithm_t alg = alg_arg;
7576 size_t key_bits;
7577 unsigned char *signature = NULL;
7578 size_t signature_size;
7579 size_t signature_length = 0xdeadbeef;
7580 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7581 psa_sign_hash_interruptible_operation_t sign_operation =
7582 psa_sign_hash_interruptible_operation_init();
7583 psa_verify_hash_interruptible_operation_t verify_operation =
7584 psa_verify_hash_interruptible_operation_init();
7585
7586 PSA_ASSERT(psa_crypto_init());
7587
7588 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7589 PSA_KEY_USAGE_VERIFY_HASH);
7590 psa_set_key_algorithm(&attributes, alg);
7591 psa_set_key_type(&attributes, key_type);
7592
7593 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7594 &key));
7595 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7596 key_bits = psa_get_key_bits(&attributes);
7597
7598 /* Allocate a buffer which has the size advertised by the
7599 * library. */
7600 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7601 key_bits, alg);
7602 TEST_ASSERT(signature_size != 0);
7603 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007604 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007605
7606 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7607
7608 /* --- Attempt completes prior to starts --- */
7609 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7610 signature_size,
7611 &signature_length),
7612 PSA_ERROR_BAD_STATE);
7613
7614 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7615
7616 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7617 PSA_ERROR_BAD_STATE);
7618
7619 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7620
7621 /* --- Aborts in all other places. --- */
7622 psa_sign_hash_abort(&sign_operation);
7623
7624 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7625 input_data->x, input_data->len));
7626
7627 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7628
7629 psa_interruptible_set_max_ops(1);
7630
7631 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7632 input_data->x, input_data->len));
7633
7634 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7635 signature_size,
7636 &signature_length),
7637 PSA_OPERATION_INCOMPLETE);
7638
7639 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7640
7641 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7642
7643 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7644 input_data->x, input_data->len));
7645
7646 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7647 signature_size,
7648 &signature_length));
7649
7650 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7651
7652 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7653
7654 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7655 input_data->x, input_data->len,
7656 signature, signature_length));
7657
7658 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7659
7660 psa_interruptible_set_max_ops(1);
7661
7662 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7663 input_data->x, input_data->len,
7664 signature, signature_length));
7665
7666 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7667 PSA_OPERATION_INCOMPLETE);
7668
7669 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7670
7671 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7672
7673 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7674 input_data->x, input_data->len,
7675 signature, signature_length));
7676
7677 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7678
7679 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7680
7681 /* --- Attempt double starts. --- */
7682
7683 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7684 input_data->x, input_data->len));
7685
7686 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7687 input_data->x, input_data->len),
7688 PSA_ERROR_BAD_STATE);
7689
7690 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7691
7692 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7693 input_data->x, input_data->len,
7694 signature, signature_length));
7695
7696 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7697 input_data->x, input_data->len,
7698 signature, signature_length),
7699 PSA_ERROR_BAD_STATE);
7700
7701 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7702
Paul Elliott76d671a2023-02-07 17:45:18 +00007703exit:
7704 /*
7705 * Key attributes may have been returned by psa_get_key_attributes()
7706 * thus reset them as required.
7707 */
7708 psa_reset_key_attributes(&attributes);
7709
7710 psa_destroy_key(key);
7711 mbedtls_free(signature);
7712 PSA_DONE();
7713}
7714/* END_CASE */
7715
7716/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007717/**
Paul Elliottc2033502023-02-26 17:09:14 +00007718 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007719 *
7720 * Note: This test can currently only handle ECDSA.
7721 *
7722 * 1. Test various edge cases in the interruptible sign and verify hash
7723 * interfaces.
7724 */
Paul Elliottc2033502023-02-26 17:09:14 +00007725void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007726 data_t *key_data, int alg_arg, data_t *input_data)
7727{
7728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7729 psa_key_type_t key_type = key_type_arg;
7730 psa_algorithm_t alg = alg_arg;
7731 size_t key_bits;
7732 unsigned char *signature = NULL;
7733 size_t signature_size;
7734 size_t signature_length = 0xdeadbeef;
7735 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7736 uint8_t *input_buffer = NULL;
7737 psa_sign_hash_interruptible_operation_t sign_operation =
7738 psa_sign_hash_interruptible_operation_init();
7739 psa_verify_hash_interruptible_operation_t verify_operation =
7740 psa_verify_hash_interruptible_operation_init();
7741
7742 PSA_ASSERT(psa_crypto_init());
7743
7744 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7745 PSA_KEY_USAGE_VERIFY_HASH);
7746 psa_set_key_algorithm(&attributes, alg);
7747 psa_set_key_type(&attributes, key_type);
7748
7749 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7750 &key));
7751 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7752 key_bits = psa_get_key_bits(&attributes);
7753
7754 /* Allocate a buffer which has the size advertised by the
7755 * library. */
7756 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7757 key_bits, alg);
7758 TEST_ASSERT(signature_size != 0);
7759 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007760 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007761
Paul Elliott20a36062022-12-18 13:21:25 +00007762 /* --- Change function inputs mid run, to cause an error (sign only,
7763 * verify passes all inputs to start. --- */
7764
7765 psa_interruptible_set_max_ops(1);
7766
7767 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7768 input_data->x, input_data->len));
7769
7770 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7771 signature_size,
7772 &signature_length),
7773 PSA_OPERATION_INCOMPLETE);
7774
7775 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7776 0,
7777 &signature_length),
7778 PSA_ERROR_BUFFER_TOO_SMALL);
7779
Paul Elliottc9774412023-02-06 15:14:07 +00007780 /* And test that this invalidates the operation. */
7781 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7782 0,
7783 &signature_length),
7784 PSA_ERROR_BAD_STATE);
7785
Paul Elliott20a36062022-12-18 13:21:25 +00007786 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7787
Paul Elliottf9c91a72023-02-05 18:06:38 +00007788 /* Trash the hash buffer in between start and complete, to ensure
7789 * no reliance on external buffers. */
7790 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7791
Paul Elliott6c68df42023-10-23 15:33:37 +01007792 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007793
7794 memcpy(input_buffer, input_data->x, input_data->len);
7795
7796 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7797 input_buffer, input_data->len));
7798
7799 memset(input_buffer, '!', input_data->len);
7800 mbedtls_free(input_buffer);
7801 input_buffer = NULL;
7802
7803 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7804 signature_size,
7805 &signature_length));
7806
7807 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7808
Paul Elliott6c68df42023-10-23 15:33:37 +01007809 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007810
7811 memcpy(input_buffer, input_data->x, input_data->len);
7812
7813 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7814 input_buffer, input_data->len,
7815 signature, signature_length));
7816
7817 memset(input_buffer, '!', input_data->len);
7818 mbedtls_free(input_buffer);
7819 input_buffer = NULL;
7820
7821 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7822
7823 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7824
Paul Elliott20a36062022-12-18 13:21:25 +00007825exit:
7826 /*
7827 * Key attributes may have been returned by psa_get_key_attributes()
7828 * thus reset them as required.
7829 */
7830 psa_reset_key_attributes(&attributes);
7831
7832 psa_destroy_key(key);
7833 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007834 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007835 PSA_DONE();
7836}
7837/* END_CASE */
7838
Paul Elliotta4cb9092023-02-07 18:01:55 +00007839/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007840/**
Paul Elliott57702242023-02-26 20:36:10 +00007841 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007842 *
7843 * Note: This test can currently only handle ECDSA.
7844 *
7845 * 1. Test that setting max ops is reflected in both interruptible sign and
7846 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007847 * 2. Test that changing the value of max_ops to unlimited during an operation
7848 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007849 *
7850 * 3. Test that calling get_num_ops() between complete calls gives the same
7851 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007852 */
Paul Elliott57702242023-02-26 20:36:10 +00007853void interruptible_signverify_hash_ops_tests(int key_type_arg,
7854 data_t *key_data, int alg_arg,
7855 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007856{
7857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7858 psa_key_type_t key_type = key_type_arg;
7859 psa_algorithm_t alg = alg_arg;
7860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007861 size_t key_bits;
7862 unsigned char *signature = NULL;
7863 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007864 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007865 uint32_t num_ops = 0;
7866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7867
Paul Elliotta4cb9092023-02-07 18:01:55 +00007868 psa_sign_hash_interruptible_operation_t sign_operation =
7869 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007870 psa_verify_hash_interruptible_operation_t verify_operation =
7871 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007872
7873 PSA_ASSERT(psa_crypto_init());
7874
7875 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7876 PSA_KEY_USAGE_VERIFY_HASH);
7877 psa_set_key_algorithm(&attributes, alg);
7878 psa_set_key_type(&attributes, key_type);
7879
Paul Elliottf1743e22023-02-15 18:44:16 +00007880 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7881 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7882 key_bits = psa_get_key_bits(&attributes);
7883
7884 /* Allocate a buffer which has the size advertised by the
7885 * library. */
7886 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7887
7888 TEST_ASSERT(signature_size != 0);
7889 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007890 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007891
7892 /* Check that default max ops gets set if we don't set it. */
7893 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7894 input_data->x, input_data->len));
7895
7896 TEST_EQUAL(psa_interruptible_get_max_ops(),
7897 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7898
7899 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7900
Paul Elliottf1743e22023-02-15 18:44:16 +00007901 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7902 input_data->x, input_data->len,
7903 signature, signature_size));
7904
7905 TEST_EQUAL(psa_interruptible_get_max_ops(),
7906 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7907
7908 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7909
Paul Elliotta4cb9092023-02-07 18:01:55 +00007910 /* Check that max ops gets set properly. */
7911
7912 psa_interruptible_set_max_ops(0xbeef);
7913
Paul Elliottf1743e22023-02-15 18:44:16 +00007914 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007915
Paul Elliott9e8819f2023-02-26 19:01:35 +00007916 /* --- Ensure changing the max ops mid operation works (operation should
7917 * complete successfully after setting max ops to unlimited --- */
7918 psa_interruptible_set_max_ops(1);
7919
7920 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7921 input_data->x, input_data->len));
7922
7923 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7924 signature_size,
7925 &signature_length),
7926 PSA_OPERATION_INCOMPLETE);
7927
7928 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7929
7930 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7931 signature_size,
7932 &signature_length));
7933
7934 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7935
7936 psa_interruptible_set_max_ops(1);
7937
7938 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7939 input_data->x, input_data->len,
7940 signature, signature_length));
7941
7942 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7943 PSA_OPERATION_INCOMPLETE);
7944
7945 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7946
7947 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7948
7949 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7950
Paul Elliottc1e04002023-02-26 20:27:23 +00007951 /* --- Test that not calling get_num_ops inbetween complete calls does not
7952 * result in lost ops. ---*/
7953
7954 psa_interruptible_set_max_ops(1);
7955
7956 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7957 input_data->x, input_data->len));
7958
7959 /* Continue performing the signature until complete. */
7960 do {
7961 status = psa_sign_hash_complete(&sign_operation, signature,
7962 signature_size,
7963 &signature_length);
7964
7965 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7966
7967 } while (status == PSA_OPERATION_INCOMPLETE);
7968
7969 PSA_ASSERT(status);
7970
7971 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7972
7973 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7974 input_data->x, input_data->len));
7975
7976 /* Continue performing the signature until complete. */
7977 do {
7978 status = psa_sign_hash_complete(&sign_operation, signature,
7979 signature_size,
7980 &signature_length);
7981 } while (status == PSA_OPERATION_INCOMPLETE);
7982
7983 PSA_ASSERT(status);
7984
7985 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7986
7987 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7988
7989 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7990 input_data->x, input_data->len,
7991 signature, signature_length));
7992
7993 /* Continue performing the verification until complete. */
7994 do {
7995 status = psa_verify_hash_complete(&verify_operation);
7996
7997 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7998
7999 } while (status == PSA_OPERATION_INCOMPLETE);
8000
8001 PSA_ASSERT(status);
8002
8003 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8004
8005 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8006 input_data->x, input_data->len,
8007 signature, signature_length));
8008
8009 /* Continue performing the verification until complete. */
8010 do {
8011 status = psa_verify_hash_complete(&verify_operation);
8012
8013 } while (status == PSA_OPERATION_INCOMPLETE);
8014
8015 PSA_ASSERT(status);
8016
8017 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
8018
8019 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8020
Paul Elliotta4cb9092023-02-07 18:01:55 +00008021exit:
8022 /*
8023 * Key attributes may have been returned by psa_get_key_attributes()
8024 * thus reset them as required.
8025 */
8026 psa_reset_key_attributes(&attributes);
8027
8028 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00008029 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008030 PSA_DONE();
8031}
8032/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00008033
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008034/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008035void sign_message_deterministic(int key_type_arg,
8036 data_t *key_data,
8037 int alg_arg,
8038 data_t *input_data,
8039 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02008040{
8041 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8042 psa_key_type_t key_type = key_type_arg;
8043 psa_algorithm_t alg = alg_arg;
8044 size_t key_bits;
8045 unsigned char *signature = NULL;
8046 size_t signature_size;
8047 size_t signature_length = 0xdeadbeef;
8048 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8049
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02008051
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8053 psa_set_key_algorithm(&attributes, alg);
8054 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008055
Gilles Peskine449bd832023-01-11 14:50:10 +01008056 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8057 &key));
8058 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8059 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008060
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8062 TEST_ASSERT(signature_size != 0);
8063 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008064 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008065
Gilles Peskine449bd832023-01-11 14:50:10 +01008066 PSA_ASSERT(psa_sign_message(key, alg,
8067 input_data->x, input_data->len,
8068 signature, signature_size,
8069 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02008070
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008071 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008072 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008073
8074exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008075 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008076
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 psa_destroy_key(key);
8078 mbedtls_free(signature);
8079 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02008080
8081}
8082/* END_CASE */
8083
8084/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008085void sign_message_fail(int key_type_arg,
8086 data_t *key_data,
8087 int alg_arg,
8088 data_t *input_data,
8089 int signature_size_arg,
8090 int expected_status_arg)
8091{
8092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8093 psa_key_type_t key_type = key_type_arg;
8094 psa_algorithm_t alg = alg_arg;
8095 size_t signature_size = signature_size_arg;
8096 psa_status_t actual_status;
8097 psa_status_t expected_status = expected_status_arg;
8098 unsigned char *signature = NULL;
8099 size_t signature_length = 0xdeadbeef;
8100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8101
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008102 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008103
8104 PSA_ASSERT(psa_crypto_init());
8105
8106 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8107 psa_set_key_algorithm(&attributes, alg);
8108 psa_set_key_type(&attributes, key_type);
8109
8110 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8111 &key));
8112
8113 actual_status = psa_sign_message(key, alg,
8114 input_data->x, input_data->len,
8115 signature, signature_size,
8116 &signature_length);
8117 TEST_EQUAL(actual_status, expected_status);
8118 /* The value of *signature_length is unspecified on error, but
8119 * whatever it is, it should be less than signature_size, so that
8120 * if the caller tries to read *signature_length bytes without
8121 * checking the error code then they don't overflow a buffer. */
8122 TEST_LE_U(signature_length, signature_size);
8123
8124exit:
8125 psa_reset_key_attributes(&attributes);
8126 psa_destroy_key(key);
8127 mbedtls_free(signature);
8128 PSA_DONE();
8129}
8130/* END_CASE */
8131
8132/* BEGIN_CASE */
8133void sign_verify_message(int key_type_arg,
8134 data_t *key_data,
8135 int alg_arg,
8136 data_t *input_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 size_t key_bits;
8142 unsigned char *signature = NULL;
8143 size_t signature_size;
8144 size_t signature_length = 0xdeadbeef;
8145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8146
8147 PSA_ASSERT(psa_crypto_init());
8148
8149 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8150 PSA_KEY_USAGE_VERIFY_MESSAGE);
8151 psa_set_key_algorithm(&attributes, alg);
8152 psa_set_key_type(&attributes, key_type);
8153
8154 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8155 &key));
8156 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8157 key_bits = psa_get_key_bits(&attributes);
8158
8159 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8160 TEST_ASSERT(signature_size != 0);
8161 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008162 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008163
8164 PSA_ASSERT(psa_sign_message(key, alg,
8165 input_data->x, input_data->len,
8166 signature, signature_size,
8167 &signature_length));
8168 TEST_LE_U(signature_length, signature_size);
8169 TEST_ASSERT(signature_length > 0);
8170
8171 PSA_ASSERT(psa_verify_message(key, alg,
8172 input_data->x, input_data->len,
8173 signature, signature_length));
8174
8175 if (input_data->len != 0) {
8176 /* Flip a bit in the input and verify that the signature is now
8177 * detected as invalid. Flip a bit at the beginning, not at the end,
8178 * because ECDSA may ignore the last few bits of the input. */
8179 input_data->x[0] ^= 1;
8180 TEST_EQUAL(psa_verify_message(key, alg,
8181 input_data->x, input_data->len,
8182 signature, signature_length),
8183 PSA_ERROR_INVALID_SIGNATURE);
8184 }
8185
8186exit:
8187 psa_reset_key_attributes(&attributes);
8188
8189 psa_destroy_key(key);
8190 mbedtls_free(signature);
8191 PSA_DONE();
8192}
8193/* END_CASE */
8194
8195/* BEGIN_CASE */
8196void verify_message(int key_type_arg,
8197 data_t *key_data,
8198 int alg_arg,
8199 data_t *input_data,
8200 data_t *signature_data)
8201{
8202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8203 psa_key_type_t key_type = key_type_arg;
8204 psa_algorithm_t alg = alg_arg;
8205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8206
8207 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8208
8209 PSA_ASSERT(psa_crypto_init());
8210
8211 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8212 psa_set_key_algorithm(&attributes, alg);
8213 psa_set_key_type(&attributes, key_type);
8214
8215 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8216 &key));
8217
8218 PSA_ASSERT(psa_verify_message(key, alg,
8219 input_data->x, input_data->len,
8220 signature_data->x, signature_data->len));
8221
8222exit:
8223 psa_reset_key_attributes(&attributes);
8224 psa_destroy_key(key);
8225 PSA_DONE();
8226}
8227/* END_CASE */
8228
8229/* BEGIN_CASE */
8230void verify_message_fail(int key_type_arg,
8231 data_t *key_data,
8232 int alg_arg,
8233 data_t *hash_data,
8234 data_t *signature_data,
8235 int expected_status_arg)
8236{
8237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8238 psa_key_type_t key_type = key_type_arg;
8239 psa_algorithm_t alg = alg_arg;
8240 psa_status_t actual_status;
8241 psa_status_t expected_status = expected_status_arg;
8242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8243
8244 PSA_ASSERT(psa_crypto_init());
8245
8246 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8247 psa_set_key_algorithm(&attributes, alg);
8248 psa_set_key_type(&attributes, key_type);
8249
8250 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8251 &key));
8252
8253 actual_status = psa_verify_message(key, alg,
8254 hash_data->x, hash_data->len,
8255 signature_data->x,
8256 signature_data->len);
8257 TEST_EQUAL(actual_status, expected_status);
8258
8259exit:
8260 psa_reset_key_attributes(&attributes);
8261 psa_destroy_key(key);
8262 PSA_DONE();
8263}
8264/* END_CASE */
8265
8266/* BEGIN_CASE */
8267void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008268 data_t *key_data,
8269 int alg_arg,
8270 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 data_t *label,
8272 int expected_output_length_arg,
8273 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008274{
Ronald Cron5425a212020-08-04 14:58:35 +02008275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008276 psa_key_type_t key_type = key_type_arg;
8277 psa_algorithm_t alg = alg_arg;
8278 size_t expected_output_length = expected_output_length_arg;
8279 size_t key_bits;
8280 unsigned char *output = NULL;
8281 size_t output_size;
8282 size_t output_length = ~0;
8283 psa_status_t actual_status;
8284 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008286
Gilles Peskine449bd832023-01-11 14:50:10 +01008287 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008288
Gilles Peskine656896e2018-06-29 19:12:28 +02008289 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8291 psa_set_key_algorithm(&attributes, alg);
8292 psa_set_key_type(&attributes, key_type);
8293 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8294 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008295
8296 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8298 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008299
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8301 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008302 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008303
8304 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 actual_status = psa_asymmetric_encrypt(key, alg,
8306 input_data->x, input_data->len,
8307 label->x, label->len,
8308 output, output_size,
8309 &output_length);
8310 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008311 if (actual_status == PSA_SUCCESS) {
8312 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008313 } else {
8314 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008315 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008316
Gilles Peskine68428122018-06-30 18:42:41 +02008317 /* If the label is empty, the test framework puts a non-null pointer
8318 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008320 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 if (output_size != 0) {
8322 memset(output, 0, output_size);
8323 }
8324 actual_status = psa_asymmetric_encrypt(key, alg,
8325 input_data->x, input_data->len,
8326 NULL, label->len,
8327 output, output_size,
8328 &output_length);
8329 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008330 if (actual_status == PSA_SUCCESS) {
8331 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008332 } else {
8333 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008334 }
Gilles Peskine68428122018-06-30 18:42:41 +02008335 }
8336
Gilles Peskine656896e2018-06-29 19:12:28 +02008337exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008338 /*
8339 * Key attributes may have been returned by psa_get_key_attributes()
8340 * thus reset them as required.
8341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008343
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 psa_destroy_key(key);
8345 mbedtls_free(output);
8346 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008347}
8348/* END_CASE */
8349
8350/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008351void asymmetric_encrypt_decrypt(int key_type_arg,
8352 data_t *key_data,
8353 int alg_arg,
8354 data_t *input_data,
8355 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356{
Ronald Cron5425a212020-08-04 14:58:35 +02008357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008358 psa_key_type_t key_type = key_type_arg;
8359 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008360 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008361 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008362 size_t output_size;
8363 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008364 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008365 size_t output2_size;
8366 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008368
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008370
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8372 psa_set_key_algorithm(&attributes, alg);
8373 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8376 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008377
8378 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8380 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008381
Gilles Peskine449bd832023-01-11 14:50:10 +01008382 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8383 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008384 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008385
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008386 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 TEST_LE_U(output2_size,
8388 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8389 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008390 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008391
Gilles Peskineeebd7382018-06-08 18:11:54 +02008392 /* We test encryption by checking that encrypt-then-decrypt gives back
8393 * the original plaintext because of the non-optional random
8394 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8396 input_data->x, input_data->len,
8397 label->x, label->len,
8398 output, output_size,
8399 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008400 /* We don't know what ciphertext length to expect, but check that
8401 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008403
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8405 output, output_length,
8406 label->x, label->len,
8407 output2, output2_size,
8408 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008409 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008410 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008411
8412exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008413 /*
8414 * Key attributes may have been returned by psa_get_key_attributes()
8415 * thus reset them as required.
8416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008418
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 psa_destroy_key(key);
8420 mbedtls_free(output);
8421 mbedtls_free(output2);
8422 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008423}
8424/* END_CASE */
8425
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008427void asymmetric_decrypt(int key_type_arg,
8428 data_t *key_data,
8429 int alg_arg,
8430 data_t *input_data,
8431 data_t *label,
8432 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008433{
Ronald Cron5425a212020-08-04 14:58:35 +02008434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008435 psa_key_type_t key_type = key_type_arg;
8436 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008437 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008438 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008439 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008440 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008442
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008444
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8446 psa_set_key_algorithm(&attributes, alg);
8447 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008448
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8450 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8453 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008454
8455 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8457 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008458 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8461 input_data->x, input_data->len,
8462 label->x, label->len,
8463 output,
8464 output_size,
8465 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008466 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008467 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008468
Gilles Peskine68428122018-06-30 18:42:41 +02008469 /* If the label is empty, the test framework puts a non-null pointer
8470 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008472 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 if (output_size != 0) {
8474 memset(output, 0, output_size);
8475 }
8476 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8477 input_data->x, input_data->len,
8478 NULL, label->len,
8479 output,
8480 output_size,
8481 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008482 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008483 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008484 }
8485
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008486exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 psa_reset_key_attributes(&attributes);
8488 psa_destroy_key(key);
8489 mbedtls_free(output);
8490 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008491}
8492/* END_CASE */
8493
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008494/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008495void asymmetric_decrypt_fail(int key_type_arg,
8496 data_t *key_data,
8497 int alg_arg,
8498 data_t *input_data,
8499 data_t *label,
8500 int output_size_arg,
8501 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008502{
Ronald Cron5425a212020-08-04 14:58:35 +02008503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008504 psa_key_type_t key_type = key_type_arg;
8505 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008506 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008507 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008508 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008509 psa_status_t actual_status;
8510 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008512
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008513 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008514
Gilles Peskine449bd832023-01-11 14:50:10 +01008515 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008516
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8518 psa_set_key_algorithm(&attributes, alg);
8519 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008520
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8522 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008523
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 actual_status = psa_asymmetric_decrypt(key, alg,
8525 input_data->x, input_data->len,
8526 label->x, label->len,
8527 output, output_size,
8528 &output_length);
8529 TEST_EQUAL(actual_status, expected_status);
8530 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008531
Gilles Peskine68428122018-06-30 18:42:41 +02008532 /* If the label is empty, the test framework puts a non-null pointer
8533 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008535 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 if (output_size != 0) {
8537 memset(output, 0, output_size);
8538 }
8539 actual_status = psa_asymmetric_decrypt(key, alg,
8540 input_data->x, input_data->len,
8541 NULL, label->len,
8542 output, output_size,
8543 &output_length);
8544 TEST_EQUAL(actual_status, expected_status);
8545 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008546 }
8547
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008548exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 psa_reset_key_attributes(&attributes);
8550 psa_destroy_key(key);
8551 mbedtls_free(output);
8552 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008553}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008554/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008555
8556/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008557void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008558{
8559 /* Test each valid way of initializing the object, except for `= {0}`, as
8560 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8561 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008562 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008563 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008565 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8566 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008569
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008570 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8572 PSA_ERROR_BAD_STATE);
8573 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8574 PSA_ERROR_BAD_STATE);
8575 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8576 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008577
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008578 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 PSA_ASSERT(psa_key_derivation_abort(&func));
8580 PSA_ASSERT(psa_key_derivation_abort(&init));
8581 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008582}
8583/* END_CASE */
8584
Janos Follath16de4a42019-06-13 16:32:24 +01008585/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008586void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008587{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008588 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008589 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008590 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008591
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008593
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8595 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008596
8597exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 psa_key_derivation_abort(&operation);
8599 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008600}
8601/* END_CASE */
8602
Janos Follathaf3c2a02019-06-12 12:34:34 +01008603/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308604void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008606{
8607 psa_algorithm_t alg = alg_arg;
8608 size_t capacity = capacity_arg;
8609 psa_status_t expected_status = expected_status_arg;
8610 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8611
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008613
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008615
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8617 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008618
8619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 psa_key_derivation_abort(&operation);
8621 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008622}
8623/* END_CASE */
8624
8625/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308626void parse_binary_string_test(data_t *input, int output)
8627{
8628 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308629 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308630 TEST_EQUAL(value, output);
8631}
8632/* END_CASE */
8633
8634/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008635void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308636 int step_arg1, int key_type_arg1, data_t *input1,
8637 int expected_status_arg1,
8638 int step_arg2, int key_type_arg2, data_t *input2,
8639 int expected_status_arg2,
8640 int step_arg3, int key_type_arg3, data_t *input3,
8641 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008643{
8644 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308646 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 psa_status_t expected_statuses[] = { expected_status_arg1,
8648 expected_status_arg2,
8649 expected_status_arg3 };
8650 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008651 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8652 MBEDTLS_SVC_KEY_ID_INIT,
8653 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008654 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8656 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008657 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008658 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008659 psa_status_t expected_output_status = expected_output_status_arg;
8660 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008661
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008663
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8665 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008666
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8670 mbedtls_test_set_step(i);
8671 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008672 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308673 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8674 key_types[i] != INPUT_INTEGER) {
8675 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 PSA_ASSERT(psa_import_key(&attributes,
8677 inputs[i]->x, inputs[i]->len,
8678 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308679 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008681 // When taking a private key as secret input, use key agreement
8682 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008683 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8684 &operation, keys[i]),
8685 expected_statuses[i]);
8686 } else {
8687 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8688 keys[i]),
8689 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008690 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308692 if (key_types[i] == INPUT_INTEGER) {
8693 TEST_EQUAL(psa_key_derivation_input_integer(
8694 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308695 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308696 expected_statuses[i]);
8697 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308698 TEST_EQUAL(psa_key_derivation_input_bytes(
8699 &operation, steps[i],
8700 inputs[i]->x, inputs[i]->len),
8701 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308702 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008703 }
8704 }
8705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 if (output_key_type != PSA_KEY_TYPE_NONE) {
8707 psa_reset_key_attributes(&attributes);
8708 psa_set_key_type(&attributes, output_key_type);
8709 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008710 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 psa_key_derivation_output_key(&attributes, &operation,
8712 &output_key);
8713 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008714 uint8_t buffer[1];
8715 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 psa_key_derivation_output_bytes(&operation,
8717 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008718 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008720
Janos Follathaf3c2a02019-06-12 12:34:34 +01008721exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 psa_key_derivation_abort(&operation);
8723 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8724 psa_destroy_key(keys[i]);
8725 }
8726 psa_destroy_key(output_key);
8727 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008728}
8729/* END_CASE */
8730
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308731/* BEGIN_CASE*/
8732void derive_input_invalid_cost(int alg_arg, int64_t cost)
8733{
8734 psa_algorithm_t alg = alg_arg;
8735 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8736
8737 PSA_ASSERT(psa_crypto_init());
8738 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8739
8740 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8741 PSA_KEY_DERIVATION_INPUT_COST,
8742 cost),
8743 PSA_ERROR_NOT_SUPPORTED);
8744
8745exit:
8746 psa_key_derivation_abort(&operation);
8747 PSA_DONE();
8748}
8749/* END_CASE*/
8750
Janos Follathd958bb72019-07-03 15:02:16 +01008751/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008752void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008753{
Janos Follathd958bb72019-07-03 15:02:16 +01008754 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008756 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008757 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008758 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008760 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008762 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008764 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8765 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008768
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008770
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8772 psa_set_key_algorithm(&attributes, alg);
8773 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008774
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 PSA_ASSERT(psa_import_key(&attributes,
8776 key_data, sizeof(key_data),
8777 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008778
8779 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8781 input1, input1_length,
8782 input2, input2_length,
Ryan Everettc1cc6682024-03-12 16:17:43 +00008783 capacity, 0)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008784 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008786
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008787 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8789 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008790
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008792
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8794 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008795
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 psa_key_derivation_abort(&operation);
8798 psa_destroy_key(key);
8799 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008800}
8801/* END_CASE */
8802
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008803/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008804void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008805{
8806 uint8_t output_buffer[16];
8807 size_t buffer_size = 16;
8808 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008809 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008810
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8812 output_buffer, buffer_size)
8813 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8816 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008817
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008819
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8821 output_buffer, buffer_size)
8822 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008823
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8825 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008826
8827exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008829}
8830/* END_CASE */
8831
8832/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008833void derive_output(int alg_arg,
8834 int step1_arg, data_t *input1, int expected_status_arg1,
8835 int step2_arg, data_t *input2, int expected_status_arg2,
8836 int step3_arg, data_t *input3, int expected_status_arg3,
8837 int step4_arg, data_t *input4, int expected_status_arg4,
8838 data_t *key_agreement_peer_key,
8839 int requested_capacity_arg,
8840 data_t *expected_output1,
8841 data_t *expected_output2,
8842 int other_key_input_type,
8843 int key_input_type,
8844 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008845{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008846 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8848 data_t *inputs[] = { input1, input2, input3, input4 };
8849 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8850 MBEDTLS_SVC_KEY_ID_INIT,
8851 MBEDTLS_SVC_KEY_ID_INIT,
8852 MBEDTLS_SVC_KEY_ID_INIT };
8853 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8854 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008855 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008856 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008857 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008858 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008859 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008861 size_t output_buffer_size = 0;
8862 uint8_t *output_buffer = NULL;
8863 size_t expected_capacity;
8864 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008865 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8866 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8867 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8868 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008869 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008870 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008871 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8874 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008875 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 }
8877 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008878 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008880 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008881 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008883
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008884 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8886 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8887 requested_capacity));
8888 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8889 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008890 case 0:
8891 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308892 case PSA_KEY_DERIVATION_INPUT_COST:
8893 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308894 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308895 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308896 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308897 if (statuses[i] != PSA_SUCCESS) {
8898 goto exit;
8899 }
8900 break;
8901 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008902 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008904 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 TEST_EQUAL(psa_key_derivation_input_bytes(
8906 &operation, steps[i],
8907 inputs[i]->x, inputs[i]->len),
8908 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008909
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008911 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008913 break;
8914 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8916 psa_set_key_algorithm(&attributes1, alg);
8917 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008918
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 PSA_ASSERT(psa_import_key(&attributes1,
8920 inputs[i]->x, inputs[i]->len,
8921 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008922
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8924 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8925 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8926 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008927 }
8928
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308929 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308931 keys[i]),
8932 statuses[i]);
8933
8934 if (statuses[i] != PSA_SUCCESS) {
8935 goto exit;
8936 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008937 break;
8938 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008939 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008940 break;
8941 }
8942 break;
8943 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008945 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8947 steps[i],
8948 inputs[i]->x,
8949 inputs[i]->len),
8950 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008951 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008952 case 1: // input key, type DERIVE
8953 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8955 psa_set_key_algorithm(&attributes2, alg);
8956 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008957
8958 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 if (other_key_input_type == 11) {
8960 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8961 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 PSA_ASSERT(psa_import_key(&attributes2,
8964 inputs[i]->x, inputs[i]->len,
8965 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008966
Gilles Peskine449bd832023-01-11 14:50:10 +01008967 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8968 steps[i],
8969 keys[i]),
8970 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008971 break;
8972 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8974 psa_set_key_algorithm(&attributes3, alg);
8975 psa_set_key_type(&attributes3,
8976 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 PSA_ASSERT(psa_import_key(&attributes3,
8979 inputs[i]->x, inputs[i]->len,
8980 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008981
Gilles Peskine449bd832023-01-11 14:50:10 +01008982 TEST_EQUAL(psa_key_derivation_key_agreement(
8983 &operation,
8984 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8985 keys[i], key_agreement_peer_key->x,
8986 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008987 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008988 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008989 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008990 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008991 }
8992
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008994 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008996 break;
8997 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 TEST_EQUAL(psa_key_derivation_input_bytes(
8999 &operation, steps[i],
9000 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02009001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02009003 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009005 break;
9006 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01009007 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009008
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9010 &current_capacity));
9011 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009012 expected_capacity = requested_capacity;
9013
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009015 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
9016
9017 /* For output key derivation secret must be provided using
9018 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009020 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
9024 psa_set_key_algorithm(&attributes4, alg);
9025 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
9026 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009027
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
9029 &derived_key), expected_status);
9030 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009031 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009033 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 status = psa_key_derivation_output_bytes(&operation,
9035 output_buffer, output_sizes[i]);
9036 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009037 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 TEST_ASSERT(status == PSA_SUCCESS ||
9039 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009040 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 } else if (expected_capacity == 0 ||
9042 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009043 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009045 expected_capacity = 0;
9046 continue;
9047 }
9048 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 PSA_ASSERT(status);
9050 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009051 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009052 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009054 /* Check the operation status. */
9055 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9057 &current_capacity));
9058 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009059 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009060 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009062
9063exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 mbedtls_free(output_buffer);
9065 psa_key_derivation_abort(&operation);
9066 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
9067 psa_destroy_key(keys[i]);
9068 }
9069 psa_destroy_key(derived_key);
9070 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009071}
9072/* END_CASE */
9073
9074/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009075void derive_full(int alg_arg,
9076 data_t *key_data,
9077 data_t *input1,
9078 data_t *input2,
9079 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02009080{
Ronald Cron5425a212020-08-04 14:58:35 +02009081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009082 psa_algorithm_t alg = alg_arg;
9083 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009084 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05309085 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02009086 size_t expected_capacity = requested_capacity;
9087 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009089
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02009091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9093 psa_set_key_algorithm(&attributes, alg);
9094 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009095
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9097 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9100 input1->x, input1->len,
9101 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009102 requested_capacity, 0)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9107 &current_capacity));
9108 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009109
9110 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009111 while (current_capacity > 0) {
9112 size_t read_size = sizeof(output_buffer);
9113 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009114 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 }
9116 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9117 output_buffer,
9118 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009119 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009120 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9121 &current_capacity));
9122 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009123 }
9124
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009125 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9127 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009128
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009130
9131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 psa_key_derivation_abort(&operation);
9133 psa_destroy_key(key);
9134 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009135}
9136/* END_CASE */
9137
Stephan Koch78109f52023-04-12 14:19:36 +02009138/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009139void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9140 int derivation_step,
9141 int capacity, int expected_capacity_status_arg,
9142 data_t *expected_output,
9143 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009144{
9145 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9146 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009147 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009148 uint8_t *output_buffer = NULL;
9149 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009150 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9151 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9152 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009153
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009154 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009156
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9158 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9159 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009160
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9162 step, input->x, input->len),
9163 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009164
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009166 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009168
Gilles Peskine449bd832023-01-11 14:50:10 +01009169 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9170 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 TEST_EQUAL(status, expected_output_status);
9173 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009174 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009175 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009177
9178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 mbedtls_free(output_buffer);
9180 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009181 PSA_DONE();
9182}
9183/* END_CASE */
9184
Janos Follathe60c9052019-07-03 13:51:30 +01009185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009186void derive_key_exercise(int alg_arg,
9187 data_t *key_data,
9188 data_t *input1,
9189 data_t *input2,
9190 int derived_type_arg,
9191 int derived_bits_arg,
9192 int derived_usage_arg,
9193 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009194{
Ronald Cron5425a212020-08-04 14:58:35 +02009195 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9196 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009197 psa_algorithm_t alg = alg_arg;
9198 psa_key_type_t derived_type = derived_type_arg;
9199 size_t derived_bits = derived_bits_arg;
9200 psa_key_usage_t derived_usage = derived_usage_arg;
9201 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009203 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009205 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009206
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009208
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9210 psa_set_key_algorithm(&attributes, alg);
9211 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9212 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9213 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009214
9215 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9217 input1->x, input1->len,
9218 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009219 capacity, 0)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 }
Janos Follathe60c9052019-07-03 13:51:30 +01009222
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 psa_set_key_usage_flags(&attributes, derived_usage);
9224 psa_set_key_algorithm(&attributes, derived_alg);
9225 psa_set_key_type(&attributes, derived_type);
9226 psa_set_key_bits(&attributes, derived_bits);
9227 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9228 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009229
9230 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9232 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9233 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009234
9235 /* Exercise the derived key. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00009236 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg, 0)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009239
9240exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009241 /*
9242 * Key attributes may have been returned by psa_get_key_attributes()
9243 * thus reset them as required.
9244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009246
Gilles Peskine449bd832023-01-11 14:50:10 +01009247 psa_key_derivation_abort(&operation);
9248 psa_destroy_key(base_key);
9249 psa_destroy_key(derived_key);
9250 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009251}
9252/* END_CASE */
9253
Janos Follath42fd8882019-07-03 14:17:09 +01009254/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009255void derive_key_export(int alg_arg,
9256 data_t *key_data,
9257 data_t *input1,
9258 data_t *input2,
9259 int bytes1_arg,
9260 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009261{
Ronald Cron5425a212020-08-04 14:58:35 +02009262 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9263 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009264 psa_algorithm_t alg = alg_arg;
9265 size_t bytes1 = bytes1_arg;
9266 size_t bytes2 = bytes2_arg;
9267 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009268 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009269 uint8_t *output_buffer = NULL;
9270 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009271 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9272 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009273 size_t length;
9274
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009275 TEST_CALLOC(output_buffer, capacity);
9276 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009277 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009278
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9280 psa_set_key_algorithm(&base_attributes, alg);
9281 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9282 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9283 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009284
9285 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9287 input1->x, input1->len,
9288 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009289 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 }
Janos Follath42fd8882019-07-03 14:17:09 +01009292
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9294 output_buffer,
9295 capacity));
9296 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009297
9298 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9300 input1->x, input1->len,
9301 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009302 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009303 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 }
Janos Follath42fd8882019-07-03 14:17:09 +01009305
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9307 psa_set_key_algorithm(&derived_attributes, 0);
9308 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9309 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9310 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9311 &derived_key));
9312 PSA_ASSERT(psa_export_key(derived_key,
9313 export_buffer, bytes1,
9314 &length));
9315 TEST_EQUAL(length, bytes1);
9316 PSA_ASSERT(psa_destroy_key(derived_key));
9317 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9318 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9319 &derived_key));
9320 PSA_ASSERT(psa_export_key(derived_key,
9321 export_buffer + bytes1, bytes2,
9322 &length));
9323 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009324
9325 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009326 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009327 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009328
9329exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 mbedtls_free(output_buffer);
9331 mbedtls_free(export_buffer);
9332 psa_key_derivation_abort(&operation);
9333 psa_destroy_key(base_key);
9334 psa_destroy_key(derived_key);
9335 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009336}
9337/* END_CASE */
9338
9339/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009340void derive_key_type(int alg_arg,
9341 data_t *key_data,
9342 data_t *input1,
9343 data_t *input2,
9344 int key_type_arg, int bits_arg,
9345 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009346{
9347 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9348 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9349 const psa_algorithm_t alg = alg_arg;
9350 const psa_key_type_t key_type = key_type_arg;
9351 const size_t bits = bits_arg;
9352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9353 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009354 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009355 uint8_t *export_buffer = NULL;
9356 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9357 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9358 size_t export_length;
9359
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009360 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009362
Gilles Peskine449bd832023-01-11 14:50:10 +01009363 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9364 psa_set_key_algorithm(&base_attributes, alg);
9365 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9366 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9367 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009368
Gilles Peskine449bd832023-01-11 14:50:10 +01009369 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009370 &operation, base_key, alg,
9371 input1->x, input1->len,
9372 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009373 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9378 psa_set_key_algorithm(&derived_attributes, 0);
9379 psa_set_key_type(&derived_attributes, key_type);
9380 psa_set_key_bits(&derived_attributes, bits);
9381 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9382 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009383
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 PSA_ASSERT(psa_export_key(derived_key,
9385 export_buffer, export_buffer_size,
9386 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009387 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009388 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009389
9390exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 mbedtls_free(export_buffer);
9392 psa_key_derivation_abort(&operation);
9393 psa_destroy_key(base_key);
9394 psa_destroy_key(derived_key);
9395 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009396}
9397/* END_CASE */
9398
9399/* BEGIN_CASE */
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009400void derive_key_ext(int alg_arg,
9401 data_t *key_data,
9402 data_t *input1,
9403 data_t *input2,
9404 int key_type_arg, int bits_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009405 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +01009406 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009407 psa_status_t expected_status,
9408 data_t *expected_export)
9409{
9410 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9411 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9412 const psa_algorithm_t alg = alg_arg;
9413 const psa_key_type_t key_type = key_type_arg;
9414 const size_t bits = bits_arg;
Gilles Peskine092ce512024-02-20 12:31:24 +01009415 psa_key_production_parameters_t *params = NULL;
9416 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009417 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9418 const size_t export_buffer_size =
9419 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
9420 uint8_t *export_buffer = NULL;
9421 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9422 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9423 size_t export_length;
9424
9425 TEST_CALLOC(export_buffer, export_buffer_size);
9426 PSA_ASSERT(psa_crypto_init());
9427
9428 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9429 psa_set_key_algorithm(&base_attributes, alg);
9430 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9431 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9432 &base_key));
9433
9434 if (mbedtls_test_psa_setup_key_derivation_wrap(
9435 &operation, base_key, alg,
9436 input1->x, input1->len,
9437 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009438 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009439 goto exit;
9440 }
9441
9442 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, key_type);
9445 psa_set_key_bits(&derived_attributes, bits);
Gilles Peskine092ce512024-02-20 12:31:24 +01009446 if (!setup_key_production_parameters(&params, &params_data_length,
9447 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009448 goto exit;
9449 }
9450
9451 TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01009452 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009453 &derived_key),
9454 expected_status);
9455
9456 if (expected_status == PSA_SUCCESS) {
9457 PSA_ASSERT(psa_export_key(derived_key,
9458 export_buffer, export_buffer_size,
9459 &export_length));
9460 TEST_MEMORY_COMPARE(export_buffer, export_length,
9461 expected_export->x, expected_export->len);
9462 }
9463
9464exit:
9465 mbedtls_free(export_buffer);
Gilles Peskine092ce512024-02-20 12:31:24 +01009466 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009467 psa_key_derivation_abort(&operation);
9468 psa_destroy_key(base_key);
9469 psa_destroy_key(derived_key);
9470 PSA_DONE();
9471}
9472/* END_CASE */
9473
9474/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009475void derive_key(int alg_arg,
9476 data_t *key_data, data_t *input1, data_t *input2,
9477 int type_arg, int bits_arg,
9478 int expected_status_arg,
9479 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009480{
Ronald Cron5425a212020-08-04 14:58:35 +02009481 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9482 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009483 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009484 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009485 size_t bits = bits_arg;
9486 psa_status_t expected_status = expected_status_arg;
9487 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9488 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9489 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9490
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009492
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9494 psa_set_key_algorithm(&base_attributes, alg);
9495 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9496 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9497 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009498
Gilles Peskine449bd832023-01-11 14:50:10 +01009499 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9500 input1->x, input1->len,
9501 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009502 SIZE_MAX, 0)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009503 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009505
Gilles Peskine449bd832023-01-11 14:50:10 +01009506 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9507 psa_set_key_algorithm(&derived_attributes, 0);
9508 psa_set_key_type(&derived_attributes, type);
9509 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009510
9511 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 psa_key_derivation_output_key(&derived_attributes,
9513 &operation,
9514 &derived_key);
9515 if (is_large_output > 0) {
9516 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9517 }
9518 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009519
9520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009521 psa_key_derivation_abort(&operation);
9522 psa_destroy_key(base_key);
9523 psa_destroy_key(derived_key);
9524 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009525}
9526/* END_CASE */
9527
9528/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009529void key_agreement_setup(int alg_arg,
9530 int our_key_type_arg, int our_key_alg_arg,
9531 data_t *our_key_data, data_t *peer_key_data,
9532 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009533{
Ronald Cron5425a212020-08-04 14:58:35 +02009534 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009535 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009536 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009537 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009538 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009540 psa_status_t expected_status = expected_status_arg;
9541 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009542
Gilles Peskine449bd832023-01-11 14:50:10 +01009543 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009544
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9546 psa_set_key_algorithm(&attributes, our_key_alg);
9547 psa_set_key_type(&attributes, our_key_type);
9548 PSA_ASSERT(psa_import_key(&attributes,
9549 our_key_data->x, our_key_data->len,
9550 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009551
Gilles Peskine77f40d82019-04-11 21:27:06 +02009552 /* The tests currently include inputs that should fail at either step.
9553 * Test cases that fail at the setup step should be changed to call
9554 * key_derivation_setup instead, and this function should be renamed
9555 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 status = psa_key_derivation_setup(&operation, alg);
9557 if (status == PSA_SUCCESS) {
9558 TEST_EQUAL(psa_key_derivation_key_agreement(
9559 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9560 our_key,
9561 peer_key_data->x, peer_key_data->len),
9562 expected_status);
9563 } else {
9564 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009565 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009566
9567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 psa_key_derivation_abort(&operation);
9569 psa_destroy_key(our_key);
9570 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009571}
9572/* END_CASE */
9573
9574/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009575void raw_key_agreement(int alg_arg,
9576 int our_key_type_arg, data_t *our_key_data,
9577 data_t *peer_key_data,
9578 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009579{
Ronald Cron5425a212020-08-04 14:58:35 +02009580 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009581 psa_algorithm_t alg = alg_arg;
9582 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009584 unsigned char *output = NULL;
9585 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009586 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009587
Gilles Peskine449bd832023-01-11 14:50:10 +01009588 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009589
Gilles Peskine449bd832023-01-11 14:50:10 +01009590 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9591 psa_set_key_algorithm(&attributes, alg);
9592 psa_set_key_type(&attributes, our_key_type);
9593 PSA_ASSERT(psa_import_key(&attributes,
9594 our_key_data->x, our_key_data->len,
9595 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009596
Gilles Peskine449bd832023-01-11 14:50:10 +01009597 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9598 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009599
Gilles Peskine992bee82022-04-13 23:25:52 +02009600 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 TEST_LE_U(expected_output->len,
9602 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9603 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9604 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009605
9606 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009607 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009608 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9609 peer_key_data->x, peer_key_data->len,
9610 output, expected_output->len,
9611 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009612 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009613 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009615 output = NULL;
9616 output_length = ~0;
9617
9618 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009619 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9621 peer_key_data->x, peer_key_data->len,
9622 output, expected_output->len + 1,
9623 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009624 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009625 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009627 output = NULL;
9628 output_length = ~0;
9629
9630 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009631 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009632 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9633 peer_key_data->x, peer_key_data->len,
9634 output, expected_output->len - 1,
9635 &output_length),
9636 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009637 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009638 TEST_LE_U(output_length, expected_output->len - 1);
9639 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009640 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009641
9642exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 mbedtls_free(output);
9644 psa_destroy_key(our_key);
9645 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009646}
9647/* END_CASE */
9648
9649/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009650void key_agreement_capacity(int alg_arg,
9651 int our_key_type_arg, data_t *our_key_data,
9652 data_t *peer_key_data,
9653 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009654{
Ronald Cron5425a212020-08-04 14:58:35 +02009655 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009656 psa_algorithm_t alg = alg_arg;
9657 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009658 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009660 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009661 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009662
Gilles Peskine449bd832023-01-11 14:50:10 +01009663 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009664
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9666 psa_set_key_algorithm(&attributes, alg);
9667 psa_set_key_type(&attributes, our_key_type);
9668 PSA_ASSERT(psa_import_key(&attributes,
9669 our_key_data->x, our_key_data->len,
9670 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9673 PSA_ASSERT(psa_key_derivation_key_agreement(
9674 &operation,
9675 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9676 peer_key_data->x, peer_key_data->len));
9677 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009678 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009679 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9680 PSA_KEY_DERIVATION_INPUT_INFO,
9681 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009682 }
Gilles Peskine59685592018-09-18 12:11:34 +02009683
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009684 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 PSA_ASSERT(psa_key_derivation_get_capacity(
9686 &operation, &actual_capacity));
9687 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009688
Gilles Peskinebf491972018-10-25 22:36:12 +02009689 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009690 while (actual_capacity > sizeof(output)) {
9691 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9692 output, sizeof(output)));
9693 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009694 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009695 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9696 output, actual_capacity));
9697 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9698 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009699
Gilles Peskine59685592018-09-18 12:11:34 +02009700exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009701 psa_key_derivation_abort(&operation);
9702 psa_destroy_key(our_key);
9703 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009704}
9705/* END_CASE */
9706
Valerio Settiad819672023-12-29 12:14:41 +01009707/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9708void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009709{
9710 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009711 psa_ecc_family_t ecc_family = psa_family_arg;
9712 size_t bits = bits_arg;
9713 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009714
Valerio Settiad819672023-12-29 12:14:41 +01009715 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9716 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009717 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009718}
9719/* END_CASE */
9720
Valerio Settiac739522024-01-04 10:22:01 +01009721/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9722void ecc_conversion_functions_fail()
9723{
9724 size_t bits;
9725
Valerio Settidb6e0292024-01-05 10:15:45 +01009726 /* Invalid legacy curve identifiers. */
9727 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9728 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009729 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9730 TEST_EQUAL(0, bits);
9731
9732 /* Invalid PSA EC family. */
9733 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9734 /* Invalid bit-size for a valid EC family. */
9735 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9736
9737 /* Twisted-Edward curves are not supported yet. */
9738 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9739 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9740 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9741 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9742}
9743/* END_CASE */
9744
9745
Valerio Settibf999cb2023-12-28 17:48:13 +01009746/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009747void key_agreement_output(int alg_arg,
9748 int our_key_type_arg, data_t *our_key_data,
9749 data_t *peer_key_data,
9750 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009751{
Ronald Cron5425a212020-08-04 14:58:35 +02009752 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009753 psa_algorithm_t alg = alg_arg;
9754 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009755 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009757 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009758
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009759 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009760 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009761
Gilles Peskine449bd832023-01-11 14:50:10 +01009762 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009763
Gilles Peskine449bd832023-01-11 14:50:10 +01009764 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9765 psa_set_key_algorithm(&attributes, alg);
9766 psa_set_key_type(&attributes, our_key_type);
9767 PSA_ASSERT(psa_import_key(&attributes,
9768 our_key_data->x, our_key_data->len,
9769 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009770
Gilles Peskine449bd832023-01-11 14:50:10 +01009771 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9772 PSA_ASSERT(psa_key_derivation_key_agreement(
9773 &operation,
9774 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9775 peer_key_data->x, peer_key_data->len));
9776 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009777 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9779 PSA_KEY_DERIVATION_INPUT_INFO,
9780 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009781 }
Gilles Peskine59685592018-09-18 12:11:34 +02009782
Gilles Peskine449bd832023-01-11 14:50:10 +01009783 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9784 actual_output,
9785 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009786 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009787 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009788 if (expected_output2->len != 0) {
9789 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9790 actual_output,
9791 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009792 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009793 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009794 }
Gilles Peskine59685592018-09-18 12:11:34 +02009795
9796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009797 psa_key_derivation_abort(&operation);
9798 psa_destroy_key(our_key);
9799 PSA_DONE();
9800 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009801}
9802/* END_CASE */
9803
9804/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009805void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009806{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009807 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009808 unsigned char *output = NULL;
9809 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009810 size_t i;
9811 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009812
Gilles Peskine449bd832023-01-11 14:50:10 +01009813 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009814
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009815 TEST_CALLOC(output, bytes);
9816 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009817
Gilles Peskine449bd832023-01-11 14:50:10 +01009818 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009819
Gilles Peskinea50d7392018-06-21 10:22:13 +02009820 /* Run several times, to ensure that every output byte will be
9821 * nonzero at least once with overwhelming probability
9822 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009823 for (run = 0; run < 10; run++) {
9824 if (bytes != 0) {
9825 memset(output, 0, bytes);
9826 }
9827 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009828
Gilles Peskine449bd832023-01-11 14:50:10 +01009829 for (i = 0; i < bytes; i++) {
9830 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009831 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009832 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009833 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009834 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009835
9836 /* Check that every byte was changed to nonzero at least once. This
9837 * validates that psa_generate_random is overwriting every byte of
9838 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009839 for (i = 0; i < bytes; i++) {
9840 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009841 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009842
9843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009844 PSA_DONE();
9845 mbedtls_free(output);
9846 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009847}
9848/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009849
Ryan3a1b7862024-03-01 17:24:04 +00009850#if defined MBEDTLS_THREADING_PTHREAD
9851
9852/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */
9853void concurrently_generate_keys(int type_arg,
9854 int bits_arg,
9855 int usage_arg,
9856 int alg_arg,
9857 int expected_status_arg,
9858 int is_large_key_arg,
9859 int arg_thread_count,
9860 int reps_arg)
9861{
9862 size_t thread_count = (size_t) arg_thread_count;
9863 mbedtls_test_thread_t *threads = NULL;
9864 generate_key_context gkc;
9865 gkc.type = type_arg;
9866 gkc.usage = usage_arg;
9867 gkc.bits = bits_arg;
9868 gkc.alg = alg_arg;
9869 gkc.expected_status = expected_status_arg;
9870 gkc.is_large_key = is_large_key_arg;
9871 gkc.reps = reps_arg;
9872 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9873
9874 PSA_ASSERT(psa_crypto_init());
9875
9876 psa_set_key_usage_flags(&attributes, usage_arg);
9877 psa_set_key_algorithm(&attributes, alg_arg);
9878 psa_set_key_type(&attributes, type_arg);
9879 psa_set_key_bits(&attributes, bits_arg);
9880 gkc.attributes = &attributes;
9881
9882 TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count);
9883
9884 /* Split threads to generate key then destroy key. */
9885 for (size_t i = 0; i < thread_count; i++) {
9886 TEST_EQUAL(
9887 mbedtls_test_thread_create(&threads[i], thread_generate_key,
9888 (void *) &gkc), 0);
9889 }
9890
9891 /* Join threads. */
9892 for (size_t i = 0; i < thread_count; i++) {
9893 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
9894 }
9895
9896exit:
9897 mbedtls_free(threads);
9898 PSA_DONE();
9899}
9900/* END_CASE */
9901#endif
9902
Gilles Peskine12313cd2018-06-20 00:20:32 +02009903/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009904void generate_key(int type_arg,
9905 int bits_arg,
9906 int usage_arg,
9907 int alg_arg,
9908 int expected_status_arg,
9909 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009910{
Ronald Cron5425a212020-08-04 14:58:35 +02009911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009912 psa_key_type_t type = type_arg;
9913 psa_key_usage_t usage = usage_arg;
9914 size_t bits = bits_arg;
9915 psa_algorithm_t alg = alg_arg;
9916 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009918 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009919
Gilles Peskine449bd832023-01-11 14:50:10 +01009920 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009921
Gilles Peskine449bd832023-01-11 14:50:10 +01009922 psa_set_key_usage_flags(&attributes, usage);
9923 psa_set_key_algorithm(&attributes, alg);
9924 psa_set_key_type(&attributes, type);
9925 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009926
9927 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009928 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009929
Gilles Peskine449bd832023-01-11 14:50:10 +01009930 if (is_large_key > 0) {
9931 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9932 }
9933 TEST_EQUAL(status, expected_status);
9934 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009935 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009936 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009937
9938 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009939 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9940 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9941 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009942
Gilles Peskine818ca122018-06-20 18:16:48 +02009943 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00009944 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009946 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009947
9948exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009949 /*
9950 * Key attributes may have been returned by psa_get_key_attributes()
9951 * thus reset them as required.
9952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009953 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009954
Gilles Peskine449bd832023-01-11 14:50:10 +01009955 psa_destroy_key(key);
9956 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009957}
9958/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009959
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009960/* BEGIN_CASE */
9961void generate_key_ext(int type_arg,
9962 int bits_arg,
9963 int usage_arg,
9964 int alg_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009965 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +01009966 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009967 int expected_status_arg)
9968{
9969 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9970 psa_key_type_t type = type_arg;
9971 psa_key_usage_t usage = usage_arg;
9972 size_t bits = bits_arg;
9973 psa_algorithm_t alg = alg_arg;
9974 psa_status_t expected_status = expected_status_arg;
9975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine092ce512024-02-20 12:31:24 +01009976 psa_key_production_parameters_t *params = NULL;
9977 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009978 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
9979
9980 PSA_ASSERT(psa_crypto_init());
9981
9982 psa_set_key_usage_flags(&attributes, usage);
9983 psa_set_key_algorithm(&attributes, alg);
9984 psa_set_key_type(&attributes, type);
9985 psa_set_key_bits(&attributes, bits);
9986
Gilles Peskine092ce512024-02-20 12:31:24 +01009987 if (!setup_key_production_parameters(&params, &params_data_length,
9988 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009989 goto exit;
9990 }
9991
9992 /* Generate a key */
9993 psa_status_t status = psa_generate_key_ext(&attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01009994 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009995 &key);
9996
9997 TEST_EQUAL(status, expected_status);
9998 if (expected_status != PSA_SUCCESS) {
9999 goto exit;
10000 }
10001
10002 /* Test the key information */
10003 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
10004 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
10005 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
10006
Gilles Peskine7a18f962024-02-12 16:48:11 +010010007#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
10008 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +010010009 TEST_ASSERT(rsa_test_e(key, bits, params_data));
Gilles Peskine7a18f962024-02-12 16:48:11 +010010010 }
10011#endif
10012
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010013 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010014 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010015 goto exit;
10016 }
10017
10018exit:
10019 /*
10020 * Key attributes may have been returned by psa_get_key_attributes()
10021 * thus reset them as required.
10022 */
10023 psa_reset_key_attributes(&got_attributes);
Gilles Peskine092ce512024-02-20 12:31:24 +010010024 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010025 psa_destroy_key(key);
10026 PSA_DONE();
10027}
10028/* END_CASE */
10029
10030/* BEGIN_CASE */
Gilles Peskine092ce512024-02-20 12:31:24 +010010031void key_production_parameters_init()
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010032{
Gilles Peskine092ce512024-02-20 12:31:24 +010010033 psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT;
10034 psa_key_production_parameters_t zero;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010035 memset(&zero, 0, sizeof(zero));
10036
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010037 TEST_EQUAL(init.flags, 0);
10038 TEST_EQUAL(zero.flags, 0);
10039}
10040/* END_CASE */
10041
Darryl Greend49a4992018-06-18 17:27:26 +010010042/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010010043void persistent_key_load_key_from_storage(data_t *data,
10044 int type_arg, int bits_arg,
10045 int usage_flags_arg, int alg_arg,
10046 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +010010047{
Gilles Peskine449bd832023-01-11 14:50:10 +010010048 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +020010050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10051 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010052 psa_key_type_t type = type_arg;
10053 size_t bits = bits_arg;
10054 psa_key_usage_t usage_flags = usage_flags_arg;
10055 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +020010056 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +010010057 unsigned char *first_export = NULL;
10058 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010010059 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -070010060 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +010010061 size_t second_exported_length;
10062
Gilles Peskine449bd832023-01-11 14:50:10 +010010063 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010064 TEST_CALLOC(first_export, export_size);
10065 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010066 }
Darryl Greend49a4992018-06-18 17:27:26 +010010067
Gilles Peskine449bd832023-01-11 14:50:10 +010010068 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010069
Gilles Peskine449bd832023-01-11 14:50:10 +010010070 psa_set_key_id(&attributes, key_id);
10071 psa_set_key_usage_flags(&attributes, usage_flags);
10072 psa_set_key_algorithm(&attributes, alg);
10073 psa_set_key_type(&attributes, type);
10074 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +010010075
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010077 case IMPORT_KEY:
10078 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010079 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
10080 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010081 break;
Darryl Greend49a4992018-06-18 17:27:26 +010010082
Darryl Green0c6575a2018-11-07 16:05:30 +000010083 case GENERATE_KEY:
10084 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010085 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010086 break;
10087
10088 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +010010089#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 {
10091 /* Create base key */
10092 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
10093 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
10094 psa_set_key_usage_flags(&base_attributes,
10095 PSA_KEY_USAGE_DERIVE);
10096 psa_set_key_algorithm(&base_attributes, derive_alg);
10097 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
10098 PSA_ASSERT(psa_import_key(&base_attributes,
10099 data->x, data->len,
10100 &base_key));
10101 /* Derive a key. */
10102 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
10103 PSA_ASSERT(psa_key_derivation_input_key(
10104 &operation,
10105 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
10106 PSA_ASSERT(psa_key_derivation_input_bytes(
10107 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
10108 NULL, 0));
10109 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
10110 &operation,
10111 &key));
10112 PSA_ASSERT(psa_key_derivation_abort(&operation));
10113 PSA_ASSERT(psa_destroy_key(base_key));
10114 base_key = MBEDTLS_SVC_KEY_ID_INIT;
10115 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010116#else
Gilles Peskine449bd832023-01-11 14:50:10 +010010117 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010118#endif
10119 break;
10120
10121 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010010122 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010123 break;
Darryl Green0c6575a2018-11-07 16:05:30 +000010124 }
Gilles Peskine449bd832023-01-11 14:50:10 +010010125 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +010010126
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010127 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010128 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10129 PSA_ASSERT(psa_export_key(key,
10130 first_export, export_size,
10131 &first_exported_length));
10132 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010133 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010134 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +010010135 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010136 }
Darryl Greend49a4992018-06-18 17:27:26 +010010137
10138 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +010010139 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010140 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +010010141 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010142
Darryl Greend49a4992018-06-18 17:27:26 +010010143 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +010010144 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
10145 TEST_ASSERT(mbedtls_svc_key_id_equal(
10146 psa_get_key_id(&attributes), key_id));
10147 TEST_EQUAL(psa_get_key_lifetime(&attributes),
10148 PSA_KEY_LIFETIME_PERSISTENT);
10149 TEST_EQUAL(psa_get_key_type(&attributes), type);
10150 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
10151 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
10152 mbedtls_test_update_key_usage_flags(usage_flags));
10153 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +010010154
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010155 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010156 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10157 PSA_ASSERT(psa_export_key(key,
10158 second_export, export_size,
10159 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010160 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010161 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +000010162 }
10163
10164 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010165 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010166 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010167 }
Darryl Greend49a4992018-06-18 17:27:26 +010010168
10169exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010170 /*
10171 * Key attributes may have been returned by psa_get_key_attributes()
10172 * thus reset them as required.
10173 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010174 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010175
Gilles Peskine449bd832023-01-11 14:50:10 +010010176 mbedtls_free(first_export);
10177 mbedtls_free(second_export);
10178 psa_key_derivation_abort(&operation);
10179 psa_destroy_key(base_key);
10180 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010181 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +010010182}
10183/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +020010184
Neil Armstronga557cb82022-06-10 08:58:32 +020010185/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010186void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
10187 int primitive_arg, int hash_arg, int role_arg,
10188 int test_input, data_t *pw_data,
10189 int inj_err_type_arg,
10190 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010191{
10192 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10193 psa_pake_operation_t operation = psa_pake_operation_init();
10194 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010195 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +020010196 psa_key_type_t key_type_pw = key_type_pw_arg;
10197 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010198 psa_algorithm_t hash_alg = hash_arg;
10199 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010202 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
10203 psa_status_t expected_error = expected_error_arg;
10204 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010205 unsigned char *output_buffer = NULL;
10206 size_t output_len = 0;
10207
Gilles Peskine449bd832023-01-11 14:50:10 +010010208 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010209
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010210 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +010010211 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010212 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010213
Gilles Peskine449bd832023-01-11 14:50:10 +010010214 if (pw_data->len > 0) {
10215 psa_set_key_usage_flags(&attributes, key_usage_pw);
10216 psa_set_key_algorithm(&attributes, alg);
10217 psa_set_key_type(&attributes, key_type_pw);
10218 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10219 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010220 }
10221
Gilles Peskine449bd832023-01-11 14:50:10 +010010222 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10223 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10224 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010225
Gilles Peskine449bd832023-01-11 14:50:10 +010010226 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010227
Gilles Peskine449bd832023-01-11 14:50:10 +010010228 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10229 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10230 expected_error);
10231 PSA_ASSERT(psa_pake_abort(&operation));
10232 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10233 expected_error);
10234 PSA_ASSERT(psa_pake_abort(&operation));
10235 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10236 expected_error);
10237 PSA_ASSERT(psa_pake_abort(&operation));
10238 TEST_EQUAL(psa_pake_set_role(&operation, role),
10239 expected_error);
10240 PSA_ASSERT(psa_pake_abort(&operation));
10241 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10242 NULL, 0, NULL),
10243 expected_error);
10244 PSA_ASSERT(psa_pake_abort(&operation));
10245 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10246 expected_error);
10247 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010248 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010249 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010250
Gilles Peskine449bd832023-01-11 14:50:10 +010010251 status = psa_pake_setup(&operation, &cipher_suite);
10252 if (status != PSA_SUCCESS) {
10253 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010254 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010255 }
10256
Gilles Peskine449bd832023-01-11 14:50:10 +010010257 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10258 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10259 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010260 goto exit;
10261 }
10262
Gilles Peskine449bd832023-01-11 14:50:10 +010010263 status = psa_pake_set_role(&operation, role);
10264 if (status != PSA_SUCCESS) {
10265 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010266 goto exit;
10267 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010268
Gilles Peskine449bd832023-01-11 14:50:10 +010010269 if (pw_data->len > 0) {
10270 status = psa_pake_set_password_key(&operation, key);
10271 if (status != PSA_SUCCESS) {
10272 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010273 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010274 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010275 }
10276
Gilles Peskine449bd832023-01-11 14:50:10 +010010277 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10278 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10279 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010280 goto exit;
10281 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010282
Gilles Peskine449bd832023-01-11 14:50:10 +010010283 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10284 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10285 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010286 goto exit;
10287 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010288
Gilles Peskine449bd832023-01-11 14:50:10 +010010289 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010290 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010291 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10292 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010293 goto exit;
10294 }
10295
Gilles Peskine449bd832023-01-11 14:50:10 +010010296 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010297 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010298 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10299 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010300 goto exit;
10301 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010302
Gilles Peskine449bd832023-01-11 14:50:10 +010010303 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10304 PSA_PAKE_STEP_KEY_SHARE);
10305 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10306 PSA_PAKE_STEP_ZK_PUBLIC);
10307 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10308 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010309
Gilles Peskine449bd832023-01-11 14:50:10 +010010310 if (test_input) {
10311 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10312 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10313 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010314 goto exit;
10315 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010316
Gilles Peskine449bd832023-01-11 14:50:10 +010010317 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10318 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10319 output_buffer, size_zk_proof),
10320 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010321 goto exit;
10322 }
10323
Gilles Peskine449bd832023-01-11 14:50:10 +010010324 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10325 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10326 output_buffer, size_zk_proof),
10327 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010328 goto exit;
10329 }
10330
Gilles Peskine449bd832023-01-11 14:50:10 +010010331 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10332 output_buffer, size_key_share);
10333 if (status != PSA_SUCCESS) {
10334 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010335 goto exit;
10336 }
10337
Gilles Peskine449bd832023-01-11 14:50:10 +010010338 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10339 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10340 output_buffer, size_zk_public + 1),
10341 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010342 goto exit;
10343 }
10344
Gilles Peskine449bd832023-01-11 14:50:10 +010010345 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010346 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010347 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10348 output_buffer, size_zk_public + 1);
10349 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10350 output_buffer, size_zk_public),
10351 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010352 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010353 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010354 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010355 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10356 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10357 NULL, 0, NULL),
10358 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010359 goto exit;
10360 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010361
Gilles Peskine449bd832023-01-11 14:50:10 +010010362 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10363 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10364 output_buffer, buf_size, &output_len),
10365 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010366 goto exit;
10367 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010368
Gilles Peskine449bd832023-01-11 14:50:10 +010010369 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10370 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10371 output_buffer, buf_size, &output_len),
10372 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010373 goto exit;
10374 }
10375
Gilles Peskine449bd832023-01-11 14:50:10 +010010376 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10377 output_buffer, buf_size, &output_len);
10378 if (status != PSA_SUCCESS) {
10379 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010380 goto exit;
10381 }
10382
Gilles Peskine449bd832023-01-11 14:50:10 +010010383 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010384
Gilles Peskine449bd832023-01-11 14:50:10 +010010385 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10386 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10387 output_buffer, size_zk_public - 1, &output_len),
10388 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010389 goto exit;
10390 }
10391
Gilles Peskine449bd832023-01-11 14:50:10 +010010392 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010393 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010394 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10395 output_buffer, size_zk_public - 1, &output_len);
10396 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10397 output_buffer, buf_size, &output_len),
10398 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010399 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010400 }
10401 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010402
10403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010404 PSA_ASSERT(psa_destroy_key(key));
10405 PSA_ASSERT(psa_pake_abort(&operation));
10406 mbedtls_free(output_buffer);
10407 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010408}
10409/* END_CASE */
10410
Neil Armstronga557cb82022-06-10 08:58:32 +020010411/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010412void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10413 int client_input_first, int inject_error,
10414 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010415{
10416 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10417 psa_pake_operation_t server = psa_pake_operation_init();
10418 psa_pake_operation_t client = psa_pake_operation_init();
10419 psa_algorithm_t alg = alg_arg;
10420 psa_algorithm_t hash_alg = hash_arg;
10421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10423
Gilles Peskine449bd832023-01-11 14:50:10 +010010424 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010425
Gilles Peskine449bd832023-01-11 14:50:10 +010010426 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10427 psa_set_key_algorithm(&attributes, alg);
10428 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10429 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10430 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010431
Gilles Peskine449bd832023-01-11 14:50:10 +010010432 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10433 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10434 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010435
10436
Gilles Peskine449bd832023-01-11 14:50:10 +010010437 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10438 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010439
Gilles Peskine449bd832023-01-11 14:50:10 +010010440 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10441 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010442
Gilles Peskine449bd832023-01-11 14:50:10 +010010443 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10444 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010445
Gilles Peskine449bd832023-01-11 14:50:10 +010010446 ecjpake_do_round(alg, primitive_arg, &server, &client,
10447 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010448
Gilles Peskine449bd832023-01-11 14:50:10 +010010449 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010451 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010452
Gilles Peskine449bd832023-01-11 14:50:10 +010010453 ecjpake_do_round(alg, primitive_arg, &server, &client,
10454 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010455
10456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010457 psa_destroy_key(key);
10458 psa_pake_abort(&server);
10459 psa_pake_abort(&client);
10460 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010461}
10462/* END_CASE */
10463
10464/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010465void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10466 int derive_alg_arg, data_t *pw_data,
10467 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010468{
10469 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10470 psa_pake_operation_t server = psa_pake_operation_init();
10471 psa_pake_operation_t client = psa_pake_operation_init();
10472 psa_algorithm_t alg = alg_arg;
10473 psa_algorithm_t hash_alg = hash_arg;
10474 psa_algorithm_t derive_alg = derive_alg_arg;
10475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10477 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010478 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010479 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010480 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010481 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010482
Gilles Peskine449bd832023-01-11 14:50:10 +010010483 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010484
Gilles Peskine449bd832023-01-11 14:50:10 +010010485 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10486 psa_set_key_algorithm(&attributes, alg);
10487 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10488 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10489 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010490
Gilles Peskine449bd832023-01-11 14:50:10 +010010491 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10492 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10493 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010494
Neil Armstrong1e855602022-06-15 11:32:11 +020010495 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010496 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10497 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010498
Gilles Peskine449bd832023-01-11 14:50:10 +010010499 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10500 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10501 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10502 PSA_KEY_DERIVATION_INPUT_SEED,
10503 (const uint8_t *) "", 0));
10504 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10505 PSA_KEY_DERIVATION_INPUT_SEED,
10506 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010507 }
10508
Gilles Peskine449bd832023-01-11 14:50:10 +010010509 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10510 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010511
Gilles Peskine449bd832023-01-11 14:50:10 +010010512 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10513 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010514
Gilles Peskine449bd832023-01-11 14:50:10 +010010515 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10516 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010517
Gilles Peskine449bd832023-01-11 14:50:10 +010010518 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10519 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10520 PSA_ERROR_BAD_STATE);
10521 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10522 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010523 goto exit;
10524 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010525
Neil Armstrongf983caf2022-06-15 15:27:48 +020010526 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010527 ecjpake_do_round(alg, primitive_arg, &server, &client,
10528 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010529
Gilles Peskine449bd832023-01-11 14:50:10 +010010530 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10531 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10532 PSA_ERROR_BAD_STATE);
10533 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10534 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010535 goto exit;
10536 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010537
Neil Armstrongf983caf2022-06-15 15:27:48 +020010538 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010539 ecjpake_do_round(alg, primitive_arg, &server, &client,
10540 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010541
Gilles Peskine449bd832023-01-11 14:50:10 +010010542 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10543 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010544
10545exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010546 psa_key_derivation_abort(&server_derive);
10547 psa_key_derivation_abort(&client_derive);
10548 psa_destroy_key(key);
10549 psa_pake_abort(&server);
10550 psa_pake_abort(&client);
10551 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010552}
10553/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010554
10555/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010556void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010557{
10558 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10559 const size_t bits = 256;
10560 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010561 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010562 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010563 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010564
10565 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10566 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010567 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10568 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10569 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10570 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010571 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010572 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10573 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010574
10575 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010576 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10577 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10578 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10579 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10580 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10581 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010582
10583 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010584 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10585 PSA_PAKE_OUTPUT_MAX_SIZE);
10586 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10587 PSA_PAKE_OUTPUT_MAX_SIZE);
10588 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10589 PSA_PAKE_OUTPUT_MAX_SIZE);
10590 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10591 PSA_PAKE_INPUT_MAX_SIZE);
10592 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10593 PSA_PAKE_INPUT_MAX_SIZE);
10594 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10595 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010596}
10597/* END_CASE */