blob: 6cb074449598f6ed2fa6fe2819342570802c019e [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)
Ryan Everett50619992024-03-12 16:55:14 +00001341
1342typedef struct same_key_context {
1343 data_t *data;
1344 mbedtls_svc_key_id_t key;
1345 psa_key_attributes_t *attributes;
1346 int type;
1347 int bits;
1348 /* The following two parameters are used to ensure that when multiple
1349 * threads attempt to load/destroy the key, exactly one thread succeeds. */
1350 int key_loaded;
1351 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(key_loaded_mutex);
1352}
1353same_key_context;
1354
1355/* Attempt to import the key in ctx. This handles any valid error codes
1356 * and reports an error for any invalid codes. This function also insures
1357 * that once imported by some thread, all threads can use the key. */
1358void *thread_import_key(void *ctx)
1359{
1360 mbedtls_svc_key_id_t returned_key_id;
1361 same_key_context *skc = (struct same_key_context *) ctx;
1362 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1363
1364 /* Import the key, exactly one thread must succceed. */
1365 psa_status_t status = psa_import_key(skc->attributes, skc->data->x,
1366 skc->data->len, &returned_key_id);
1367 switch (status) {
1368 case PSA_SUCCESS:
1369 if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) {
1370 if (skc->key_loaded) {
1371 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1372 /* More than one thread has succeeded, report a failure. */
1373 TEST_EQUAL(skc->key_loaded, 0);
1374 }
1375 skc->key_loaded = 1;
1376 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1377 }
1378 break;
1379 case PSA_ERROR_INSUFFICIENT_MEMORY:
1380 /* If all of the key slots are reserved when a thread
1381 * locks the mutex to reserve a new slot, it will return
1382 * PSA_ERROR_INSUFFICIENT_MEMORY; this is correct behaviour.
1383 * There is a chance for this to occur here when the number of
1384 * threads running this function is larger than the number of
1385 * free key slots. Each thread reserves an empty key slot,
1386 * unlocks the mutex, then relocks it to finalize key creation.
1387 * It is at that point where the thread sees that the key
1388 * already exists, releases the reserved slot,
1389 * and returns PSA_ERROR_ALREADY_EXISTS.
1390 * There is no guarantee that the key is loaded upon this return
1391 * code, so we can't test the key information. Just stop this
1392 * thread from executing, note that this is not an error. */
1393 goto exit;
1394 break;
1395 case PSA_ERROR_ALREADY_EXISTS:
1396 /* The key has been loaded by a different thread. */
1397 break;
1398 default:
1399 PSA_ASSERT(status);
1400 }
1401 /* At this point the key must exist, test the key information. */
1402 status = psa_get_key_attributes(skc->key, &got_attributes);
1403 if (status == PSA_ERROR_INSUFFICIENT_MEMORY) {
1404 /* This is not a test failure. The following sequence of events
1405 * causes this to occur:
1406 * 1: This thread successfuly imports a persistent key skc->key.
1407 * 2: N threads reserve an empty key slot in psa_import_key,
1408 * where N is equal to the number of free key slots.
1409 * 3: A final thread attempts to reserve an empty key slot, kicking
1410 * skc->key (which has no registered readers) out of its slot.
1411 * 4: This thread calls psa_get_key_attributes(skc->key,...):
1412 * it sees that skc->key is not in a slot, attempts to load it and
1413 * finds that there are no free slots.
1414 * This thread returns PSA_ERROR_INSUFFICIENT_MEMORY.
1415 *
1416 * The PSA spec allows this behaviour, it is an unavoidable consequence
1417 * of allowing persistent keys to be kicked out of the key store while
1418 * they are still valid. */
1419 goto exit;
1420 }
1421 PSA_ASSERT(status);
1422 TEST_EQUAL(psa_get_key_type(&got_attributes), skc->type);
1423 TEST_EQUAL(psa_get_key_bits(&got_attributes), skc->bits);
1424
1425exit:
1426 /* Key attributes may have been returned by psa_get_key_attributes(),
1427 * reset them as required. */
1428 psa_reset_key_attributes(&got_attributes);
1429 return NULL;
1430}
1431
1432void *thread_use_and_destroy_key(void *ctx)
1433{
1434 same_key_context *skc = (struct same_key_context *) ctx;
1435
1436 /* Do something with the key according
1437 * to its type and permitted usage. */
1438 TEST_ASSERT(mbedtls_test_psa_exercise_key(skc->key,
1439 skc->attributes->policy.usage,
1440 skc->attributes->policy.alg, 1));
1441
1442 psa_status_t status = psa_destroy_key(skc->key);
1443 if (status == PSA_SUCCESS) {
1444 if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) {
1445 /* Ensure that we are the only thread to succeed. */
1446 if (skc->key_loaded != 1) {
1447 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1448 //Will always fail
1449 TEST_EQUAL(skc->key_loaded, 1);
1450 }
1451 skc->key_loaded = 0;
1452 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1453 }
1454 } else {
1455 TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE);
1456 }
1457
1458exit:
1459 return NULL;
1460}
1461
Ryan3a1b7862024-03-01 17:24:04 +00001462typedef struct generate_key_context {
1463 psa_key_type_t type;
1464 psa_key_usage_t usage;
1465 size_t bits;
1466 psa_algorithm_t alg;
1467 psa_status_t expected_status;
1468 psa_key_attributes_t *attributes;
1469 int is_large_key;
1470 int reps;
1471}
1472generate_key_context;
1473void *thread_generate_key(void *ctx)
1474{
1475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1476 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1477 generate_key_context *gkc = (struct generate_key_context *) ctx;
1478
1479 /* If there are race conditions, it is likely the case that they do not
1480 * arise every time the code runs. We repeat the code to increase the
1481 * chance that any race conditions will be hit. */
1482 for (int n = 0; n < gkc->reps; n++) {
1483 /* Generate a key */
1484 psa_status_t status = psa_generate_key(gkc->attributes, &key);
1485
1486 if (gkc->is_large_key > 0) {
1487 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1488 }
1489
1490 TEST_EQUAL(status, gkc->expected_status);
1491 if (gkc->expected_status != PSA_SUCCESS) {
1492 PSA_ASSERT(psa_destroy_key(key));
1493 goto exit;
1494 }
1495
1496 /* Test the key information */
1497 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1498 TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type);
1499 TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits);
1500
1501 /* Do something with the key according
1502 * to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00001503 if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) {
Ryan3a1b7862024-03-01 17:24:04 +00001504 psa_destroy_key(key);
1505 goto exit;
1506 }
1507 psa_reset_key_attributes(&got_attributes);
1508
1509 PSA_ASSERT(psa_destroy_key(key));
1510 }
1511exit:
1512 /*
1513 * Key attributes may have been returned by psa_get_key_attributes()
1514 * thus reset them as required.
1515 */
1516 psa_reset_key_attributes(&got_attributes);
1517 return NULL;
1518}
1519#endif /* MBEDTLS_THREADING_PTHREAD */
1520
Gilles Peskinee59236f2018-01-27 23:32:46 +01001521/* END_HEADER */
1522
1523/* BEGIN_DEPENDENCIES
1524 * depends_on:MBEDTLS_PSA_CRYPTO_C
1525 * END_DEPENDENCIES
1526 */
1527
1528/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001529void psa_can_do_hash()
1530{
1531 /* We can't test that this is specific to drivers until partial init has
1532 * been implemented, but we can at least test before/after full init. */
1533 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1534 PSA_INIT();
1535 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1536 PSA_DONE();
1537}
1538/* END_CASE */
1539
1540/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001542{
1543 size_t max_truncated_mac_size =
1544 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1545
1546 /* Check that the length for a truncated MAC always fits in the algorithm
1547 * encoding. The shifted mask is the maximum truncated value. The
1548 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001550}
1551/* END_CASE */
1552
1553/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554void import_with_policy(int type_arg,
1555 int usage_arg, int alg_arg,
1556 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001557{
1558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1559 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001561 psa_key_type_t type = type_arg;
1562 psa_key_usage_t usage = usage_arg;
1563 psa_algorithm_t alg = alg_arg;
1564 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001566 psa_status_t status;
1567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 psa_set_key_type(&attributes, type);
1571 psa_set_key_usage_flags(&attributes, usage);
1572 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 status = psa_import_key(&attributes,
1575 key_material, sizeof(key_material),
1576 &key);
1577 TEST_EQUAL(status, expected_status);
1578 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001579 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1583 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1584 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1585 mbedtls_test_update_key_usage_flags(usage));
1586 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1587 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 PSA_ASSERT(psa_destroy_key(key));
1590 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001591
1592exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001593 /*
1594 * Key attributes may have been returned by psa_get_key_attributes()
1595 * thus reset them as required.
1596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 psa_destroy_key(key);
1600 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001601}
1602/* END_CASE */
1603
1604/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605void import_with_data(data_t *data, int type_arg,
1606 int attr_bits_arg,
1607 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001608{
1609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1610 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001611 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001612 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001613 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001614 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 psa_set_key_type(&attributes, type);
1620 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001623 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1624 *
1625 * This can happen with a type supported only by a driver:
1626 * - the driver sees the invalid data (for example wrong size) and thinks
1627 * "well perhaps this is a key size I don't support" so it returns
1628 * NOT_SUPPORTED which is correct at this point;
1629 * - we fallback to built-ins, which don't support this type, so return
1630 * NOT_SUPPORTED which again is correct at this point.
1631 */
1632 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1633 status == PSA_ERROR_NOT_SUPPORTED) {
1634 ; // OK
1635 } else {
1636 TEST_EQUAL(status, expected_status);
1637 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001639 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1643 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1644 if (attr_bits != 0) {
1645 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1646 }
1647 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 PSA_ASSERT(psa_destroy_key(key));
1650 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001651
1652exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001653 /*
1654 * Key attributes may have been returned by psa_get_key_attributes()
1655 * thus reset them as required.
1656 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 psa_destroy_key(key);
1660 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001661}
1662/* END_CASE */
1663
1664/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001665/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001666void import_large_key(int type_arg, int byte_size_arg,
1667 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001668{
1669 psa_key_type_t type = type_arg;
1670 size_t byte_size = byte_size_arg;
1671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1672 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001674 psa_status_t status;
1675 uint8_t *buffer = NULL;
1676 size_t buffer_size = byte_size + 1;
1677 size_t n;
1678
Steven Cooreman69967ce2021-01-18 18:01:08 +01001679 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001680 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001681 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001685
1686 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1688 psa_set_key_type(&attributes, type);
1689 status = psa_import_key(&attributes, buffer, byte_size, &key);
1690 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1691 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 if (status == PSA_SUCCESS) {
1694 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1695 TEST_EQUAL(psa_get_key_type(&attributes), type);
1696 TEST_EQUAL(psa_get_key_bits(&attributes),
1697 PSA_BYTES_TO_BITS(byte_size));
1698 ASSERT_NO_SLOT_NUMBER(&attributes);
1699 memset(buffer, 0, byte_size + 1);
1700 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1701 for (n = 0; n < byte_size; n++) {
1702 TEST_EQUAL(buffer[n], 'K');
1703 }
1704 for (n = byte_size; n < buffer_size; n++) {
1705 TEST_EQUAL(buffer[n], 0);
1706 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001707 }
1708
1709exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001710 /*
1711 * Key attributes may have been returned by psa_get_key_attributes()
1712 * thus reset them as required.
1713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 psa_destroy_key(key);
1717 PSA_DONE();
1718 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001719}
1720/* END_CASE */
1721
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001722/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001723/* Import an RSA key with a valid structure (but not valid numbers
1724 * inside, beyond having sensible size and parity). This is expected to
1725 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001727{
Ronald Cron5425a212020-08-04 14:58:35 +02001728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001729 size_t bits = bits_arg;
1730 psa_status_t expected_status = expected_status_arg;
1731 psa_status_t status;
1732 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001733 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001734 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001736 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001737 unsigned char *p;
1738 int ret;
1739 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001740 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001743 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1746 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001747 length = ret;
1748
1749 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 psa_set_key_type(&attributes, type);
1751 status = psa_import_key(&attributes, p, length, &key);
1752 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (status == PSA_SUCCESS) {
1755 PSA_ASSERT(psa_destroy_key(key));
1756 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001757
1758exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 mbedtls_free(buffer);
1760 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001761}
1762/* END_CASE */
1763
1764/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765void import_export(data_t *data,
1766 int type_arg,
1767 int usage_arg, int alg_arg,
1768 int lifetime_arg,
1769 int expected_bits,
1770 int export_size_delta,
1771 int expected_export_status_arg,
1772 /*whether reexport must give the original input exactly*/
1773 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001774{
Ronald Cron5425a212020-08-04 14:58:35 +02001775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001776 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001777 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001778 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001779 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301780 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001781 unsigned char *exported = NULL;
1782 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001783 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001784 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001785 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001787 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001788
Moran Pekercb088e72018-07-17 17:36:59 +03001789 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001790 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001792 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 }
1794 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 psa_set_key_lifetime(&attributes, lifetime);
1797 psa_set_key_usage_flags(&attributes, usage_arg);
1798 psa_set_key_algorithm(&attributes, alg);
1799 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001800
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001801 if (PSA_KEY_TYPE_IS_DH(type) &&
1802 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001803 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1804 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001805 }
1806
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001807 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001808 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001809 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001810
1811 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1813 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1814 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1815 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001816
1817 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 status = psa_export_key(key, exported, export_size, &exported_length);
1819 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001820
1821 /* The exported length must be set by psa_export_key() to a value between 0
1822 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1824 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1825 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1828 export_size - exported_length));
1829 if (status != PSA_SUCCESS) {
1830 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001831 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001832 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001833
Gilles Peskineea38a922021-02-13 00:05:16 +01001834 /* Run sanity checks on the exported key. For non-canonical inputs,
1835 * this validates the canonical representations. For canonical inputs,
1836 * this doesn't directly validate the implementation, but it still helps
1837 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (!psa_key_lifetime_is_external(lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00001839 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301840 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 }
Archana4d7ae1d2021-07-07 02:50:22 +05301842 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001845 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001847 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1849 &key2));
1850 PSA_ASSERT(psa_export_key(key2,
1851 reexported,
1852 export_size,
1853 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001854 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001855 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001857 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 TEST_LE_U(exported_length,
1859 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1860 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001861 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1862 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1863 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1864 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1865 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001866
1867destroy:
1868 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 PSA_ASSERT(psa_destroy_key(key));
1870 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001871
1872exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001873 /*
1874 * Key attributes may have been returned by psa_get_key_attributes()
1875 * thus reset them as required.
1876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 psa_reset_key_attributes(&got_attributes);
1878 psa_destroy_key(key);
1879 mbedtls_free(exported);
1880 mbedtls_free(reexported);
1881 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001882}
1883/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001884
Moran Pekerf709f4a2018-06-06 17:26:04 +03001885/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886void import_export_public_key(data_t *data,
1887 int type_arg, // key pair or public key
1888 int alg_arg,
1889 int lifetime_arg,
1890 int export_size_delta,
1891 int expected_export_status_arg,
1892 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001893{
Ronald Cron5425a212020-08-04 14:58:35 +02001894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001895 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001896 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001897 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001898 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301899 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001900 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001901 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001902 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 psa_set_key_lifetime(&attributes, lifetime);
1908 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1909 psa_set_key_algorithm(&attributes, alg);
1910 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001911
1912 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001914
Gilles Peskine49c25912018-10-29 15:15:31 +01001915 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001916 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 status = psa_export_public_key(key,
1918 exported, export_size,
1919 &exported_length);
1920 TEST_EQUAL(status, expected_export_status);
1921 if (status == PSA_SUCCESS) {
1922 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001923 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1925 bits = psa_get_key_bits(&attributes);
1926 TEST_LE_U(expected_public_key->len,
1927 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1928 TEST_LE_U(expected_public_key->len,
1929 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1930 TEST_LE_U(expected_public_key->len,
1931 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001932 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001933 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001934 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001935exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001936 /*
1937 * Key attributes may have been returned by psa_get_key_attributes()
1938 * thus reset them as required.
1939 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 mbedtls_free(exported);
1943 psa_destroy_key(key);
1944 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001945}
1946/* END_CASE */
1947
Ryan Everett50619992024-03-12 16:55:14 +00001948
1949#if defined(MBEDTLS_THREADING_PTHREAD)
1950/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */
1951void concurrently_use_same_persistent_key(data_t *data,
1952 int type_arg,
1953 int bits_arg,
1954 int alg_arg,
1955 int thread_count_arg)
1956{
1957 size_t thread_count = (size_t) thread_count_arg;
1958 mbedtls_test_thread_t *threads = NULL;
1959 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
1960 same_key_context skc;
1961 skc.data = data;
1962 skc.key = key_id;
1963 skc.type = type_arg;
1964 skc.bits = bits_arg;
1965 skc.key_loaded = 0;
1966 mbedtls_mutex_init(&skc.key_loaded_mutex);
1967 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg);
1968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1969
1970 PSA_ASSERT(psa_crypto_init());
1971
1972 psa_set_key_id(&attributes, key_id);
1973 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
1974 psa_set_key_usage_flags(&attributes, usage);
1975 psa_set_key_algorithm(&attributes, alg_arg);
1976 psa_set_key_type(&attributes, type_arg);
1977 psa_set_key_bits(&attributes, bits_arg);
1978 skc.attributes = &attributes;
1979
1980 TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count);
1981
1982 /* Test that when multiple threads import the same key,
1983 * exactly one thread succeeds and the rest fail with valid errors.
1984 * Also test that all threads can use the key as soon as it has been
1985 * imported. */
1986 for (size_t i = 0; i < thread_count; i++) {
1987 TEST_EQUAL(
1988 mbedtls_test_thread_create(&threads[i], thread_import_key,
1989 (void *) &skc), 0);
1990 }
1991
1992 /* Join threads. */
1993 for (size_t i = 0; i < thread_count; i++) {
1994 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
1995 }
1996
1997 /* Test that when multiple threads use and destroy a key no corruption
1998 * occurs, and exactly one thread succeeds when destroying the key. */
1999 for (size_t i = 0; i < thread_count; i++) {
2000 TEST_EQUAL(
2001 mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key,
2002 (void *) &skc), 0);
2003 }
2004
2005 /* Join threads. */
2006 for (size_t i = 0; i < thread_count; i++) {
2007 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
2008 }
2009 /* Ensure that one thread succeeded in destroying the key. */
2010 TEST_ASSERT(!skc.key_loaded);
2011exit:
2012 psa_reset_key_attributes(&attributes);
2013 mbedtls_mutex_free(&skc.key_loaded_mutex);
2014 mbedtls_free(threads);
2015 PSA_DONE();
2016}
2017/* END_CASE */
2018#endif
2019
Gilles Peskine20035e32018-02-03 22:44:14 +01002020/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002021void import_and_exercise_key(data_t *data,
2022 int type_arg,
2023 int bits_arg,
2024 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002025{
Ronald Cron5425a212020-08-04 14:58:35 +02002026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002027 psa_key_type_t type = type_arg;
2028 size_t bits = bits_arg;
2029 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02002031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002032 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 psa_set_key_usage_flags(&attributes, usage);
2037 psa_set_key_algorithm(&attributes, alg);
2038 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002039
2040 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002042
2043 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2045 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
2046 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002047
2048 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00002049 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02002050 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 PSA_ASSERT(psa_destroy_key(key));
2054 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02002055
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002056exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002057 /*
2058 * Key attributes may have been returned by psa_get_key_attributes()
2059 * thus reset them as required.
2060 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 psa_reset_key_attributes(&attributes);
2064 psa_destroy_key(key);
2065 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002066}
2067/* END_CASE */
2068
2069/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002070void effective_key_attributes(int type_arg, int expected_type_arg,
2071 int bits_arg, int expected_bits_arg,
2072 int usage_arg, int expected_usage_arg,
2073 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02002074{
Ronald Cron5425a212020-08-04 14:58:35 +02002075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01002076 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002077 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01002078 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002079 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02002080 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002081 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02002082 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002083 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02002085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02002087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 psa_set_key_usage_flags(&attributes, usage);
2089 psa_set_key_algorithm(&attributes, alg);
2090 psa_set_key_type(&attributes, key_type);
2091 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 PSA_ASSERT(psa_generate_key(&attributes, &key));
2094 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2097 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
2098 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
2099 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
2100 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02002101
2102exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002103 /*
2104 * Key attributes may have been returned by psa_get_key_attributes()
2105 * thus reset them as required.
2106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 psa_destroy_key(key);
2110 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002111}
2112/* END_CASE */
2113
2114/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115void check_key_policy(int type_arg, int bits_arg,
2116 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01002117{
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
2119 usage_arg,
2120 mbedtls_test_update_key_usage_flags(usage_arg),
2121 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01002122 goto exit;
2123}
2124/* END_CASE */
2125
2126/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002127void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00002128{
2129 /* Test each valid way of initializing the object, except for `= {0}`, as
2130 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2131 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002132 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002134 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
2135 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
2140 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
2141 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 TEST_EQUAL(psa_get_key_type(&func), 0);
2144 TEST_EQUAL(psa_get_key_type(&init), 0);
2145 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 TEST_EQUAL(psa_get_key_bits(&func), 0);
2148 TEST_EQUAL(psa_get_key_bits(&init), 0);
2149 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
2152 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
2153 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
2156 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
2157 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00002158}
2159/* END_CASE */
2160
2161/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162void mac_key_policy(int policy_usage_arg,
2163 int policy_alg_arg,
2164 int key_type_arg,
2165 data_t *key_data,
2166 int exercise_alg_arg,
2167 int expected_status_sign_arg,
2168 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02002169{
Ronald Cron5425a212020-08-04 14:58:35 +02002170 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00002172 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002173 psa_key_type_t key_type = key_type_arg;
2174 psa_algorithm_t policy_alg = policy_alg_arg;
2175 psa_algorithm_t exercise_alg = exercise_alg_arg;
2176 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02002178 psa_status_t expected_status_sign = expected_status_sign_arg;
2179 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 psa_set_key_usage_flags(&attributes, policy_usage);
2185 psa_set_key_algorithm(&attributes, policy_alg);
2186 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2189 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
2192 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2195 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002196
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002197 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002199 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
2201 input, 128,
2202 mac, PSA_MAC_MAX_SIZE, &mac_len),
2203 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002204
Neil Armstrong3af9b972022-02-07 12:20:21 +01002205 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 PSA_ASSERT(psa_mac_abort(&operation));
2207 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2208 if (status == PSA_SUCCESS) {
2209 status = psa_mac_update(&operation, input, 128);
2210 if (status == PSA_SUCCESS) {
2211 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
2212 &mac_len),
2213 expected_status_sign);
2214 } else {
2215 TEST_EQUAL(status, expected_status_sign);
2216 }
2217 } else {
2218 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002219 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01002221
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002222 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 status = psa_mac_verify(key, exercise_alg, input, 128,
2224 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2227 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2228 } else {
2229 TEST_EQUAL(status, expected_status_verify);
2230 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01002231
Neil Armstrong3af9b972022-02-07 12:20:21 +01002232 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2234 if (status == PSA_SUCCESS) {
2235 status = psa_mac_update(&operation, input, 128);
2236 if (status == PSA_SUCCESS) {
2237 status = psa_mac_verify_finish(&operation, mac, mac_len);
2238 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2239 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2240 } else {
2241 TEST_EQUAL(status, expected_status_verify);
2242 }
2243 } else {
2244 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002245 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 } else {
2247 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002248 }
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 memset(mac, 0, sizeof(mac));
2253 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2254 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002255
2256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 psa_mac_abort(&operation);
2258 psa_destroy_key(key);
2259 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002260}
2261/* END_CASE */
2262
2263/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264void cipher_key_policy(int policy_usage_arg,
2265 int policy_alg,
2266 int key_type,
2267 data_t *key_data,
2268 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002269{
Ronald Cron5425a212020-08-04 14:58:35 +02002270 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002272 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002273 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002274 size_t output_buffer_size = 0;
2275 size_t input_buffer_size = 0;
2276 size_t output_length = 0;
2277 uint8_t *output = NULL;
2278 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002279 psa_status_t status;
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
2282 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
2283 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002284
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002285 TEST_CALLOC(input, input_buffer_size);
2286 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 psa_set_key_usage_flags(&attributes, policy_usage);
2291 psa_set_key_algorithm(&attributes, policy_alg);
2292 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2295 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002296
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002297 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 TEST_EQUAL(policy_usage,
2299 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002300
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002301 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2303 output, output_buffer_size,
2304 &output_length);
2305 if (policy_alg == exercise_alg &&
2306 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2307 PSA_ASSERT(status);
2308 } else {
2309 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2310 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002311
2312 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2314 if (policy_alg == exercise_alg &&
2315 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2316 PSA_ASSERT(status);
2317 } else {
2318 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2319 }
2320 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002321
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002322 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2324 input, input_buffer_size,
2325 &output_length);
2326 if (policy_alg == exercise_alg &&
2327 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2328 PSA_ASSERT(status);
2329 } else {
2330 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2331 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002332
2333 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2335 if (policy_alg == exercise_alg &&
2336 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2337 PSA_ASSERT(status);
2338 } else {
2339 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2340 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002341
2342exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 psa_cipher_abort(&operation);
2344 mbedtls_free(input);
2345 mbedtls_free(output);
2346 psa_destroy_key(key);
2347 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002348}
2349/* END_CASE */
2350
2351/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352void aead_key_policy(int policy_usage_arg,
2353 int policy_alg,
2354 int key_type,
2355 data_t *key_data,
2356 int nonce_length_arg,
2357 int tag_length_arg,
2358 int exercise_alg,
2359 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002360{
Ronald Cron5425a212020-08-04 14:58:35 +02002361 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002363 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002364 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002365 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002366 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002368 size_t nonce_length = nonce_length_arg;
2369 unsigned char tag[16];
2370 size_t tag_length = tag_length_arg;
2371 size_t output_length;
2372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 TEST_LE_U(nonce_length, sizeof(nonce));
2374 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 psa_set_key_usage_flags(&attributes, policy_usage);
2379 psa_set_key_algorithm(&attributes, policy_alg);
2380 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2383 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002384
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002385 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 TEST_EQUAL(policy_usage,
2387 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002388
Neil Armstrong752d8112022-02-07 14:51:11 +01002389 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 status = psa_aead_encrypt(key, exercise_alg,
2391 nonce, nonce_length,
2392 NULL, 0,
2393 NULL, 0,
2394 tag, tag_length,
2395 &output_length);
2396 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2397 TEST_EQUAL(status, expected_status);
2398 } else {
2399 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2400 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002401
Neil Armstrong752d8112022-02-07 14:51:11 +01002402 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2404 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2405 TEST_EQUAL(status, expected_status);
2406 } else {
2407 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2408 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002409
2410 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 memset(tag, 0, sizeof(tag));
2412 status = psa_aead_decrypt(key, exercise_alg,
2413 nonce, nonce_length,
2414 NULL, 0,
2415 tag, tag_length,
2416 NULL, 0,
2417 &output_length);
2418 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2419 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2420 } else if (expected_status == PSA_SUCCESS) {
2421 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2422 } else {
2423 TEST_EQUAL(status, expected_status);
2424 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002425
Neil Armstrong752d8112022-02-07 14:51:11 +01002426 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 PSA_ASSERT(psa_aead_abort(&operation));
2428 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2429 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2430 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2431 } else {
2432 TEST_EQUAL(status, expected_status);
2433 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002434
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002435exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 PSA_ASSERT(psa_aead_abort(&operation));
2437 psa_destroy_key(key);
2438 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002439}
2440/* END_CASE */
2441
2442/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002443void asymmetric_encryption_key_policy(int policy_usage_arg,
2444 int policy_alg,
2445 int key_type,
2446 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002447 int exercise_alg,
2448 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002449{
Ronald Cron5425a212020-08-04 14:58:35 +02002450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002452 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002453 psa_status_t status;
2454 size_t key_bits;
2455 size_t buffer_length;
2456 unsigned char *buffer = NULL;
2457 size_t output_length;
2458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 psa_set_key_usage_flags(&attributes, policy_usage);
2462 psa_set_key_algorithm(&attributes, policy_alg);
2463 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002464
Valerio Settif202c292024-01-15 10:42:37 +01002465 if (use_opaque_key) {
2466 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2467 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2468 }
2469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2471 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002472
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002473 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 TEST_EQUAL(policy_usage,
2475 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2478 key_bits = psa_get_key_bits(&attributes);
2479 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2480 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002481 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 status = psa_asymmetric_encrypt(key, exercise_alg,
2484 NULL, 0,
2485 NULL, 0,
2486 buffer, buffer_length,
2487 &output_length);
2488 if (policy_alg == exercise_alg &&
2489 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2490 PSA_ASSERT(status);
2491 } else {
2492 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2493 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (buffer_length != 0) {
2496 memset(buffer, 0, buffer_length);
2497 }
2498 status = psa_asymmetric_decrypt(key, exercise_alg,
2499 buffer, buffer_length,
2500 NULL, 0,
2501 buffer, buffer_length,
2502 &output_length);
2503 if (policy_alg == exercise_alg &&
2504 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2505 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2506 } else {
2507 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2508 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002509
2510exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002511 /*
2512 * Key attributes may have been returned by psa_get_key_attributes()
2513 * thus reset them as required.
2514 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 psa_destroy_key(key);
2518 PSA_DONE();
2519 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002524void asymmetric_signature_key_policy(int policy_usage_arg,
2525 int policy_alg,
2526 int key_type,
2527 data_t *key_data,
2528 int exercise_alg,
2529 int payload_length_arg,
2530 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002531{
Ronald Cron5425a212020-08-04 14:58:35 +02002532 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002534 psa_key_usage_t policy_usage = policy_usage_arg;
2535 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002536 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002538 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2539 * compatible with the policy and `payload_length_arg` is supposed to be
2540 * a valid input length to sign. If `payload_length_arg <= 0`,
2541 * `exercise_alg` is supposed to be forbidden by the policy. */
2542 int compatible_alg = payload_length_arg > 0;
2543 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002545 size_t signature_length;
2546
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002547 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002548 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 TEST_EQUAL(expected_usage,
2550 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 psa_set_key_usage_flags(&attributes, policy_usage);
2555 psa_set_key_algorithm(&attributes, policy_alg);
2556 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2559 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 status = psa_sign_hash(key, exercise_alg,
2564 payload, payload_length,
2565 signature, sizeof(signature),
2566 &signature_length);
2567 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2568 PSA_ASSERT(status);
2569 } else {
2570 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2571 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 memset(signature, 0, sizeof(signature));
2574 status = psa_verify_hash(key, exercise_alg,
2575 payload, payload_length,
2576 signature, sizeof(signature));
2577 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2578 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2579 } else {
2580 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2581 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2584 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2585 status = psa_sign_message(key, exercise_alg,
2586 payload, payload_length,
2587 signature, sizeof(signature),
2588 &signature_length);
2589 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2590 PSA_ASSERT(status);
2591 } else {
2592 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2593 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 memset(signature, 0, sizeof(signature));
2596 status = psa_verify_message(key, exercise_alg,
2597 payload, payload_length,
2598 signature, sizeof(signature));
2599 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2600 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2601 } else {
2602 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2603 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002604 }
2605
Gilles Peskined5b33222018-06-18 22:20:03 +02002606exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 psa_destroy_key(key);
2608 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002609}
2610/* END_CASE */
2611
Janos Follathba3fab92019-06-11 14:50:16 +01002612/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002613void derive_key_policy(int policy_usage,
2614 int policy_alg,
2615 int key_type,
2616 data_t *key_data,
2617 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002618{
Ronald Cron5425a212020-08-04 14:58:35 +02002619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002621 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002622 psa_status_t status;
2623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 psa_set_key_usage_flags(&attributes, policy_usage);
2627 psa_set_key_algorithm(&attributes, policy_alg);
2628 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2631 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2636 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2637 PSA_ASSERT(psa_key_derivation_input_bytes(
2638 &operation,
2639 PSA_KEY_DERIVATION_INPUT_SEED,
2640 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002641 }
Janos Follathba3fab92019-06-11 14:50:16 +01002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 status = psa_key_derivation_input_key(&operation,
2644 PSA_KEY_DERIVATION_INPUT_SECRET,
2645 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (policy_alg == exercise_alg &&
2648 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2649 PSA_ASSERT(status);
2650 } else {
2651 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2652 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002653
2654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 psa_key_derivation_abort(&operation);
2656 psa_destroy_key(key);
2657 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002658}
2659/* END_CASE */
2660
2661/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662void agreement_key_policy(int policy_usage,
2663 int policy_alg,
2664 int key_type_arg,
2665 data_t *key_data,
2666 int exercise_alg,
2667 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002668{
Ronald Cron5425a212020-08-04 14:58:35 +02002669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002671 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002672 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002673 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002674 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 psa_set_key_usage_flags(&attributes, policy_usage);
2679 psa_set_key_algorithm(&attributes, policy_alg);
2680 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2683 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Ryan Everett73e4ea32024-03-12 16:29:55 +00002686 status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002689
2690exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 psa_key_derivation_abort(&operation);
2692 psa_destroy_key(key);
2693 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002694}
2695/* END_CASE */
2696
2697/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002698void key_policy_alg2(int key_type_arg, data_t *key_data,
2699 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002700{
Ronald Cron5425a212020-08-04 14:58:35 +02002701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002702 psa_key_type_t key_type = key_type_arg;
2703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2704 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2705 psa_key_usage_t usage = usage_arg;
2706 psa_algorithm_t alg = alg_arg;
2707 psa_algorithm_t alg2 = alg2_arg;
2708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 psa_set_key_usage_flags(&attributes, usage);
2712 psa_set_key_algorithm(&attributes, alg);
2713 psa_set_key_enrollment_algorithm(&attributes, alg2);
2714 psa_set_key_type(&attributes, key_type);
2715 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2716 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002717
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002718 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 usage = mbedtls_test_update_key_usage_flags(usage);
2720 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2721 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2722 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2723 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002724
Ryan Everett0a271fd2024-03-12 16:34:02 +00002725 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002728 if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002731
2732exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002733 /*
2734 * Key attributes may have been returned by psa_get_key_attributes()
2735 * thus reset them as required.
2736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 psa_destroy_key(key);
2740 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002741}
2742/* END_CASE */
2743
2744/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745void raw_agreement_key_policy(int policy_usage,
2746 int policy_alg,
2747 int key_type_arg,
2748 data_t *key_data,
2749 int exercise_alg,
2750 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002751{
Ronald Cron5425a212020-08-04 14:58:35 +02002752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002754 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002755 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002756 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002757 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 psa_set_key_usage_flags(&attributes, policy_usage);
2762 psa_set_key_algorithm(&attributes, policy_alg);
2763 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2766 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002767
Ryan Everett81630282024-03-12 16:21:12 +00002768 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key, 0);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002771
2772exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 psa_key_derivation_abort(&operation);
2774 psa_destroy_key(key);
2775 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002776}
2777/* END_CASE */
2778
2779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780void copy_success(int source_usage_arg,
2781 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002782 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 int type_arg, data_t *material,
2784 int copy_attributes,
2785 int target_usage_arg,
2786 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002787 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 int expected_usage_arg,
2789 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002790{
Gilles Peskineca25db92019-04-19 11:43:08 +02002791 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2792 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002793 psa_key_usage_t expected_usage = expected_usage_arg;
2794 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002795 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302796 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2797 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002798 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2799 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002800 uint8_t *export_buffer = NULL;
2801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002803
Gilles Peskineca25db92019-04-19 11:43:08 +02002804 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2806 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2807 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2808 psa_set_key_type(&source_attributes, type_arg);
2809 psa_set_key_lifetime(&source_attributes, source_lifetime);
2810 PSA_ASSERT(psa_import_key(&source_attributes,
2811 material->x, material->len,
2812 &source_key));
2813 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002814
Gilles Peskineca25db92019-04-19 11:43:08 +02002815 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002817 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002818 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 if (target_usage_arg != -1) {
2822 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2823 }
2824 if (target_alg_arg != -1) {
2825 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2826 }
2827 if (target_alg2_arg != -1) {
2828 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2829 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002830
Archana8a180362021-07-05 02:18:48 +05302831
Gilles Peskine57ab7212019-01-28 13:03:09 +01002832 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 PSA_ASSERT(psa_copy_key(source_key,
2834 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002835
2836 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002838
2839 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2841 TEST_EQUAL(psa_get_key_type(&source_attributes),
2842 psa_get_key_type(&target_attributes));
2843 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2844 psa_get_key_bits(&target_attributes));
2845 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2846 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2847 TEST_EQUAL(expected_alg2,
2848 psa_get_key_enrollment_algorithm(&target_attributes));
2849 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002850 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002851 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2853 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002854 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002855 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002856 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 if (!psa_key_lifetime_is_external(target_lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00002859 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) {
Archana8a180362021-07-05 02:18:48 +05302860 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002862 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) {
Archana8a180362021-07-05 02:18:48 +05302863 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 }
Archana8a180362021-07-05 02:18:48 +05302865 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002868
2869exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002870 /*
2871 * Source and target key attributes may have been returned by
2872 * psa_get_key_attributes() thus reset them as required.
2873 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 psa_reset_key_attributes(&source_attributes);
2875 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 PSA_DONE();
2878 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002879}
2880/* END_CASE */
2881
2882/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883void copy_fail(int source_usage_arg,
2884 int source_alg_arg, int source_alg2_arg,
2885 int source_lifetime_arg,
2886 int type_arg, data_t *material,
2887 int target_type_arg, int target_bits_arg,
2888 int target_usage_arg,
2889 int target_alg_arg, int target_alg2_arg,
2890 int target_id_arg, int target_lifetime_arg,
2891 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002892{
2893 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2894 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002895 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2896 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002900
2901 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2903 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2904 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2905 psa_set_key_type(&source_attributes, type_arg);
2906 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2907 PSA_ASSERT(psa_import_key(&source_attributes,
2908 material->x, material->len,
2909 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002910
2911 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 psa_set_key_id(&target_attributes, key_id);
2913 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2914 psa_set_key_type(&target_attributes, target_type_arg);
2915 psa_set_key_bits(&target_attributes, target_bits_arg);
2916 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2917 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2918 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002919
2920 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 TEST_EQUAL(psa_copy_key(source_key,
2922 &target_attributes, &target_key),
2923 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002926
Gilles Peskine4a644642019-05-03 17:14:08 +02002927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 psa_reset_key_attributes(&source_attributes);
2929 psa_reset_key_attributes(&target_attributes);
2930 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002931}
2932/* END_CASE */
2933
2934/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002936{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002937 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002938 /* Test each valid way of initializing the object, except for `= {0}`, as
2939 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2940 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002941 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002943 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2944 psa_hash_operation_t zero;
2945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002947
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002948 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2950 PSA_ERROR_BAD_STATE);
2951 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2952 PSA_ERROR_BAD_STATE);
2953 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2954 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002955
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002956 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 PSA_ASSERT(psa_hash_abort(&func));
2958 PSA_ASSERT(psa_hash_abort(&init));
2959 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002960}
2961/* END_CASE */
2962
2963/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002964void hash_setup(int alg_arg,
2965 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002966{
2967 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002968 uint8_t *output = NULL;
2969 size_t output_size = 0;
2970 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002971 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002972 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002973 psa_status_t status;
2974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976
Neil Armstrongedb20862022-02-07 15:47:44 +01002977 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002979 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 status = psa_hash_compute(alg, NULL, 0,
2982 output, output_size, &output_length);
2983 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002984
2985 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 status = psa_hash_setup(&operation, alg);
2987 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002988
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002989 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002991
2992 /* If setup failed, reproduce the failure, so as to
2993 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (status != PSA_SUCCESS) {
2995 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2996 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002997
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002998 /* Now the operation object should be reusable. */
2999#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
3001 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003002#endif
3003
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003004exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 mbedtls_free(output);
3006 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003007}
3008/* END_CASE */
3009
3010/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011void hash_compute_fail(int alg_arg, data_t *input,
3012 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01003013{
3014 psa_algorithm_t alg = alg_arg;
3015 uint8_t *output = NULL;
3016 size_t output_size = output_size_arg;
3017 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003018 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01003019 psa_status_t expected_status = expected_status_arg;
3020 psa_status_t status;
3021
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003022 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01003025
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003026 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 status = psa_hash_compute(alg, input->x, input->len,
3028 output, output_size, &output_length);
3029 TEST_EQUAL(status, expected_status);
3030 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003031
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003032 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 status = psa_hash_setup(&operation, alg);
3034 if (status == PSA_SUCCESS) {
3035 status = psa_hash_update(&operation, input->x, input->len);
3036 if (status == PSA_SUCCESS) {
3037 status = psa_hash_finish(&operation, output, output_size,
3038 &output_length);
3039 if (status == PSA_SUCCESS) {
3040 TEST_LE_U(output_length, output_size);
3041 } else {
3042 TEST_EQUAL(status, expected_status);
3043 }
3044 } else {
3045 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003046 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 } else {
3048 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003049 }
3050
Gilles Peskine0a749c82019-11-28 19:33:58 +01003051exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 PSA_ASSERT(psa_hash_abort(&operation));
3053 mbedtls_free(output);
3054 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01003055}
3056/* END_CASE */
3057
3058/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003059void hash_compare_fail(int alg_arg, data_t *input,
3060 data_t *reference_hash,
3061 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01003062{
3063 psa_algorithm_t alg = alg_arg;
3064 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01003065 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01003066 psa_status_t status;
3067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01003069
Neil Armstrong55a1be12022-02-07 11:23:20 +01003070 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 status = psa_hash_compare(alg, input->x, input->len,
3072 reference_hash->x, reference_hash->len);
3073 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01003074
Neil Armstrong55a1be12022-02-07 11:23:20 +01003075 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 status = psa_hash_setup(&operation, alg);
3077 if (status == PSA_SUCCESS) {
3078 status = psa_hash_update(&operation, input->x, input->len);
3079 if (status == PSA_SUCCESS) {
3080 status = psa_hash_verify(&operation, reference_hash->x,
3081 reference_hash->len);
3082 TEST_EQUAL(status, expected_status);
3083 } else {
3084 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01003085 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 } else {
3087 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01003088 }
3089
Gilles Peskine88e08462020-01-28 20:43:00 +01003090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 PSA_ASSERT(psa_hash_abort(&operation));
3092 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01003093}
3094/* END_CASE */
3095
3096/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003097void hash_compute_compare(int alg_arg, data_t *input,
3098 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01003099{
3100 psa_algorithm_t alg = alg_arg;
3101 uint8_t output[PSA_HASH_MAX_SIZE + 1];
3102 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01003103 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01003104 size_t i;
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01003107
Neil Armstrongca30a002022-02-07 11:40:23 +01003108 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
3110 output, PSA_HASH_LENGTH(alg),
3111 &output_length));
3112 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003113 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003114 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003115
Neil Armstrongca30a002022-02-07 11:40:23 +01003116 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 PSA_ASSERT(psa_hash_setup(&operation, alg));
3118 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3119 PSA_ASSERT(psa_hash_finish(&operation, output,
3120 PSA_HASH_LENGTH(alg),
3121 &output_length));
3122 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003123 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003124 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01003125
3126 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
3128 output, sizeof(output),
3129 &output_length));
3130 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003131 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003132 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003133
Neil Armstrongca30a002022-02-07 11:40:23 +01003134 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 PSA_ASSERT(psa_hash_setup(&operation, alg));
3136 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3137 PSA_ASSERT(psa_hash_finish(&operation, output,
3138 sizeof(output), &output_length));
3139 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003140 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003141 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01003142
3143 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
3145 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01003146
Neil Armstrongca30a002022-02-07 11:40:23 +01003147 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 PSA_ASSERT(psa_hash_setup(&operation, alg));
3149 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3150 PSA_ASSERT(psa_hash_verify(&operation, output,
3151 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01003152
3153 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3155 output, output_length + 1),
3156 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003157
Neil Armstrongca30a002022-02-07 11:40:23 +01003158 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 PSA_ASSERT(psa_hash_setup(&operation, alg));
3160 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3161 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
3162 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003163
3164 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3166 output, output_length - 1),
3167 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003168
Neil Armstrongca30a002022-02-07 11:40:23 +01003169 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_ASSERT(psa_hash_setup(&operation, alg));
3171 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3172 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
3173 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003174
Gilles Peskine0a749c82019-11-28 19:33:58 +01003175 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 for (i = 0; i < output_length; i++) {
3177 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003178 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01003179
3180 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3182 output, output_length),
3183 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003184
3185 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 PSA_ASSERT(psa_hash_setup(&operation, alg));
3187 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3188 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
3189 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003190
Gilles Peskine0a749c82019-11-28 19:33:58 +01003191 output[i] ^= 1;
3192 }
3193
3194exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 PSA_ASSERT(psa_hash_abort(&operation));
3196 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01003197}
3198/* END_CASE */
3199
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003200/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003201void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02003202{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003203 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02003204 unsigned char input[] = "";
3205 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003206 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02003207 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3208 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
3210 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003211 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02003212 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00003213 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02003214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02003216
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003217 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 PSA_ASSERT(psa_hash_setup(&operation, alg));
3219 ASSERT_OPERATION_IS_ACTIVE(operation);
3220 TEST_EQUAL(psa_hash_setup(&operation, alg),
3221 PSA_ERROR_BAD_STATE);
3222 ASSERT_OPERATION_IS_INACTIVE(operation);
3223 PSA_ASSERT(psa_hash_abort(&operation));
3224 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003225
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003226 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3228 PSA_ERROR_BAD_STATE);
3229 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003230
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003231 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01003233 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 ASSERT_OPERATION_IS_ACTIVE(operation);
3235 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3236 PSA_ERROR_BAD_STATE);
3237 ASSERT_OPERATION_IS_INACTIVE(operation);
3238 PSA_ASSERT(psa_hash_abort(&operation));
3239 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003240
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003241 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 PSA_ASSERT(psa_hash_setup(&operation, alg));
3243 PSA_ASSERT(psa_hash_finish(&operation,
3244 hash, sizeof(hash), &hash_len));
3245 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3246 PSA_ERROR_BAD_STATE);
3247 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003248
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003249 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 TEST_EQUAL(psa_hash_verify(&operation,
3251 valid_hash, sizeof(valid_hash)),
3252 PSA_ERROR_BAD_STATE);
3253 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003254
3255 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 PSA_ASSERT(psa_hash_setup(&operation, alg));
3257 PSA_ASSERT(psa_hash_finish(&operation,
3258 hash, sizeof(hash), &hash_len));
3259 TEST_EQUAL(psa_hash_verify(&operation,
3260 valid_hash, sizeof(valid_hash)),
3261 PSA_ERROR_BAD_STATE);
3262 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003263
3264 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 PSA_ASSERT(psa_hash_setup(&operation, alg));
3266 ASSERT_OPERATION_IS_ACTIVE(operation);
3267 PSA_ASSERT(psa_hash_verify(&operation,
3268 valid_hash, sizeof(valid_hash)));
3269 ASSERT_OPERATION_IS_INACTIVE(operation);
3270 TEST_EQUAL(psa_hash_verify(&operation,
3271 valid_hash, sizeof(valid_hash)),
3272 PSA_ERROR_BAD_STATE);
3273 ASSERT_OPERATION_IS_INACTIVE(operation);
3274 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003275
3276 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 TEST_EQUAL(psa_hash_finish(&operation,
3278 hash, sizeof(hash), &hash_len),
3279 PSA_ERROR_BAD_STATE);
3280 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003281
3282 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 PSA_ASSERT(psa_hash_setup(&operation, alg));
3284 PSA_ASSERT(psa_hash_finish(&operation,
3285 hash, sizeof(hash), &hash_len));
3286 TEST_EQUAL(psa_hash_finish(&operation,
3287 hash, sizeof(hash), &hash_len),
3288 PSA_ERROR_BAD_STATE);
3289 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003290
3291 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 PSA_ASSERT(psa_hash_setup(&operation, alg));
3293 PSA_ASSERT(psa_hash_verify(&operation,
3294 valid_hash, sizeof(valid_hash)));
3295 TEST_EQUAL(psa_hash_finish(&operation,
3296 hash, sizeof(hash), &hash_len),
3297 PSA_ERROR_BAD_STATE);
3298 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003299
3300exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003302}
3303/* END_CASE */
3304
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003305/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003307{
3308 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003309 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3310 * appended to it */
3311 unsigned char hash[] = {
3312 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3313 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3315 };
3316 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003317 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003320
itayzafrir27e69452018-11-01 14:26:34 +02003321 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 PSA_ASSERT(psa_hash_setup(&operation, alg));
3323 ASSERT_OPERATION_IS_ACTIVE(operation);
3324 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3325 PSA_ERROR_INVALID_SIGNATURE);
3326 ASSERT_OPERATION_IS_INACTIVE(operation);
3327 PSA_ASSERT(psa_hash_abort(&operation));
3328 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003329
itayzafrir27e69452018-11-01 14:26:34 +02003330 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 PSA_ASSERT(psa_hash_setup(&operation, alg));
3332 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3333 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003334
itayzafrir27e69452018-11-01 14:26:34 +02003335 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 PSA_ASSERT(psa_hash_setup(&operation, alg));
3337 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3338 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003339
itayzafrirec93d302018-10-18 18:01:10 +03003340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003342}
3343/* END_CASE */
3344
Ronald Cronee414c72021-03-18 18:50:08 +01003345/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003347{
3348 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003349 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003351 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003352 size_t hash_len;
3353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003355
itayzafrir58028322018-10-25 10:22:01 +03003356 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 PSA_ASSERT(psa_hash_setup(&operation, alg));
3358 TEST_EQUAL(psa_hash_finish(&operation,
3359 hash, expected_size - 1, &hash_len),
3360 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003361
3362exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003364}
3365/* END_CASE */
3366
Ronald Cronee414c72021-03-18 18:50:08 +01003367/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003368void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003369{
3370 psa_algorithm_t alg = PSA_ALG_SHA_256;
3371 unsigned char hash[PSA_HASH_MAX_SIZE];
3372 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3373 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3374 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3375 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3376 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3377 size_t hash_len;
3378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 PSA_ASSERT(psa_crypto_init());
3380 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3383 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3384 PSA_ASSERT(psa_hash_finish(&op_finished,
3385 hash, sizeof(hash), &hash_len));
3386 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3387 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3390 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3393 PSA_ASSERT(psa_hash_finish(&op_init,
3394 hash, sizeof(hash), &hash_len));
3395 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3396 PSA_ASSERT(psa_hash_finish(&op_finished,
3397 hash, sizeof(hash), &hash_len));
3398 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3399 PSA_ASSERT(psa_hash_finish(&op_aborted,
3400 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003401
3402exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 psa_hash_abort(&op_source);
3404 psa_hash_abort(&op_init);
3405 psa_hash_abort(&op_setup);
3406 psa_hash_abort(&op_finished);
3407 psa_hash_abort(&op_aborted);
3408 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003409}
3410/* END_CASE */
3411
Ronald Cronee414c72021-03-18 18:50:08 +01003412/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003413void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003414{
3415 psa_algorithm_t alg = PSA_ALG_SHA_256;
3416 unsigned char hash[PSA_HASH_MAX_SIZE];
3417 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3418 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3419 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3420 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3421 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3422 size_t hash_len;
3423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003425
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3427 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3428 PSA_ASSERT(psa_hash_finish(&op_finished,
3429 hash, sizeof(hash), &hash_len));
3430 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3431 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003432
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3434 PSA_ASSERT(psa_hash_finish(&op_target,
3435 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003436
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3438 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3439 PSA_ERROR_BAD_STATE);
3440 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3441 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003442
3443exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 psa_hash_abort(&op_target);
3445 psa_hash_abort(&op_init);
3446 psa_hash_abort(&op_setup);
3447 psa_hash_abort(&op_finished);
3448 psa_hash_abort(&op_aborted);
3449 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003450}
3451/* END_CASE */
3452
itayzafrir58028322018-10-25 10:22:01 +03003453/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003454void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003455{
Jaeden Amero252ef282019-02-15 14:05:35 +00003456 const uint8_t input[1] = { 0 };
3457
Jaeden Amero769ce272019-01-04 11:48:03 +00003458 /* Test each valid way of initializing the object, except for `= {0}`, as
3459 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3460 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003461 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003463 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3464 psa_mac_operation_t zero;
3465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003467
Jaeden Amero252ef282019-02-15 14:05:35 +00003468 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 TEST_EQUAL(psa_mac_update(&func,
3470 input, sizeof(input)),
3471 PSA_ERROR_BAD_STATE);
3472 TEST_EQUAL(psa_mac_update(&init,
3473 input, sizeof(input)),
3474 PSA_ERROR_BAD_STATE);
3475 TEST_EQUAL(psa_mac_update(&zero,
3476 input, sizeof(input)),
3477 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003478
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003479 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 PSA_ASSERT(psa_mac_abort(&func));
3481 PSA_ASSERT(psa_mac_abort(&init));
3482 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003483}
3484/* END_CASE */
3485
3486/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003487void mac_setup(int key_type_arg,
3488 data_t *key,
3489 int alg_arg,
3490 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003491{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003492 psa_key_type_t key_type = key_type_arg;
3493 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003494 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003495 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003496 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3497#if defined(KNOWN_SUPPORTED_MAC_ALG)
3498 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3499#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003500
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3504 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003505 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 }
3507 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003508
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003509 /* The operation object should be reusable. */
3510#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3512 smoke_test_key_data,
3513 sizeof(smoke_test_key_data),
3514 KNOWN_SUPPORTED_MAC_ALG,
3515 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003516 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 }
3518 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003519#endif
3520
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003523}
3524/* END_CASE */
3525
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003526/* 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 +01003527void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003528{
Ronald Cron5425a212020-08-04 14:58:35 +02003529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003530 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3531 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003532 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003533 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3534 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3536 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003538 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3539 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3540 size_t sign_mac_length = 0;
3541 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3542 const uint8_t verify_mac[] = {
3543 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3544 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3546 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 PSA_ASSERT(psa_crypto_init());
3549 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3550 psa_set_key_algorithm(&attributes, alg);
3551 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003552
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3554 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003555
Jaeden Amero252ef282019-02-15 14:05:35 +00003556 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3558 PSA_ERROR_BAD_STATE);
3559 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003560
3561 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3563 &sign_mac_length),
3564 PSA_ERROR_BAD_STATE);
3565 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003566
3567 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 TEST_EQUAL(psa_mac_verify_finish(&operation,
3569 verify_mac, sizeof(verify_mac)),
3570 PSA_ERROR_BAD_STATE);
3571 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003572
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003573 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003574 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3575 ASSERT_OPERATION_IS_ACTIVE(operation);
3576 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3577 PSA_ERROR_BAD_STATE);
3578 ASSERT_OPERATION_IS_INACTIVE(operation);
3579 PSA_ASSERT(psa_mac_abort(&operation));
3580 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003581
Jaeden Amero252ef282019-02-15 14:05:35 +00003582 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3584 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3585 PSA_ASSERT(psa_mac_sign_finish(&operation,
3586 sign_mac, sizeof(sign_mac),
3587 &sign_mac_length));
3588 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3589 PSA_ERROR_BAD_STATE);
3590 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003591
3592 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3594 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3595 PSA_ASSERT(psa_mac_verify_finish(&operation,
3596 verify_mac, sizeof(verify_mac)));
3597 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3598 PSA_ERROR_BAD_STATE);
3599 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003600
3601 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3603 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3604 PSA_ASSERT(psa_mac_sign_finish(&operation,
3605 sign_mac, sizeof(sign_mac),
3606 &sign_mac_length));
3607 TEST_EQUAL(psa_mac_sign_finish(&operation,
3608 sign_mac, sizeof(sign_mac),
3609 &sign_mac_length),
3610 PSA_ERROR_BAD_STATE);
3611 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003612
3613 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3615 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3616 PSA_ASSERT(psa_mac_verify_finish(&operation,
3617 verify_mac, sizeof(verify_mac)));
3618 TEST_EQUAL(psa_mac_verify_finish(&operation,
3619 verify_mac, sizeof(verify_mac)),
3620 PSA_ERROR_BAD_STATE);
3621 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003622
3623 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003624 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3625 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3626 ASSERT_OPERATION_IS_ACTIVE(operation);
3627 TEST_EQUAL(psa_mac_verify_finish(&operation,
3628 verify_mac, sizeof(verify_mac)),
3629 PSA_ERROR_BAD_STATE);
3630 ASSERT_OPERATION_IS_INACTIVE(operation);
3631 PSA_ASSERT(psa_mac_abort(&operation));
3632 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003633
3634 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003635 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3636 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3637 ASSERT_OPERATION_IS_ACTIVE(operation);
3638 TEST_EQUAL(psa_mac_sign_finish(&operation,
3639 sign_mac, sizeof(sign_mac),
3640 &sign_mac_length),
3641 PSA_ERROR_BAD_STATE);
3642 ASSERT_OPERATION_IS_INACTIVE(operation);
3643 PSA_ASSERT(psa_mac_abort(&operation));
3644 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003645
Gilles Peskine449bd832023-01-11 14:50:10 +01003646 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003647
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003648exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003649 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003650}
3651/* END_CASE */
3652
3653/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654void mac_sign_verify_multi(int key_type_arg,
3655 data_t *key_data,
3656 int alg_arg,
3657 data_t *input,
3658 int is_verify,
3659 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003660{
3661 size_t data_part_len = 0;
3662
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003664 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003666
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 if (mac_multipart_internal_func(key_type_arg, key_data,
3668 alg_arg,
3669 input, data_part_len,
3670 expected_mac,
3671 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003672 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003674
3675 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003677
Gilles Peskine449bd832023-01-11 14:50:10 +01003678 if (mac_multipart_internal_func(key_type_arg, key_data,
3679 alg_arg,
3680 input, data_part_len,
3681 expected_mac,
3682 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003683 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003684 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003685 }
3686
3687 /* Goto is required to silence warnings about unused labels, as we
3688 * don't actually do any test assertions in this function. */
3689 goto exit;
3690}
3691/* END_CASE */
3692
3693/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003694void mac_sign(int key_type_arg,
3695 data_t *key_data,
3696 int alg_arg,
3697 data_t *input,
3698 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003699{
Ronald Cron5425a212020-08-04 14:58:35 +02003700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003703 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003705 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003706 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003708 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003709 const size_t output_sizes_to_test[] = {
3710 0,
3711 1,
3712 expected_mac->len - 1,
3713 expected_mac->len,
3714 expected_mac->len + 1,
3715 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003716
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003718 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003720
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003722
Gilles Peskine449bd832023-01-11 14:50:10 +01003723 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3724 psa_set_key_algorithm(&attributes, alg);
3725 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003726
Gilles Peskine449bd832023-01-11 14:50:10 +01003727 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3728 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003729
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003731 const size_t output_size = output_sizes_to_test[i];
3732 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003733 (output_size >= expected_mac->len ? PSA_SUCCESS :
3734 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003735
Gilles Peskine449bd832023-01-11 14:50:10 +01003736 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003737 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003738
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003739 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 TEST_EQUAL(psa_mac_compute(key, alg,
3741 input->x, input->len,
3742 actual_mac, output_size, &mac_length),
3743 expected_status);
3744 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003745 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003746 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003747 }
3748
Gilles Peskine449bd832023-01-11 14:50:10 +01003749 if (output_size > 0) {
3750 memset(actual_mac, 0, output_size);
3751 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003752
3753 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3755 PSA_ASSERT(psa_mac_update(&operation,
3756 input->x, input->len));
3757 TEST_EQUAL(psa_mac_sign_finish(&operation,
3758 actual_mac, output_size,
3759 &mac_length),
3760 expected_status);
3761 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003762
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003764 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003765 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003766 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003768 actual_mac = NULL;
3769 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003770
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003771exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 psa_mac_abort(&operation);
3773 psa_destroy_key(key);
3774 PSA_DONE();
3775 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003776}
3777/* END_CASE */
3778
3779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003780void mac_verify(int key_type_arg,
3781 data_t *key_data,
3782 int alg_arg,
3783 data_t *input,
3784 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003785{
Ronald Cron5425a212020-08-04 14:58:35 +02003786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003787 psa_key_type_t key_type = key_type_arg;
3788 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003789 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003791 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003792
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003794
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003796
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3798 psa_set_key_algorithm(&attributes, alg);
3799 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003800
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3802 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003803
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003804 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3806 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003807
3808 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3810 PSA_ASSERT(psa_mac_update(&operation,
3811 input->x, input->len));
3812 PSA_ASSERT(psa_mac_verify_finish(&operation,
3813 expected_mac->x,
3814 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003815
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003816 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 TEST_EQUAL(psa_mac_verify(key, alg,
3818 input->x, input->len,
3819 expected_mac->x,
3820 expected_mac->len - 1),
3821 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003822
3823 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3825 PSA_ASSERT(psa_mac_update(&operation,
3826 input->x, input->len));
3827 TEST_EQUAL(psa_mac_verify_finish(&operation,
3828 expected_mac->x,
3829 expected_mac->len - 1),
3830 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003831
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003832 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003833 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3835 TEST_EQUAL(psa_mac_verify(key, alg,
3836 input->x, input->len,
3837 perturbed_mac, expected_mac->len + 1),
3838 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003839
3840 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3842 PSA_ASSERT(psa_mac_update(&operation,
3843 input->x, input->len));
3844 TEST_EQUAL(psa_mac_verify_finish(&operation,
3845 perturbed_mac,
3846 expected_mac->len + 1),
3847 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003848
3849 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 for (size_t i = 0; i < expected_mac->len; i++) {
3851 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003852 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003853
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 TEST_EQUAL(psa_mac_verify(key, alg,
3855 input->x, input->len,
3856 perturbed_mac, expected_mac->len),
3857 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003858
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3860 PSA_ASSERT(psa_mac_update(&operation,
3861 input->x, input->len));
3862 TEST_EQUAL(psa_mac_verify_finish(&operation,
3863 perturbed_mac,
3864 expected_mac->len),
3865 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003866 perturbed_mac[i] ^= 1;
3867 }
3868
Gilles Peskine8c9def32018-02-08 10:02:12 +01003869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 psa_mac_abort(&operation);
3871 psa_destroy_key(key);
3872 PSA_DONE();
3873 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003874}
3875/* END_CASE */
3876
3877/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003878void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003879{
Jaeden Ameroab439972019-02-15 14:12:05 +00003880 const uint8_t input[1] = { 0 };
3881 unsigned char output[1] = { 0 };
3882 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003883 /* Test each valid way of initializing the object, except for `= {0}`, as
3884 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3885 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003886 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003888 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3889 psa_cipher_operation_t zero;
3890
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003892
Jaeden Ameroab439972019-02-15 14:12:05 +00003893 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 TEST_EQUAL(psa_cipher_update(&func,
3895 input, sizeof(input),
3896 output, sizeof(output),
3897 &output_length),
3898 PSA_ERROR_BAD_STATE);
3899 TEST_EQUAL(psa_cipher_update(&init,
3900 input, sizeof(input),
3901 output, sizeof(output),
3902 &output_length),
3903 PSA_ERROR_BAD_STATE);
3904 TEST_EQUAL(psa_cipher_update(&zero,
3905 input, sizeof(input),
3906 output, sizeof(output),
3907 &output_length),
3908 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003909
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003910 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 PSA_ASSERT(psa_cipher_abort(&func));
3912 PSA_ASSERT(psa_cipher_abort(&init));
3913 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003914}
3915/* END_CASE */
3916
3917/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003918void cipher_setup(int key_type_arg,
3919 data_t *key,
3920 int alg_arg,
3921 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003922{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003923 psa_key_type_t key_type = key_type_arg;
3924 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003925 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003926 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003927 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003928#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003929 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3930#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003931
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003933
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3935 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003936 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 }
3938 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003939
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003940 /* The operation object should be reusable. */
3941#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3943 smoke_test_key_data,
3944 sizeof(smoke_test_key_data),
3945 KNOWN_SUPPORTED_CIPHER_ALG,
3946 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 }
3949 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003950#endif
3951
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003952exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 psa_cipher_abort(&operation);
3954 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003955}
3956/* END_CASE */
3957
Ronald Cronee414c72021-03-18 18:50:08 +01003958/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003959void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003960{
Ronald Cron5425a212020-08-04 14:58:35 +02003961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003962 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3963 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003965 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003966 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003967 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003968 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 0xaa, 0xaa, 0xaa, 0xaa
3970 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003971 const uint8_t text[] = {
3972 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 0xbb, 0xbb, 0xbb, 0xbb
3974 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003975 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003976 size_t length = 0;
3977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 PSA_ASSERT(psa_crypto_init());
3979 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3980 psa_set_key_algorithm(&attributes, alg);
3981 psa_set_key_type(&attributes, key_type);
3982 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3983 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003984
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003985 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3987 ASSERT_OPERATION_IS_ACTIVE(operation);
3988 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3989 PSA_ERROR_BAD_STATE);
3990 ASSERT_OPERATION_IS_INACTIVE(operation);
3991 PSA_ASSERT(psa_cipher_abort(&operation));
3992 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003993
3994 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3996 ASSERT_OPERATION_IS_ACTIVE(operation);
3997 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3998 PSA_ERROR_BAD_STATE);
3999 ASSERT_OPERATION_IS_INACTIVE(operation);
4000 PSA_ASSERT(psa_cipher_abort(&operation));
4001 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004002
Jaeden Ameroab439972019-02-15 14:12:05 +00004003 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 TEST_EQUAL(psa_cipher_generate_iv(&operation,
4005 buffer, sizeof(buffer),
4006 &length),
4007 PSA_ERROR_BAD_STATE);
4008 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004009
4010 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4012 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4013 buffer, sizeof(buffer),
4014 &length));
4015 ASSERT_OPERATION_IS_ACTIVE(operation);
4016 TEST_EQUAL(psa_cipher_generate_iv(&operation,
4017 buffer, sizeof(buffer),
4018 &length),
4019 PSA_ERROR_BAD_STATE);
4020 ASSERT_OPERATION_IS_INACTIVE(operation);
4021 PSA_ASSERT(psa_cipher_abort(&operation));
4022 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004023
4024 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4026 PSA_ASSERT(psa_cipher_set_iv(&operation,
4027 iv, sizeof(iv)));
4028 TEST_EQUAL(psa_cipher_generate_iv(&operation,
4029 buffer, sizeof(buffer),
4030 &length),
4031 PSA_ERROR_BAD_STATE);
4032 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004033
4034 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 TEST_EQUAL(psa_cipher_set_iv(&operation,
4036 iv, sizeof(iv)),
4037 PSA_ERROR_BAD_STATE);
4038 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004039
4040 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4042 PSA_ASSERT(psa_cipher_set_iv(&operation,
4043 iv, sizeof(iv)));
4044 ASSERT_OPERATION_IS_ACTIVE(operation);
4045 TEST_EQUAL(psa_cipher_set_iv(&operation,
4046 iv, sizeof(iv)),
4047 PSA_ERROR_BAD_STATE);
4048 ASSERT_OPERATION_IS_INACTIVE(operation);
4049 PSA_ASSERT(psa_cipher_abort(&operation));
4050 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004051
4052 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4054 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4055 buffer, sizeof(buffer),
4056 &length));
4057 TEST_EQUAL(psa_cipher_set_iv(&operation,
4058 iv, sizeof(iv)),
4059 PSA_ERROR_BAD_STATE);
4060 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004061
4062 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 TEST_EQUAL(psa_cipher_update(&operation,
4064 text, sizeof(text),
4065 buffer, sizeof(buffer),
4066 &length),
4067 PSA_ERROR_BAD_STATE);
4068 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004069
4070 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4072 ASSERT_OPERATION_IS_ACTIVE(operation);
4073 TEST_EQUAL(psa_cipher_update(&operation,
4074 text, sizeof(text),
4075 buffer, sizeof(buffer),
4076 &length),
4077 PSA_ERROR_BAD_STATE);
4078 ASSERT_OPERATION_IS_INACTIVE(operation);
4079 PSA_ASSERT(psa_cipher_abort(&operation));
4080 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004081
4082 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4084 PSA_ASSERT(psa_cipher_set_iv(&operation,
4085 iv, sizeof(iv)));
4086 PSA_ASSERT(psa_cipher_finish(&operation,
4087 buffer, sizeof(buffer), &length));
4088 TEST_EQUAL(psa_cipher_update(&operation,
4089 text, sizeof(text),
4090 buffer, sizeof(buffer),
4091 &length),
4092 PSA_ERROR_BAD_STATE);
4093 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004094
4095 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 TEST_EQUAL(psa_cipher_finish(&operation,
4097 buffer, sizeof(buffer), &length),
4098 PSA_ERROR_BAD_STATE);
4099 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004100
4101 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00004103 /* Not calling update means we are encrypting an empty buffer, which is OK
4104 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 ASSERT_OPERATION_IS_ACTIVE(operation);
4106 TEST_EQUAL(psa_cipher_finish(&operation,
4107 buffer, sizeof(buffer), &length),
4108 PSA_ERROR_BAD_STATE);
4109 ASSERT_OPERATION_IS_INACTIVE(operation);
4110 PSA_ASSERT(psa_cipher_abort(&operation));
4111 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004112
4113 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4115 PSA_ASSERT(psa_cipher_set_iv(&operation,
4116 iv, sizeof(iv)));
4117 PSA_ASSERT(psa_cipher_finish(&operation,
4118 buffer, sizeof(buffer), &length));
4119 TEST_EQUAL(psa_cipher_finish(&operation,
4120 buffer, sizeof(buffer), &length),
4121 PSA_ERROR_BAD_STATE);
4122 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02004125
Jaeden Ameroab439972019-02-15 14:12:05 +00004126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 psa_cipher_abort(&operation);
4128 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004129}
4130/* END_CASE */
4131
4132/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004133void cipher_encrypt_fail(int alg_arg,
4134 int key_type_arg,
4135 data_t *key_data,
4136 data_t *input,
4137 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004138{
Ronald Cron5425a212020-08-04 14:58:35 +02004139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004140 psa_status_t status;
4141 psa_key_type_t key_type = key_type_arg;
4142 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02004143 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004145 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
4146 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004147 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004148 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004149 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004150 size_t function_output_length;
4151 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (PSA_ERROR_BAD_STATE != expected_status) {
4155 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4158 psa_set_key_algorithm(&attributes, alg);
4159 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4162 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004163 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4166 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004167 }
4168
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004169 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
4171 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004174
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004175 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 status = psa_cipher_encrypt_setup(&operation, key, alg);
4177 if (status == PSA_SUCCESS) {
4178 if (alg != PSA_ALG_ECB_NO_PADDING) {
4179 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4180 iv, iv_size,
4181 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004182 }
4183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 status = psa_cipher_update(&operation, input->x, input->len,
4185 output, output_buffer_size,
4186 &function_output_length);
4187 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004188 output_length += function_output_length;
4189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 status = psa_cipher_finish(&operation, output + output_length,
4191 output_buffer_size - output_length,
4192 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 TEST_EQUAL(status, expected_status);
4195 } else {
4196 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 } else {
4199 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004200 }
4201
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 psa_cipher_abort(&operation);
4204 mbedtls_free(output);
4205 psa_destroy_key(key);
4206 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004207}
4208/* END_CASE */
4209
4210/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004211void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
4212 data_t *input, int iv_length,
4213 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02004214{
4215 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4216 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4218 size_t output_buffer_size = 0;
4219 unsigned char *output = NULL;
4220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004222 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4227 psa_set_key_algorithm(&attributes, alg);
4228 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4231 &key));
4232 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4233 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
4234 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02004235
4236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 psa_cipher_abort(&operation);
4238 mbedtls_free(output);
4239 psa_destroy_key(key);
4240 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02004241}
4242/* END_CASE */
4243
4244/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004245void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
4246 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004247{
4248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4249 psa_key_type_t key_type = key_type_arg;
4250 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004251 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4252 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004253 unsigned char *output = NULL;
4254 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02004255 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004259
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004260 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 TEST_LE_U(ciphertext->len,
4262 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
4263 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
4264 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
4265 TEST_LE_U(plaintext->len,
4266 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
4267 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
4268 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004269
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004270
4271 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 psa_set_key_usage_flags(&attributes,
4273 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4274 psa_set_key_algorithm(&attributes, alg);
4275 psa_set_key_type(&attributes, key_type);
4276 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4277 &key));
4278 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4279 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004280 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004281
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004282 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4284 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4285 PSA_ERROR_BAD_STATE);
4286 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4287 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4288 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004289
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004290 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4292 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4293 &length),
4294 PSA_ERROR_BAD_STATE);
4295 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4296 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4297 &length),
4298 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004299
Gilles Peskine286c3142022-04-20 17:09:38 +02004300 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004302 output_length = 0;
4303 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 PSA_ASSERT(psa_cipher_update(&operation,
4305 plaintext->x, plaintext->len,
4306 output, output_buffer_size,
4307 &length));
4308 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004309 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 PSA_ASSERT(psa_cipher_finish(&operation,
4311 mbedtls_buffer_offset(output, output_length),
4312 output_buffer_size - output_length,
4313 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004314 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004315 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004316 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004317
Gilles Peskine286c3142022-04-20 17:09:38 +02004318 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004320 output_length = 0;
4321 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 PSA_ASSERT(psa_cipher_update(&operation,
4323 ciphertext->x, ciphertext->len,
4324 output, output_buffer_size,
4325 &length));
4326 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004327 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 PSA_ASSERT(psa_cipher_finish(&operation,
4329 mbedtls_buffer_offset(output, output_length),
4330 output_buffer_size - output_length,
4331 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004332 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004333 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004334 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004335
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004336 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004337 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4339 output, output_buffer_size,
4340 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004341 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004342 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004343
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004344 /* One-shot decryption */
4345 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4347 output, output_buffer_size,
4348 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004349 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004350 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004351
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004352exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 PSA_ASSERT(psa_cipher_abort(&operation));
4354 mbedtls_free(output);
4355 psa_cipher_abort(&operation);
4356 psa_destroy_key(key);
4357 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004358}
4359/* END_CASE */
4360
4361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004362void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004363{
4364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4365 psa_algorithm_t alg = alg_arg;
4366 psa_key_type_t key_type = key_type_arg;
4367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4368 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4369 psa_status_t status;
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4374 psa_set_key_algorithm(&attributes, alg);
4375 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004376
4377 /* Usage of either of these two size macros would cause divide by zero
4378 * with incorrect key types previously. Input length should be irrelevant
4379 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4381 0);
4382 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004383
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4386 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004387
4388 /* Should fail due to invalid alg type (to support invalid key type).
4389 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004393
4394exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 psa_cipher_abort(&operation);
4396 psa_destroy_key(key);
4397 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004398}
4399/* END_CASE */
4400
4401/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004402void cipher_encrypt_validation(int alg_arg,
4403 int key_type_arg,
4404 data_t *key_data,
4405 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004406{
4407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4408 psa_key_type_t key_type = key_type_arg;
4409 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004411 unsigned char *output1 = NULL;
4412 size_t output1_buffer_size = 0;
4413 size_t output1_length = 0;
4414 unsigned char *output2 = NULL;
4415 size_t output2_buffer_size = 0;
4416 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004417 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004418 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4424 psa_set_key_algorithm(&attributes, alg);
4425 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4428 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4429 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004430 TEST_CALLOC(output1, output1_buffer_size);
4431 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4434 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004435
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004436 /* The one-shot cipher encryption uses generated iv so validating
4437 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4439 output1_buffer_size, &output1_length));
4440 TEST_LE_U(output1_length,
4441 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4442 TEST_LE_U(output1_length,
4443 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4446 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 PSA_ASSERT(psa_cipher_update(&operation,
4449 input->x, input->len,
4450 output2, output2_buffer_size,
4451 &function_output_length));
4452 TEST_LE_U(function_output_length,
4453 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4454 TEST_LE_U(function_output_length,
4455 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004456 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 PSA_ASSERT(psa_cipher_finish(&operation,
4459 output2 + output2_length,
4460 output2_buffer_size - output2_length,
4461 &function_output_length));
4462 TEST_LE_U(function_output_length,
4463 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4464 TEST_LE_U(function_output_length,
4465 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004466 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004469 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004470 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004471
Gilles Peskine50e586b2018-06-08 14:28:46 +02004472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 psa_cipher_abort(&operation);
4474 mbedtls_free(output1);
4475 mbedtls_free(output2);
4476 psa_destroy_key(key);
4477 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004478}
4479/* END_CASE */
4480
4481/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004482void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4483 data_t *key_data, data_t *iv,
4484 data_t *input,
4485 int first_part_size_arg,
4486 int output1_length_arg, int output2_length_arg,
4487 data_t *expected_output,
4488 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004489{
Ronald Cron5425a212020-08-04 14:58:35 +02004490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004491 psa_key_type_t key_type = key_type_arg;
4492 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004493 psa_status_t status;
4494 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004495 size_t first_part_size = first_part_size_arg;
4496 size_t output1_length = output1_length_arg;
4497 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004498 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004499 size_t output_buffer_size = 0;
4500 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004501 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004502 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4508 psa_set_key_algorithm(&attributes, alg);
4509 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4512 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (iv->len > 0) {
4517 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004518 }
4519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4521 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004522 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 TEST_LE_U(first_part_size, input->len);
4525 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4526 output, output_buffer_size,
4527 &function_output_length));
4528 TEST_ASSERT(function_output_length == output1_length);
4529 TEST_LE_U(function_output_length,
4530 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4531 TEST_LE_U(function_output_length,
4532 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004533 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 if (first_part_size < input->len) {
4536 PSA_ASSERT(psa_cipher_update(&operation,
4537 input->x + first_part_size,
4538 input->len - first_part_size,
4539 (output_buffer_size == 0 ? NULL :
4540 output + total_output_length),
4541 output_buffer_size - total_output_length,
4542 &function_output_length));
4543 TEST_ASSERT(function_output_length == output2_length);
4544 TEST_LE_U(function_output_length,
4545 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4546 alg,
4547 input->len - first_part_size));
4548 TEST_LE_U(function_output_length,
4549 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004550 total_output_length += function_output_length;
4551 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 status = psa_cipher_finish(&operation,
4554 (output_buffer_size == 0 ? NULL :
4555 output + total_output_length),
4556 output_buffer_size - total_output_length,
4557 &function_output_length);
4558 TEST_LE_U(function_output_length,
4559 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4560 TEST_LE_U(function_output_length,
4561 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004562 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 if (expected_status == PSA_SUCCESS) {
4566 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004567
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004568 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004569 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004570 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004571
4572exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 psa_cipher_abort(&operation);
4574 mbedtls_free(output);
4575 psa_destroy_key(key);
4576 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004577}
4578/* END_CASE */
4579
4580/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004581void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4582 data_t *key_data, data_t *iv,
4583 data_t *input,
4584 int first_part_size_arg,
4585 int output1_length_arg, int output2_length_arg,
4586 data_t *expected_output,
4587 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004588{
Ronald Cron5425a212020-08-04 14:58:35 +02004589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004590 psa_key_type_t key_type = key_type_arg;
4591 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004592 psa_status_t status;
4593 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004594 size_t first_part_size = first_part_size_arg;
4595 size_t output1_length = output1_length_arg;
4596 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004597 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004598 size_t output_buffer_size = 0;
4599 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004600 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004601 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4607 psa_set_key_algorithm(&attributes, alg);
4608 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4611 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004612
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004614
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if (iv->len > 0) {
4616 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004617 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4620 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004621 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 TEST_LE_U(first_part_size, input->len);
4624 PSA_ASSERT(psa_cipher_update(&operation,
4625 input->x, first_part_size,
4626 output, output_buffer_size,
4627 &function_output_length));
4628 TEST_ASSERT(function_output_length == output1_length);
4629 TEST_LE_U(function_output_length,
4630 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4631 TEST_LE_U(function_output_length,
4632 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004633 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if (first_part_size < input->len) {
4636 PSA_ASSERT(psa_cipher_update(&operation,
4637 input->x + first_part_size,
4638 input->len - first_part_size,
4639 (output_buffer_size == 0 ? NULL :
4640 output + total_output_length),
4641 output_buffer_size - total_output_length,
4642 &function_output_length));
4643 TEST_ASSERT(function_output_length == output2_length);
4644 TEST_LE_U(function_output_length,
4645 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4646 alg,
4647 input->len - first_part_size));
4648 TEST_LE_U(function_output_length,
4649 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004650 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004651 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 status = psa_cipher_finish(&operation,
4654 (output_buffer_size == 0 ? NULL :
4655 output + total_output_length),
4656 output_buffer_size - total_output_length,
4657 &function_output_length);
4658 TEST_LE_U(function_output_length,
4659 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4660 TEST_LE_U(function_output_length,
4661 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004662 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (expected_status == PSA_SUCCESS) {
4666 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004667
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004668 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004669 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004670 }
4671
Gilles Peskine50e586b2018-06-08 14:28:46 +02004672exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 psa_cipher_abort(&operation);
4674 mbedtls_free(output);
4675 psa_destroy_key(key);
4676 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004677}
4678/* END_CASE */
4679
Gilles Peskine50e586b2018-06-08 14:28:46 +02004680/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004681void cipher_decrypt_fail(int alg_arg,
4682 int key_type_arg,
4683 data_t *key_data,
4684 data_t *iv,
4685 data_t *input_arg,
4686 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004687{
4688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4689 psa_status_t status;
4690 psa_key_type_t key_type = key_type_arg;
4691 psa_algorithm_t alg = alg_arg;
4692 psa_status_t expected_status = expected_status_arg;
4693 unsigned char *input = NULL;
4694 size_t input_buffer_size = 0;
4695 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004696 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004697 size_t output_buffer_size = 0;
4698 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004699 size_t function_output_length;
4700 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (PSA_ERROR_BAD_STATE != expected_status) {
4704 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4707 psa_set_key_algorithm(&attributes, alg);
4708 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4711 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004712 }
4713
4714 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4716 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004717 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 memcpy(input, iv->x, iv->len);
4719 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004720 }
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004723 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004724
Neil Armstrong66a479f2022-02-07 15:41:19 +01004725 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4727 output_buffer_size, &output_length);
4728 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004729
Neil Armstrong66a479f2022-02-07 15:41:19 +01004730 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 status = psa_cipher_decrypt_setup(&operation, key, alg);
4732 if (status == PSA_SUCCESS) {
4733 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4734 input_arg->len) +
4735 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004736 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (iv->len > 0) {
4739 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (status != PSA_SUCCESS) {
4742 TEST_EQUAL(status, expected_status);
4743 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004744 }
4745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (status == PSA_SUCCESS) {
4747 status = psa_cipher_update(&operation,
4748 input_arg->x, input_arg->len,
4749 output_multi, output_buffer_size,
4750 &function_output_length);
4751 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004752 output_length = function_output_length;
4753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 status = psa_cipher_finish(&operation,
4755 output_multi + output_length,
4756 output_buffer_size - output_length,
4757 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 TEST_EQUAL(status, expected_status);
4760 } else {
4761 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004762 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 } else {
4764 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004765 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 } else {
4767 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004768 }
4769
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 psa_cipher_abort(&operation);
4772 mbedtls_free(input);
4773 mbedtls_free(output);
4774 mbedtls_free(output_multi);
4775 psa_destroy_key(key);
4776 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004777}
4778/* END_CASE */
4779
4780/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781void cipher_decrypt(int alg_arg,
4782 int key_type_arg,
4783 data_t *key_data,
4784 data_t *iv,
4785 data_t *input_arg,
4786 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004787{
4788 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4789 psa_key_type_t key_type = key_type_arg;
4790 psa_algorithm_t alg = alg_arg;
4791 unsigned char *input = NULL;
4792 size_t input_buffer_size = 0;
4793 unsigned char *output = NULL;
4794 size_t output_buffer_size = 0;
4795 size_t output_length = 0;
4796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4801 psa_set_key_algorithm(&attributes, alg);
4802 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004803
4804 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4806 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004807 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 memcpy(input, iv->x, iv->len);
4809 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004810 }
4811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004813 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4816 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4819 output_buffer_size, &output_length));
4820 TEST_LE_U(output_length,
4821 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4822 TEST_LE_U(output_length,
4823 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004824
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004825 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004826 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004827exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 mbedtls_free(input);
4829 mbedtls_free(output);
4830 psa_destroy_key(key);
4831 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004832}
4833/* END_CASE */
4834
4835/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836void cipher_verify_output(int alg_arg,
4837 int key_type_arg,
4838 data_t *key_data,
4839 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004840{
Ronald Cron5425a212020-08-04 14:58:35 +02004841 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004842 psa_key_type_t key_type = key_type_arg;
4843 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004844 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004845 size_t output1_size = 0;
4846 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004847 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004848 size_t output2_size = 0;
4849 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4855 psa_set_key_algorithm(&attributes, alg);
4856 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4859 &key));
4860 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004861 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4864 output1, output1_size,
4865 &output1_length));
4866 TEST_LE_U(output1_length,
4867 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4868 TEST_LE_U(output1_length,
4869 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004870
4871 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004872 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4875 output2, output2_size,
4876 &output2_length));
4877 TEST_LE_U(output2_length,
4878 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4879 TEST_LE_U(output2_length,
4880 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004881
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004882 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004883
4884exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 mbedtls_free(output1);
4886 mbedtls_free(output2);
4887 psa_destroy_key(key);
4888 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004889}
4890/* END_CASE */
4891
4892/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004893void cipher_verify_output_multipart(int alg_arg,
4894 int key_type_arg,
4895 data_t *key_data,
4896 data_t *input,
4897 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004898{
Ronald Cron5425a212020-08-04 14:58:35 +02004899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004900 psa_key_type_t key_type = key_type_arg;
4901 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004902 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004904 size_t iv_size = 16;
4905 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004906 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004907 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004908 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004909 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004910 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004911 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004912 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004913 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4914 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4920 psa_set_key_algorithm(&attributes, alg);
4921 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4924 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4927 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 if (alg != PSA_ALG_ECB_NO_PADDING) {
4930 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4931 iv, iv_size,
4932 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004933 }
4934
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4936 TEST_LE_U(output1_buffer_size,
4937 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004938 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4943 output1, output1_buffer_size,
4944 &function_output_length));
4945 TEST_LE_U(function_output_length,
4946 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4947 TEST_LE_U(function_output_length,
4948 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004949 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 PSA_ASSERT(psa_cipher_update(&operation1,
4952 input->x + first_part_size,
4953 input->len - first_part_size,
4954 output1, output1_buffer_size,
4955 &function_output_length));
4956 TEST_LE_U(function_output_length,
4957 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4958 alg,
4959 input->len - first_part_size));
4960 TEST_LE_U(function_output_length,
4961 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004962 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 PSA_ASSERT(psa_cipher_finish(&operation1,
4965 output1 + output1_length,
4966 output1_buffer_size - output1_length,
4967 &function_output_length));
4968 TEST_LE_U(function_output_length,
4969 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4970 TEST_LE_U(function_output_length,
4971 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004972 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004975
Gilles Peskine048b7f02018-06-08 14:20:49 +02004976 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 TEST_LE_U(output2_buffer_size,
4978 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4979 TEST_LE_U(output2_buffer_size,
4980 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004981 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (iv_length > 0) {
4984 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4985 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004986 }
Moran Pekerded84402018-06-06 16:36:50 +03004987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4989 output2, output2_buffer_size,
4990 &function_output_length));
4991 TEST_LE_U(function_output_length,
4992 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4993 TEST_LE_U(function_output_length,
4994 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004995 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 PSA_ASSERT(psa_cipher_update(&operation2,
4998 output1 + first_part_size,
4999 output1_length - first_part_size,
5000 output2, output2_buffer_size,
5001 &function_output_length));
5002 TEST_LE_U(function_output_length,
5003 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
5004 alg,
5005 output1_length - first_part_size));
5006 TEST_LE_U(function_output_length,
5007 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02005008 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03005009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 PSA_ASSERT(psa_cipher_finish(&operation2,
5011 output2 + output2_length,
5012 output2_buffer_size - output2_length,
5013 &function_output_length));
5014 TEST_LE_U(function_output_length,
5015 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
5016 TEST_LE_U(function_output_length,
5017 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02005018 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02005019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02005021
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005022 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02005023
5024exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 psa_cipher_abort(&operation1);
5026 psa_cipher_abort(&operation2);
5027 mbedtls_free(output1);
5028 mbedtls_free(output2);
5029 psa_destroy_key(key);
5030 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02005031}
5032/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02005033
Gilles Peskine20035e32018-02-03 22:44:14 +01005034/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005035void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
5036 int alg_arg,
5037 data_t *nonce,
5038 data_t *additional_data,
5039 data_t *input_data,
5040 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005041{
Ronald Cron5425a212020-08-04 14:58:35 +02005042 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005043 psa_key_type_t key_type = key_type_arg;
5044 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005045 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005046 unsigned char *output_data = NULL;
5047 size_t output_size = 0;
5048 size_t output_length = 0;
5049 unsigned char *output_data2 = NULL;
5050 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01005051 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02005052 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5058 psa_set_key_algorithm(&attributes, alg);
5059 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5062 &key));
5063 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5064 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5067 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005068 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
5069 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
5071 expected_result != PSA_ERROR_NOT_SUPPORTED) {
5072 TEST_EQUAL(output_size,
5073 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5074 TEST_LE_U(output_size,
5075 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01005076 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005077 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 status = psa_aead_encrypt(key, alg,
5080 nonce->x, nonce->len,
5081 additional_data->x,
5082 additional_data->len,
5083 input_data->x, input_data->len,
5084 output_data, output_size,
5085 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01005086
5087 /* If the operation is not supported, just skip and not fail in case the
5088 * encryption involves a common limitation of cryptography hardwares and
5089 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (status == PSA_ERROR_NOT_SUPPORTED) {
5091 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5092 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01005093 }
5094
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005098 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005099
Gilles Peskine003a4a92019-05-14 16:09:40 +02005100 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
5101 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 TEST_EQUAL(input_data->len,
5103 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02005104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 TEST_LE_U(input_data->len,
5106 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 TEST_EQUAL(psa_aead_decrypt(key, alg,
5109 nonce->x, nonce->len,
5110 additional_data->x,
5111 additional_data->len,
5112 output_data, output_length,
5113 output_data2, output_length,
5114 &output_length2),
5115 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02005116
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005117 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005118 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005119 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005120
Gilles Peskinea1cac842018-06-11 19:33:02 +02005121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 psa_destroy_key(key);
5123 mbedtls_free(output_data);
5124 mbedtls_free(output_data2);
5125 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005126}
5127/* END_CASE */
5128
5129/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005130void aead_encrypt(int key_type_arg, data_t *key_data,
5131 int alg_arg,
5132 data_t *nonce,
5133 data_t *additional_data,
5134 data_t *input_data,
5135 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005136{
Ronald Cron5425a212020-08-04 14:58:35 +02005137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005138 psa_key_type_t key_type = key_type_arg;
5139 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005140 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005141 unsigned char *output_data = NULL;
5142 size_t output_size = 0;
5143 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01005145 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5150 psa_set_key_algorithm(&attributes, alg);
5151 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5154 &key));
5155 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5156 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5159 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005160 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
5161 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 TEST_EQUAL(output_size,
5163 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5164 TEST_LE_U(output_size,
5165 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005166 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 status = psa_aead_encrypt(key, alg,
5169 nonce->x, nonce->len,
5170 additional_data->x, additional_data->len,
5171 input_data->x, input_data->len,
5172 output_data, output_size,
5173 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005174
Ronald Cron28a45ed2021-02-09 20:35:42 +01005175 /* If the operation is not supported, just skip and not fail in case the
5176 * encryption involves a common limitation of cryptography hardwares and
5177 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (status == PSA_ERROR_NOT_SUPPORTED) {
5179 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5180 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01005181 }
Steven Cooremand588ea12021-01-11 19:36:04 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005184 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005185 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02005186
Gilles Peskinea1cac842018-06-11 19:33:02 +02005187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 psa_destroy_key(key);
5189 mbedtls_free(output_data);
5190 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005191}
5192/* END_CASE */
5193
5194/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005195void aead_decrypt(int key_type_arg, data_t *key_data,
5196 int alg_arg,
5197 data_t *nonce,
5198 data_t *additional_data,
5199 data_t *input_data,
5200 data_t *expected_data,
5201 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005202{
Ronald Cron5425a212020-08-04 14:58:35 +02005203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005204 psa_key_type_t key_type = key_type_arg;
5205 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005206 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005207 unsigned char *output_data = NULL;
5208 size_t output_size = 0;
5209 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02005211 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01005212 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005213
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5217 psa_set_key_algorithm(&attributes, alg);
5218 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005219
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5221 &key));
5222 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5223 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5226 alg);
5227 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
5228 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01005229 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
5230 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 TEST_EQUAL(output_size,
5232 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5233 TEST_LE_U(output_size,
5234 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01005235 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005236 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 status = psa_aead_decrypt(key, alg,
5239 nonce->x, nonce->len,
5240 additional_data->x,
5241 additional_data->len,
5242 input_data->x, input_data->len,
5243 output_data, output_size,
5244 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01005245
Ronald Cron28a45ed2021-02-09 20:35:42 +01005246 /* If the operation is not supported, just skip and not fail in case the
5247 * decryption involves a common limitation of cryptography hardwares and
5248 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (status == PSA_ERROR_NOT_SUPPORTED) {
5250 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5251 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01005252 }
Steven Cooremand588ea12021-01-11 19:36:04 +01005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005257 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005258 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02005260
Gilles Peskinea1cac842018-06-11 19:33:02 +02005261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 psa_destroy_key(key);
5263 mbedtls_free(output_data);
5264 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005265}
5266/* END_CASE */
5267
5268/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005269void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
5270 int alg_arg,
5271 data_t *nonce,
5272 data_t *additional_data,
5273 data_t *input_data,
5274 int do_set_lengths,
5275 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005276{
Paul Elliottd3f82412021-06-16 16:52:21 +01005277 size_t ad_part_len = 0;
5278 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005279 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
5282 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (do_set_lengths) {
5285 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005286 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005288 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005290 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005291
5292 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 if (!aead_multipart_internal_func(key_type_arg, key_data,
5294 alg_arg, nonce,
5295 additional_data,
5296 ad_part_len,
5297 input_data, -1,
5298 set_lengths_method,
5299 expected_output,
5300 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005301 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005302 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 /* length(0) part, length(ad_part_len) part, length(0) part... */
5305 mbedtls_test_set_step(1000 + ad_part_len);
5306
5307 if (!aead_multipart_internal_func(key_type_arg, key_data,
5308 alg_arg, nonce,
5309 additional_data,
5310 ad_part_len,
5311 input_data, -1,
5312 set_lengths_method,
5313 expected_output,
5314 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005315 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 }
5317 }
5318
5319 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5320 /* Split data into length(data_part_len) parts. */
5321 mbedtls_test_set_step(2000 + data_part_len);
5322
5323 if (do_set_lengths) {
5324 if (data_part_len & 0x01) {
5325 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5326 } else {
5327 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5328 }
5329 }
5330
5331 if (!aead_multipart_internal_func(key_type_arg, key_data,
5332 alg_arg, nonce,
5333 additional_data, -1,
5334 input_data, data_part_len,
5335 set_lengths_method,
5336 expected_output,
5337 1, 0)) {
5338 break;
5339 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005340
5341 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (!aead_multipart_internal_func(key_type_arg, key_data,
5345 alg_arg, nonce,
5346 additional_data, -1,
5347 input_data, data_part_len,
5348 set_lengths_method,
5349 expected_output,
5350 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005351 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005353 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005354
Paul Elliott8fc45162021-06-23 16:06:01 +01005355 /* Goto is required to silence warnings about unused labels, as we
5356 * don't actually do any test assertions in this function. */
5357 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005358}
5359/* END_CASE */
5360
5361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005362void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5363 int alg_arg,
5364 data_t *nonce,
5365 data_t *additional_data,
5366 data_t *input_data,
5367 int do_set_lengths,
5368 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005369{
Paul Elliottd3f82412021-06-16 16:52:21 +01005370 size_t ad_part_len = 0;
5371 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005372 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005375 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if (do_set_lengths) {
5379 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005380 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005382 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005384 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 if (!aead_multipart_internal_func(key_type_arg, key_data,
5387 alg_arg, nonce,
5388 additional_data,
5389 ad_part_len,
5390 input_data, -1,
5391 set_lengths_method,
5392 expected_output,
5393 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005394 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005396
5397 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if (!aead_multipart_internal_func(key_type_arg, key_data,
5401 alg_arg, nonce,
5402 additional_data,
5403 ad_part_len,
5404 input_data, -1,
5405 set_lengths_method,
5406 expected_output,
5407 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005408 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005410 }
5411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005413 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005415
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 if (do_set_lengths) {
5417 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005418 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005420 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005422 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if (!aead_multipart_internal_func(key_type_arg, key_data,
5425 alg_arg, nonce,
5426 additional_data, -1,
5427 input_data, data_part_len,
5428 set_lengths_method,
5429 expected_output,
5430 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005431 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005433
5434 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 if (!aead_multipart_internal_func(key_type_arg, key_data,
5438 alg_arg, nonce,
5439 additional_data, -1,
5440 input_data, data_part_len,
5441 set_lengths_method,
5442 expected_output,
5443 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005444 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005446 }
5447
Paul Elliott8fc45162021-06-23 16:06:01 +01005448 /* Goto is required to silence warnings about unused labels, as we
5449 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005450 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005451}
5452/* END_CASE */
5453
5454/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005455void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5456 int alg_arg,
5457 int nonce_length,
5458 int expected_nonce_length_arg,
5459 data_t *additional_data,
5460 data_t *input_data,
5461 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005462{
5463
5464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5465 psa_key_type_t key_type = key_type_arg;
5466 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005467 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005468 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5470 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005471 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005472 size_t actual_nonce_length = 0;
5473 size_t expected_nonce_length = expected_nonce_length_arg;
5474 unsigned char *output = NULL;
5475 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005476 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005477 size_t ciphertext_size = 0;
5478 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005479 size_t tag_length = 0;
5480 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5485 psa_set_key_algorithm(&attributes, alg);
5486 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005487
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5489 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005490
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005494
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005495 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005496
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005500
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005501 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005504
5505 /* If the operation is not supported, just skip and not fail in case the
5506 * encryption involves a common limitation of cryptography hardwares and
5507 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 if (status == PSA_ERROR_NOT_SUPPORTED) {
5509 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5510 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005511 }
5512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5516 nonce_length,
5517 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005518
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (expected_status == PSA_SUCCESS) {
5524 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5525 alg));
5526 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005529
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005531 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5533 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5536 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5539 output, output_size,
5540 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5543 &ciphertext_length, tag_buffer,
5544 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005545 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005546
5547exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 psa_destroy_key(key);
5549 mbedtls_free(output);
5550 mbedtls_free(ciphertext);
5551 psa_aead_abort(&operation);
5552 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005553}
5554/* END_CASE */
5555
5556/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005557void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5558 int alg_arg,
5559 int nonce_length_arg,
5560 int set_lengths_method_arg,
5561 data_t *additional_data,
5562 data_t *input_data,
5563 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005564{
5565
5566 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5567 psa_key_type_t key_type = key_type_arg;
5568 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005569 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005570 uint8_t *nonce_buffer = NULL;
5571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5572 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5573 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005574 unsigned char *output = NULL;
5575 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005576 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005577 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005578 size_t ciphertext_size = 0;
5579 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005580 size_t tag_length = 0;
5581 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005582 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005583 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5588 psa_set_key_algorithm(&attributes, alg);
5589 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5592 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005597
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005598 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005603
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005604 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005607
5608 /* If the operation is not supported, just skip and not fail in case the
5609 * encryption involves a common limitation of cryptography hardwares and
5610 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 if (status == PSA_ERROR_NOT_SUPPORTED) {
5612 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5613 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005614 }
5615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005617
Paul Elliott4023ffd2021-09-10 16:21:22 +01005618 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 if (nonce_length_arg == -1) {
5620 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005621 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 nonce_length = 0;
5623 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005624 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005626 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (nonce_buffer) {
5629 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005630 nonce_buffer[index] = 'a' + index;
5631 }
Paul Elliott66696b52021-08-16 18:42:41 +01005632 }
Paul Elliott863864a2021-07-23 17:28:31 +01005633 }
5634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5636 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5637 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005638 }
5639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 if (expected_status == PSA_SUCCESS) {
5645 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5646 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5647 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005650 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 }
Paul Elliott863864a2021-07-23 17:28:31 +01005652
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005653 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5655 additional_data->len),
5656 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5659 output, output_size,
5660 &ciphertext_length),
5661 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5664 &ciphertext_length, tag_buffer,
5665 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5666 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005667 }
5668
5669exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 psa_destroy_key(key);
5671 mbedtls_free(output);
5672 mbedtls_free(ciphertext);
5673 mbedtls_free(nonce_buffer);
5674 psa_aead_abort(&operation);
5675 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005676}
5677/* END_CASE */
5678
5679/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005680void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005681 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005682 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005683 data_t *nonce,
5684 data_t *additional_data,
5685 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005687{
5688
5689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5690 psa_key_type_t key_type = key_type_arg;
5691 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005692 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5694 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5695 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005696 unsigned char *output = NULL;
5697 unsigned char *ciphertext = NULL;
5698 size_t output_size = output_size_arg;
5699 size_t ciphertext_size = 0;
5700 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005701 size_t tag_length = 0;
5702 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5707 psa_set_key_algorithm(&attributes, alg);
5708 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5711 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005714
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005715 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005718
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005719 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005722
5723 /* If the operation is not supported, just skip and not fail in case the
5724 * encryption involves a common limitation of cryptography hardwares and
5725 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 if (status == PSA_ERROR_NOT_SUPPORTED) {
5727 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5728 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005729 }
5730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5734 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5739 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 status = psa_aead_update(&operation, input_data->x, input_data->len,
5742 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005747 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5749 &ciphertext_length, tag_buffer,
5750 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005751 }
5752
5753exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 psa_destroy_key(key);
5755 mbedtls_free(output);
5756 mbedtls_free(ciphertext);
5757 psa_aead_abort(&operation);
5758 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005759}
5760/* END_CASE */
5761
Paul Elliott91b021e2021-07-23 18:52:31 +01005762/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005763void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5764 int alg_arg,
5765 int finish_ciphertext_size_arg,
5766 int tag_size_arg,
5767 data_t *nonce,
5768 data_t *additional_data,
5769 data_t *input_data,
5770 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005771{
5772
5773 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5774 psa_key_type_t key_type = key_type_arg;
5775 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005776 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5778 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5779 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005780 unsigned char *ciphertext = NULL;
5781 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005782 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005783 size_t ciphertext_size = 0;
5784 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5786 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005787 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5792 psa_set_key_algorithm(&attributes, alg);
5793 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5796 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005801
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005802 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005803
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005804 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005805
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005806 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005809
5810 /* If the operation is not supported, just skip and not fail in case the
5811 * encryption involves a common limitation of cryptography hardwares and
5812 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 if (status == PSA_ERROR_NOT_SUPPORTED) {
5814 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5815 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005816 }
5817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5823 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5826 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5829 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005830
5831 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 status = psa_aead_finish(&operation, finish_ciphertext,
5833 finish_ciphertext_size,
5834 &ciphertext_length, tag_buffer,
5835 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005838
5839exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 psa_destroy_key(key);
5841 mbedtls_free(ciphertext);
5842 mbedtls_free(finish_ciphertext);
5843 mbedtls_free(tag_buffer);
5844 psa_aead_abort(&operation);
5845 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005846}
5847/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005848
5849/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005850void aead_multipart_verify(int key_type_arg, data_t *key_data,
5851 int alg_arg,
5852 data_t *nonce,
5853 data_t *additional_data,
5854 data_t *input_data,
5855 data_t *tag,
5856 int tag_usage_arg,
5857 int expected_setup_status_arg,
5858 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005859{
5860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5861 psa_key_type_t key_type = key_type_arg;
5862 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005863 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5865 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5866 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005867 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005868 unsigned char *plaintext = NULL;
5869 unsigned char *finish_plaintext = NULL;
5870 size_t plaintext_size = 0;
5871 size_t plaintext_length = 0;
5872 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005873 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005874 unsigned char *tag_buffer = NULL;
5875 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5880 psa_set_key_algorithm(&attributes, alg);
5881 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5884 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5889 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005890
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005891 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005894
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005895 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005896
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005898
5899 /* If the operation is not supported, just skip and not fail in case the
5900 * encryption involves a common limitation of cryptography hardwares and
5901 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if (status == PSA_ERROR_NOT_SUPPORTED) {
5903 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5904 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005905 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005907
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005909 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 }
Paul Elliott9961a662021-09-17 19:19:02 +01005911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 status = psa_aead_set_lengths(&operation, additional_data->len,
5917 input_data->len);
5918 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5921 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005922
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5924 input_data->len,
5925 plaintext, plaintext_size,
5926 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005929 tag_buffer = tag->x;
5930 tag_size = tag->len;
5931 }
5932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 status = psa_aead_verify(&operation, finish_plaintext,
5934 verify_plaintext_size,
5935 &plaintext_length,
5936 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005939
5940exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 psa_destroy_key(key);
5942 mbedtls_free(plaintext);
5943 mbedtls_free(finish_plaintext);
5944 psa_aead_abort(&operation);
5945 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005946}
5947/* END_CASE */
5948
Paul Elliott9961a662021-09-17 19:19:02 +01005949/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005950void aead_multipart_setup(int key_type_arg, data_t *key_data,
5951 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005952{
5953 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5954 psa_key_type_t key_type = key_type_arg;
5955 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005956 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5958 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5959 psa_status_t expected_status = expected_status_arg;
5960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 psa_set_key_usage_flags(&attributes,
5964 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5965 psa_set_key_algorithm(&attributes, alg);
5966 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5969 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005980
5981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 psa_destroy_key(key);
5983 psa_aead_abort(&operation);
5984 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005985}
5986/* END_CASE */
5987
5988/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005989void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5990 int alg_arg,
5991 data_t *nonce,
5992 data_t *additional_data,
5993 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005994{
5995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5996 psa_key_type_t key_type = key_type_arg;
5997 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005998 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005999 unsigned char *output_data = NULL;
6000 unsigned char *final_data = NULL;
6001 size_t output_size = 0;
6002 size_t finish_output_size = 0;
6003 size_t output_length = 0;
6004 size_t key_bits = 0;
6005 size_t tag_length = 0;
6006 size_t tag_size = 0;
6007 size_t nonce_length = 0;
6008 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
6009 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
6010 size_t output_part_length = 0;
6011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01006014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 psa_set_key_usage_flags(&attributes,
6016 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
6017 psa_set_key_algorithm(&attributes, alg);
6018 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6021 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6024 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006031
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006032 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006037
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006038 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006039
6040 /* Test all operations error without calling setup first. */
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6043 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6048 PSA_AEAD_NONCE_MAX_SIZE,
6049 &nonce_length),
6050 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006053
Paul Elliott481be342021-07-16 17:38:47 +01006054 /* ------------------------------------------------------- */
6055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6057 input_data->len),
6058 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006061
Paul Elliott481be342021-07-16 17:38:47 +01006062 /* ------------------------------------------------------- */
6063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6065 additional_data->len),
6066 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006069
Paul Elliott481be342021-07-16 17:38:47 +01006070 /* ------------------------------------------------------- */
6071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6073 input_data->len, output_data,
6074 output_size, &output_length),
6075 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006078
Paul Elliott481be342021-07-16 17:38:47 +01006079 /* ------------------------------------------------------- */
6080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6082 finish_output_size,
6083 &output_part_length,
6084 tag_buffer, tag_length,
6085 &tag_size),
6086 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006089
Paul Elliott481be342021-07-16 17:38:47 +01006090 /* ------------------------------------------------------- */
6091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6093 finish_output_size,
6094 &output_part_length,
6095 tag_buffer,
6096 tag_length),
6097 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006100
6101 /* Test for double setups. */
6102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
6106 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006109
Paul Elliott481be342021-07-16 17:38:47 +01006110 /* ------------------------------------------------------- */
6111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
6115 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006118
Paul Elliott374a2be2021-07-16 17:53:40 +01006119 /* ------------------------------------------------------- */
6120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
6124 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006127
6128 /* ------------------------------------------------------- */
6129
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
6133 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006136
Paul Elliottc23a9a02021-06-21 18:32:46 +01006137 /* Test for not setting a nonce. */
6138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6142 additional_data->len),
6143 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006146
Paul Elliott7f628422021-09-01 12:08:29 +01006147 /* ------------------------------------------------------- */
6148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6152 input_data->len, output_data,
6153 output_size, &output_length),
6154 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01006157
Paul Elliottbdc2c682021-09-21 18:37:10 +01006158 /* ------------------------------------------------------- */
6159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6163 finish_output_size,
6164 &output_part_length,
6165 tag_buffer, tag_length,
6166 &tag_size),
6167 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006170
6171 /* ------------------------------------------------------- */
6172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01006174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6176 finish_output_size,
6177 &output_part_length,
6178 tag_buffer,
6179 tag_length),
6180 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006181
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006183
Paul Elliottc23a9a02021-06-21 18:32:46 +01006184 /* Test for double setting nonce. */
6185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6191 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006194
Paul Elliott374a2be2021-07-16 17:53:40 +01006195 /* Test for double generating nonce. */
6196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6200 PSA_AEAD_NONCE_MAX_SIZE,
6201 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6204 PSA_AEAD_NONCE_MAX_SIZE,
6205 &nonce_length),
6206 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006207
6208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006210
6211 /* Test for generate nonce then set and vice versa */
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_generate_nonce(&operation, nonce_buffer,
6216 PSA_AEAD_NONCE_MAX_SIZE,
6217 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
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
Andrzej Kurekad837522021-12-15 15:28:49 +01006224 /* Test for generating nonce after calling set lengths */
6225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6229 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6232 PSA_AEAD_NONCE_MAX_SIZE,
6233 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006236
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006237 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 if (operation.alg == PSA_ALG_CCM) {
6242 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6243 input_data->len),
6244 PSA_ERROR_INVALID_ARGUMENT);
6245 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6246 PSA_AEAD_NONCE_MAX_SIZE,
6247 &nonce_length),
6248 PSA_ERROR_BAD_STATE);
6249 } else {
6250 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6251 input_data->len));
6252 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6253 PSA_AEAD_NONCE_MAX_SIZE,
6254 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006255 }
6256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006258
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006259 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00006260#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6264 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6265 input_data->len),
6266 PSA_ERROR_INVALID_ARGUMENT);
6267 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6268 PSA_AEAD_NONCE_MAX_SIZE,
6269 &nonce_length),
6270 PSA_ERROR_BAD_STATE);
6271 } else {
6272 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6273 input_data->len));
6274 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6275 PSA_AEAD_NONCE_MAX_SIZE,
6276 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006277 }
6278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00006280#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01006281
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006282 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6287 PSA_AEAD_NONCE_MAX_SIZE,
6288 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 if (operation.alg == PSA_ALG_CCM) {
6291 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6292 input_data->len),
6293 PSA_ERROR_INVALID_ARGUMENT);
6294 } else {
6295 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6296 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006297 }
6298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006300
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006301 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006302 /* Test for setting nonce after calling set lengths */
6303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6307 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006312
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006313 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 if (operation.alg == PSA_ALG_CCM) {
6318 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6319 input_data->len),
6320 PSA_ERROR_INVALID_ARGUMENT);
6321 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6322 PSA_ERROR_BAD_STATE);
6323 } else {
6324 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6325 input_data->len));
6326 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006327 }
6328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006330
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006331 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006332#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6336 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6337 input_data->len),
6338 PSA_ERROR_INVALID_ARGUMENT);
6339 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6340 PSA_ERROR_BAD_STATE);
6341 } else {
6342 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6343 input_data->len));
6344 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006345 }
6346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006348#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006349
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006350 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 if (operation.alg == PSA_ALG_CCM) {
6357 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6358 input_data->len),
6359 PSA_ERROR_INVALID_ARGUMENT);
6360 } else {
6361 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6362 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006363 }
6364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006366
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006367 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006368#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 if (operation.alg == PSA_ALG_GCM) {
6372 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6373 SIZE_MAX),
6374 PSA_ERROR_INVALID_ARGUMENT);
6375 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6376 PSA_ERROR_BAD_STATE);
6377 } else if (operation.alg != PSA_ALG_CCM) {
6378 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6379 SIZE_MAX));
6380 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006381 }
6382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006384#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006385
Tom Cosgrove1797b052022-12-04 17:19:59 +00006386 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006387#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (operation.alg == PSA_ALG_GCM) {
6393 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6394 SIZE_MAX),
6395 PSA_ERROR_INVALID_ARGUMENT);
6396 } else if (operation.alg != PSA_ALG_CCM) {
6397 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6398 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006399 }
6400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006402#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006403
6404 /* ------------------------------------------------------- */
6405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6411 PSA_AEAD_NONCE_MAX_SIZE,
6412 &nonce_length),
6413 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006416
Paul Elliott7220cae2021-06-22 17:25:57 +01006417 /* Test for generating nonce in decrypt setup. */
6418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6422 PSA_AEAD_NONCE_MAX_SIZE,
6423 &nonce_length),
6424 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006427
Paul Elliottc23a9a02021-06-21 18:32:46 +01006428 /* Test for setting lengths twice. */
6429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6435 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6438 input_data->len),
6439 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006442
Andrzej Kurekad837522021-12-15 15:28:49 +01006443 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006446
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6452 additional_data->len),
6453 PSA_ERROR_BAD_STATE);
6454 } else {
6455 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6456 additional_data->len));
6457
6458 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6459 input_data->len),
6460 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006461 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006463
6464 /* ------------------------------------------------------- */
6465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006467
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006469
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 if (operation.alg == PSA_ALG_CCM) {
6471 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6472 input_data->len, output_data,
6473 output_size, &output_length),
6474 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 } else {
6477 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6478 input_data->len, output_data,
6479 output_size, &output_length));
6480
6481 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6482 input_data->len),
6483 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006484 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006486
6487 /* ------------------------------------------------------- */
6488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 if (operation.alg == PSA_ALG_CCM) {
6494 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6495 finish_output_size,
6496 &output_part_length,
6497 tag_buffer, tag_length,
6498 &tag_size));
6499 } else {
6500 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6501 finish_output_size,
6502 &output_part_length,
6503 tag_buffer, tag_length,
6504 &tag_size));
6505
6506 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6507 input_data->len),
6508 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006509 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006511
6512 /* Test for setting lengths after generating nonce + already starting data. */
6513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6517 PSA_AEAD_NONCE_MAX_SIZE,
6518 &nonce_length));
6519 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6522 additional_data->len),
6523 PSA_ERROR_BAD_STATE);
6524 } else {
6525 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6526 additional_data->len));
6527
6528 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6529 input_data->len),
6530 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006531 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006533
6534 /* ------------------------------------------------------- */
6535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6539 PSA_AEAD_NONCE_MAX_SIZE,
6540 &nonce_length));
6541 if (operation.alg == PSA_ALG_CCM) {
6542 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6543 input_data->len, output_data,
6544 output_size, &output_length),
6545 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 } else {
6548 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6549 input_data->len, output_data,
6550 output_size, &output_length));
6551
6552 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6553 input_data->len),
6554 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006555 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006557
6558 /* ------------------------------------------------------- */
6559
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6563 PSA_AEAD_NONCE_MAX_SIZE,
6564 &nonce_length));
6565 if (operation.alg == PSA_ALG_CCM) {
6566 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6567 finish_output_size,
6568 &output_part_length,
6569 tag_buffer, tag_length,
6570 &tag_size));
6571 } else {
6572 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6573 finish_output_size,
6574 &output_part_length,
6575 tag_buffer, tag_length,
6576 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6579 input_data->len),
6580 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006581 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006583
Paul Elliott243080c2021-07-21 19:01:17 +01006584 /* Test for not sending any additional data or data after setting non zero
6585 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006586
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006588
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6592 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6595 finish_output_size,
6596 &output_part_length,
6597 tag_buffer, tag_length,
6598 &tag_size),
6599 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006600
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006602
Paul Elliott243080c2021-07-21 19:01:17 +01006603 /* Test for not sending any additional data or data after setting non-zero
6604 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006607
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006609
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6611 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006612
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6614 finish_output_size,
6615 &output_part_length,
6616 tag_buffer,
6617 tag_length),
6618 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006621
Paul Elliott243080c2021-07-21 19:01:17 +01006622 /* Test for not sending any additional data after setting a non-zero length
6623 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006624
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006628
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6630 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006631
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6633 input_data->len, output_data,
6634 output_size, &output_length),
6635 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006636
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006638
Paul Elliottf94bd992021-09-19 18:15:59 +01006639 /* Test for not sending any data after setting a non-zero length for it.*/
6640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006642
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006644
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6646 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6649 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6652 finish_output_size,
6653 &output_part_length,
6654 tag_buffer, tag_length,
6655 &tag_size),
6656 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006659
Paul Elliottb0450fe2021-09-01 15:06:26 +01006660 /* Test for sending too much additional data after setting lengths. */
6661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006663
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006665
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006667
6668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6670 additional_data->len),
6671 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006674
Paul Elliotta2a09b02021-09-22 14:56:40 +01006675 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006678
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006680
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6682 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006683
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6685 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006686
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6688 1),
6689 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006690
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006692
Paul Elliottb0450fe2021-09-01 15:06:26 +01006693 /* Test for sending too much data after setting lengths. */
6694
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006696
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006698
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6702 input_data->len, output_data,
6703 output_size, &output_length),
6704 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006705
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006707
Paul Elliotta2a09b02021-09-22 14:56:40 +01006708 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006709
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006713
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6715 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6718 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6721 input_data->len, output_data,
6722 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006723
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6725 1, output_data,
6726 output_size, &output_length),
6727 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006730
Paul Elliottc23a9a02021-06-21 18:32:46 +01006731 /* Test sending additional data after data. */
6732
Gilles Peskine449bd832023-01-11 14:50:10 +01006733 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006734
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006736
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 if (operation.alg != PSA_ALG_CCM) {
6738 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6739 input_data->len, output_data,
6740 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006741
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6743 additional_data->len),
6744 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006747
Paul Elliott534d0b42021-06-22 19:15:20 +01006748 /* Test calling finish on decryption. */
6749
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6755 finish_output_size,
6756 &output_part_length,
6757 tag_buffer, tag_length,
6758 &tag_size),
6759 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006762
6763 /* Test calling verify on encryption. */
6764
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006766
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6770 finish_output_size,
6771 &output_part_length,
6772 tag_buffer,
6773 tag_length),
6774 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006775
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006777
6778
Paul Elliottc23a9a02021-06-21 18:32:46 +01006779exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 psa_destroy_key(key);
6781 psa_aead_abort(&operation);
6782 mbedtls_free(output_data);
6783 mbedtls_free(final_data);
6784 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006785}
6786/* END_CASE */
6787
6788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006789void signature_size(int type_arg,
6790 int bits,
6791 int alg_arg,
6792 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006793{
6794 psa_key_type_t type = type_arg;
6795 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006797
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006799
Gilles Peskinee59236f2018-01-27 23:32:46 +01006800exit:
6801 ;
6802}
6803/* END_CASE */
6804
6805/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006806void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6807 int alg_arg, data_t *input_data,
6808 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006809{
Ronald Cron5425a212020-08-04 14:58:35 +02006810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006811 psa_key_type_t key_type = key_type_arg;
6812 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006813 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006814 unsigned char *signature = NULL;
6815 size_t signature_size;
6816 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6822 psa_set_key_algorithm(&attributes, alg);
6823 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6826 &key));
6827 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6828 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006829
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006830 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006831 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6833 key_bits, alg);
6834 TEST_ASSERT(signature_size != 0);
6835 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006836 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006837
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006838 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 PSA_ASSERT(psa_sign_hash(key, alg,
6840 input_data->x, input_data->len,
6841 signature, signature_size,
6842 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006843 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006844 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006845 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006846
6847exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006848 /*
6849 * Key attributes may have been returned by psa_get_key_attributes()
6850 * thus reset them as required.
6851 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 psa_destroy_key(key);
6855 mbedtls_free(signature);
6856 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006857}
6858/* END_CASE */
6859
Paul Elliott712d5122022-12-07 14:03:10 +00006860/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006861/**
6862 * sign_hash_interruptible() test intentions:
6863 *
6864 * Note: This test can currently only handle ECDSA.
6865 *
6866 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006867 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006868 *
6869 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6870 * expected for different max_ops values.
6871 *
6872 * 3. Test that the number of ops done prior to start and after abort is zero
6873 * and that each successful stage completes some ops (this is not mandated by
6874 * the PSA specification, but is currently the case).
6875 *
6876 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6877 * complete() calls does not alter the number of ops returned.
6878 */
Paul Elliott712d5122022-12-07 14:03:10 +00006879void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6880 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006881 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006882{
6883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6884 psa_key_type_t key_type = key_type_arg;
6885 psa_algorithm_t alg = alg_arg;
6886 size_t key_bits;
6887 unsigned char *signature = NULL;
6888 size_t signature_size;
6889 size_t signature_length = 0xdeadbeef;
6890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6891 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006892 uint32_t num_ops = 0;
6893 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006894 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006895 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006896 size_t min_completes = 0;
6897 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006898
Paul Elliott712d5122022-12-07 14:03:10 +00006899 psa_sign_hash_interruptible_operation_t operation =
6900 psa_sign_hash_interruptible_operation_init();
6901
6902 PSA_ASSERT(psa_crypto_init());
6903
6904 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6905 psa_set_key_algorithm(&attributes, alg);
6906 psa_set_key_type(&attributes, key_type);
6907
6908 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6909 &key));
6910 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6911 key_bits = psa_get_key_bits(&attributes);
6912
6913 /* Allocate a buffer which has the size advertised by the
6914 * library. */
6915 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6916 key_bits, alg);
6917 TEST_ASSERT(signature_size != 0);
6918 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006919 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006920
Paul Elliott0c683352022-12-16 19:16:56 +00006921 psa_interruptible_set_max_ops(max_ops);
6922
Paul Elliott6f600372023-02-06 18:41:05 +00006923 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6924 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006925
Paul Elliott712d5122022-12-07 14:03:10 +00006926 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6927 TEST_ASSERT(num_ops_prior == 0);
6928
6929 /* Start performing the signature. */
6930 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6931 input_data->x, input_data->len));
6932
6933 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6934 TEST_ASSERT(num_ops_prior == 0);
6935
6936 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006937 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006938 status = psa_sign_hash_complete(&operation, signature, signature_size,
6939 &signature_length);
6940
Paul Elliott0c683352022-12-16 19:16:56 +00006941 num_completes++;
6942
Paul Elliott712d5122022-12-07 14:03:10 +00006943 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6944 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006945 /* We are asserting here that every complete makes progress
6946 * (completes some ops), which is true of the internal
6947 * implementation and probably any implementation, however this is
6948 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006949 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006950
Paul Elliott712d5122022-12-07 14:03:10 +00006951 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006952
6953 /* Ensure calling get_num_ops() twice still returns the same
6954 * number of ops as previously reported. */
6955 num_ops = psa_sign_hash_get_num_ops(&operation);
6956
6957 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006958 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006959 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006960
6961 TEST_ASSERT(status == PSA_SUCCESS);
6962
Paul Elliott0c683352022-12-16 19:16:56 +00006963 TEST_LE_U(min_completes, num_completes);
6964 TEST_LE_U(num_completes, max_completes);
6965
Paul Elliott712d5122022-12-07 14:03:10 +00006966 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006967 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006968 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006969
6970 PSA_ASSERT(psa_sign_hash_abort(&operation));
6971
Paul Elliott59ad9452022-12-18 15:09:02 +00006972 num_ops = psa_sign_hash_get_num_ops(&operation);
6973 TEST_ASSERT(num_ops == 0);
6974
Paul Elliott712d5122022-12-07 14:03:10 +00006975exit:
6976
6977 /*
6978 * Key attributes may have been returned by psa_get_key_attributes()
6979 * thus reset them as required.
6980 */
6981 psa_reset_key_attributes(&attributes);
6982
6983 psa_destroy_key(key);
6984 mbedtls_free(signature);
6985 PSA_DONE();
6986}
6987/* END_CASE */
6988
Gilles Peskine20035e32018-02-03 22:44:14 +01006989/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006990void sign_hash_fail(int key_type_arg, data_t *key_data,
6991 int alg_arg, data_t *input_data,
6992 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006993{
Ronald Cron5425a212020-08-04 14:58:35 +02006994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006995 psa_key_type_t key_type = key_type_arg;
6996 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006997 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006998 psa_status_t actual_status;
6999 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01007000 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01007001 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01007003
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007004 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01007005
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01007007
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
7009 psa_set_key_algorithm(&attributes, alg);
7010 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07007011
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7013 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01007014
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 actual_status = psa_sign_hash(key, alg,
7016 input_data->x, input_data->len,
7017 signature, signature_size,
7018 &signature_length);
7019 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02007020 /* The value of *signature_length is unspecified on error, but
7021 * whatever it is, it should be less than signature_size, so that
7022 * if the caller tries to read *signature_length bytes without
7023 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01007025
7026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 psa_reset_key_attributes(&attributes);
7028 psa_destroy_key(key);
7029 mbedtls_free(signature);
7030 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01007031}
7032/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03007033
Paul Elliott91007972022-12-16 12:21:24 +00007034/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007035/**
7036 * sign_hash_fail_interruptible() test intentions:
7037 *
7038 * Note: This test can currently only handle ECDSA.
7039 *
7040 * 1. Test that various failure cases for interruptible sign hash fail with the
7041 * correct error codes, and at the correct point (at start or during
7042 * complete).
7043 *
7044 * 2. Test the number of calls to psa_sign_hash_complete() required are as
7045 * expected for different max_ops values.
7046 *
7047 * 3. Test that the number of ops done prior to start and after abort is zero
7048 * and that each successful stage completes some ops (this is not mandated by
7049 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007050 *
7051 * 4. Check that calling complete() when start() fails and complete()
7052 * after completion results in a BAD_STATE error.
7053 *
7054 * 5. Check that calling start() again after start fails results in a BAD_STATE
7055 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007056 */
Paul Elliott91007972022-12-16 12:21:24 +00007057void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7058 int alg_arg, data_t *input_data,
7059 int signature_size_arg,
7060 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007061 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007062 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007063{
7064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7065 psa_key_type_t key_type = key_type_arg;
7066 psa_algorithm_t alg = alg_arg;
7067 size_t signature_size = signature_size_arg;
7068 psa_status_t actual_status;
7069 psa_status_t expected_start_status = expected_start_status_arg;
7070 psa_status_t expected_complete_status = expected_complete_status_arg;
7071 unsigned char *signature = NULL;
7072 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007073 uint32_t num_ops = 0;
7074 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007075 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007076 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007077 size_t min_completes = 0;
7078 size_t max_completes = 0;
7079
Paul Elliott91007972022-12-16 12:21:24 +00007080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7081 psa_sign_hash_interruptible_operation_t operation =
7082 psa_sign_hash_interruptible_operation_init();
7083
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007084 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00007085
7086 PSA_ASSERT(psa_crypto_init());
7087
7088 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
7089 psa_set_key_algorithm(&attributes, alg);
7090 psa_set_key_type(&attributes, key_type);
7091
7092 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7093 &key));
7094
Paul Elliott0c683352022-12-16 19:16:56 +00007095 psa_interruptible_set_max_ops(max_ops);
7096
Paul Elliott6f600372023-02-06 18:41:05 +00007097 interruptible_signverify_get_minmax_completes(max_ops,
7098 expected_complete_status,
7099 &min_completes,
7100 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007101
Paul Elliott91007972022-12-16 12:21:24 +00007102 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
7103 TEST_ASSERT(num_ops_prior == 0);
7104
7105 /* Start performing the signature. */
7106 actual_status = psa_sign_hash_start(&operation, key, alg,
7107 input_data->x, input_data->len);
7108
7109 TEST_EQUAL(actual_status, expected_start_status);
7110
Paul Elliottc9774412023-02-06 15:14:07 +00007111 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007112 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007113 * start failed. */
7114 actual_status = psa_sign_hash_complete(&operation, signature,
7115 signature_size,
7116 &signature_length);
7117
7118 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7119
7120 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007121 actual_status = psa_sign_hash_start(&operation, key, alg,
7122 input_data->x, input_data->len);
7123
7124 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7125 }
7126
Paul Elliott91007972022-12-16 12:21:24 +00007127 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
7128 TEST_ASSERT(num_ops_prior == 0);
7129
Paul Elliott91007972022-12-16 12:21:24 +00007130 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007131 do {
Paul Elliott91007972022-12-16 12:21:24 +00007132 actual_status = psa_sign_hash_complete(&operation, signature,
7133 signature_size,
7134 &signature_length);
7135
Paul Elliott0c683352022-12-16 19:16:56 +00007136 num_completes++;
7137
Paul Elliott334d7262023-01-20 17:29:41 +00007138 if (actual_status == PSA_SUCCESS ||
7139 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007140 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007141 /* We are asserting here that every complete makes progress
7142 * (completes some ops), which is true of the internal
7143 * implementation and probably any implementation, however this is
7144 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007145 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007146
Paul Elliott91007972022-12-16 12:21:24 +00007147 num_ops_prior = num_ops;
7148 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007149 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007150
Paul Elliottc9774412023-02-06 15:14:07 +00007151 TEST_EQUAL(actual_status, expected_complete_status);
7152
Paul Elliottefebad02023-02-15 16:56:45 +00007153 /* Check that another complete returns BAD_STATE. */
7154 actual_status = psa_sign_hash_complete(&operation, signature,
7155 signature_size,
7156 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00007157
Paul Elliottefebad02023-02-15 16:56:45 +00007158 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007159
Paul Elliott91007972022-12-16 12:21:24 +00007160 PSA_ASSERT(psa_sign_hash_abort(&operation));
7161
Paul Elliott59ad9452022-12-18 15:09:02 +00007162 num_ops = psa_sign_hash_get_num_ops(&operation);
7163 TEST_ASSERT(num_ops == 0);
7164
Paul Elliott91007972022-12-16 12:21:24 +00007165 /* The value of *signature_length is unspecified on error, but
7166 * whatever it is, it should be less than signature_size, so that
7167 * if the caller tries to read *signature_length bytes without
7168 * checking the error code then they don't overflow a buffer. */
7169 TEST_LE_U(signature_length, signature_size);
7170
Paul Elliott0c683352022-12-16 19:16:56 +00007171 TEST_LE_U(min_completes, num_completes);
7172 TEST_LE_U(num_completes, max_completes);
7173
Paul Elliott91007972022-12-16 12:21:24 +00007174exit:
7175 psa_reset_key_attributes(&attributes);
7176 psa_destroy_key(key);
7177 mbedtls_free(signature);
7178 PSA_DONE();
7179}
7180/* END_CASE */
7181
mohammad16038cc1cee2018-03-28 01:21:33 +03007182/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007183void sign_verify_hash(int key_type_arg, data_t *key_data,
7184 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02007185{
Ronald Cron5425a212020-08-04 14:58:35 +02007186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02007187 psa_key_type_t key_type = key_type_arg;
7188 psa_algorithm_t alg = alg_arg;
7189 size_t key_bits;
7190 unsigned char *signature = NULL;
7191 size_t signature_size;
7192 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02007194
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02007196
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
7198 psa_set_key_algorithm(&attributes, alg);
7199 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02007200
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7202 &key));
7203 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7204 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02007205
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007206 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02007207 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7209 key_bits, alg);
7210 TEST_ASSERT(signature_size != 0);
7211 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007212 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02007213
7214 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 PSA_ASSERT(psa_sign_hash(key, alg,
7216 input_data->x, input_data->len,
7217 signature, signature_size,
7218 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007219 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 TEST_LE_U(signature_length, signature_size);
7221 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02007222
7223 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 PSA_ASSERT(psa_verify_hash(key, alg,
7225 input_data->x, input_data->len,
7226 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007227
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02007229 /* Flip a bit in the input and verify that the signature is now
7230 * detected as invalid. Flip a bit at the beginning, not at the end,
7231 * because ECDSA may ignore the last few bits of the input. */
7232 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 TEST_EQUAL(psa_verify_hash(key, alg,
7234 input_data->x, input_data->len,
7235 signature, signature_length),
7236 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02007237 }
7238
7239exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007240 /*
7241 * Key attributes may have been returned by psa_get_key_attributes()
7242 * thus reset them as required.
7243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007245
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 psa_destroy_key(key);
7247 mbedtls_free(signature);
7248 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02007249}
7250/* END_CASE */
7251
Paul Elliott712d5122022-12-07 14:03:10 +00007252/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007253/**
7254 * sign_verify_hash_interruptible() test intentions:
7255 *
7256 * Note: This test can currently only handle ECDSA.
7257 *
Paul Elliott8c092052023-03-06 17:49:14 +00007258 * 1. Test that we can sign an input hash with the given keypair and then
7259 * afterwards verify that signature. This is currently the only way to test
7260 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00007261 *
7262 * 2. Test that after corrupting the hash, the verification detects an invalid
7263 * signature.
7264 *
7265 * 3. Test the number of calls to psa_sign_hash_complete() required are as
7266 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00007267 *
7268 * 4. Test that the number of ops done prior to starting signing and after abort
7269 * is zero and that each successful signing stage completes some ops (this is
7270 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00007271 */
Paul Elliott712d5122022-12-07 14:03:10 +00007272void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00007273 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007274 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007275{
7276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7277 psa_key_type_t key_type = key_type_arg;
7278 psa_algorithm_t alg = alg_arg;
7279 size_t key_bits;
7280 unsigned char *signature = NULL;
7281 size_t signature_size;
7282 size_t signature_length = 0xdeadbeef;
7283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7284 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007285 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00007286 uint32_t num_ops = 0;
7287 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007288 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007289 size_t min_completes = 0;
7290 size_t max_completes = 0;
7291
Paul Elliott712d5122022-12-07 14:03:10 +00007292 psa_sign_hash_interruptible_operation_t sign_operation =
7293 psa_sign_hash_interruptible_operation_init();
7294 psa_verify_hash_interruptible_operation_t verify_operation =
7295 psa_verify_hash_interruptible_operation_init();
7296
7297 PSA_ASSERT(psa_crypto_init());
7298
Paul Elliott0c683352022-12-16 19:16:56 +00007299 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7300 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007301 psa_set_key_algorithm(&attributes, alg);
7302 psa_set_key_type(&attributes, key_type);
7303
7304 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7305 &key));
7306 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7307 key_bits = psa_get_key_bits(&attributes);
7308
7309 /* Allocate a buffer which has the size advertised by the
7310 * library. */
7311 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7312 key_bits, alg);
7313 TEST_ASSERT(signature_size != 0);
7314 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007315 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007316
Paul Elliott0c683352022-12-16 19:16:56 +00007317 psa_interruptible_set_max_ops(max_ops);
7318
Paul Elliott6f600372023-02-06 18:41:05 +00007319 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7320 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007321
Paul Elliott7c173082023-02-26 18:44:45 +00007322 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7323 TEST_ASSERT(num_ops_prior == 0);
7324
Paul Elliott712d5122022-12-07 14:03:10 +00007325 /* Start performing the signature. */
7326 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7327 input_data->x, input_data->len));
7328
Paul Elliott7c173082023-02-26 18:44:45 +00007329 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7330 TEST_ASSERT(num_ops_prior == 0);
7331
Paul Elliott712d5122022-12-07 14:03:10 +00007332 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007333 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007334
Paul Elliott0c683352022-12-16 19:16:56 +00007335 status = psa_sign_hash_complete(&sign_operation, signature,
7336 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007337 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007338
7339 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007340
7341 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7342 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7343 /* 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. */
7347 TEST_ASSERT(num_ops > num_ops_prior);
7348
7349 num_ops_prior = num_ops;
7350 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007351 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007352
7353 TEST_ASSERT(status == PSA_SUCCESS);
7354
Paul Elliott0c683352022-12-16 19:16:56 +00007355 TEST_LE_U(min_completes, num_completes);
7356 TEST_LE_U(num_completes, max_completes);
7357
Paul Elliott712d5122022-12-07 14:03:10 +00007358 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7359
Paul Elliott7c173082023-02-26 18:44:45 +00007360 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7361 TEST_ASSERT(num_ops == 0);
7362
Paul Elliott712d5122022-12-07 14:03:10 +00007363 /* Check that the signature length looks sensible. */
7364 TEST_LE_U(signature_length, signature_size);
7365 TEST_ASSERT(signature_length > 0);
7366
Paul Elliott0c683352022-12-16 19:16:56 +00007367 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007368
7369 /* Start verification. */
7370 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7371 input_data->x, input_data->len,
7372 signature, signature_length));
7373
7374 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007375 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007376 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007377
7378 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007379 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007380
7381 TEST_ASSERT(status == PSA_SUCCESS);
7382
Paul Elliott0c683352022-12-16 19:16:56 +00007383 TEST_LE_U(min_completes, num_completes);
7384 TEST_LE_U(num_completes, max_completes);
7385
Paul Elliott712d5122022-12-07 14:03:10 +00007386 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7387
7388 verify_operation = psa_verify_hash_interruptible_operation_init();
7389
7390 if (input_data->len != 0) {
7391 /* Flip a bit in the input and verify that the signature is now
7392 * detected as invalid. Flip a bit at the beginning, not at the end,
7393 * because ECDSA may ignore the last few bits of the input. */
7394 input_data->x[0] ^= 1;
7395
Paul Elliott712d5122022-12-07 14:03:10 +00007396 /* Start verification. */
7397 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7398 input_data->x, input_data->len,
7399 signature, signature_length));
7400
7401 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007402 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007403 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007404 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007405
7406 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7407 }
7408
7409 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7410
7411exit:
7412 /*
7413 * Key attributes may have been returned by psa_get_key_attributes()
7414 * thus reset them as required.
7415 */
7416 psa_reset_key_attributes(&attributes);
7417
7418 psa_destroy_key(key);
7419 mbedtls_free(signature);
7420 PSA_DONE();
7421}
7422/* END_CASE */
7423
Gilles Peskine9911b022018-06-29 17:30:48 +02007424/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007425void verify_hash(int key_type_arg, data_t *key_data,
7426 int alg_arg, data_t *hash_data,
7427 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007428{
Ronald Cron5425a212020-08-04 14:58:35 +02007429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007430 psa_key_type_t key_type = key_type_arg;
7431 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007433
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007435
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007437
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7439 psa_set_key_algorithm(&attributes, alg);
7440 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007441
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7443 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 PSA_ASSERT(psa_verify_hash(key, alg,
7446 hash_data->x, hash_data->len,
7447 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007448
itayzafrir5c753392018-05-08 11:18:38 +03007449exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 psa_reset_key_attributes(&attributes);
7451 psa_destroy_key(key);
7452 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007453}
7454/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007455
Paul Elliott712d5122022-12-07 14:03:10 +00007456/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007457/**
7458 * verify_hash_interruptible() test intentions:
7459 *
7460 * Note: This test can currently only handle ECDSA.
7461 *
7462 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007463 * only). Given this test only does verification it can accept public keys as
7464 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007465 *
7466 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7467 * expected for different max_ops values.
7468 *
7469 * 3. Test that the number of ops done prior to start and after abort is zero
7470 * and that each successful stage completes some ops (this is not mandated by
7471 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007472 *
7473 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7474 * complete() calls does not alter the number of ops returned.
7475 *
7476 * 5. Test that after corrupting the hash, the verification detects an invalid
7477 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007478 */
Paul Elliott712d5122022-12-07 14:03:10 +00007479void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7480 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007481 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007482{
7483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7484 psa_key_type_t key_type = key_type_arg;
7485 psa_algorithm_t alg = alg_arg;
7486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7487 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007488 uint32_t num_ops = 0;
7489 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007490 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007491 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007492 size_t min_completes = 0;
7493 size_t max_completes = 0;
7494
Paul Elliott712d5122022-12-07 14:03:10 +00007495 psa_verify_hash_interruptible_operation_t operation =
7496 psa_verify_hash_interruptible_operation_init();
7497
7498 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7499
7500 PSA_ASSERT(psa_crypto_init());
7501
7502 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7503 psa_set_key_algorithm(&attributes, alg);
7504 psa_set_key_type(&attributes, key_type);
7505
7506 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7507 &key));
7508
Paul Elliott0c683352022-12-16 19:16:56 +00007509 psa_interruptible_set_max_ops(max_ops);
7510
Paul Elliott6f600372023-02-06 18:41:05 +00007511 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7512 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007513
Paul Elliott712d5122022-12-07 14:03:10 +00007514 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7515
7516 TEST_ASSERT(num_ops_prior == 0);
7517
7518 /* Start verification. */
7519 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7520 hash_data->x, hash_data->len,
7521 signature_data->x, signature_data->len)
7522 );
7523
7524 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7525
7526 TEST_ASSERT(num_ops_prior == 0);
7527
7528 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007529 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007530 status = psa_verify_hash_complete(&operation);
7531
Paul Elliott0c683352022-12-16 19:16:56 +00007532 num_completes++;
7533
Paul Elliott712d5122022-12-07 14:03:10 +00007534 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7535 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007536 /* We are asserting here that every complete makes progress
7537 * (completes some ops), which is true of the internal
7538 * implementation and probably any implementation, however this is
7539 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007540 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007541
Paul Elliott712d5122022-12-07 14:03:10 +00007542 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007543
7544 /* Ensure calling get_num_ops() twice still returns the same
7545 * number of ops as previously reported. */
7546 num_ops = psa_verify_hash_get_num_ops(&operation);
7547
7548 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007549 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007550 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007551
7552 TEST_ASSERT(status == PSA_SUCCESS);
7553
Paul Elliott0c683352022-12-16 19:16:56 +00007554 TEST_LE_U(min_completes, num_completes);
7555 TEST_LE_U(num_completes, max_completes);
7556
Paul Elliott712d5122022-12-07 14:03:10 +00007557 PSA_ASSERT(psa_verify_hash_abort(&operation));
7558
Paul Elliott59ad9452022-12-18 15:09:02 +00007559 num_ops = psa_verify_hash_get_num_ops(&operation);
7560 TEST_ASSERT(num_ops == 0);
7561
Paul Elliott8359c142023-02-24 18:40:10 +00007562 if (hash_data->len != 0) {
7563 /* Flip a bit in the hash and verify that the signature is now detected
7564 * as invalid. Flip a bit at the beginning, not at the end, because
7565 * ECDSA may ignore the last few bits of the input. */
7566 hash_data->x[0] ^= 1;
7567
7568 /* Start verification. */
7569 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7570 hash_data->x, hash_data->len,
7571 signature_data->x, signature_data->len));
7572
7573 /* Continue performing the signature until complete. */
7574 do {
7575 status = psa_verify_hash_complete(&operation);
7576 } while (status == PSA_OPERATION_INCOMPLETE);
7577
7578 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7579 }
7580
Paul Elliott712d5122022-12-07 14:03:10 +00007581exit:
7582 psa_reset_key_attributes(&attributes);
7583 psa_destroy_key(key);
7584 PSA_DONE();
7585}
7586/* END_CASE */
7587
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007588/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007589void verify_hash_fail(int key_type_arg, data_t *key_data,
7590 int alg_arg, data_t *hash_data,
7591 data_t *signature_data,
7592 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007593{
Ronald Cron5425a212020-08-04 14:58:35 +02007594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007595 psa_key_type_t key_type = key_type_arg;
7596 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007597 psa_status_t actual_status;
7598 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007600
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007602
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7604 psa_set_key_algorithm(&attributes, alg);
7605 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7608 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 actual_status = psa_verify_hash(key, alg,
7611 hash_data->x, hash_data->len,
7612 signature_data->x, signature_data->len);
7613 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007614
7615exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 psa_reset_key_attributes(&attributes);
7617 psa_destroy_key(key);
7618 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007619}
7620/* END_CASE */
7621
Paul Elliott91007972022-12-16 12:21:24 +00007622/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007623/**
7624 * verify_hash_fail_interruptible() test intentions:
7625 *
7626 * Note: This test can currently only handle ECDSA.
7627 *
7628 * 1. Test that various failure cases for interruptible verify hash fail with
7629 * the correct error codes, and at the correct point (at start or during
7630 * complete).
7631 *
7632 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7633 * expected for different max_ops values.
7634 *
7635 * 3. Test that the number of ops done prior to start and after abort is zero
7636 * and that each successful stage completes some ops (this is not mandated by
7637 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007638 *
7639 * 4. Check that calling complete() when start() fails and complete()
7640 * after completion results in a BAD_STATE error.
7641 *
7642 * 5. Check that calling start() again after start fails results in a BAD_STATE
7643 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007644 */
Paul Elliott91007972022-12-16 12:21:24 +00007645void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7646 int alg_arg, data_t *hash_data,
7647 data_t *signature_data,
7648 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007649 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007650 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007651{
7652 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7653 psa_key_type_t key_type = key_type_arg;
7654 psa_algorithm_t alg = alg_arg;
7655 psa_status_t actual_status;
7656 psa_status_t expected_start_status = expected_start_status_arg;
7657 psa_status_t expected_complete_status = expected_complete_status_arg;
7658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007659 uint32_t num_ops = 0;
7660 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007661 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007662 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007663 size_t min_completes = 0;
7664 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007665 psa_verify_hash_interruptible_operation_t operation =
7666 psa_verify_hash_interruptible_operation_init();
7667
7668 PSA_ASSERT(psa_crypto_init());
7669
7670 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7671 psa_set_key_algorithm(&attributes, alg);
7672 psa_set_key_type(&attributes, key_type);
7673
7674 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7675 &key));
7676
Paul Elliott0c683352022-12-16 19:16:56 +00007677 psa_interruptible_set_max_ops(max_ops);
7678
Paul Elliott6f600372023-02-06 18:41:05 +00007679 interruptible_signverify_get_minmax_completes(max_ops,
7680 expected_complete_status,
7681 &min_completes,
7682 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007683
Paul Elliott91007972022-12-16 12:21:24 +00007684 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7685 TEST_ASSERT(num_ops_prior == 0);
7686
7687 /* Start verification. */
7688 actual_status = psa_verify_hash_start(&operation, key, alg,
7689 hash_data->x, hash_data->len,
7690 signature_data->x,
7691 signature_data->len);
7692
7693 TEST_EQUAL(actual_status, expected_start_status);
7694
Paul Elliottc9774412023-02-06 15:14:07 +00007695 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007696 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007697 * start failed. */
7698 actual_status = psa_verify_hash_complete(&operation);
7699
7700 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7701
7702 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007703 actual_status = psa_verify_hash_start(&operation, key, alg,
7704 hash_data->x, hash_data->len,
7705 signature_data->x,
7706 signature_data->len);
7707
7708 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7709 }
7710
Paul Elliott91007972022-12-16 12:21:24 +00007711 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7712 TEST_ASSERT(num_ops_prior == 0);
7713
Paul Elliott91007972022-12-16 12:21:24 +00007714 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007715 do {
Paul Elliott91007972022-12-16 12:21:24 +00007716 actual_status = psa_verify_hash_complete(&operation);
7717
Paul Elliott0c683352022-12-16 19:16:56 +00007718 num_completes++;
7719
Paul Elliott334d7262023-01-20 17:29:41 +00007720 if (actual_status == PSA_SUCCESS ||
7721 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007722 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007723 /* We are asserting here that every complete makes progress
7724 * (completes some ops), which is true of the internal
7725 * implementation and probably any implementation, however this is
7726 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007727 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007728
Paul Elliott91007972022-12-16 12:21:24 +00007729 num_ops_prior = num_ops;
7730 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007731 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007732
Paul Elliottc9774412023-02-06 15:14:07 +00007733 TEST_EQUAL(actual_status, expected_complete_status);
7734
Paul Elliottefebad02023-02-15 16:56:45 +00007735 /* Check that another complete returns BAD_STATE. */
7736 actual_status = psa_verify_hash_complete(&operation);
7737 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007738
Paul Elliott0c683352022-12-16 19:16:56 +00007739 TEST_LE_U(min_completes, num_completes);
7740 TEST_LE_U(num_completes, max_completes);
7741
Paul Elliott91007972022-12-16 12:21:24 +00007742 PSA_ASSERT(psa_verify_hash_abort(&operation));
7743
Paul Elliott59ad9452022-12-18 15:09:02 +00007744 num_ops = psa_verify_hash_get_num_ops(&operation);
7745 TEST_ASSERT(num_ops == 0);
7746
Paul Elliott91007972022-12-16 12:21:24 +00007747exit:
7748 psa_reset_key_attributes(&attributes);
7749 psa_destroy_key(key);
7750 PSA_DONE();
7751}
7752/* END_CASE */
7753
Paul Elliott20a36062022-12-18 13:21:25 +00007754/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007755/**
7756 * interruptible_signverify_hash_state_test() test intentions:
7757 *
7758 * Note: This test can currently only handle ECDSA.
7759 *
7760 * 1. Test that calling the various interruptible sign and verify hash functions
7761 * in incorrect orders returns BAD_STATE errors.
7762 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007763void interruptible_signverify_hash_state_test(int key_type_arg,
7764 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007765{
7766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7767 psa_key_type_t key_type = key_type_arg;
7768 psa_algorithm_t alg = alg_arg;
7769 size_t key_bits;
7770 unsigned char *signature = NULL;
7771 size_t signature_size;
7772 size_t signature_length = 0xdeadbeef;
7773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7774 psa_sign_hash_interruptible_operation_t sign_operation =
7775 psa_sign_hash_interruptible_operation_init();
7776 psa_verify_hash_interruptible_operation_t verify_operation =
7777 psa_verify_hash_interruptible_operation_init();
7778
7779 PSA_ASSERT(psa_crypto_init());
7780
7781 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7782 PSA_KEY_USAGE_VERIFY_HASH);
7783 psa_set_key_algorithm(&attributes, alg);
7784 psa_set_key_type(&attributes, key_type);
7785
7786 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7787 &key));
7788 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7789 key_bits = psa_get_key_bits(&attributes);
7790
7791 /* Allocate a buffer which has the size advertised by the
7792 * library. */
7793 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7794 key_bits, alg);
7795 TEST_ASSERT(signature_size != 0);
7796 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007797 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007798
7799 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7800
7801 /* --- Attempt completes prior to starts --- */
7802 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7803 signature_size,
7804 &signature_length),
7805 PSA_ERROR_BAD_STATE);
7806
7807 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7808
7809 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7810 PSA_ERROR_BAD_STATE);
7811
7812 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7813
7814 /* --- Aborts in all other places. --- */
7815 psa_sign_hash_abort(&sign_operation);
7816
7817 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7818 input_data->x, input_data->len));
7819
7820 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7821
7822 psa_interruptible_set_max_ops(1);
7823
7824 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7825 input_data->x, input_data->len));
7826
7827 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7828 signature_size,
7829 &signature_length),
7830 PSA_OPERATION_INCOMPLETE);
7831
7832 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7833
7834 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7835
7836 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7837 input_data->x, input_data->len));
7838
7839 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7840 signature_size,
7841 &signature_length));
7842
7843 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7844
7845 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7846
7847 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7848 input_data->x, input_data->len,
7849 signature, signature_length));
7850
7851 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7852
7853 psa_interruptible_set_max_ops(1);
7854
7855 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7856 input_data->x, input_data->len,
7857 signature, signature_length));
7858
7859 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7860 PSA_OPERATION_INCOMPLETE);
7861
7862 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7863
7864 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7865
7866 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7867 input_data->x, input_data->len,
7868 signature, signature_length));
7869
7870 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7871
7872 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7873
7874 /* --- Attempt double starts. --- */
7875
7876 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7877 input_data->x, input_data->len));
7878
7879 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7880 input_data->x, input_data->len),
7881 PSA_ERROR_BAD_STATE);
7882
7883 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7884
7885 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7886 input_data->x, input_data->len,
7887 signature, signature_length));
7888
7889 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7890 input_data->x, input_data->len,
7891 signature, signature_length),
7892 PSA_ERROR_BAD_STATE);
7893
7894 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7895
Paul Elliott76d671a2023-02-07 17:45:18 +00007896exit:
7897 /*
7898 * Key attributes may have been returned by psa_get_key_attributes()
7899 * thus reset them as required.
7900 */
7901 psa_reset_key_attributes(&attributes);
7902
7903 psa_destroy_key(key);
7904 mbedtls_free(signature);
7905 PSA_DONE();
7906}
7907/* END_CASE */
7908
7909/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007910/**
Paul Elliottc2033502023-02-26 17:09:14 +00007911 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007912 *
7913 * Note: This test can currently only handle ECDSA.
7914 *
7915 * 1. Test various edge cases in the interruptible sign and verify hash
7916 * interfaces.
7917 */
Paul Elliottc2033502023-02-26 17:09:14 +00007918void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007919 data_t *key_data, int alg_arg, data_t *input_data)
7920{
7921 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7922 psa_key_type_t key_type = key_type_arg;
7923 psa_algorithm_t alg = alg_arg;
7924 size_t key_bits;
7925 unsigned char *signature = NULL;
7926 size_t signature_size;
7927 size_t signature_length = 0xdeadbeef;
7928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7929 uint8_t *input_buffer = NULL;
7930 psa_sign_hash_interruptible_operation_t sign_operation =
7931 psa_sign_hash_interruptible_operation_init();
7932 psa_verify_hash_interruptible_operation_t verify_operation =
7933 psa_verify_hash_interruptible_operation_init();
7934
7935 PSA_ASSERT(psa_crypto_init());
7936
7937 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7938 PSA_KEY_USAGE_VERIFY_HASH);
7939 psa_set_key_algorithm(&attributes, alg);
7940 psa_set_key_type(&attributes, key_type);
7941
7942 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7943 &key));
7944 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7945 key_bits = psa_get_key_bits(&attributes);
7946
7947 /* Allocate a buffer which has the size advertised by the
7948 * library. */
7949 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7950 key_bits, alg);
7951 TEST_ASSERT(signature_size != 0);
7952 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007953 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007954
Paul Elliott20a36062022-12-18 13:21:25 +00007955 /* --- Change function inputs mid run, to cause an error (sign only,
7956 * verify passes all inputs to start. --- */
7957
7958 psa_interruptible_set_max_ops(1);
7959
7960 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7961 input_data->x, input_data->len));
7962
7963 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7964 signature_size,
7965 &signature_length),
7966 PSA_OPERATION_INCOMPLETE);
7967
7968 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7969 0,
7970 &signature_length),
7971 PSA_ERROR_BUFFER_TOO_SMALL);
7972
Paul Elliottc9774412023-02-06 15:14:07 +00007973 /* And test that this invalidates the operation. */
7974 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7975 0,
7976 &signature_length),
7977 PSA_ERROR_BAD_STATE);
7978
Paul Elliott20a36062022-12-18 13:21:25 +00007979 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7980
Paul Elliottf9c91a72023-02-05 18:06:38 +00007981 /* Trash the hash buffer in between start and complete, to ensure
7982 * no reliance on external buffers. */
7983 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7984
Paul Elliott6c68df42023-10-23 15:33:37 +01007985 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007986
7987 memcpy(input_buffer, input_data->x, input_data->len);
7988
7989 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7990 input_buffer, input_data->len));
7991
7992 memset(input_buffer, '!', input_data->len);
7993 mbedtls_free(input_buffer);
7994 input_buffer = NULL;
7995
7996 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7997 signature_size,
7998 &signature_length));
7999
8000 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8001
Paul Elliott6c68df42023-10-23 15:33:37 +01008002 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00008003
8004 memcpy(input_buffer, input_data->x, input_data->len);
8005
8006 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8007 input_buffer, input_data->len,
8008 signature, signature_length));
8009
8010 memset(input_buffer, '!', input_data->len);
8011 mbedtls_free(input_buffer);
8012 input_buffer = NULL;
8013
8014 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
8015
8016 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8017
Paul Elliott20a36062022-12-18 13:21:25 +00008018exit:
8019 /*
8020 * Key attributes may have been returned by psa_get_key_attributes()
8021 * thus reset them as required.
8022 */
8023 psa_reset_key_attributes(&attributes);
8024
8025 psa_destroy_key(key);
8026 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01008027 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00008028 PSA_DONE();
8029}
8030/* END_CASE */
8031
Paul Elliotta4cb9092023-02-07 18:01:55 +00008032/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00008033/**
Paul Elliott57702242023-02-26 20:36:10 +00008034 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00008035 *
8036 * Note: This test can currently only handle ECDSA.
8037 *
8038 * 1. Test that setting max ops is reflected in both interruptible sign and
8039 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00008040 * 2. Test that changing the value of max_ops to unlimited during an operation
8041 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00008042 *
8043 * 3. Test that calling get_num_ops() between complete calls gives the same
8044 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00008045 */
Paul Elliott57702242023-02-26 20:36:10 +00008046void interruptible_signverify_hash_ops_tests(int key_type_arg,
8047 data_t *key_data, int alg_arg,
8048 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00008049{
8050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8051 psa_key_type_t key_type = key_type_arg;
8052 psa_algorithm_t alg = alg_arg;
8053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00008054 size_t key_bits;
8055 unsigned char *signature = NULL;
8056 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00008057 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00008058 uint32_t num_ops = 0;
8059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8060
Paul Elliotta4cb9092023-02-07 18:01:55 +00008061 psa_sign_hash_interruptible_operation_t sign_operation =
8062 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00008063 psa_verify_hash_interruptible_operation_t verify_operation =
8064 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00008065
8066 PSA_ASSERT(psa_crypto_init());
8067
8068 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
8069 PSA_KEY_USAGE_VERIFY_HASH);
8070 psa_set_key_algorithm(&attributes, alg);
8071 psa_set_key_type(&attributes, key_type);
8072
Paul Elliottf1743e22023-02-15 18:44:16 +00008073 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
8074 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8075 key_bits = psa_get_key_bits(&attributes);
8076
8077 /* Allocate a buffer which has the size advertised by the
8078 * library. */
8079 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8080
8081 TEST_ASSERT(signature_size != 0);
8082 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008083 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008084
8085 /* Check that default max ops gets set if we don't set it. */
8086 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8087 input_data->x, input_data->len));
8088
8089 TEST_EQUAL(psa_interruptible_get_max_ops(),
8090 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8091
8092 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8093
Paul Elliottf1743e22023-02-15 18:44:16 +00008094 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8095 input_data->x, input_data->len,
8096 signature, signature_size));
8097
8098 TEST_EQUAL(psa_interruptible_get_max_ops(),
8099 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8100
8101 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8102
Paul Elliotta4cb9092023-02-07 18:01:55 +00008103 /* Check that max ops gets set properly. */
8104
8105 psa_interruptible_set_max_ops(0xbeef);
8106
Paul Elliottf1743e22023-02-15 18:44:16 +00008107 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008108
Paul Elliott9e8819f2023-02-26 19:01:35 +00008109 /* --- Ensure changing the max ops mid operation works (operation should
8110 * complete successfully after setting max ops to unlimited --- */
8111 psa_interruptible_set_max_ops(1);
8112
8113 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8114 input_data->x, input_data->len));
8115
8116 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
8117 signature_size,
8118 &signature_length),
8119 PSA_OPERATION_INCOMPLETE);
8120
8121 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8122
8123 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
8124 signature_size,
8125 &signature_length));
8126
8127 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8128
8129 psa_interruptible_set_max_ops(1);
8130
8131 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8132 input_data->x, input_data->len,
8133 signature, signature_length));
8134
8135 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
8136 PSA_OPERATION_INCOMPLETE);
8137
8138 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8139
8140 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
8141
8142 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8143
Paul Elliottc1e04002023-02-26 20:27:23 +00008144 /* --- Test that not calling get_num_ops inbetween complete calls does not
8145 * result in lost ops. ---*/
8146
8147 psa_interruptible_set_max_ops(1);
8148
8149 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8150 input_data->x, input_data->len));
8151
8152 /* Continue performing the signature until complete. */
8153 do {
8154 status = psa_sign_hash_complete(&sign_operation, signature,
8155 signature_size,
8156 &signature_length);
8157
8158 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
8159
8160 } while (status == PSA_OPERATION_INCOMPLETE);
8161
8162 PSA_ASSERT(status);
8163
8164 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8165
8166 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8167 input_data->x, input_data->len));
8168
8169 /* Continue performing the signature until complete. */
8170 do {
8171 status = psa_sign_hash_complete(&sign_operation, signature,
8172 signature_size,
8173 &signature_length);
8174 } while (status == PSA_OPERATION_INCOMPLETE);
8175
8176 PSA_ASSERT(status);
8177
8178 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
8179
8180 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8181
8182 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8183 input_data->x, input_data->len,
8184 signature, signature_length));
8185
8186 /* Continue performing the verification until complete. */
8187 do {
8188 status = psa_verify_hash_complete(&verify_operation);
8189
8190 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
8191
8192 } while (status == PSA_OPERATION_INCOMPLETE);
8193
8194 PSA_ASSERT(status);
8195
8196 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8197
8198 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8199 input_data->x, input_data->len,
8200 signature, signature_length));
8201
8202 /* Continue performing the verification until complete. */
8203 do {
8204 status = psa_verify_hash_complete(&verify_operation);
8205
8206 } while (status == PSA_OPERATION_INCOMPLETE);
8207
8208 PSA_ASSERT(status);
8209
8210 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
8211
8212 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8213
Paul Elliotta4cb9092023-02-07 18:01:55 +00008214exit:
8215 /*
8216 * Key attributes may have been returned by psa_get_key_attributes()
8217 * thus reset them as required.
8218 */
8219 psa_reset_key_attributes(&attributes);
8220
8221 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00008222 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008223 PSA_DONE();
8224}
8225/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00008226
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008227/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008228void sign_message_deterministic(int key_type_arg,
8229 data_t *key_data,
8230 int alg_arg,
8231 data_t *input_data,
8232 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02008233{
8234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8235 psa_key_type_t key_type = key_type_arg;
8236 psa_algorithm_t alg = alg_arg;
8237 size_t key_bits;
8238 unsigned char *signature = NULL;
8239 size_t signature_size;
8240 size_t signature_length = 0xdeadbeef;
8241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8242
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02008244
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8246 psa_set_key_algorithm(&attributes, alg);
8247 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008248
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8250 &key));
8251 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8252 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008253
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8255 TEST_ASSERT(signature_size != 0);
8256 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008257 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008258
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 PSA_ASSERT(psa_sign_message(key, alg,
8260 input_data->x, input_data->len,
8261 signature, signature_size,
8262 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02008263
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008264 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008265 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008266
8267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008269
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 psa_destroy_key(key);
8271 mbedtls_free(signature);
8272 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02008273
8274}
8275/* END_CASE */
8276
8277/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008278void sign_message_fail(int key_type_arg,
8279 data_t *key_data,
8280 int alg_arg,
8281 data_t *input_data,
8282 int signature_size_arg,
8283 int expected_status_arg)
8284{
8285 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8286 psa_key_type_t key_type = key_type_arg;
8287 psa_algorithm_t alg = alg_arg;
8288 size_t signature_size = signature_size_arg;
8289 psa_status_t actual_status;
8290 psa_status_t expected_status = expected_status_arg;
8291 unsigned char *signature = NULL;
8292 size_t signature_length = 0xdeadbeef;
8293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8294
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008295 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008296
8297 PSA_ASSERT(psa_crypto_init());
8298
8299 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8300 psa_set_key_algorithm(&attributes, alg);
8301 psa_set_key_type(&attributes, key_type);
8302
8303 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8304 &key));
8305
8306 actual_status = psa_sign_message(key, alg,
8307 input_data->x, input_data->len,
8308 signature, signature_size,
8309 &signature_length);
8310 TEST_EQUAL(actual_status, expected_status);
8311 /* The value of *signature_length is unspecified on error, but
8312 * whatever it is, it should be less than signature_size, so that
8313 * if the caller tries to read *signature_length bytes without
8314 * checking the error code then they don't overflow a buffer. */
8315 TEST_LE_U(signature_length, signature_size);
8316
8317exit:
8318 psa_reset_key_attributes(&attributes);
8319 psa_destroy_key(key);
8320 mbedtls_free(signature);
8321 PSA_DONE();
8322}
8323/* END_CASE */
8324
8325/* BEGIN_CASE */
8326void sign_verify_message(int key_type_arg,
8327 data_t *key_data,
8328 int alg_arg,
8329 data_t *input_data)
8330{
8331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8332 psa_key_type_t key_type = key_type_arg;
8333 psa_algorithm_t alg = alg_arg;
8334 size_t key_bits;
8335 unsigned char *signature = NULL;
8336 size_t signature_size;
8337 size_t signature_length = 0xdeadbeef;
8338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8339
8340 PSA_ASSERT(psa_crypto_init());
8341
8342 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8343 PSA_KEY_USAGE_VERIFY_MESSAGE);
8344 psa_set_key_algorithm(&attributes, alg);
8345 psa_set_key_type(&attributes, key_type);
8346
8347 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8348 &key));
8349 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8350 key_bits = psa_get_key_bits(&attributes);
8351
8352 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8353 TEST_ASSERT(signature_size != 0);
8354 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008355 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008356
8357 PSA_ASSERT(psa_sign_message(key, alg,
8358 input_data->x, input_data->len,
8359 signature, signature_size,
8360 &signature_length));
8361 TEST_LE_U(signature_length, signature_size);
8362 TEST_ASSERT(signature_length > 0);
8363
8364 PSA_ASSERT(psa_verify_message(key, alg,
8365 input_data->x, input_data->len,
8366 signature, signature_length));
8367
8368 if (input_data->len != 0) {
8369 /* Flip a bit in the input and verify that the signature is now
8370 * detected as invalid. Flip a bit at the beginning, not at the end,
8371 * because ECDSA may ignore the last few bits of the input. */
8372 input_data->x[0] ^= 1;
8373 TEST_EQUAL(psa_verify_message(key, alg,
8374 input_data->x, input_data->len,
8375 signature, signature_length),
8376 PSA_ERROR_INVALID_SIGNATURE);
8377 }
8378
8379exit:
8380 psa_reset_key_attributes(&attributes);
8381
8382 psa_destroy_key(key);
8383 mbedtls_free(signature);
8384 PSA_DONE();
8385}
8386/* END_CASE */
8387
8388/* BEGIN_CASE */
8389void verify_message(int key_type_arg,
8390 data_t *key_data,
8391 int alg_arg,
8392 data_t *input_data,
8393 data_t *signature_data)
8394{
8395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8396 psa_key_type_t key_type = key_type_arg;
8397 psa_algorithm_t alg = alg_arg;
8398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8399
8400 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8401
8402 PSA_ASSERT(psa_crypto_init());
8403
8404 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8405 psa_set_key_algorithm(&attributes, alg);
8406 psa_set_key_type(&attributes, key_type);
8407
8408 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8409 &key));
8410
8411 PSA_ASSERT(psa_verify_message(key, alg,
8412 input_data->x, input_data->len,
8413 signature_data->x, signature_data->len));
8414
8415exit:
8416 psa_reset_key_attributes(&attributes);
8417 psa_destroy_key(key);
8418 PSA_DONE();
8419}
8420/* END_CASE */
8421
8422/* BEGIN_CASE */
8423void verify_message_fail(int key_type_arg,
8424 data_t *key_data,
8425 int alg_arg,
8426 data_t *hash_data,
8427 data_t *signature_data,
8428 int expected_status_arg)
8429{
8430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8431 psa_key_type_t key_type = key_type_arg;
8432 psa_algorithm_t alg = alg_arg;
8433 psa_status_t actual_status;
8434 psa_status_t expected_status = expected_status_arg;
8435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8436
8437 PSA_ASSERT(psa_crypto_init());
8438
8439 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8440 psa_set_key_algorithm(&attributes, alg);
8441 psa_set_key_type(&attributes, key_type);
8442
8443 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8444 &key));
8445
8446 actual_status = psa_verify_message(key, alg,
8447 hash_data->x, hash_data->len,
8448 signature_data->x,
8449 signature_data->len);
8450 TEST_EQUAL(actual_status, expected_status);
8451
8452exit:
8453 psa_reset_key_attributes(&attributes);
8454 psa_destroy_key(key);
8455 PSA_DONE();
8456}
8457/* END_CASE */
8458
8459/* BEGIN_CASE */
8460void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008461 data_t *key_data,
8462 int alg_arg,
8463 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 data_t *label,
8465 int expected_output_length_arg,
8466 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008467{
Ronald Cron5425a212020-08-04 14:58:35 +02008468 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008469 psa_key_type_t key_type = key_type_arg;
8470 psa_algorithm_t alg = alg_arg;
8471 size_t expected_output_length = expected_output_length_arg;
8472 size_t key_bits;
8473 unsigned char *output = NULL;
8474 size_t output_size;
8475 size_t output_length = ~0;
8476 psa_status_t actual_status;
8477 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008479
Gilles Peskine449bd832023-01-11 14:50:10 +01008480 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008481
Gilles Peskine656896e2018-06-29 19:12:28 +02008482 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8484 psa_set_key_algorithm(&attributes, alg);
8485 psa_set_key_type(&attributes, key_type);
8486 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8487 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008488
8489 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8491 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008492
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8494 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008495 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008496
8497 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008498 actual_status = psa_asymmetric_encrypt(key, alg,
8499 input_data->x, input_data->len,
8500 label->x, label->len,
8501 output, output_size,
8502 &output_length);
8503 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008504 if (actual_status == PSA_SUCCESS) {
8505 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008506 } else {
8507 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008508 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008509
Gilles Peskine68428122018-06-30 18:42:41 +02008510 /* If the label is empty, the test framework puts a non-null pointer
8511 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008513 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 if (output_size != 0) {
8515 memset(output, 0, output_size);
8516 }
8517 actual_status = psa_asymmetric_encrypt(key, alg,
8518 input_data->x, input_data->len,
8519 NULL, label->len,
8520 output, output_size,
8521 &output_length);
8522 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008523 if (actual_status == PSA_SUCCESS) {
8524 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008525 } else {
8526 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008527 }
Gilles Peskine68428122018-06-30 18:42:41 +02008528 }
8529
Gilles Peskine656896e2018-06-29 19:12:28 +02008530exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008531 /*
8532 * Key attributes may have been returned by psa_get_key_attributes()
8533 * thus reset them as required.
8534 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008536
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 psa_destroy_key(key);
8538 mbedtls_free(output);
8539 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008540}
8541/* END_CASE */
8542
8543/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008544void asymmetric_encrypt_decrypt(int key_type_arg,
8545 data_t *key_data,
8546 int alg_arg,
8547 data_t *input_data,
8548 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008549{
Ronald Cron5425a212020-08-04 14:58:35 +02008550 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008551 psa_key_type_t key_type = key_type_arg;
8552 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008553 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008554 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008555 size_t output_size;
8556 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008557 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008558 size_t output2_size;
8559 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008561
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008563
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8565 psa_set_key_algorithm(&attributes, alg);
8566 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8569 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008570
8571 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8573 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008574
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8576 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008577 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008578
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008579 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 TEST_LE_U(output2_size,
8581 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8582 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008583 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008584
Gilles Peskineeebd7382018-06-08 18:11:54 +02008585 /* We test encryption by checking that encrypt-then-decrypt gives back
8586 * the original plaintext because of the non-optional random
8587 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008588 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8589 input_data->x, input_data->len,
8590 label->x, label->len,
8591 output, output_size,
8592 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008593 /* We don't know what ciphertext length to expect, but check that
8594 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008595 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008596
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8598 output, output_length,
8599 label->x, label->len,
8600 output2, output2_size,
8601 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008602 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008603 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008604
8605exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008606 /*
8607 * Key attributes may have been returned by psa_get_key_attributes()
8608 * thus reset them as required.
8609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008611
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 psa_destroy_key(key);
8613 mbedtls_free(output);
8614 mbedtls_free(output2);
8615 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008616}
8617/* END_CASE */
8618
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008620void asymmetric_decrypt(int key_type_arg,
8621 data_t *key_data,
8622 int alg_arg,
8623 data_t *input_data,
8624 data_t *label,
8625 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008626{
Ronald Cron5425a212020-08-04 14:58:35 +02008627 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008628 psa_key_type_t key_type = key_type_arg;
8629 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008630 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008631 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008632 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008633 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008635
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008637
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8639 psa_set_key_algorithm(&attributes, alg);
8640 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8643 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008644
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8646 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008647
8648 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8650 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008651 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008652
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8654 input_data->x, input_data->len,
8655 label->x, label->len,
8656 output,
8657 output_size,
8658 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008659 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008660 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008661
Gilles Peskine68428122018-06-30 18:42:41 +02008662 /* If the label is empty, the test framework puts a non-null pointer
8663 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008665 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 if (output_size != 0) {
8667 memset(output, 0, output_size);
8668 }
8669 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8670 input_data->x, input_data->len,
8671 NULL, label->len,
8672 output,
8673 output_size,
8674 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008675 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008676 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008677 }
8678
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008679exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 psa_reset_key_attributes(&attributes);
8681 psa_destroy_key(key);
8682 mbedtls_free(output);
8683 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008684}
8685/* END_CASE */
8686
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008687/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008688void asymmetric_decrypt_fail(int key_type_arg,
8689 data_t *key_data,
8690 int alg_arg,
8691 data_t *input_data,
8692 data_t *label,
8693 int output_size_arg,
8694 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008695{
Ronald Cron5425a212020-08-04 14:58:35 +02008696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008697 psa_key_type_t key_type = key_type_arg;
8698 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008699 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008700 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008701 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008702 psa_status_t actual_status;
8703 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008705
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008706 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008707
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008709
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8711 psa_set_key_algorithm(&attributes, alg);
8712 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008713
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8715 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008716
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 actual_status = psa_asymmetric_decrypt(key, alg,
8718 input_data->x, input_data->len,
8719 label->x, label->len,
8720 output, output_size,
8721 &output_length);
8722 TEST_EQUAL(actual_status, expected_status);
8723 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008724
Gilles Peskine68428122018-06-30 18:42:41 +02008725 /* If the label is empty, the test framework puts a non-null pointer
8726 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008728 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 if (output_size != 0) {
8730 memset(output, 0, output_size);
8731 }
8732 actual_status = psa_asymmetric_decrypt(key, alg,
8733 input_data->x, input_data->len,
8734 NULL, label->len,
8735 output, output_size,
8736 &output_length);
8737 TEST_EQUAL(actual_status, expected_status);
8738 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008739 }
8740
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008741exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 psa_reset_key_attributes(&attributes);
8743 psa_destroy_key(key);
8744 mbedtls_free(output);
8745 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008746}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008747/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008748
8749/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008750void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008751{
8752 /* Test each valid way of initializing the object, except for `= {0}`, as
8753 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8754 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008755 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008756 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008758 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8759 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008760
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008762
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008763 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8765 PSA_ERROR_BAD_STATE);
8766 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8767 PSA_ERROR_BAD_STATE);
8768 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8769 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008770
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008771 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 PSA_ASSERT(psa_key_derivation_abort(&func));
8773 PSA_ASSERT(psa_key_derivation_abort(&init));
8774 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008775}
8776/* END_CASE */
8777
Janos Follath16de4a42019-06-13 16:32:24 +01008778/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008779void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008780{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008781 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008782 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008783 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008786
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8788 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008789
8790exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 psa_key_derivation_abort(&operation);
8792 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008793}
8794/* END_CASE */
8795
Janos Follathaf3c2a02019-06-12 12:34:34 +01008796/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308797void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008799{
8800 psa_algorithm_t alg = alg_arg;
8801 size_t capacity = capacity_arg;
8802 psa_status_t expected_status = expected_status_arg;
8803 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8804
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008806
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008808
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8810 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008811
8812exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 psa_key_derivation_abort(&operation);
8814 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008815}
8816/* END_CASE */
8817
8818/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308819void parse_binary_string_test(data_t *input, int output)
8820{
8821 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308822 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308823 TEST_EQUAL(value, output);
8824}
8825/* END_CASE */
8826
8827/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008828void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308829 int step_arg1, int key_type_arg1, data_t *input1,
8830 int expected_status_arg1,
8831 int step_arg2, int key_type_arg2, data_t *input2,
8832 int expected_status_arg2,
8833 int step_arg3, int key_type_arg3, data_t *input3,
8834 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008836{
8837 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308839 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 psa_status_t expected_statuses[] = { expected_status_arg1,
8841 expected_status_arg2,
8842 expected_status_arg3 };
8843 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008844 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8845 MBEDTLS_SVC_KEY_ID_INIT,
8846 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008847 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8848 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8849 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008850 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008851 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008852 psa_status_t expected_output_status = expected_output_status_arg;
8853 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8858 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008859
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8863 mbedtls_test_set_step(i);
8864 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008865 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308866 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8867 key_types[i] != INPUT_INTEGER) {
8868 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 PSA_ASSERT(psa_import_key(&attributes,
8870 inputs[i]->x, inputs[i]->len,
8871 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308872 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008874 // When taking a private key as secret input, use key agreement
8875 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
Ryan Everett73e4ea32024-03-12 16:29:55 +00008877 &operation, keys[i], 0),
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 expected_statuses[i]);
8879 } else {
8880 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8881 keys[i]),
8882 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008883 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308885 if (key_types[i] == INPUT_INTEGER) {
8886 TEST_EQUAL(psa_key_derivation_input_integer(
8887 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308888 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308889 expected_statuses[i]);
8890 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308891 TEST_EQUAL(psa_key_derivation_input_bytes(
8892 &operation, steps[i],
8893 inputs[i]->x, inputs[i]->len),
8894 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308895 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008896 }
8897 }
8898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 if (output_key_type != PSA_KEY_TYPE_NONE) {
8900 psa_reset_key_attributes(&attributes);
8901 psa_set_key_type(&attributes, output_key_type);
8902 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008903 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 psa_key_derivation_output_key(&attributes, &operation,
8905 &output_key);
8906 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008907 uint8_t buffer[1];
8908 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 psa_key_derivation_output_bytes(&operation,
8910 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008911 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008913
Janos Follathaf3c2a02019-06-12 12:34:34 +01008914exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 psa_key_derivation_abort(&operation);
8916 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8917 psa_destroy_key(keys[i]);
8918 }
8919 psa_destroy_key(output_key);
8920 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008921}
8922/* END_CASE */
8923
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308924/* BEGIN_CASE*/
8925void derive_input_invalid_cost(int alg_arg, int64_t cost)
8926{
8927 psa_algorithm_t alg = alg_arg;
8928 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8929
8930 PSA_ASSERT(psa_crypto_init());
8931 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8932
8933 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8934 PSA_KEY_DERIVATION_INPUT_COST,
8935 cost),
8936 PSA_ERROR_NOT_SUPPORTED);
8937
8938exit:
8939 psa_key_derivation_abort(&operation);
8940 PSA_DONE();
8941}
8942/* END_CASE*/
8943
Janos Follathd958bb72019-07-03 15:02:16 +01008944/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008945void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008946{
Janos Follathd958bb72019-07-03 15:02:16 +01008947 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008948 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008949 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008950 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008951 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008953 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008955 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008957 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8958 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008961
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008963
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8965 psa_set_key_algorithm(&attributes, alg);
8966 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 PSA_ASSERT(psa_import_key(&attributes,
8969 key_data, sizeof(key_data),
8970 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008971
8972 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8974 input1, input1_length,
8975 input2, input2_length,
Ryan Everettc1cc6682024-03-12 16:17:43 +00008976 capacity, 0)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008977 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008979
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008980 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8982 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008983
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008985
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8987 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008988
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 psa_key_derivation_abort(&operation);
8991 psa_destroy_key(key);
8992 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008993}
8994/* END_CASE */
8995
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008996/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008997void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008998{
8999 uint8_t output_buffer[16];
9000 size_t buffer_size = 16;
9001 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009002 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009003
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
9005 output_buffer, buffer_size)
9006 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
9009 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009010
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009012
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
9014 output_buffer, buffer_size)
9015 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009016
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
9018 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009019
9020exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03009022}
9023/* END_CASE */
9024
9025/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009026void derive_output(int alg_arg,
9027 int step1_arg, data_t *input1, int expected_status_arg1,
9028 int step2_arg, data_t *input2, int expected_status_arg2,
9029 int step3_arg, data_t *input3, int expected_status_arg3,
9030 int step4_arg, data_t *input4, int expected_status_arg4,
9031 data_t *key_agreement_peer_key,
9032 int requested_capacity_arg,
9033 data_t *expected_output1,
9034 data_t *expected_output2,
9035 int other_key_input_type,
9036 int key_input_type,
9037 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009038{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009039 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009040 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
9041 data_t *inputs[] = { input1, input2, input3, input4 };
9042 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
9043 MBEDTLS_SVC_KEY_ID_INIT,
9044 MBEDTLS_SVC_KEY_ID_INIT,
9045 MBEDTLS_SVC_KEY_ID_INIT };
9046 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
9047 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009048 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009049 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009050 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009052 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009054 size_t output_buffer_size = 0;
9055 uint8_t *output_buffer = NULL;
9056 size_t expected_capacity;
9057 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009058 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
9059 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
9060 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
9061 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009062 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009063 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02009064 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
9067 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009068 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 }
9070 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009071 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009073 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009074 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009076
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009077 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9079 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
9080 requested_capacity));
9081 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
9082 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02009083 case 0:
9084 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309085 case PSA_KEY_DERIVATION_INPUT_COST:
9086 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05309087 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05309088 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05309089 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309090 if (statuses[i] != PSA_SUCCESS) {
9091 goto exit;
9092 }
9093 break;
9094 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02009095 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009097 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 TEST_EQUAL(psa_key_derivation_input_bytes(
9099 &operation, steps[i],
9100 inputs[i]->x, inputs[i]->len),
9101 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02009102
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02009104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009105 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009106 break;
9107 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
9109 psa_set_key_algorithm(&attributes1, alg);
9110 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009111
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 PSA_ASSERT(psa_import_key(&attributes1,
9113 inputs[i]->x, inputs[i]->len,
9114 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
9117 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
9118 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
9119 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009120 }
9121
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309122 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309124 keys[i]),
9125 statuses[i]);
9126
9127 if (statuses[i] != PSA_SUCCESS) {
9128 goto exit;
9129 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009130 break;
9131 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009132 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009133 break;
9134 }
9135 break;
9136 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009138 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9140 steps[i],
9141 inputs[i]->x,
9142 inputs[i]->len),
9143 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009144 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02009145 case 1: // input key, type DERIVE
9146 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
9148 psa_set_key_algorithm(&attributes2, alg);
9149 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009150
9151 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 if (other_key_input_type == 11) {
9153 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
9154 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 PSA_ASSERT(psa_import_key(&attributes2,
9157 inputs[i]->x, inputs[i]->len,
9158 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 TEST_EQUAL(psa_key_derivation_input_key(&operation,
9161 steps[i],
9162 keys[i]),
9163 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009164 break;
9165 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
9167 psa_set_key_algorithm(&attributes3, alg);
9168 psa_set_key_type(&attributes3,
9169 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009170
Gilles Peskine449bd832023-01-11 14:50:10 +01009171 PSA_ASSERT(psa_import_key(&attributes3,
9172 inputs[i]->x, inputs[i]->len,
9173 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009174
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 TEST_EQUAL(psa_key_derivation_key_agreement(
9176 &operation,
9177 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
9178 keys[i], key_agreement_peer_key->x,
9179 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009180 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009181 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009182 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009183 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01009184 }
9185
Gilles Peskine449bd832023-01-11 14:50:10 +01009186 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009189 break;
9190 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009191 TEST_EQUAL(psa_key_derivation_input_bytes(
9192 &operation, steps[i],
9193 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02009194
Gilles Peskine449bd832023-01-11 14:50:10 +01009195 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02009196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009197 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009198 break;
9199 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01009200 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009201
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9203 &current_capacity));
9204 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009205 expected_capacity = requested_capacity;
9206
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009208 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
9209
9210 /* For output key derivation secret must be provided using
9211 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009213 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009215
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
9217 psa_set_key_algorithm(&attributes4, alg);
9218 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
9219 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009220
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
9222 &derived_key), expected_status);
9223 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009224 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009226 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 status = psa_key_derivation_output_bytes(&operation,
9228 output_buffer, output_sizes[i]);
9229 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009230 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 TEST_ASSERT(status == PSA_SUCCESS ||
9232 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009233 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 } else if (expected_capacity == 0 ||
9235 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009236 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009238 expected_capacity = 0;
9239 continue;
9240 }
9241 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 PSA_ASSERT(status);
9243 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009244 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009245 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009247 /* Check the operation status. */
9248 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9250 &current_capacity));
9251 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009252 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009253 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009255
9256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 mbedtls_free(output_buffer);
9258 psa_key_derivation_abort(&operation);
9259 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
9260 psa_destroy_key(keys[i]);
9261 }
9262 psa_destroy_key(derived_key);
9263 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009264}
9265/* END_CASE */
9266
9267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009268void derive_full(int alg_arg,
9269 data_t *key_data,
9270 data_t *input1,
9271 data_t *input2,
9272 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02009273{
Ronald Cron5425a212020-08-04 14:58:35 +02009274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009275 psa_algorithm_t alg = alg_arg;
9276 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009277 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05309278 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02009279 size_t expected_capacity = requested_capacity;
9280 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009282
Gilles Peskine449bd832023-01-11 14:50:10 +01009283 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02009284
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9286 psa_set_key_algorithm(&attributes, alg);
9287 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009288
Gilles Peskine449bd832023-01-11 14:50:10 +01009289 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9290 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009291
Gilles Peskine449bd832023-01-11 14:50:10 +01009292 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9293 input1->x, input1->len,
9294 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009295 requested_capacity, 0)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009296 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009298
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9300 &current_capacity));
9301 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009302
9303 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 while (current_capacity > 0) {
9305 size_t read_size = sizeof(output_buffer);
9306 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009307 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 }
9309 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9310 output_buffer,
9311 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009312 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009313 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9314 &current_capacity));
9315 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009316 }
9317
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009318 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9320 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009321
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009323
9324exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 psa_key_derivation_abort(&operation);
9326 psa_destroy_key(key);
9327 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009328}
9329/* END_CASE */
9330
Stephan Koch78109f52023-04-12 14:19:36 +02009331/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009332void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9333 int derivation_step,
9334 int capacity, int expected_capacity_status_arg,
9335 data_t *expected_output,
9336 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009337{
9338 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9339 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009340 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009341 uint8_t *output_buffer = NULL;
9342 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009343 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9344 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9345 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009346
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009347 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009348 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009349
Gilles Peskine449bd832023-01-11 14:50:10 +01009350 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9351 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9352 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009353
Gilles Peskine449bd832023-01-11 14:50:10 +01009354 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9355 step, input->x, input->len),
9356 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009357
Gilles Peskine449bd832023-01-11 14:50:10 +01009358 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009361
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9363 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009364
Gilles Peskine449bd832023-01-11 14:50:10 +01009365 TEST_EQUAL(status, expected_output_status);
9366 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009367 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009368 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009369 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009370
9371exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009372 mbedtls_free(output_buffer);
9373 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009374 PSA_DONE();
9375}
9376/* END_CASE */
9377
Janos Follathe60c9052019-07-03 13:51:30 +01009378/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009379void derive_key_exercise(int alg_arg,
9380 data_t *key_data,
9381 data_t *input1,
9382 data_t *input2,
9383 int derived_type_arg,
9384 int derived_bits_arg,
9385 int derived_usage_arg,
9386 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009387{
Ronald Cron5425a212020-08-04 14:58:35 +02009388 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9389 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009390 psa_algorithm_t alg = alg_arg;
9391 psa_key_type_t derived_type = derived_type_arg;
9392 size_t derived_bits = derived_bits_arg;
9393 psa_key_usage_t derived_usage = derived_usage_arg;
9394 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009395 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009396 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009398 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009399
Gilles Peskine449bd832023-01-11 14:50:10 +01009400 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009401
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9403 psa_set_key_algorithm(&attributes, alg);
9404 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9405 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9406 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009407
9408 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9410 input1->x, input1->len,
9411 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009412 capacity, 0)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009413 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 }
Janos Follathe60c9052019-07-03 13:51:30 +01009415
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 psa_set_key_usage_flags(&attributes, derived_usage);
9417 psa_set_key_algorithm(&attributes, derived_alg);
9418 psa_set_key_type(&attributes, derived_type);
9419 psa_set_key_bits(&attributes, derived_bits);
9420 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9421 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009422
9423 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9425 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9426 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009427
9428 /* Exercise the derived key. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00009429 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg, 0)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009430 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009432
9433exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009434 /*
9435 * Key attributes may have been returned by psa_get_key_attributes()
9436 * thus reset them as required.
9437 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009439
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 psa_key_derivation_abort(&operation);
9441 psa_destroy_key(base_key);
9442 psa_destroy_key(derived_key);
9443 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009444}
9445/* END_CASE */
9446
Janos Follath42fd8882019-07-03 14:17:09 +01009447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009448void derive_key_export(int alg_arg,
9449 data_t *key_data,
9450 data_t *input1,
9451 data_t *input2,
9452 int bytes1_arg,
9453 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009454{
Ronald Cron5425a212020-08-04 14:58:35 +02009455 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9456 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009457 psa_algorithm_t alg = alg_arg;
9458 size_t bytes1 = bytes1_arg;
9459 size_t bytes2 = bytes2_arg;
9460 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009461 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009462 uint8_t *output_buffer = NULL;
9463 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009464 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9465 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009466 size_t length;
9467
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009468 TEST_CALLOC(output_buffer, capacity);
9469 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009470 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009471
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9473 psa_set_key_algorithm(&base_attributes, alg);
9474 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9475 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9476 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009477
9478 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9480 input1->x, input1->len,
9481 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009482 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009483 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009484 }
Janos Follath42fd8882019-07-03 14:17:09 +01009485
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9487 output_buffer,
9488 capacity));
9489 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009490
9491 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9493 input1->x, input1->len,
9494 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009495 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 }
Janos Follath42fd8882019-07-03 14:17:09 +01009498
Gilles Peskine449bd832023-01-11 14:50:10 +01009499 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9500 psa_set_key_algorithm(&derived_attributes, 0);
9501 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9502 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9503 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9504 &derived_key));
9505 PSA_ASSERT(psa_export_key(derived_key,
9506 export_buffer, bytes1,
9507 &length));
9508 TEST_EQUAL(length, bytes1);
9509 PSA_ASSERT(psa_destroy_key(derived_key));
9510 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9511 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9512 &derived_key));
9513 PSA_ASSERT(psa_export_key(derived_key,
9514 export_buffer + bytes1, bytes2,
9515 &length));
9516 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009517
9518 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009519 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009520 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009521
9522exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 mbedtls_free(output_buffer);
9524 mbedtls_free(export_buffer);
9525 psa_key_derivation_abort(&operation);
9526 psa_destroy_key(base_key);
9527 psa_destroy_key(derived_key);
9528 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009529}
9530/* END_CASE */
9531
9532/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009533void derive_key_type(int alg_arg,
9534 data_t *key_data,
9535 data_t *input1,
9536 data_t *input2,
9537 int key_type_arg, int bits_arg,
9538 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009539{
9540 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9541 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9542 const psa_algorithm_t alg = alg_arg;
9543 const psa_key_type_t key_type = key_type_arg;
9544 const size_t bits = bits_arg;
9545 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9546 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009548 uint8_t *export_buffer = NULL;
9549 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9550 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9551 size_t export_length;
9552
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009553 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009555
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9557 psa_set_key_algorithm(&base_attributes, alg);
9558 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9559 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9560 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009561
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009563 &operation, base_key, alg,
9564 input1->x, input1->len,
9565 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009566 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009567 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009569
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9571 psa_set_key_algorithm(&derived_attributes, 0);
9572 psa_set_key_type(&derived_attributes, key_type);
9573 psa_set_key_bits(&derived_attributes, bits);
9574 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9575 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 PSA_ASSERT(psa_export_key(derived_key,
9578 export_buffer, export_buffer_size,
9579 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009580 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009581 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009582
9583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009584 mbedtls_free(export_buffer);
9585 psa_key_derivation_abort(&operation);
9586 psa_destroy_key(base_key);
9587 psa_destroy_key(derived_key);
9588 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009589}
9590/* END_CASE */
9591
9592/* BEGIN_CASE */
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009593void derive_key_ext(int alg_arg,
9594 data_t *key_data,
9595 data_t *input1,
9596 data_t *input2,
9597 int key_type_arg, int bits_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009598 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +01009599 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009600 psa_status_t expected_status,
9601 data_t *expected_export)
9602{
9603 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9604 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9605 const psa_algorithm_t alg = alg_arg;
9606 const psa_key_type_t key_type = key_type_arg;
9607 const size_t bits = bits_arg;
Gilles Peskine092ce512024-02-20 12:31:24 +01009608 psa_key_production_parameters_t *params = NULL;
9609 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009610 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9611 const size_t export_buffer_size =
9612 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
9613 uint8_t *export_buffer = NULL;
9614 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9615 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9616 size_t export_length;
9617
9618 TEST_CALLOC(export_buffer, export_buffer_size);
9619 PSA_ASSERT(psa_crypto_init());
9620
9621 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9622 psa_set_key_algorithm(&base_attributes, alg);
9623 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9624 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9625 &base_key));
9626
9627 if (mbedtls_test_psa_setup_key_derivation_wrap(
9628 &operation, base_key, alg,
9629 input1->x, input1->len,
9630 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009631 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009632 goto exit;
9633 }
9634
9635 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9636 psa_set_key_algorithm(&derived_attributes, 0);
9637 psa_set_key_type(&derived_attributes, key_type);
9638 psa_set_key_bits(&derived_attributes, bits);
Gilles Peskine092ce512024-02-20 12:31:24 +01009639 if (!setup_key_production_parameters(&params, &params_data_length,
9640 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009641 goto exit;
9642 }
9643
9644 TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01009645 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009646 &derived_key),
9647 expected_status);
9648
9649 if (expected_status == PSA_SUCCESS) {
9650 PSA_ASSERT(psa_export_key(derived_key,
9651 export_buffer, export_buffer_size,
9652 &export_length));
9653 TEST_MEMORY_COMPARE(export_buffer, export_length,
9654 expected_export->x, expected_export->len);
9655 }
9656
9657exit:
9658 mbedtls_free(export_buffer);
Gilles Peskine092ce512024-02-20 12:31:24 +01009659 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009660 psa_key_derivation_abort(&operation);
9661 psa_destroy_key(base_key);
9662 psa_destroy_key(derived_key);
9663 PSA_DONE();
9664}
9665/* END_CASE */
9666
9667/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009668void derive_key(int alg_arg,
9669 data_t *key_data, data_t *input1, data_t *input2,
9670 int type_arg, int bits_arg,
9671 int expected_status_arg,
9672 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009673{
Ronald Cron5425a212020-08-04 14:58:35 +02009674 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9675 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009676 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009677 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009678 size_t bits = bits_arg;
9679 psa_status_t expected_status = expected_status_arg;
9680 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9681 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9682 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9683
Gilles Peskine449bd832023-01-11 14:50:10 +01009684 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009685
Gilles Peskine449bd832023-01-11 14:50:10 +01009686 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9687 psa_set_key_algorithm(&base_attributes, alg);
9688 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9689 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9690 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009691
Gilles Peskine449bd832023-01-11 14:50:10 +01009692 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9693 input1->x, input1->len,
9694 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009695 SIZE_MAX, 0)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009697 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009698
Gilles Peskine449bd832023-01-11 14:50:10 +01009699 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9700 psa_set_key_algorithm(&derived_attributes, 0);
9701 psa_set_key_type(&derived_attributes, type);
9702 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009703
9704 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009705 psa_key_derivation_output_key(&derived_attributes,
9706 &operation,
9707 &derived_key);
9708 if (is_large_output > 0) {
9709 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9710 }
9711 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009712
9713exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009714 psa_key_derivation_abort(&operation);
9715 psa_destroy_key(base_key);
9716 psa_destroy_key(derived_key);
9717 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009718}
9719/* END_CASE */
9720
9721/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009722void key_agreement_setup(int alg_arg,
9723 int our_key_type_arg, int our_key_alg_arg,
9724 data_t *our_key_data, data_t *peer_key_data,
9725 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009726{
Ronald Cron5425a212020-08-04 14:58:35 +02009727 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009728 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009729 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009730 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009731 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009733 psa_status_t expected_status = expected_status_arg;
9734 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009735
Gilles Peskine449bd832023-01-11 14:50:10 +01009736 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009737
Gilles Peskine449bd832023-01-11 14:50:10 +01009738 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9739 psa_set_key_algorithm(&attributes, our_key_alg);
9740 psa_set_key_type(&attributes, our_key_type);
9741 PSA_ASSERT(psa_import_key(&attributes,
9742 our_key_data->x, our_key_data->len,
9743 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009744
Gilles Peskine77f40d82019-04-11 21:27:06 +02009745 /* The tests currently include inputs that should fail at either step.
9746 * Test cases that fail at the setup step should be changed to call
9747 * key_derivation_setup instead, and this function should be renamed
9748 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009749 status = psa_key_derivation_setup(&operation, alg);
9750 if (status == PSA_SUCCESS) {
9751 TEST_EQUAL(psa_key_derivation_key_agreement(
9752 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9753 our_key,
9754 peer_key_data->x, peer_key_data->len),
9755 expected_status);
9756 } else {
9757 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009758 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009759
9760exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009761 psa_key_derivation_abort(&operation);
9762 psa_destroy_key(our_key);
9763 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009764}
9765/* END_CASE */
9766
9767/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009768void raw_key_agreement(int alg_arg,
9769 int our_key_type_arg, data_t *our_key_data,
9770 data_t *peer_key_data,
9771 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009772{
Ronald Cron5425a212020-08-04 14:58:35 +02009773 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009774 psa_algorithm_t alg = alg_arg;
9775 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009777 unsigned char *output = NULL;
9778 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009779 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009780
Gilles Peskine449bd832023-01-11 14:50:10 +01009781 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009782
Gilles Peskine449bd832023-01-11 14:50:10 +01009783 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9784 psa_set_key_algorithm(&attributes, alg);
9785 psa_set_key_type(&attributes, our_key_type);
9786 PSA_ASSERT(psa_import_key(&attributes,
9787 our_key_data->x, our_key_data->len,
9788 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009789
Gilles Peskine449bd832023-01-11 14:50:10 +01009790 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9791 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009792
Gilles Peskine992bee82022-04-13 23:25:52 +02009793 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009794 TEST_LE_U(expected_output->len,
9795 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9796 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9797 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009798
9799 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009800 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009801 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9802 peer_key_data->x, peer_key_data->len,
9803 output, expected_output->len,
9804 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009805 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009806 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009807 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009808 output = NULL;
9809 output_length = ~0;
9810
9811 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009812 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009813 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9814 peer_key_data->x, peer_key_data->len,
9815 output, expected_output->len + 1,
9816 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009817 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009818 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009819 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009820 output = NULL;
9821 output_length = ~0;
9822
9823 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009824 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009825 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9826 peer_key_data->x, peer_key_data->len,
9827 output, expected_output->len - 1,
9828 &output_length),
9829 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009830 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009831 TEST_LE_U(output_length, expected_output->len - 1);
9832 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009833 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009834
9835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009836 mbedtls_free(output);
9837 psa_destroy_key(our_key);
9838 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009839}
9840/* END_CASE */
9841
9842/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009843void key_agreement_capacity(int alg_arg,
9844 int our_key_type_arg, data_t *our_key_data,
9845 data_t *peer_key_data,
9846 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009847{
Ronald Cron5425a212020-08-04 14:58:35 +02009848 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009849 psa_algorithm_t alg = alg_arg;
9850 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009851 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009853 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009854 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009855
Gilles Peskine449bd832023-01-11 14:50:10 +01009856 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009857
Gilles Peskine449bd832023-01-11 14:50:10 +01009858 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9859 psa_set_key_algorithm(&attributes, alg);
9860 psa_set_key_type(&attributes, our_key_type);
9861 PSA_ASSERT(psa_import_key(&attributes,
9862 our_key_data->x, our_key_data->len,
9863 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009864
Gilles Peskine449bd832023-01-11 14:50:10 +01009865 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9866 PSA_ASSERT(psa_key_derivation_key_agreement(
9867 &operation,
9868 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9869 peer_key_data->x, peer_key_data->len));
9870 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009871 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009872 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9873 PSA_KEY_DERIVATION_INPUT_INFO,
9874 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009875 }
Gilles Peskine59685592018-09-18 12:11:34 +02009876
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009877 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009878 PSA_ASSERT(psa_key_derivation_get_capacity(
9879 &operation, &actual_capacity));
9880 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009881
Gilles Peskinebf491972018-10-25 22:36:12 +02009882 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009883 while (actual_capacity > sizeof(output)) {
9884 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9885 output, sizeof(output)));
9886 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009887 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009888 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9889 output, actual_capacity));
9890 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9891 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009892
Gilles Peskine59685592018-09-18 12:11:34 +02009893exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009894 psa_key_derivation_abort(&operation);
9895 psa_destroy_key(our_key);
9896 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009897}
9898/* END_CASE */
9899
Valerio Settiad819672023-12-29 12:14:41 +01009900/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9901void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009902{
9903 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009904 psa_ecc_family_t ecc_family = psa_family_arg;
9905 size_t bits = bits_arg;
9906 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009907
Valerio Settiad819672023-12-29 12:14:41 +01009908 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9909 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009910 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009911}
9912/* END_CASE */
9913
Valerio Settiac739522024-01-04 10:22:01 +01009914/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9915void ecc_conversion_functions_fail()
9916{
9917 size_t bits;
9918
Valerio Settidb6e0292024-01-05 10:15:45 +01009919 /* Invalid legacy curve identifiers. */
9920 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9921 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009922 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9923 TEST_EQUAL(0, bits);
9924
9925 /* Invalid PSA EC family. */
9926 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9927 /* Invalid bit-size for a valid EC family. */
9928 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9929
9930 /* Twisted-Edward curves are not supported yet. */
9931 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9932 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9933 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9934 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9935}
9936/* END_CASE */
9937
9938
Valerio Settibf999cb2023-12-28 17:48:13 +01009939/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009940void key_agreement_output(int alg_arg,
9941 int our_key_type_arg, data_t *our_key_data,
9942 data_t *peer_key_data,
9943 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009944{
Ronald Cron5425a212020-08-04 14:58:35 +02009945 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009946 psa_algorithm_t alg = alg_arg;
9947 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009948 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009950 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009951
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009952 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009953 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009954
Gilles Peskine449bd832023-01-11 14:50:10 +01009955 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009956
Gilles Peskine449bd832023-01-11 14:50:10 +01009957 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9958 psa_set_key_algorithm(&attributes, alg);
9959 psa_set_key_type(&attributes, our_key_type);
9960 PSA_ASSERT(psa_import_key(&attributes,
9961 our_key_data->x, our_key_data->len,
9962 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009963
Gilles Peskine449bd832023-01-11 14:50:10 +01009964 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9965 PSA_ASSERT(psa_key_derivation_key_agreement(
9966 &operation,
9967 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9968 peer_key_data->x, peer_key_data->len));
9969 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009970 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009971 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9972 PSA_KEY_DERIVATION_INPUT_INFO,
9973 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009974 }
Gilles Peskine59685592018-09-18 12:11:34 +02009975
Gilles Peskine449bd832023-01-11 14:50:10 +01009976 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9977 actual_output,
9978 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009979 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009980 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009981 if (expected_output2->len != 0) {
9982 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9983 actual_output,
9984 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009985 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009986 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009987 }
Gilles Peskine59685592018-09-18 12:11:34 +02009988
9989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009990 psa_key_derivation_abort(&operation);
9991 psa_destroy_key(our_key);
9992 PSA_DONE();
9993 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009994}
9995/* END_CASE */
9996
9997/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009998void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009999{
Gilles Peskinea50d7392018-06-21 10:22:13 +020010000 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +020010001 unsigned char *output = NULL;
10002 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +020010003 size_t i;
10004 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +020010005
Gilles Peskine449bd832023-01-11 14:50:10 +010010006 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +000010007
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010008 TEST_CALLOC(output, bytes);
10009 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +020010010
Gilles Peskine449bd832023-01-11 14:50:10 +010010011 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +020010012
Gilles Peskinea50d7392018-06-21 10:22:13 +020010013 /* Run several times, to ensure that every output byte will be
10014 * nonzero at least once with overwhelming probability
10015 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +010010016 for (run = 0; run < 10; run++) {
10017 if (bytes != 0) {
10018 memset(output, 0, bytes);
10019 }
10020 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +020010021
Gilles Peskine449bd832023-01-11 14:50:10 +010010022 for (i = 0; i < bytes; i++) {
10023 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +020010024 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010010025 }
Gilles Peskinea50d7392018-06-21 10:22:13 +020010026 }
Gilles Peskine05d69892018-06-19 22:00:52 +020010027 }
Gilles Peskinea50d7392018-06-21 10:22:13 +020010028
10029 /* Check that every byte was changed to nonzero at least once. This
10030 * validates that psa_generate_random is overwriting every byte of
10031 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010032 for (i = 0; i < bytes; i++) {
10033 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +020010034 }
Gilles Peskine05d69892018-06-19 22:00:52 +020010035
10036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010037 PSA_DONE();
10038 mbedtls_free(output);
10039 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +020010040}
10041/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +020010042
Ryan3a1b7862024-03-01 17:24:04 +000010043#if defined MBEDTLS_THREADING_PTHREAD
10044
10045/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */
10046void concurrently_generate_keys(int type_arg,
10047 int bits_arg,
10048 int usage_arg,
10049 int alg_arg,
10050 int expected_status_arg,
10051 int is_large_key_arg,
10052 int arg_thread_count,
10053 int reps_arg)
10054{
10055 size_t thread_count = (size_t) arg_thread_count;
10056 mbedtls_test_thread_t *threads = NULL;
10057 generate_key_context gkc;
10058 gkc.type = type_arg;
10059 gkc.usage = usage_arg;
10060 gkc.bits = bits_arg;
10061 gkc.alg = alg_arg;
10062 gkc.expected_status = expected_status_arg;
10063 gkc.is_large_key = is_large_key_arg;
10064 gkc.reps = reps_arg;
10065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10066
10067 PSA_ASSERT(psa_crypto_init());
10068
10069 psa_set_key_usage_flags(&attributes, usage_arg);
10070 psa_set_key_algorithm(&attributes, alg_arg);
10071 psa_set_key_type(&attributes, type_arg);
10072 psa_set_key_bits(&attributes, bits_arg);
10073 gkc.attributes = &attributes;
10074
10075 TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count);
10076
10077 /* Split threads to generate key then destroy key. */
10078 for (size_t i = 0; i < thread_count; i++) {
10079 TEST_EQUAL(
10080 mbedtls_test_thread_create(&threads[i], thread_generate_key,
10081 (void *) &gkc), 0);
10082 }
10083
10084 /* Join threads. */
10085 for (size_t i = 0; i < thread_count; i++) {
10086 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
10087 }
10088
10089exit:
10090 mbedtls_free(threads);
10091 PSA_DONE();
10092}
10093/* END_CASE */
10094#endif
10095
Gilles Peskine12313cd2018-06-20 00:20:32 +020010096/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010097void generate_key(int type_arg,
10098 int bits_arg,
10099 int usage_arg,
10100 int alg_arg,
10101 int expected_status_arg,
10102 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +020010103{
Ronald Cron5425a212020-08-04 14:58:35 +020010104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +020010105 psa_key_type_t type = type_arg;
10106 psa_key_usage_t usage = usage_arg;
10107 size_t bits = bits_arg;
10108 psa_algorithm_t alg = alg_arg;
10109 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +020010110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +020010111 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +020010112
Gilles Peskine449bd832023-01-11 14:50:10 +010010113 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +020010114
Gilles Peskine449bd832023-01-11 14:50:10 +010010115 psa_set_key_usage_flags(&attributes, usage);
10116 psa_set_key_algorithm(&attributes, alg);
10117 psa_set_key_type(&attributes, type);
10118 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +020010119
10120 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010121 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +010010122
Gilles Peskine449bd832023-01-11 14:50:10 +010010123 if (is_large_key > 0) {
10124 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
10125 }
10126 TEST_EQUAL(status, expected_status);
10127 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +020010128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010129 }
Gilles Peskine12313cd2018-06-20 00:20:32 +020010130
10131 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +010010132 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
10133 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
10134 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +020010135
Gilles Peskine818ca122018-06-20 18:16:48 +020010136 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010137 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +020010138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010139 }
Gilles Peskine12313cd2018-06-20 00:20:32 +020010140
10141exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010142 /*
10143 * Key attributes may have been returned by psa_get_key_attributes()
10144 * thus reset them as required.
10145 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010146 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010147
Gilles Peskine449bd832023-01-11 14:50:10 +010010148 psa_destroy_key(key);
10149 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +020010150}
10151/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +030010152
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010153/* BEGIN_CASE */
10154void generate_key_ext(int type_arg,
10155 int bits_arg,
10156 int usage_arg,
10157 int alg_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +010010158 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +010010159 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010160 int expected_status_arg)
10161{
10162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10163 psa_key_type_t type = type_arg;
10164 psa_key_usage_t usage = usage_arg;
10165 size_t bits = bits_arg;
10166 psa_algorithm_t alg = alg_arg;
10167 psa_status_t expected_status = expected_status_arg;
10168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine092ce512024-02-20 12:31:24 +010010169 psa_key_production_parameters_t *params = NULL;
10170 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010171 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
10172
10173 PSA_ASSERT(psa_crypto_init());
10174
10175 psa_set_key_usage_flags(&attributes, usage);
10176 psa_set_key_algorithm(&attributes, alg);
10177 psa_set_key_type(&attributes, type);
10178 psa_set_key_bits(&attributes, bits);
10179
Gilles Peskine092ce512024-02-20 12:31:24 +010010180 if (!setup_key_production_parameters(&params, &params_data_length,
10181 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010182 goto exit;
10183 }
10184
10185 /* Generate a key */
10186 psa_status_t status = psa_generate_key_ext(&attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +010010187 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010188 &key);
10189
10190 TEST_EQUAL(status, expected_status);
10191 if (expected_status != PSA_SUCCESS) {
10192 goto exit;
10193 }
10194
10195 /* Test the key information */
10196 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
10197 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
10198 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
10199
Gilles Peskine7a18f962024-02-12 16:48:11 +010010200#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
10201 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +010010202 TEST_ASSERT(rsa_test_e(key, bits, params_data));
Gilles Peskine7a18f962024-02-12 16:48:11 +010010203 }
10204#endif
10205
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010206 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010207 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010208 goto exit;
10209 }
10210
10211exit:
10212 /*
10213 * Key attributes may have been returned by psa_get_key_attributes()
10214 * thus reset them as required.
10215 */
10216 psa_reset_key_attributes(&got_attributes);
Gilles Peskine092ce512024-02-20 12:31:24 +010010217 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010218 psa_destroy_key(key);
10219 PSA_DONE();
10220}
10221/* END_CASE */
10222
10223/* BEGIN_CASE */
Gilles Peskine092ce512024-02-20 12:31:24 +010010224void key_production_parameters_init()
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010225{
Gilles Peskine092ce512024-02-20 12:31:24 +010010226 psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT;
10227 psa_key_production_parameters_t zero;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010228 memset(&zero, 0, sizeof(zero));
10229
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010230 TEST_EQUAL(init.flags, 0);
10231 TEST_EQUAL(zero.flags, 0);
10232}
10233/* END_CASE */
10234
Darryl Greend49a4992018-06-18 17:27:26 +010010235/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010010236void persistent_key_load_key_from_storage(data_t *data,
10237 int type_arg, int bits_arg,
10238 int usage_flags_arg, int alg_arg,
10239 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +010010240{
Gilles Peskine449bd832023-01-11 14:50:10 +010010241 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +020010243 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10244 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010245 psa_key_type_t type = type_arg;
10246 size_t bits = bits_arg;
10247 psa_key_usage_t usage_flags = usage_flags_arg;
10248 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +020010249 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +010010250 unsigned char *first_export = NULL;
10251 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010010252 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -070010253 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +010010254 size_t second_exported_length;
10255
Gilles Peskine449bd832023-01-11 14:50:10 +010010256 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010257 TEST_CALLOC(first_export, export_size);
10258 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010259 }
Darryl Greend49a4992018-06-18 17:27:26 +010010260
Gilles Peskine449bd832023-01-11 14:50:10 +010010261 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010262
Gilles Peskine449bd832023-01-11 14:50:10 +010010263 psa_set_key_id(&attributes, key_id);
10264 psa_set_key_usage_flags(&attributes, usage_flags);
10265 psa_set_key_algorithm(&attributes, alg);
10266 psa_set_key_type(&attributes, type);
10267 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +010010268
Gilles Peskine449bd832023-01-11 14:50:10 +010010269 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010270 case IMPORT_KEY:
10271 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010272 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
10273 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010274 break;
Darryl Greend49a4992018-06-18 17:27:26 +010010275
Darryl Green0c6575a2018-11-07 16:05:30 +000010276 case GENERATE_KEY:
10277 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010278 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010279 break;
10280
10281 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +010010282#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +010010283 {
10284 /* Create base key */
10285 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
10286 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
10287 psa_set_key_usage_flags(&base_attributes,
10288 PSA_KEY_USAGE_DERIVE);
10289 psa_set_key_algorithm(&base_attributes, derive_alg);
10290 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
10291 PSA_ASSERT(psa_import_key(&base_attributes,
10292 data->x, data->len,
10293 &base_key));
10294 /* Derive a key. */
10295 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
10296 PSA_ASSERT(psa_key_derivation_input_key(
10297 &operation,
10298 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
10299 PSA_ASSERT(psa_key_derivation_input_bytes(
10300 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
10301 NULL, 0));
10302 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
10303 &operation,
10304 &key));
10305 PSA_ASSERT(psa_key_derivation_abort(&operation));
10306 PSA_ASSERT(psa_destroy_key(base_key));
10307 base_key = MBEDTLS_SVC_KEY_ID_INIT;
10308 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010309#else
Gilles Peskine449bd832023-01-11 14:50:10 +010010310 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010311#endif
10312 break;
10313
10314 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010010315 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010316 break;
Darryl Green0c6575a2018-11-07 16:05:30 +000010317 }
Gilles Peskine449bd832023-01-11 14:50:10 +010010318 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +010010319
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010320 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010321 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10322 PSA_ASSERT(psa_export_key(key,
10323 first_export, export_size,
10324 &first_exported_length));
10325 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010326 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010327 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +010010328 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010329 }
Darryl Greend49a4992018-06-18 17:27:26 +010010330
10331 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +010010332 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010333 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +010010334 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010335
Darryl Greend49a4992018-06-18 17:27:26 +010010336 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +010010337 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
10338 TEST_ASSERT(mbedtls_svc_key_id_equal(
10339 psa_get_key_id(&attributes), key_id));
10340 TEST_EQUAL(psa_get_key_lifetime(&attributes),
10341 PSA_KEY_LIFETIME_PERSISTENT);
10342 TEST_EQUAL(psa_get_key_type(&attributes), type);
10343 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
10344 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
10345 mbedtls_test_update_key_usage_flags(usage_flags));
10346 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +010010347
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010348 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010349 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10350 PSA_ASSERT(psa_export_key(key,
10351 second_export, export_size,
10352 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010353 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010354 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +000010355 }
10356
10357 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010358 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010360 }
Darryl Greend49a4992018-06-18 17:27:26 +010010361
10362exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010363 /*
10364 * Key attributes may have been returned by psa_get_key_attributes()
10365 * thus reset them as required.
10366 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010367 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010368
Gilles Peskine449bd832023-01-11 14:50:10 +010010369 mbedtls_free(first_export);
10370 mbedtls_free(second_export);
10371 psa_key_derivation_abort(&operation);
10372 psa_destroy_key(base_key);
10373 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010374 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +010010375}
10376/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +020010377
Neil Armstronga557cb82022-06-10 08:58:32 +020010378/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010379void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
10380 int primitive_arg, int hash_arg, int role_arg,
10381 int test_input, data_t *pw_data,
10382 int inj_err_type_arg,
10383 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010384{
10385 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10386 psa_pake_operation_t operation = psa_pake_operation_init();
10387 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010388 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +020010389 psa_key_type_t key_type_pw = key_type_pw_arg;
10390 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010391 psa_algorithm_t hash_alg = hash_arg;
10392 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010393 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10394 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010395 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
10396 psa_status_t expected_error = expected_error_arg;
10397 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010398 unsigned char *output_buffer = NULL;
10399 size_t output_len = 0;
10400
Gilles Peskine449bd832023-01-11 14:50:10 +010010401 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010402
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010403 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +010010404 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010405 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010406
Gilles Peskine449bd832023-01-11 14:50:10 +010010407 if (pw_data->len > 0) {
10408 psa_set_key_usage_flags(&attributes, key_usage_pw);
10409 psa_set_key_algorithm(&attributes, alg);
10410 psa_set_key_type(&attributes, key_type_pw);
10411 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10412 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010413 }
10414
Gilles Peskine449bd832023-01-11 14:50:10 +010010415 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10416 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10417 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010418
Gilles Peskine449bd832023-01-11 14:50:10 +010010419 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010420
Gilles Peskine449bd832023-01-11 14:50:10 +010010421 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10422 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10423 expected_error);
10424 PSA_ASSERT(psa_pake_abort(&operation));
10425 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10426 expected_error);
10427 PSA_ASSERT(psa_pake_abort(&operation));
10428 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10429 expected_error);
10430 PSA_ASSERT(psa_pake_abort(&operation));
10431 TEST_EQUAL(psa_pake_set_role(&operation, role),
10432 expected_error);
10433 PSA_ASSERT(psa_pake_abort(&operation));
10434 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10435 NULL, 0, NULL),
10436 expected_error);
10437 PSA_ASSERT(psa_pake_abort(&operation));
10438 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10439 expected_error);
10440 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010441 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010442 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010443
Gilles Peskine449bd832023-01-11 14:50:10 +010010444 status = psa_pake_setup(&operation, &cipher_suite);
10445 if (status != PSA_SUCCESS) {
10446 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010447 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010448 }
10449
Gilles Peskine449bd832023-01-11 14:50:10 +010010450 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10451 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10452 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010453 goto exit;
10454 }
10455
Gilles Peskine449bd832023-01-11 14:50:10 +010010456 status = psa_pake_set_role(&operation, role);
10457 if (status != PSA_SUCCESS) {
10458 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010459 goto exit;
10460 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010461
Gilles Peskine449bd832023-01-11 14:50:10 +010010462 if (pw_data->len > 0) {
10463 status = psa_pake_set_password_key(&operation, key);
10464 if (status != PSA_SUCCESS) {
10465 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010466 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010467 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010468 }
10469
Gilles Peskine449bd832023-01-11 14:50:10 +010010470 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10471 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10472 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010473 goto exit;
10474 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010475
Gilles Peskine449bd832023-01-11 14:50:10 +010010476 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10477 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10478 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010479 goto exit;
10480 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010481
Gilles Peskine449bd832023-01-11 14:50:10 +010010482 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010483 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010484 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10485 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010486 goto exit;
10487 }
10488
Gilles Peskine449bd832023-01-11 14:50:10 +010010489 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010490 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010491 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10492 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010493 goto exit;
10494 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010495
Gilles Peskine449bd832023-01-11 14:50:10 +010010496 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10497 PSA_PAKE_STEP_KEY_SHARE);
10498 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10499 PSA_PAKE_STEP_ZK_PUBLIC);
10500 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10501 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010502
Gilles Peskine449bd832023-01-11 14:50:10 +010010503 if (test_input) {
10504 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10505 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10506 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010507 goto exit;
10508 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010509
Gilles Peskine449bd832023-01-11 14:50:10 +010010510 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10511 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10512 output_buffer, size_zk_proof),
10513 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010514 goto exit;
10515 }
10516
Gilles Peskine449bd832023-01-11 14:50:10 +010010517 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10518 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10519 output_buffer, size_zk_proof),
10520 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010521 goto exit;
10522 }
10523
Gilles Peskine449bd832023-01-11 14:50:10 +010010524 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10525 output_buffer, size_key_share);
10526 if (status != PSA_SUCCESS) {
10527 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010528 goto exit;
10529 }
10530
Gilles Peskine449bd832023-01-11 14:50:10 +010010531 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10532 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10533 output_buffer, size_zk_public + 1),
10534 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010535 goto exit;
10536 }
10537
Gilles Peskine449bd832023-01-11 14:50:10 +010010538 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010539 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010540 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10541 output_buffer, size_zk_public + 1);
10542 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10543 output_buffer, size_zk_public),
10544 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010545 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010546 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010547 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010548 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10549 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10550 NULL, 0, NULL),
10551 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010552 goto exit;
10553 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010554
Gilles Peskine449bd832023-01-11 14:50:10 +010010555 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10556 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10557 output_buffer, buf_size, &output_len),
10558 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010559 goto exit;
10560 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010561
Gilles Peskine449bd832023-01-11 14:50:10 +010010562 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10563 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10564 output_buffer, buf_size, &output_len),
10565 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010566 goto exit;
10567 }
10568
Gilles Peskine449bd832023-01-11 14:50:10 +010010569 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10570 output_buffer, buf_size, &output_len);
10571 if (status != PSA_SUCCESS) {
10572 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010573 goto exit;
10574 }
10575
Gilles Peskine449bd832023-01-11 14:50:10 +010010576 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010577
Gilles Peskine449bd832023-01-11 14:50:10 +010010578 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10579 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10580 output_buffer, size_zk_public - 1, &output_len),
10581 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010582 goto exit;
10583 }
10584
Gilles Peskine449bd832023-01-11 14:50:10 +010010585 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010586 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010587 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10588 output_buffer, size_zk_public - 1, &output_len);
10589 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10590 output_buffer, buf_size, &output_len),
10591 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010592 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010593 }
10594 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010595
10596exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010597 PSA_ASSERT(psa_destroy_key(key));
10598 PSA_ASSERT(psa_pake_abort(&operation));
10599 mbedtls_free(output_buffer);
10600 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010601}
10602/* END_CASE */
10603
Neil Armstronga557cb82022-06-10 08:58:32 +020010604/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010605void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10606 int client_input_first, int inject_error,
10607 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010608{
10609 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10610 psa_pake_operation_t server = psa_pake_operation_init();
10611 psa_pake_operation_t client = psa_pake_operation_init();
10612 psa_algorithm_t alg = alg_arg;
10613 psa_algorithm_t hash_alg = hash_arg;
10614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10616
Gilles Peskine449bd832023-01-11 14:50:10 +010010617 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010618
Gilles Peskine449bd832023-01-11 14:50:10 +010010619 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10620 psa_set_key_algorithm(&attributes, alg);
10621 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10622 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10623 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010624
Gilles Peskine449bd832023-01-11 14:50:10 +010010625 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10626 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10627 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010628
10629
Gilles Peskine449bd832023-01-11 14:50:10 +010010630 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10631 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010632
Gilles Peskine449bd832023-01-11 14:50:10 +010010633 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10634 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010635
Gilles Peskine449bd832023-01-11 14:50:10 +010010636 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10637 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010638
Gilles Peskine449bd832023-01-11 14:50:10 +010010639 ecjpake_do_round(alg, primitive_arg, &server, &client,
10640 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010641
Gilles Peskine449bd832023-01-11 14:50:10 +010010642 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010644 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010645
Gilles Peskine449bd832023-01-11 14:50:10 +010010646 ecjpake_do_round(alg, primitive_arg, &server, &client,
10647 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010648
10649exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010650 psa_destroy_key(key);
10651 psa_pake_abort(&server);
10652 psa_pake_abort(&client);
10653 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010654}
10655/* END_CASE */
10656
10657/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010658void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10659 int derive_alg_arg, data_t *pw_data,
10660 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010661{
10662 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10663 psa_pake_operation_t server = psa_pake_operation_init();
10664 psa_pake_operation_t client = psa_pake_operation_init();
10665 psa_algorithm_t alg = alg_arg;
10666 psa_algorithm_t hash_alg = hash_arg;
10667 psa_algorithm_t derive_alg = derive_alg_arg;
10668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10670 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010671 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010672 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010673 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010674 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010675
Gilles Peskine449bd832023-01-11 14:50:10 +010010676 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010677
Gilles Peskine449bd832023-01-11 14:50:10 +010010678 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10679 psa_set_key_algorithm(&attributes, alg);
10680 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10681 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10682 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010683
Gilles Peskine449bd832023-01-11 14:50:10 +010010684 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10685 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10686 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010687
Neil Armstrong1e855602022-06-15 11:32:11 +020010688 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010689 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10690 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010691
Gilles Peskine449bd832023-01-11 14:50:10 +010010692 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10693 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10694 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10695 PSA_KEY_DERIVATION_INPUT_SEED,
10696 (const uint8_t *) "", 0));
10697 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10698 PSA_KEY_DERIVATION_INPUT_SEED,
10699 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010700 }
10701
Gilles Peskine449bd832023-01-11 14:50:10 +010010702 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10703 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010704
Gilles Peskine449bd832023-01-11 14:50:10 +010010705 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10706 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010707
Gilles Peskine449bd832023-01-11 14:50:10 +010010708 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10709 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010710
Gilles Peskine449bd832023-01-11 14:50:10 +010010711 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10712 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10713 PSA_ERROR_BAD_STATE);
10714 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10715 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010716 goto exit;
10717 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010718
Neil Armstrongf983caf2022-06-15 15:27:48 +020010719 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010720 ecjpake_do_round(alg, primitive_arg, &server, &client,
10721 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010722
Gilles Peskine449bd832023-01-11 14:50:10 +010010723 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10724 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10725 PSA_ERROR_BAD_STATE);
10726 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10727 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010728 goto exit;
10729 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010730
Neil Armstrongf983caf2022-06-15 15:27:48 +020010731 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010732 ecjpake_do_round(alg, primitive_arg, &server, &client,
10733 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010734
Gilles Peskine449bd832023-01-11 14:50:10 +010010735 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10736 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010737
10738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010739 psa_key_derivation_abort(&server_derive);
10740 psa_key_derivation_abort(&client_derive);
10741 psa_destroy_key(key);
10742 psa_pake_abort(&server);
10743 psa_pake_abort(&client);
10744 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010745}
10746/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010747
10748/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010749void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010750{
10751 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10752 const size_t bits = 256;
10753 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010754 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010755 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010756 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010757
10758 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10759 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010760 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10761 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10762 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10763 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010764 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010765 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10766 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010767
10768 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010769 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10770 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10771 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10772 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10773 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10774 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010775
10776 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010777 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10778 PSA_PAKE_OUTPUT_MAX_SIZE);
10779 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10780 PSA_PAKE_OUTPUT_MAX_SIZE);
10781 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10782 PSA_PAKE_OUTPUT_MAX_SIZE);
10783 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10784 PSA_PAKE_INPUT_MAX_SIZE);
10785 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10786 PSA_PAKE_INPUT_MAX_SIZE);
10787 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10788 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010789}
10790/* END_CASE */