blob: 7a242fd73d79d51b36ef3e9db209f2fe76a23984 [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
Ryan Everett6c488702024-03-14 17:49:44 +00001364 /* Import the key, exactly one thread must succeed. */
Ryan Everett50619992024-03-12 16:55:14 +00001365 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. */
Ryan Everett3de040f2024-03-14 17:50:06 +00001373 TEST_FAIL("The same key has been loaded into the key store multiple times.");
Ryan Everett50619992024-03-12 16:55:14 +00001374 }
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);
Ryan Everett3de040f2024-03-14 17:50:06 +00001448 TEST_FAIL("The same key has been destroyed multiple times.");
Ryan Everett50619992024-03-12 16:55:14 +00001449 }
1450 skc->key_loaded = 0;
1451 mbedtls_mutex_unlock(&skc->key_loaded_mutex);
1452 }
1453 } else {
1454 TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE);
1455 }
1456
1457exit:
1458 return NULL;
1459}
1460
Ryan3a1b7862024-03-01 17:24:04 +00001461typedef struct generate_key_context {
1462 psa_key_type_t type;
1463 psa_key_usage_t usage;
1464 size_t bits;
1465 psa_algorithm_t alg;
1466 psa_status_t expected_status;
1467 psa_key_attributes_t *attributes;
1468 int is_large_key;
1469 int reps;
1470}
1471generate_key_context;
1472void *thread_generate_key(void *ctx)
1473{
1474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1475 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1476 generate_key_context *gkc = (struct generate_key_context *) ctx;
1477
1478 /* If there are race conditions, it is likely the case that they do not
1479 * arise every time the code runs. We repeat the code to increase the
1480 * chance that any race conditions will be hit. */
1481 for (int n = 0; n < gkc->reps; n++) {
1482 /* Generate a key */
1483 psa_status_t status = psa_generate_key(gkc->attributes, &key);
1484
1485 if (gkc->is_large_key > 0) {
1486 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1487 }
1488
1489 TEST_EQUAL(status, gkc->expected_status);
1490 if (gkc->expected_status != PSA_SUCCESS) {
1491 PSA_ASSERT(psa_destroy_key(key));
1492 goto exit;
1493 }
1494
1495 /* Test the key information */
1496 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1497 TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type);
1498 TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits);
1499
1500 /* Do something with the key according
1501 * to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00001502 if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) {
Ryan3a1b7862024-03-01 17:24:04 +00001503 psa_destroy_key(key);
1504 goto exit;
1505 }
1506 psa_reset_key_attributes(&got_attributes);
1507
1508 PSA_ASSERT(psa_destroy_key(key));
1509 }
1510exit:
1511 /*
1512 * Key attributes may have been returned by psa_get_key_attributes()
1513 * thus reset them as required.
1514 */
1515 psa_reset_key_attributes(&got_attributes);
1516 return NULL;
1517}
1518#endif /* MBEDTLS_THREADING_PTHREAD */
1519
Gilles Peskinee59236f2018-01-27 23:32:46 +01001520/* END_HEADER */
1521
1522/* BEGIN_DEPENDENCIES
1523 * depends_on:MBEDTLS_PSA_CRYPTO_C
1524 * END_DEPENDENCIES
1525 */
1526
1527/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001528void psa_can_do_hash()
1529{
1530 /* We can't test that this is specific to drivers until partial init has
1531 * been implemented, but we can at least test before/after full init. */
1532 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1533 PSA_INIT();
1534 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1535 PSA_DONE();
1536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001541{
1542 size_t max_truncated_mac_size =
1543 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1544
1545 /* Check that the length for a truncated MAC always fits in the algorithm
1546 * encoding. The shifted mask is the maximum truncated value. The
1547 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001549}
1550/* END_CASE */
1551
1552/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553void import_with_policy(int type_arg,
1554 int usage_arg, int alg_arg,
1555 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001556{
1557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1558 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001560 psa_key_type_t type = type_arg;
1561 psa_key_usage_t usage = usage_arg;
1562 psa_algorithm_t alg = alg_arg;
1563 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001565 psa_status_t status;
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 psa_set_key_type(&attributes, type);
1570 psa_set_key_usage_flags(&attributes, usage);
1571 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 status = psa_import_key(&attributes,
1574 key_material, sizeof(key_material),
1575 &key);
1576 TEST_EQUAL(status, expected_status);
1577 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001578 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1582 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1583 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1584 mbedtls_test_update_key_usage_flags(usage));
1585 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1586 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 PSA_ASSERT(psa_destroy_key(key));
1589 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001590
1591exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001592 /*
1593 * Key attributes may have been returned by psa_get_key_attributes()
1594 * thus reset them as required.
1595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 psa_destroy_key(key);
1599 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001600}
1601/* END_CASE */
1602
1603/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604void import_with_data(data_t *data, int type_arg,
1605 int attr_bits_arg,
1606 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001607{
1608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1609 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001611 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001612 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001613 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001614 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 psa_set_key_type(&attributes, type);
1619 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001622 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1623 *
1624 * This can happen with a type supported only by a driver:
1625 * - the driver sees the invalid data (for example wrong size) and thinks
1626 * "well perhaps this is a key size I don't support" so it returns
1627 * NOT_SUPPORTED which is correct at this point;
1628 * - we fallback to built-ins, which don't support this type, so return
1629 * NOT_SUPPORTED which again is correct at this point.
1630 */
1631 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1632 status == PSA_ERROR_NOT_SUPPORTED) {
1633 ; // OK
1634 } else {
1635 TEST_EQUAL(status, expected_status);
1636 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1642 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1643 if (attr_bits != 0) {
1644 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1645 }
1646 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 PSA_ASSERT(psa_destroy_key(key));
1649 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650
1651exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001652 /*
1653 * Key attributes may have been returned by psa_get_key_attributes()
1654 * thus reset them as required.
1655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 psa_destroy_key(key);
1659 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660}
1661/* END_CASE */
1662
1663/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001664/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665void import_large_key(int type_arg, int byte_size_arg,
1666 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001667{
1668 psa_key_type_t type = type_arg;
1669 size_t byte_size = byte_size_arg;
1670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1671 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001672 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001673 psa_status_t status;
1674 uint8_t *buffer = NULL;
1675 size_t buffer_size = byte_size + 1;
1676 size_t n;
1677
Steven Cooreman69967ce2021-01-18 18:01:08 +01001678 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001679 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001680 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001684
1685 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1687 psa_set_key_type(&attributes, type);
1688 status = psa_import_key(&attributes, buffer, byte_size, &key);
1689 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1690 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (status == PSA_SUCCESS) {
1693 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1694 TEST_EQUAL(psa_get_key_type(&attributes), type);
1695 TEST_EQUAL(psa_get_key_bits(&attributes),
1696 PSA_BYTES_TO_BITS(byte_size));
1697 ASSERT_NO_SLOT_NUMBER(&attributes);
1698 memset(buffer, 0, byte_size + 1);
1699 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1700 for (n = 0; n < byte_size; n++) {
1701 TEST_EQUAL(buffer[n], 'K');
1702 }
1703 for (n = byte_size; n < buffer_size; n++) {
1704 TEST_EQUAL(buffer[n], 0);
1705 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001706 }
1707
1708exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001709 /*
1710 * Key attributes may have been returned by psa_get_key_attributes()
1711 * thus reset them as required.
1712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 psa_destroy_key(key);
1716 PSA_DONE();
1717 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001718}
1719/* END_CASE */
1720
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001721/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001722/* Import an RSA key with a valid structure (but not valid numbers
1723 * inside, beyond having sensible size and parity). This is expected to
1724 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001726{
Ronald Cron5425a212020-08-04 14:58:35 +02001727 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001728 size_t bits = bits_arg;
1729 psa_status_t expected_status = expected_status_arg;
1730 psa_status_t status;
1731 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001732 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001733 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001735 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001736 unsigned char *p;
1737 int ret;
1738 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001742 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1745 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001746 length = ret;
1747
1748 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 psa_set_key_type(&attributes, type);
1750 status = psa_import_key(&attributes, p, length, &key);
1751 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (status == PSA_SUCCESS) {
1754 PSA_ASSERT(psa_destroy_key(key));
1755 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001756
1757exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 mbedtls_free(buffer);
1759 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001764void import_export(data_t *data,
1765 int type_arg,
1766 int usage_arg, int alg_arg,
1767 int lifetime_arg,
1768 int expected_bits,
1769 int export_size_delta,
1770 int expected_export_status_arg,
1771 /*whether reexport must give the original input exactly*/
1772 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001773{
Ronald Cron5425a212020-08-04 14:58:35 +02001774 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001775 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001776 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001777 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001778 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301779 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001780 unsigned char *exported = NULL;
1781 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001782 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001783 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001784 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001786 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001787
Moran Pekercb088e72018-07-17 17:36:59 +03001788 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001789 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001791 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 }
1793 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 psa_set_key_lifetime(&attributes, lifetime);
1796 psa_set_key_usage_flags(&attributes, usage_arg);
1797 psa_set_key_algorithm(&attributes, alg);
1798 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001799
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001800 if (PSA_KEY_TYPE_IS_DH(type) &&
1801 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001802 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1803 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001804 }
1805
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001806 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001807 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001808 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001809
1810 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1812 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1813 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1814 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001815
1816 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 status = psa_export_key(key, exported, export_size, &exported_length);
1818 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001819
1820 /* The exported length must be set by psa_export_key() to a value between 0
1821 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1823 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1824 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1827 export_size - exported_length));
1828 if (status != PSA_SUCCESS) {
1829 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001830 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001831 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001832
Gilles Peskineea38a922021-02-13 00:05:16 +01001833 /* Run sanity checks on the exported key. For non-canonical inputs,
1834 * this validates the canonical representations. For canonical inputs,
1835 * this doesn't directly validate the implementation, but it still helps
1836 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (!psa_key_lifetime_is_external(lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00001838 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 }
Archana4d7ae1d2021-07-07 02:50:22 +05301841 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001844 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001846 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1848 &key2));
1849 PSA_ASSERT(psa_export_key(key2,
1850 reexported,
1851 export_size,
1852 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001853 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001854 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001856 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 TEST_LE_U(exported_length,
1858 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1859 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001860 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1861 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1862 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1863 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1864 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001865
1866destroy:
1867 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 PSA_ASSERT(psa_destroy_key(key));
1869 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001870
1871exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001872 /*
1873 * Key attributes may have been returned by psa_get_key_attributes()
1874 * thus reset them as required.
1875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 psa_reset_key_attributes(&got_attributes);
1877 psa_destroy_key(key);
1878 mbedtls_free(exported);
1879 mbedtls_free(reexported);
1880 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001881}
1882/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001883
Moran Pekerf709f4a2018-06-06 17:26:04 +03001884/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885void import_export_public_key(data_t *data,
1886 int type_arg, // key pair or public key
1887 int alg_arg,
1888 int lifetime_arg,
1889 int export_size_delta,
1890 int expected_export_status_arg,
1891 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001892{
Ronald Cron5425a212020-08-04 14:58:35 +02001893 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001894 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001895 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001896 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001897 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301898 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001899 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001900 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001901 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 psa_set_key_lifetime(&attributes, lifetime);
1907 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1908 psa_set_key_algorithm(&attributes, alg);
1909 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001910
1911 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001913
Gilles Peskine49c25912018-10-29 15:15:31 +01001914 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001915 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 status = psa_export_public_key(key,
1917 exported, export_size,
1918 &exported_length);
1919 TEST_EQUAL(status, expected_export_status);
1920 if (status == PSA_SUCCESS) {
1921 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001922 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1924 bits = psa_get_key_bits(&attributes);
1925 TEST_LE_U(expected_public_key->len,
1926 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1927 TEST_LE_U(expected_public_key->len,
1928 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1929 TEST_LE_U(expected_public_key->len,
1930 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001931 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001932 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001933 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001934exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001935 /*
1936 * Key attributes may have been returned by psa_get_key_attributes()
1937 * thus reset them as required.
1938 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 mbedtls_free(exported);
1942 psa_destroy_key(key);
1943 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001944}
1945/* END_CASE */
1946
Ryan Everett50619992024-03-12 16:55:14 +00001947
1948#if defined(MBEDTLS_THREADING_PTHREAD)
1949/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */
1950void concurrently_use_same_persistent_key(data_t *data,
1951 int type_arg,
1952 int bits_arg,
1953 int alg_arg,
1954 int thread_count_arg)
1955{
1956 size_t thread_count = (size_t) thread_count_arg;
1957 mbedtls_test_thread_t *threads = NULL;
1958 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
1959 same_key_context skc;
1960 skc.data = data;
1961 skc.key = key_id;
1962 skc.type = type_arg;
1963 skc.bits = bits_arg;
1964 skc.key_loaded = 0;
1965 mbedtls_mutex_init(&skc.key_loaded_mutex);
1966 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg);
1967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1968
1969 PSA_ASSERT(psa_crypto_init());
1970
1971 psa_set_key_id(&attributes, key_id);
1972 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
1973 psa_set_key_usage_flags(&attributes, usage);
1974 psa_set_key_algorithm(&attributes, alg_arg);
1975 psa_set_key_type(&attributes, type_arg);
1976 psa_set_key_bits(&attributes, bits_arg);
1977 skc.attributes = &attributes;
1978
1979 TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count);
1980
1981 /* Test that when multiple threads import the same key,
1982 * exactly one thread succeeds and the rest fail with valid errors.
1983 * Also test that all threads can use the key as soon as it has been
1984 * imported. */
1985 for (size_t i = 0; i < thread_count; i++) {
1986 TEST_EQUAL(
1987 mbedtls_test_thread_create(&threads[i], thread_import_key,
1988 (void *) &skc), 0);
1989 }
1990
1991 /* Join threads. */
1992 for (size_t i = 0; i < thread_count; i++) {
1993 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
1994 }
1995
1996 /* Test that when multiple threads use and destroy a key no corruption
1997 * occurs, and exactly one thread succeeds when destroying the key. */
1998 for (size_t i = 0; i < thread_count; i++) {
1999 TEST_EQUAL(
2000 mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key,
2001 (void *) &skc), 0);
2002 }
2003
2004 /* Join threads. */
2005 for (size_t i = 0; i < thread_count; i++) {
2006 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
2007 }
2008 /* Ensure that one thread succeeded in destroying the key. */
2009 TEST_ASSERT(!skc.key_loaded);
2010exit:
2011 psa_reset_key_attributes(&attributes);
2012 mbedtls_mutex_free(&skc.key_loaded_mutex);
2013 mbedtls_free(threads);
2014 PSA_DONE();
2015}
2016/* END_CASE */
2017#endif
2018
Gilles Peskine20035e32018-02-03 22:44:14 +01002019/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020void import_and_exercise_key(data_t *data,
2021 int type_arg,
2022 int bits_arg,
2023 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002024{
Ronald Cron5425a212020-08-04 14:58:35 +02002025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002026 psa_key_type_t type = type_arg;
2027 size_t bits = bits_arg;
2028 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02002030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002031 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 psa_set_key_usage_flags(&attributes, usage);
2036 psa_set_key_algorithm(&attributes, alg);
2037 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002038
2039 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002041
2042 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2044 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
2045 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002046
2047 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00002048 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02002049 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 PSA_ASSERT(psa_destroy_key(key));
2053 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02002054
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002056 /*
2057 * Key attributes may have been returned by psa_get_key_attributes()
2058 * thus reset them as required.
2059 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 psa_reset_key_attributes(&attributes);
2063 psa_destroy_key(key);
2064 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02002065}
2066/* END_CASE */
2067
2068/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069void effective_key_attributes(int type_arg, int expected_type_arg,
2070 int bits_arg, int expected_bits_arg,
2071 int usage_arg, int expected_usage_arg,
2072 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02002073{
Ronald Cron5425a212020-08-04 14:58:35 +02002074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01002075 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002076 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01002077 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002078 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02002079 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002080 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02002081 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01002082 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 psa_set_key_usage_flags(&attributes, usage);
2088 psa_set_key_algorithm(&attributes, alg);
2089 psa_set_key_type(&attributes, key_type);
2090 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02002091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 PSA_ASSERT(psa_generate_key(&attributes, &key));
2093 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2096 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
2097 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
2098 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
2099 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02002100
2101exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002102 /*
2103 * Key attributes may have been returned by psa_get_key_attributes()
2104 * thus reset them as required.
2105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 psa_destroy_key(key);
2109 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002110}
2111/* END_CASE */
2112
2113/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002114void check_key_policy(int type_arg, int bits_arg,
2115 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01002116{
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
2118 usage_arg,
2119 mbedtls_test_update_key_usage_flags(usage_arg),
2120 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01002121 goto exit;
2122}
2123/* END_CASE */
2124
2125/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002126void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00002127{
2128 /* Test each valid way of initializing the object, except for `= {0}`, as
2129 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2130 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002131 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002133 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
2134 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
2139 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
2140 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 TEST_EQUAL(psa_get_key_type(&func), 0);
2143 TEST_EQUAL(psa_get_key_type(&init), 0);
2144 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 TEST_EQUAL(psa_get_key_bits(&func), 0);
2147 TEST_EQUAL(psa_get_key_bits(&init), 0);
2148 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
2151 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
2152 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
2155 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
2156 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00002157}
2158/* END_CASE */
2159
2160/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161void mac_key_policy(int policy_usage_arg,
2162 int policy_alg_arg,
2163 int key_type_arg,
2164 data_t *key_data,
2165 int exercise_alg_arg,
2166 int expected_status_sign_arg,
2167 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02002168{
Ronald Cron5425a212020-08-04 14:58:35 +02002169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00002171 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002172 psa_key_type_t key_type = key_type_arg;
2173 psa_algorithm_t policy_alg = policy_alg_arg;
2174 psa_algorithm_t exercise_alg = exercise_alg_arg;
2175 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002176 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02002177 psa_status_t expected_status_sign = expected_status_sign_arg;
2178 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 psa_set_key_usage_flags(&attributes, policy_usage);
2184 psa_set_key_algorithm(&attributes, policy_alg);
2185 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2188 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
2191 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2194 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002195
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002196 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002198 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
2200 input, 128,
2201 mac, PSA_MAC_MAX_SIZE, &mac_len),
2202 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002203
Neil Armstrong3af9b972022-02-07 12:20:21 +01002204 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 PSA_ASSERT(psa_mac_abort(&operation));
2206 status = psa_mac_sign_setup(&operation, key, exercise_alg);
2207 if (status == PSA_SUCCESS) {
2208 status = psa_mac_update(&operation, input, 128);
2209 if (status == PSA_SUCCESS) {
2210 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
2211 &mac_len),
2212 expected_status_sign);
2213 } else {
2214 TEST_EQUAL(status, expected_status_sign);
2215 }
2216 } else {
2217 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01002220
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002221 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 status = psa_mac_verify(key, exercise_alg, input, 128,
2223 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2226 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2227 } else {
2228 TEST_EQUAL(status, expected_status_verify);
2229 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01002230
Neil Armstrong3af9b972022-02-07 12:20:21 +01002231 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2233 if (status == PSA_SUCCESS) {
2234 status = psa_mac_update(&operation, input, 128);
2235 if (status == PSA_SUCCESS) {
2236 status = psa_mac_verify_finish(&operation, mac, mac_len);
2237 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
2238 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2239 } else {
2240 TEST_EQUAL(status, expected_status_verify);
2241 }
2242 } else {
2243 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002244 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 } else {
2246 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01002247 }
2248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 memset(mac, 0, sizeof(mac));
2252 status = psa_mac_verify_setup(&operation, key, exercise_alg);
2253 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002254
2255exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 psa_mac_abort(&operation);
2257 psa_destroy_key(key);
2258 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002259}
2260/* END_CASE */
2261
2262/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263void cipher_key_policy(int policy_usage_arg,
2264 int policy_alg,
2265 int key_type,
2266 data_t *key_data,
2267 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002268{
Ronald Cron5425a212020-08-04 14:58:35 +02002269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002271 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002272 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002273 size_t output_buffer_size = 0;
2274 size_t input_buffer_size = 0;
2275 size_t output_length = 0;
2276 uint8_t *output = NULL;
2277 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002278 psa_status_t status;
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
2281 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
2282 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002283
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002284 TEST_CALLOC(input, input_buffer_size);
2285 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 psa_set_key_usage_flags(&attributes, policy_usage);
2290 psa_set_key_algorithm(&attributes, policy_alg);
2291 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2294 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002295
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002296 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 TEST_EQUAL(policy_usage,
2298 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002299
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002300 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2302 output, output_buffer_size,
2303 &output_length);
2304 if (policy_alg == exercise_alg &&
2305 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2306 PSA_ASSERT(status);
2307 } else {
2308 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2309 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002310
2311 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2313 if (policy_alg == exercise_alg &&
2314 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2315 PSA_ASSERT(status);
2316 } else {
2317 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2318 }
2319 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002320
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002321 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2323 input, input_buffer_size,
2324 &output_length);
2325 if (policy_alg == exercise_alg &&
2326 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2327 PSA_ASSERT(status);
2328 } else {
2329 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2330 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002331
2332 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2334 if (policy_alg == exercise_alg &&
2335 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2336 PSA_ASSERT(status);
2337 } else {
2338 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2339 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002340
2341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 psa_cipher_abort(&operation);
2343 mbedtls_free(input);
2344 mbedtls_free(output);
2345 psa_destroy_key(key);
2346 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002347}
2348/* END_CASE */
2349
2350/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351void aead_key_policy(int policy_usage_arg,
2352 int policy_alg,
2353 int key_type,
2354 data_t *key_data,
2355 int nonce_length_arg,
2356 int tag_length_arg,
2357 int exercise_alg,
2358 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002359{
Ronald Cron5425a212020-08-04 14:58:35 +02002360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002362 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002363 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002364 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002365 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002367 size_t nonce_length = nonce_length_arg;
2368 unsigned char tag[16];
2369 size_t tag_length = tag_length_arg;
2370 size_t output_length;
2371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 TEST_LE_U(nonce_length, sizeof(nonce));
2373 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 psa_set_key_usage_flags(&attributes, policy_usage);
2378 psa_set_key_algorithm(&attributes, policy_alg);
2379 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2382 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002383
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002384 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 TEST_EQUAL(policy_usage,
2386 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002387
Neil Armstrong752d8112022-02-07 14:51:11 +01002388 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 status = psa_aead_encrypt(key, exercise_alg,
2390 nonce, nonce_length,
2391 NULL, 0,
2392 NULL, 0,
2393 tag, tag_length,
2394 &output_length);
2395 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2396 TEST_EQUAL(status, expected_status);
2397 } else {
2398 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2399 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002400
Neil Armstrong752d8112022-02-07 14:51:11 +01002401 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2403 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2404 TEST_EQUAL(status, expected_status);
2405 } else {
2406 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2407 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002408
2409 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 memset(tag, 0, sizeof(tag));
2411 status = psa_aead_decrypt(key, exercise_alg,
2412 nonce, nonce_length,
2413 NULL, 0,
2414 tag, tag_length,
2415 NULL, 0,
2416 &output_length);
2417 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2418 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2419 } else if (expected_status == PSA_SUCCESS) {
2420 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2421 } else {
2422 TEST_EQUAL(status, expected_status);
2423 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002424
Neil Armstrong752d8112022-02-07 14:51:11 +01002425 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 PSA_ASSERT(psa_aead_abort(&operation));
2427 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2428 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2429 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2430 } else {
2431 TEST_EQUAL(status, expected_status);
2432 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002433
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 PSA_ASSERT(psa_aead_abort(&operation));
2436 psa_destroy_key(key);
2437 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002438}
2439/* END_CASE */
2440
2441/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442void asymmetric_encryption_key_policy(int policy_usage_arg,
2443 int policy_alg,
2444 int key_type,
2445 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002446 int exercise_alg,
2447 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002448{
Ronald Cron5425a212020-08-04 14:58:35 +02002449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002451 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002452 psa_status_t status;
2453 size_t key_bits;
2454 size_t buffer_length;
2455 unsigned char *buffer = NULL;
2456 size_t output_length;
2457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 psa_set_key_usage_flags(&attributes, policy_usage);
2461 psa_set_key_algorithm(&attributes, policy_alg);
2462 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002463
Valerio Settif202c292024-01-15 10:42:37 +01002464 if (use_opaque_key) {
2465 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2466 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2467 }
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2470 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002471
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002472 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 TEST_EQUAL(policy_usage,
2474 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2477 key_bits = psa_get_key_bits(&attributes);
2478 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2479 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002480 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 status = psa_asymmetric_encrypt(key, exercise_alg,
2483 NULL, 0,
2484 NULL, 0,
2485 buffer, buffer_length,
2486 &output_length);
2487 if (policy_alg == exercise_alg &&
2488 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2489 PSA_ASSERT(status);
2490 } else {
2491 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2492 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (buffer_length != 0) {
2495 memset(buffer, 0, buffer_length);
2496 }
2497 status = psa_asymmetric_decrypt(key, exercise_alg,
2498 buffer, buffer_length,
2499 NULL, 0,
2500 buffer, buffer_length,
2501 &output_length);
2502 if (policy_alg == exercise_alg &&
2503 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2504 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2505 } else {
2506 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2507 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002508
2509exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002510 /*
2511 * Key attributes may have been returned by psa_get_key_attributes()
2512 * thus reset them as required.
2513 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 psa_destroy_key(key);
2517 PSA_DONE();
2518 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002519}
2520/* END_CASE */
2521
2522/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002523void asymmetric_signature_key_policy(int policy_usage_arg,
2524 int policy_alg,
2525 int key_type,
2526 data_t *key_data,
2527 int exercise_alg,
2528 int payload_length_arg,
2529 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002530{
Ronald Cron5425a212020-08-04 14:58:35 +02002531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002533 psa_key_usage_t policy_usage = policy_usage_arg;
2534 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002535 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002537 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2538 * compatible with the policy and `payload_length_arg` is supposed to be
2539 * a valid input length to sign. If `payload_length_arg <= 0`,
2540 * `exercise_alg` is supposed to be forbidden by the policy. */
2541 int compatible_alg = payload_length_arg > 0;
2542 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002544 size_t signature_length;
2545
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002546 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002547 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 TEST_EQUAL(expected_usage,
2549 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 psa_set_key_usage_flags(&attributes, policy_usage);
2554 psa_set_key_algorithm(&attributes, policy_alg);
2555 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2558 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 status = psa_sign_hash(key, exercise_alg,
2563 payload, payload_length,
2564 signature, sizeof(signature),
2565 &signature_length);
2566 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2567 PSA_ASSERT(status);
2568 } else {
2569 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2570 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 memset(signature, 0, sizeof(signature));
2573 status = psa_verify_hash(key, exercise_alg,
2574 payload, payload_length,
2575 signature, sizeof(signature));
2576 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2577 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2578 } else {
2579 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2580 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2583 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2584 status = psa_sign_message(key, exercise_alg,
2585 payload, payload_length,
2586 signature, sizeof(signature),
2587 &signature_length);
2588 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2589 PSA_ASSERT(status);
2590 } else {
2591 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2592 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 memset(signature, 0, sizeof(signature));
2595 status = psa_verify_message(key, exercise_alg,
2596 payload, payload_length,
2597 signature, sizeof(signature));
2598 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2599 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2600 } else {
2601 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2602 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002603 }
2604
Gilles Peskined5b33222018-06-18 22:20:03 +02002605exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 psa_destroy_key(key);
2607 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002608}
2609/* END_CASE */
2610
Janos Follathba3fab92019-06-11 14:50:16 +01002611/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002612void derive_key_policy(int policy_usage,
2613 int policy_alg,
2614 int key_type,
2615 data_t *key_data,
2616 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002617{
Ronald Cron5425a212020-08-04 14:58:35 +02002618 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002620 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002621 psa_status_t status;
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 psa_set_key_usage_flags(&attributes, policy_usage);
2626 psa_set_key_algorithm(&attributes, policy_alg);
2627 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2630 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2635 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2636 PSA_ASSERT(psa_key_derivation_input_bytes(
2637 &operation,
2638 PSA_KEY_DERIVATION_INPUT_SEED,
2639 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002640 }
Janos Follathba3fab92019-06-11 14:50:16 +01002641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 status = psa_key_derivation_input_key(&operation,
2643 PSA_KEY_DERIVATION_INPUT_SECRET,
2644 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (policy_alg == exercise_alg &&
2647 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2648 PSA_ASSERT(status);
2649 } else {
2650 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2651 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002652
2653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 psa_key_derivation_abort(&operation);
2655 psa_destroy_key(key);
2656 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002657}
2658/* END_CASE */
2659
2660/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002661void agreement_key_policy(int policy_usage,
2662 int policy_alg,
2663 int key_type_arg,
2664 data_t *key_data,
2665 int exercise_alg,
2666 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002667{
Ronald Cron5425a212020-08-04 14:58:35 +02002668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002670 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002671 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002672 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002673 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 psa_set_key_usage_flags(&attributes, policy_usage);
2678 psa_set_key_algorithm(&attributes, policy_alg);
2679 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2682 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Ryan Everett73e4ea32024-03-12 16:29:55 +00002685 status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002688
2689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 psa_key_derivation_abort(&operation);
2691 psa_destroy_key(key);
2692 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002693}
2694/* END_CASE */
2695
2696/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002697void key_policy_alg2(int key_type_arg, data_t *key_data,
2698 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002699{
Ronald Cron5425a212020-08-04 14:58:35 +02002700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002701 psa_key_type_t key_type = key_type_arg;
2702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2703 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2704 psa_key_usage_t usage = usage_arg;
2705 psa_algorithm_t alg = alg_arg;
2706 psa_algorithm_t alg2 = alg2_arg;
2707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 psa_set_key_usage_flags(&attributes, usage);
2711 psa_set_key_algorithm(&attributes, alg);
2712 psa_set_key_enrollment_algorithm(&attributes, alg2);
2713 psa_set_key_type(&attributes, key_type);
2714 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2715 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002716
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002717 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 usage = mbedtls_test_update_key_usage_flags(usage);
2719 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2720 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2721 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2722 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002723
Ryan Everett0a271fd2024-03-12 16:34:02 +00002724 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002727 if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002728 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002730
2731exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002732 /*
2733 * Key attributes may have been returned by psa_get_key_attributes()
2734 * thus reset them as required.
2735 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 psa_destroy_key(key);
2739 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002740}
2741/* END_CASE */
2742
2743/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002744void raw_agreement_key_policy(int policy_usage,
2745 int policy_alg,
2746 int key_type_arg,
2747 data_t *key_data,
2748 int exercise_alg,
2749 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002750{
Ronald Cron5425a212020-08-04 14:58:35 +02002751 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002753 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002754 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002755 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002756 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002759
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 psa_set_key_usage_flags(&attributes, policy_usage);
2761 psa_set_key_algorithm(&attributes, policy_alg);
2762 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2765 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002766
Ryan Everett81630282024-03-12 16:21:12 +00002767 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key, 0);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002770
2771exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 psa_key_derivation_abort(&operation);
2773 psa_destroy_key(key);
2774 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002775}
2776/* END_CASE */
2777
2778/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002779void copy_success(int source_usage_arg,
2780 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002781 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 int type_arg, data_t *material,
2783 int copy_attributes,
2784 int target_usage_arg,
2785 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002786 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 int expected_usage_arg,
2788 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002789{
Gilles Peskineca25db92019-04-19 11:43:08 +02002790 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2791 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002792 psa_key_usage_t expected_usage = expected_usage_arg;
2793 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002794 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302795 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2796 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002797 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2798 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002799 uint8_t *export_buffer = NULL;
2800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002802
Gilles Peskineca25db92019-04-19 11:43:08 +02002803 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2805 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2806 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2807 psa_set_key_type(&source_attributes, type_arg);
2808 psa_set_key_lifetime(&source_attributes, source_lifetime);
2809 PSA_ASSERT(psa_import_key(&source_attributes,
2810 material->x, material->len,
2811 &source_key));
2812 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002813
Gilles Peskineca25db92019-04-19 11:43:08 +02002814 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002816 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (target_usage_arg != -1) {
2821 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2822 }
2823 if (target_alg_arg != -1) {
2824 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2825 }
2826 if (target_alg2_arg != -1) {
2827 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2828 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002829
Archana8a180362021-07-05 02:18:48 +05302830
Gilles Peskine57ab7212019-01-28 13:03:09 +01002831 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 PSA_ASSERT(psa_copy_key(source_key,
2833 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002834
2835 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002837
2838 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2840 TEST_EQUAL(psa_get_key_type(&source_attributes),
2841 psa_get_key_type(&target_attributes));
2842 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2843 psa_get_key_bits(&target_attributes));
2844 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2845 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2846 TEST_EQUAL(expected_alg2,
2847 psa_get_key_enrollment_algorithm(&target_attributes));
2848 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002849 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002850 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2852 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002853 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002854 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002855 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (!psa_key_lifetime_is_external(target_lifetime)) {
Ryan Everett0a271fd2024-03-12 16:34:02 +00002858 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) {
Archana8a180362021-07-05 02:18:48 +05302859 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 }
Ryan Everett0a271fd2024-03-12 16:34:02 +00002861 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) {
Archana8a180362021-07-05 02:18:48 +05302862 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 }
Archana8a180362021-07-05 02:18:48 +05302864 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002867
2868exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002869 /*
2870 * Source and target key attributes may have been returned by
2871 * psa_get_key_attributes() thus reset them as required.
2872 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 psa_reset_key_attributes(&source_attributes);
2874 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 PSA_DONE();
2877 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002878}
2879/* END_CASE */
2880
2881/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002882void copy_fail(int source_usage_arg,
2883 int source_alg_arg, int source_alg2_arg,
2884 int source_lifetime_arg,
2885 int type_arg, data_t *material,
2886 int target_type_arg, int target_bits_arg,
2887 int target_usage_arg,
2888 int target_alg_arg, int target_alg2_arg,
2889 int target_id_arg, int target_lifetime_arg,
2890 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002891{
2892 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2893 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002894 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2895 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002899
2900 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2902 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2903 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2904 psa_set_key_type(&source_attributes, type_arg);
2905 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2906 PSA_ASSERT(psa_import_key(&source_attributes,
2907 material->x, material->len,
2908 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002909
2910 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 psa_set_key_id(&target_attributes, key_id);
2912 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2913 psa_set_key_type(&target_attributes, target_type_arg);
2914 psa_set_key_bits(&target_attributes, target_bits_arg);
2915 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2916 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2917 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002918
2919 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 TEST_EQUAL(psa_copy_key(source_key,
2921 &target_attributes, &target_key),
2922 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002925
Gilles Peskine4a644642019-05-03 17:14:08 +02002926exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 psa_reset_key_attributes(&source_attributes);
2928 psa_reset_key_attributes(&target_attributes);
2929 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002930}
2931/* END_CASE */
2932
2933/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002934void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002935{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002936 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002937 /* Test each valid way of initializing the object, except for `= {0}`, as
2938 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2939 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002940 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002942 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2943 psa_hash_operation_t zero;
2944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002946
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002947 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2949 PSA_ERROR_BAD_STATE);
2950 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2951 PSA_ERROR_BAD_STATE);
2952 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2953 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002954
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002955 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 PSA_ASSERT(psa_hash_abort(&func));
2957 PSA_ASSERT(psa_hash_abort(&init));
2958 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002959}
2960/* END_CASE */
2961
2962/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002963void hash_setup(int alg_arg,
2964 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002965{
2966 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002967 uint8_t *output = NULL;
2968 size_t output_size = 0;
2969 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002970 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002971 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002972 psa_status_t status;
2973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002975
Neil Armstrongedb20862022-02-07 15:47:44 +01002976 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002978 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 status = psa_hash_compute(alg, NULL, 0,
2981 output, output_size, &output_length);
2982 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002983
2984 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 status = psa_hash_setup(&operation, alg);
2986 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002987
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002988 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002990
2991 /* If setup failed, reproduce the failure, so as to
2992 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 if (status != PSA_SUCCESS) {
2994 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2995 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002996
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002997 /* Now the operation object should be reusable. */
2998#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
3000 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003001#endif
3002
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003003exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 mbedtls_free(output);
3005 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003006}
3007/* END_CASE */
3008
3009/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003010void hash_compute_fail(int alg_arg, data_t *input,
3011 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01003012{
3013 psa_algorithm_t alg = alg_arg;
3014 uint8_t *output = NULL;
3015 size_t output_size = output_size_arg;
3016 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003017 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01003018 psa_status_t expected_status = expected_status_arg;
3019 psa_status_t status;
3020
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003021 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01003024
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003025 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 status = psa_hash_compute(alg, input->x, input->len,
3027 output, output_size, &output_length);
3028 TEST_EQUAL(status, expected_status);
3029 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003030
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003031 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 status = psa_hash_setup(&operation, alg);
3033 if (status == PSA_SUCCESS) {
3034 status = psa_hash_update(&operation, input->x, input->len);
3035 if (status == PSA_SUCCESS) {
3036 status = psa_hash_finish(&operation, output, output_size,
3037 &output_length);
3038 if (status == PSA_SUCCESS) {
3039 TEST_LE_U(output_length, output_size);
3040 } else {
3041 TEST_EQUAL(status, expected_status);
3042 }
3043 } else {
3044 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003045 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 } else {
3047 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01003048 }
3049
Gilles Peskine0a749c82019-11-28 19:33:58 +01003050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 PSA_ASSERT(psa_hash_abort(&operation));
3052 mbedtls_free(output);
3053 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01003054}
3055/* END_CASE */
3056
3057/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003058void hash_compare_fail(int alg_arg, data_t *input,
3059 data_t *reference_hash,
3060 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01003061{
3062 psa_algorithm_t alg = alg_arg;
3063 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01003064 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01003065 psa_status_t status;
3066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01003068
Neil Armstrong55a1be12022-02-07 11:23:20 +01003069 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 status = psa_hash_compare(alg, input->x, input->len,
3071 reference_hash->x, reference_hash->len);
3072 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01003073
Neil Armstrong55a1be12022-02-07 11:23:20 +01003074 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 status = psa_hash_setup(&operation, alg);
3076 if (status == PSA_SUCCESS) {
3077 status = psa_hash_update(&operation, input->x, input->len);
3078 if (status == PSA_SUCCESS) {
3079 status = psa_hash_verify(&operation, reference_hash->x,
3080 reference_hash->len);
3081 TEST_EQUAL(status, expected_status);
3082 } else {
3083 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01003084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 } else {
3086 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01003087 }
3088
Gilles Peskine88e08462020-01-28 20:43:00 +01003089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 PSA_ASSERT(psa_hash_abort(&operation));
3091 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01003092}
3093/* END_CASE */
3094
3095/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003096void hash_compute_compare(int alg_arg, data_t *input,
3097 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01003098{
3099 psa_algorithm_t alg = alg_arg;
3100 uint8_t output[PSA_HASH_MAX_SIZE + 1];
3101 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01003102 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01003103 size_t i;
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01003106
Neil Armstrongca30a002022-02-07 11:40:23 +01003107 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
3109 output, PSA_HASH_LENGTH(alg),
3110 &output_length));
3111 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003112 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003113 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003114
Neil Armstrongca30a002022-02-07 11:40:23 +01003115 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 PSA_ASSERT(psa_hash_setup(&operation, alg));
3117 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3118 PSA_ASSERT(psa_hash_finish(&operation, output,
3119 PSA_HASH_LENGTH(alg),
3120 &output_length));
3121 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003122 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003123 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01003124
3125 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
3127 output, sizeof(output),
3128 &output_length));
3129 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003130 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003131 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003132
Neil Armstrongca30a002022-02-07 11:40:23 +01003133 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 PSA_ASSERT(psa_hash_setup(&operation, alg));
3135 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3136 PSA_ASSERT(psa_hash_finish(&operation, output,
3137 sizeof(output), &output_length));
3138 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003139 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003140 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01003141
3142 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
3144 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01003145
Neil Armstrongca30a002022-02-07 11:40:23 +01003146 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 PSA_ASSERT(psa_hash_setup(&operation, alg));
3148 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3149 PSA_ASSERT(psa_hash_verify(&operation, output,
3150 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01003151
3152 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3154 output, output_length + 1),
3155 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003156
Neil Armstrongca30a002022-02-07 11:40:23 +01003157 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 PSA_ASSERT(psa_hash_setup(&operation, alg));
3159 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3160 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
3161 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003162
3163 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3165 output, output_length - 1),
3166 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003167
Neil Armstrongca30a002022-02-07 11:40:23 +01003168 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 PSA_ASSERT(psa_hash_setup(&operation, alg));
3170 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3171 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
3172 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003173
Gilles Peskine0a749c82019-11-28 19:33:58 +01003174 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 for (i = 0; i < output_length; i++) {
3176 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01003177 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01003178
3179 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
3181 output, output_length),
3182 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003183
3184 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 PSA_ASSERT(psa_hash_setup(&operation, alg));
3186 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
3187 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
3188 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01003189
Gilles Peskine0a749c82019-11-28 19:33:58 +01003190 output[i] ^= 1;
3191 }
3192
3193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 PSA_ASSERT(psa_hash_abort(&operation));
3195 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01003196}
3197/* END_CASE */
3198
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003199/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003200void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02003201{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003202 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02003203 unsigned char input[] = "";
3204 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003205 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02003206 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3207 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
3209 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003210 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02003211 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00003212 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02003215
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003216 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 PSA_ASSERT(psa_hash_setup(&operation, alg));
3218 ASSERT_OPERATION_IS_ACTIVE(operation);
3219 TEST_EQUAL(psa_hash_setup(&operation, alg),
3220 PSA_ERROR_BAD_STATE);
3221 ASSERT_OPERATION_IS_INACTIVE(operation);
3222 PSA_ASSERT(psa_hash_abort(&operation));
3223 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003224
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003225 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3227 PSA_ERROR_BAD_STATE);
3228 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003229
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003230 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01003232 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 ASSERT_OPERATION_IS_ACTIVE(operation);
3234 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3235 PSA_ERROR_BAD_STATE);
3236 ASSERT_OPERATION_IS_INACTIVE(operation);
3237 PSA_ASSERT(psa_hash_abort(&operation));
3238 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003239
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003240 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 PSA_ASSERT(psa_hash_setup(&operation, alg));
3242 PSA_ASSERT(psa_hash_finish(&operation,
3243 hash, sizeof(hash), &hash_len));
3244 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
3245 PSA_ERROR_BAD_STATE);
3246 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003247
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003248 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 TEST_EQUAL(psa_hash_verify(&operation,
3250 valid_hash, sizeof(valid_hash)),
3251 PSA_ERROR_BAD_STATE);
3252 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003253
3254 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 PSA_ASSERT(psa_hash_setup(&operation, alg));
3256 PSA_ASSERT(psa_hash_finish(&operation,
3257 hash, sizeof(hash), &hash_len));
3258 TEST_EQUAL(psa_hash_verify(&operation,
3259 valid_hash, sizeof(valid_hash)),
3260 PSA_ERROR_BAD_STATE);
3261 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003262
3263 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 PSA_ASSERT(psa_hash_setup(&operation, alg));
3265 ASSERT_OPERATION_IS_ACTIVE(operation);
3266 PSA_ASSERT(psa_hash_verify(&operation,
3267 valid_hash, sizeof(valid_hash)));
3268 ASSERT_OPERATION_IS_INACTIVE(operation);
3269 TEST_EQUAL(psa_hash_verify(&operation,
3270 valid_hash, sizeof(valid_hash)),
3271 PSA_ERROR_BAD_STATE);
3272 ASSERT_OPERATION_IS_INACTIVE(operation);
3273 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003274
3275 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 TEST_EQUAL(psa_hash_finish(&operation,
3277 hash, sizeof(hash), &hash_len),
3278 PSA_ERROR_BAD_STATE);
3279 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003280
3281 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 PSA_ASSERT(psa_hash_setup(&operation, alg));
3283 PSA_ASSERT(psa_hash_finish(&operation,
3284 hash, sizeof(hash), &hash_len));
3285 TEST_EQUAL(psa_hash_finish(&operation,
3286 hash, sizeof(hash), &hash_len),
3287 PSA_ERROR_BAD_STATE);
3288 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003289
3290 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 PSA_ASSERT(psa_hash_setup(&operation, alg));
3292 PSA_ASSERT(psa_hash_verify(&operation,
3293 valid_hash, sizeof(valid_hash)));
3294 TEST_EQUAL(psa_hash_finish(&operation,
3295 hash, sizeof(hash), &hash_len),
3296 PSA_ERROR_BAD_STATE);
3297 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003298
3299exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003301}
3302/* END_CASE */
3303
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003304/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003306{
3307 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003308 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3309 * appended to it */
3310 unsigned char hash[] = {
3311 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3312 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3314 };
3315 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003316 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003317
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003319
itayzafrir27e69452018-11-01 14:26:34 +02003320 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 PSA_ASSERT(psa_hash_setup(&operation, alg));
3322 ASSERT_OPERATION_IS_ACTIVE(operation);
3323 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3324 PSA_ERROR_INVALID_SIGNATURE);
3325 ASSERT_OPERATION_IS_INACTIVE(operation);
3326 PSA_ASSERT(psa_hash_abort(&operation));
3327 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003328
itayzafrir27e69452018-11-01 14:26:34 +02003329 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 PSA_ASSERT(psa_hash_setup(&operation, alg));
3331 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3332 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003333
itayzafrir27e69452018-11-01 14:26:34 +02003334 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 PSA_ASSERT(psa_hash_setup(&operation, alg));
3336 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3337 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003338
itayzafrirec93d302018-10-18 18:01:10 +03003339exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003341}
3342/* END_CASE */
3343
Ronald Cronee414c72021-03-18 18:50:08 +01003344/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003345void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003346{
3347 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003348 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003350 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003351 size_t hash_len;
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003354
itayzafrir58028322018-10-25 10:22:01 +03003355 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 PSA_ASSERT(psa_hash_setup(&operation, alg));
3357 TEST_EQUAL(psa_hash_finish(&operation,
3358 hash, expected_size - 1, &hash_len),
3359 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003360
3361exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003363}
3364/* END_CASE */
3365
Ronald Cronee414c72021-03-18 18:50:08 +01003366/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003368{
3369 psa_algorithm_t alg = PSA_ALG_SHA_256;
3370 unsigned char hash[PSA_HASH_MAX_SIZE];
3371 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3372 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3373 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3374 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3375 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3376 size_t hash_len;
3377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 PSA_ASSERT(psa_crypto_init());
3379 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3382 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3383 PSA_ASSERT(psa_hash_finish(&op_finished,
3384 hash, sizeof(hash), &hash_len));
3385 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3386 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3389 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3392 PSA_ASSERT(psa_hash_finish(&op_init,
3393 hash, sizeof(hash), &hash_len));
3394 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3395 PSA_ASSERT(psa_hash_finish(&op_finished,
3396 hash, sizeof(hash), &hash_len));
3397 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3398 PSA_ASSERT(psa_hash_finish(&op_aborted,
3399 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003400
3401exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 psa_hash_abort(&op_source);
3403 psa_hash_abort(&op_init);
3404 psa_hash_abort(&op_setup);
3405 psa_hash_abort(&op_finished);
3406 psa_hash_abort(&op_aborted);
3407 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003408}
3409/* END_CASE */
3410
Ronald Cronee414c72021-03-18 18:50:08 +01003411/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003412void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003413{
3414 psa_algorithm_t alg = PSA_ALG_SHA_256;
3415 unsigned char hash[PSA_HASH_MAX_SIZE];
3416 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3417 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3418 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3419 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3420 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3421 size_t hash_len;
3422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003424
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3426 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3427 PSA_ASSERT(psa_hash_finish(&op_finished,
3428 hash, sizeof(hash), &hash_len));
3429 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3430 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003431
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3433 PSA_ASSERT(psa_hash_finish(&op_target,
3434 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3437 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3438 PSA_ERROR_BAD_STATE);
3439 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3440 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003441
3442exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 psa_hash_abort(&op_target);
3444 psa_hash_abort(&op_init);
3445 psa_hash_abort(&op_setup);
3446 psa_hash_abort(&op_finished);
3447 psa_hash_abort(&op_aborted);
3448 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003449}
3450/* END_CASE */
3451
itayzafrir58028322018-10-25 10:22:01 +03003452/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003453void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003454{
Jaeden Amero252ef282019-02-15 14:05:35 +00003455 const uint8_t input[1] = { 0 };
3456
Jaeden Amero769ce272019-01-04 11:48:03 +00003457 /* Test each valid way of initializing the object, except for `= {0}`, as
3458 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3459 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003460 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003462 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3463 psa_mac_operation_t zero;
3464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003466
Jaeden Amero252ef282019-02-15 14:05:35 +00003467 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 TEST_EQUAL(psa_mac_update(&func,
3469 input, sizeof(input)),
3470 PSA_ERROR_BAD_STATE);
3471 TEST_EQUAL(psa_mac_update(&init,
3472 input, sizeof(input)),
3473 PSA_ERROR_BAD_STATE);
3474 TEST_EQUAL(psa_mac_update(&zero,
3475 input, sizeof(input)),
3476 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003477
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003478 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 PSA_ASSERT(psa_mac_abort(&func));
3480 PSA_ASSERT(psa_mac_abort(&init));
3481 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003482}
3483/* END_CASE */
3484
3485/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003486void mac_setup(int key_type_arg,
3487 data_t *key,
3488 int alg_arg,
3489 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003490{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003491 psa_key_type_t key_type = key_type_arg;
3492 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003493 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003494 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003495 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3496#if defined(KNOWN_SUPPORTED_MAC_ALG)
3497 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3498#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003499
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003501
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3503 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003504 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 }
3506 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003507
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003508 /* The operation object should be reusable. */
3509#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3511 smoke_test_key_data,
3512 sizeof(smoke_test_key_data),
3513 KNOWN_SUPPORTED_MAC_ALG,
3514 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003515 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 }
3517 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003518#endif
3519
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003522}
3523/* END_CASE */
3524
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003525/* 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 +01003526void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003527{
Ronald Cron5425a212020-08-04 14:58:35 +02003528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003529 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3530 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003531 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003532 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3533 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3535 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003537 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3538 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3539 size_t sign_mac_length = 0;
3540 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3541 const uint8_t verify_mac[] = {
3542 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3543 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3545 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003546
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 PSA_ASSERT(psa_crypto_init());
3548 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3549 psa_set_key_algorithm(&attributes, alg);
3550 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3553 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003554
Jaeden Amero252ef282019-02-15 14:05:35 +00003555 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3557 PSA_ERROR_BAD_STATE);
3558 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003559
3560 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3562 &sign_mac_length),
3563 PSA_ERROR_BAD_STATE);
3564 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003565
3566 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 TEST_EQUAL(psa_mac_verify_finish(&operation,
3568 verify_mac, sizeof(verify_mac)),
3569 PSA_ERROR_BAD_STATE);
3570 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003571
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003572 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3574 ASSERT_OPERATION_IS_ACTIVE(operation);
3575 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3576 PSA_ERROR_BAD_STATE);
3577 ASSERT_OPERATION_IS_INACTIVE(operation);
3578 PSA_ASSERT(psa_mac_abort(&operation));
3579 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003580
Jaeden Amero252ef282019-02-15 14:05:35 +00003581 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3583 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3584 PSA_ASSERT(psa_mac_sign_finish(&operation,
3585 sign_mac, sizeof(sign_mac),
3586 &sign_mac_length));
3587 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3588 PSA_ERROR_BAD_STATE);
3589 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003590
3591 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3593 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3594 PSA_ASSERT(psa_mac_verify_finish(&operation,
3595 verify_mac, sizeof(verify_mac)));
3596 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3597 PSA_ERROR_BAD_STATE);
3598 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003599
3600 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3602 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3603 PSA_ASSERT(psa_mac_sign_finish(&operation,
3604 sign_mac, sizeof(sign_mac),
3605 &sign_mac_length));
3606 TEST_EQUAL(psa_mac_sign_finish(&operation,
3607 sign_mac, sizeof(sign_mac),
3608 &sign_mac_length),
3609 PSA_ERROR_BAD_STATE);
3610 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003611
3612 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003613 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3614 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3615 PSA_ASSERT(psa_mac_verify_finish(&operation,
3616 verify_mac, sizeof(verify_mac)));
3617 TEST_EQUAL(psa_mac_verify_finish(&operation,
3618 verify_mac, sizeof(verify_mac)),
3619 PSA_ERROR_BAD_STATE);
3620 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003621
3622 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003623 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3624 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3625 ASSERT_OPERATION_IS_ACTIVE(operation);
3626 TEST_EQUAL(psa_mac_verify_finish(&operation,
3627 verify_mac, sizeof(verify_mac)),
3628 PSA_ERROR_BAD_STATE);
3629 ASSERT_OPERATION_IS_INACTIVE(operation);
3630 PSA_ASSERT(psa_mac_abort(&operation));
3631 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003632
3633 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3635 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3636 ASSERT_OPERATION_IS_ACTIVE(operation);
3637 TEST_EQUAL(psa_mac_sign_finish(&operation,
3638 sign_mac, sizeof(sign_mac),
3639 &sign_mac_length),
3640 PSA_ERROR_BAD_STATE);
3641 ASSERT_OPERATION_IS_INACTIVE(operation);
3642 PSA_ASSERT(psa_mac_abort(&operation));
3643 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003644
Gilles Peskine449bd832023-01-11 14:50:10 +01003645 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003646
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003647exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003648 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003649}
3650/* END_CASE */
3651
3652/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003653void mac_sign_verify_multi(int key_type_arg,
3654 data_t *key_data,
3655 int alg_arg,
3656 data_t *input,
3657 int is_verify,
3658 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003659{
3660 size_t data_part_len = 0;
3661
Gilles Peskine449bd832023-01-11 14:50:10 +01003662 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003663 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003665
Gilles Peskine449bd832023-01-11 14:50:10 +01003666 if (mac_multipart_internal_func(key_type_arg, key_data,
3667 alg_arg,
3668 input, data_part_len,
3669 expected_mac,
3670 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003671 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003672 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003673
3674 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003676
Gilles Peskine449bd832023-01-11 14:50:10 +01003677 if (mac_multipart_internal_func(key_type_arg, key_data,
3678 alg_arg,
3679 input, data_part_len,
3680 expected_mac,
3681 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003682 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003684 }
3685
3686 /* Goto is required to silence warnings about unused labels, as we
3687 * don't actually do any test assertions in this function. */
3688 goto exit;
3689}
3690/* END_CASE */
3691
3692/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003693void mac_sign(int key_type_arg,
3694 data_t *key_data,
3695 int alg_arg,
3696 data_t *input,
3697 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003698{
Ronald Cron5425a212020-08-04 14:58:35 +02003699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003700 psa_key_type_t key_type = key_type_arg;
3701 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003702 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003704 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003705 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003706 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003707 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003708 const size_t output_sizes_to_test[] = {
3709 0,
3710 1,
3711 expected_mac->len - 1,
3712 expected_mac->len,
3713 expected_mac->len + 1,
3714 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003715
Gilles Peskine449bd832023-01-11 14:50:10 +01003716 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003717 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003718 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003719
Gilles Peskine449bd832023-01-11 14:50:10 +01003720 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003721
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3723 psa_set_key_algorithm(&attributes, alg);
3724 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003725
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3727 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003728
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003730 const size_t output_size = output_sizes_to_test[i];
3731 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003732 (output_size >= expected_mac->len ? PSA_SUCCESS :
3733 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003734
Gilles Peskine449bd832023-01-11 14:50:10 +01003735 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003736 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003737
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003738 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 TEST_EQUAL(psa_mac_compute(key, alg,
3740 input->x, input->len,
3741 actual_mac, output_size, &mac_length),
3742 expected_status);
3743 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003744 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003745 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003746 }
3747
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 if (output_size > 0) {
3749 memset(actual_mac, 0, output_size);
3750 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003751
3752 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3754 PSA_ASSERT(psa_mac_update(&operation,
3755 input->x, input->len));
3756 TEST_EQUAL(psa_mac_sign_finish(&operation,
3757 actual_mac, output_size,
3758 &mac_length),
3759 expected_status);
3760 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003761
Gilles Peskine449bd832023-01-11 14:50:10 +01003762 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003763 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003764 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003765 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003766 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003767 actual_mac = NULL;
3768 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003769
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 psa_mac_abort(&operation);
3772 psa_destroy_key(key);
3773 PSA_DONE();
3774 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003775}
3776/* END_CASE */
3777
3778/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003779void mac_verify(int key_type_arg,
3780 data_t *key_data,
3781 int alg_arg,
3782 data_t *input,
3783 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003784{
Ronald Cron5425a212020-08-04 14:58:35 +02003785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003786 psa_key_type_t key_type = key_type_arg;
3787 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003788 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003790 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003791
Gilles Peskine449bd832023-01-11 14:50:10 +01003792 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003793
Gilles Peskine449bd832023-01-11 14:50:10 +01003794 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003795
Gilles Peskine449bd832023-01-11 14:50:10 +01003796 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3797 psa_set_key_algorithm(&attributes, alg);
3798 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003799
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3801 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003802
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003803 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3805 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003806
3807 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3809 PSA_ASSERT(psa_mac_update(&operation,
3810 input->x, input->len));
3811 PSA_ASSERT(psa_mac_verify_finish(&operation,
3812 expected_mac->x,
3813 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003814
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003815 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 TEST_EQUAL(psa_mac_verify(key, alg,
3817 input->x, input->len,
3818 expected_mac->x,
3819 expected_mac->len - 1),
3820 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003821
3822 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3824 PSA_ASSERT(psa_mac_update(&operation,
3825 input->x, input->len));
3826 TEST_EQUAL(psa_mac_verify_finish(&operation,
3827 expected_mac->x,
3828 expected_mac->len - 1),
3829 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003830
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003831 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003832 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3834 TEST_EQUAL(psa_mac_verify(key, alg,
3835 input->x, input->len,
3836 perturbed_mac, expected_mac->len + 1),
3837 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003838
3839 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3841 PSA_ASSERT(psa_mac_update(&operation,
3842 input->x, input->len));
3843 TEST_EQUAL(psa_mac_verify_finish(&operation,
3844 perturbed_mac,
3845 expected_mac->len + 1),
3846 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003847
3848 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 for (size_t i = 0; i < expected_mac->len; i++) {
3850 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003851 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 TEST_EQUAL(psa_mac_verify(key, alg,
3854 input->x, input->len,
3855 perturbed_mac, expected_mac->len),
3856 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3859 PSA_ASSERT(psa_mac_update(&operation,
3860 input->x, input->len));
3861 TEST_EQUAL(psa_mac_verify_finish(&operation,
3862 perturbed_mac,
3863 expected_mac->len),
3864 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003865 perturbed_mac[i] ^= 1;
3866 }
3867
Gilles Peskine8c9def32018-02-08 10:02:12 +01003868exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 psa_mac_abort(&operation);
3870 psa_destroy_key(key);
3871 PSA_DONE();
3872 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003873}
3874/* END_CASE */
3875
3876/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003877void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003878{
Jaeden Ameroab439972019-02-15 14:12:05 +00003879 const uint8_t input[1] = { 0 };
3880 unsigned char output[1] = { 0 };
3881 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003882 /* Test each valid way of initializing the object, except for `= {0}`, as
3883 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3884 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003885 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003887 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3888 psa_cipher_operation_t zero;
3889
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003891
Jaeden Ameroab439972019-02-15 14:12:05 +00003892 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 TEST_EQUAL(psa_cipher_update(&func,
3894 input, sizeof(input),
3895 output, sizeof(output),
3896 &output_length),
3897 PSA_ERROR_BAD_STATE);
3898 TEST_EQUAL(psa_cipher_update(&init,
3899 input, sizeof(input),
3900 output, sizeof(output),
3901 &output_length),
3902 PSA_ERROR_BAD_STATE);
3903 TEST_EQUAL(psa_cipher_update(&zero,
3904 input, sizeof(input),
3905 output, sizeof(output),
3906 &output_length),
3907 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003908
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003909 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 PSA_ASSERT(psa_cipher_abort(&func));
3911 PSA_ASSERT(psa_cipher_abort(&init));
3912 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003913}
3914/* END_CASE */
3915
3916/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003917void cipher_setup(int key_type_arg,
3918 data_t *key,
3919 int alg_arg,
3920 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003921{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003922 psa_key_type_t key_type = key_type_arg;
3923 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003924 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003925 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003926 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003927#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003928 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3929#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003930
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3934 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003935 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003936 }
3937 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003938
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003939 /* The operation object should be reusable. */
3940#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3942 smoke_test_key_data,
3943 sizeof(smoke_test_key_data),
3944 KNOWN_SUPPORTED_CIPHER_ALG,
3945 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003946 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 }
3948 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003949#endif
3950
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003951exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 psa_cipher_abort(&operation);
3953 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003954}
3955/* END_CASE */
3956
Ronald Cronee414c72021-03-18 18:50:08 +01003957/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003958void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003959{
Ronald Cron5425a212020-08-04 14:58:35 +02003960 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003961 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3962 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003964 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003965 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003966 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003967 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 0xaa, 0xaa, 0xaa, 0xaa
3969 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003970 const uint8_t text[] = {
3971 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 0xbb, 0xbb, 0xbb, 0xbb
3973 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003974 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003975 size_t length = 0;
3976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 PSA_ASSERT(psa_crypto_init());
3978 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3979 psa_set_key_algorithm(&attributes, alg);
3980 psa_set_key_type(&attributes, key_type);
3981 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3982 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003983
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003984 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3986 ASSERT_OPERATION_IS_ACTIVE(operation);
3987 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3988 PSA_ERROR_BAD_STATE);
3989 ASSERT_OPERATION_IS_INACTIVE(operation);
3990 PSA_ASSERT(psa_cipher_abort(&operation));
3991 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003992
3993 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3995 ASSERT_OPERATION_IS_ACTIVE(operation);
3996 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3997 PSA_ERROR_BAD_STATE);
3998 ASSERT_OPERATION_IS_INACTIVE(operation);
3999 PSA_ASSERT(psa_cipher_abort(&operation));
4000 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004001
Jaeden Ameroab439972019-02-15 14:12:05 +00004002 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 TEST_EQUAL(psa_cipher_generate_iv(&operation,
4004 buffer, sizeof(buffer),
4005 &length),
4006 PSA_ERROR_BAD_STATE);
4007 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004008
4009 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4011 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4012 buffer, sizeof(buffer),
4013 &length));
4014 ASSERT_OPERATION_IS_ACTIVE(operation);
4015 TEST_EQUAL(psa_cipher_generate_iv(&operation,
4016 buffer, sizeof(buffer),
4017 &length),
4018 PSA_ERROR_BAD_STATE);
4019 ASSERT_OPERATION_IS_INACTIVE(operation);
4020 PSA_ASSERT(psa_cipher_abort(&operation));
4021 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004022
4023 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4025 PSA_ASSERT(psa_cipher_set_iv(&operation,
4026 iv, sizeof(iv)));
4027 TEST_EQUAL(psa_cipher_generate_iv(&operation,
4028 buffer, sizeof(buffer),
4029 &length),
4030 PSA_ERROR_BAD_STATE);
4031 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004032
4033 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 TEST_EQUAL(psa_cipher_set_iv(&operation,
4035 iv, sizeof(iv)),
4036 PSA_ERROR_BAD_STATE);
4037 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004038
4039 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4041 PSA_ASSERT(psa_cipher_set_iv(&operation,
4042 iv, sizeof(iv)));
4043 ASSERT_OPERATION_IS_ACTIVE(operation);
4044 TEST_EQUAL(psa_cipher_set_iv(&operation,
4045 iv, sizeof(iv)),
4046 PSA_ERROR_BAD_STATE);
4047 ASSERT_OPERATION_IS_INACTIVE(operation);
4048 PSA_ASSERT(psa_cipher_abort(&operation));
4049 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004050
4051 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4053 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4054 buffer, sizeof(buffer),
4055 &length));
4056 TEST_EQUAL(psa_cipher_set_iv(&operation,
4057 iv, sizeof(iv)),
4058 PSA_ERROR_BAD_STATE);
4059 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004060
4061 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 TEST_EQUAL(psa_cipher_update(&operation,
4063 text, sizeof(text),
4064 buffer, sizeof(buffer),
4065 &length),
4066 PSA_ERROR_BAD_STATE);
4067 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004068
4069 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4071 ASSERT_OPERATION_IS_ACTIVE(operation);
4072 TEST_EQUAL(psa_cipher_update(&operation,
4073 text, sizeof(text),
4074 buffer, sizeof(buffer),
4075 &length),
4076 PSA_ERROR_BAD_STATE);
4077 ASSERT_OPERATION_IS_INACTIVE(operation);
4078 PSA_ASSERT(psa_cipher_abort(&operation));
4079 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004080
4081 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4083 PSA_ASSERT(psa_cipher_set_iv(&operation,
4084 iv, sizeof(iv)));
4085 PSA_ASSERT(psa_cipher_finish(&operation,
4086 buffer, sizeof(buffer), &length));
4087 TEST_EQUAL(psa_cipher_update(&operation,
4088 text, sizeof(text),
4089 buffer, sizeof(buffer),
4090 &length),
4091 PSA_ERROR_BAD_STATE);
4092 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004093
4094 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 TEST_EQUAL(psa_cipher_finish(&operation,
4096 buffer, sizeof(buffer), &length),
4097 PSA_ERROR_BAD_STATE);
4098 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004099
4100 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00004102 /* Not calling update means we are encrypting an empty buffer, which is OK
4103 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 ASSERT_OPERATION_IS_ACTIVE(operation);
4105 TEST_EQUAL(psa_cipher_finish(&operation,
4106 buffer, sizeof(buffer), &length),
4107 PSA_ERROR_BAD_STATE);
4108 ASSERT_OPERATION_IS_INACTIVE(operation);
4109 PSA_ASSERT(psa_cipher_abort(&operation));
4110 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00004111
4112 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4114 PSA_ASSERT(psa_cipher_set_iv(&operation,
4115 iv, sizeof(iv)));
4116 PSA_ASSERT(psa_cipher_finish(&operation,
4117 buffer, sizeof(buffer), &length));
4118 TEST_EQUAL(psa_cipher_finish(&operation,
4119 buffer, sizeof(buffer), &length),
4120 PSA_ERROR_BAD_STATE);
4121 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00004122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02004124
Jaeden Ameroab439972019-02-15 14:12:05 +00004125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 psa_cipher_abort(&operation);
4127 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004128}
4129/* END_CASE */
4130
4131/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004132void cipher_encrypt_fail(int alg_arg,
4133 int key_type_arg,
4134 data_t *key_data,
4135 data_t *input,
4136 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004137{
Ronald Cron5425a212020-08-04 14:58:35 +02004138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004139 psa_status_t status;
4140 psa_key_type_t key_type = key_type_arg;
4141 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02004142 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004144 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
4145 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004146 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004147 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004148 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004149 size_t function_output_length;
4150 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 if (PSA_ERROR_BAD_STATE != expected_status) {
4154 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4157 psa_set_key_algorithm(&attributes, alg);
4158 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4161 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004162 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4165 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004166 }
4167
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004168 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
4170 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004173
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004174 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 status = psa_cipher_encrypt_setup(&operation, key, alg);
4176 if (status == PSA_SUCCESS) {
4177 if (alg != PSA_ALG_ECB_NO_PADDING) {
4178 PSA_ASSERT(psa_cipher_generate_iv(&operation,
4179 iv, iv_size,
4180 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004181 }
4182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 status = psa_cipher_update(&operation, input->x, input->len,
4184 output, output_buffer_size,
4185 &function_output_length);
4186 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004187 output_length += function_output_length;
4188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 status = psa_cipher_finish(&operation, output + output_length,
4190 output_buffer_size - output_length,
4191 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 TEST_EQUAL(status, expected_status);
4194 } else {
4195 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004196 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 } else {
4198 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01004199 }
4200
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 psa_cipher_abort(&operation);
4203 mbedtls_free(output);
4204 psa_destroy_key(key);
4205 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004206}
4207/* END_CASE */
4208
4209/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004210void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
4211 data_t *input, int iv_length,
4212 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02004213{
4214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4215 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4217 size_t output_buffer_size = 0;
4218 unsigned char *output = NULL;
4219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004221 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4226 psa_set_key_algorithm(&attributes, alg);
4227 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4230 &key));
4231 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4232 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
4233 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02004234
4235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 psa_cipher_abort(&operation);
4237 mbedtls_free(output);
4238 psa_destroy_key(key);
4239 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02004240}
4241/* END_CASE */
4242
4243/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004244void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
4245 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004246{
4247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4248 psa_key_type_t key_type = key_type_arg;
4249 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004250 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4251 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004252 unsigned char *output = NULL;
4253 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02004254 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004258
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004259 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 TEST_LE_U(ciphertext->len,
4261 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
4262 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
4263 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
4264 TEST_LE_U(plaintext->len,
4265 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
4266 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
4267 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004268
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004269
4270 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 psa_set_key_usage_flags(&attributes,
4272 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4273 psa_set_key_algorithm(&attributes, alg);
4274 psa_set_key_type(&attributes, key_type);
4275 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4276 &key));
4277 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4278 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004279 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004280
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004281 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4283 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4284 PSA_ERROR_BAD_STATE);
4285 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4286 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4287 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004288
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004289 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4291 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4292 &length),
4293 PSA_ERROR_BAD_STATE);
4294 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4295 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4296 &length),
4297 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004298
Gilles Peskine286c3142022-04-20 17:09:38 +02004299 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004301 output_length = 0;
4302 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 PSA_ASSERT(psa_cipher_update(&operation,
4304 plaintext->x, plaintext->len,
4305 output, output_buffer_size,
4306 &length));
4307 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004308 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 PSA_ASSERT(psa_cipher_finish(&operation,
4310 mbedtls_buffer_offset(output, output_length),
4311 output_buffer_size - output_length,
4312 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004313 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004314 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004315 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004316
Gilles Peskine286c3142022-04-20 17:09:38 +02004317 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004319 output_length = 0;
4320 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 PSA_ASSERT(psa_cipher_update(&operation,
4322 ciphertext->x, ciphertext->len,
4323 output, output_buffer_size,
4324 &length));
4325 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004326 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 PSA_ASSERT(psa_cipher_finish(&operation,
4328 mbedtls_buffer_offset(output, output_length),
4329 output_buffer_size - output_length,
4330 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004331 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004332 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004333 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004334
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004335 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004336 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4338 output, output_buffer_size,
4339 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004340 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004341 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004342
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004343 /* One-shot decryption */
4344 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4346 output, output_buffer_size,
4347 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004348 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004349 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004350
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 PSA_ASSERT(psa_cipher_abort(&operation));
4353 mbedtls_free(output);
4354 psa_cipher_abort(&operation);
4355 psa_destroy_key(key);
4356 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357}
4358/* END_CASE */
4359
4360/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004361void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004362{
4363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4364 psa_algorithm_t alg = alg_arg;
4365 psa_key_type_t key_type = key_type_arg;
4366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4367 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4368 psa_status_t status;
4369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4373 psa_set_key_algorithm(&attributes, alg);
4374 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004375
4376 /* Usage of either of these two size macros would cause divide by zero
4377 * with incorrect key types previously. Input length should be irrelevant
4378 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4380 0);
4381 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004382
4383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4385 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004386
4387 /* Should fail due to invalid alg type (to support invalid key type).
4388 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004392
4393exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 psa_cipher_abort(&operation);
4395 psa_destroy_key(key);
4396 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004397}
4398/* END_CASE */
4399
4400/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004401void cipher_encrypt_validation(int alg_arg,
4402 int key_type_arg,
4403 data_t *key_data,
4404 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004405{
4406 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4407 psa_key_type_t key_type = key_type_arg;
4408 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004410 unsigned char *output1 = NULL;
4411 size_t output1_buffer_size = 0;
4412 size_t output1_length = 0;
4413 unsigned char *output2 = NULL;
4414 size_t output2_buffer_size = 0;
4415 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004416 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004417 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4423 psa_set_key_algorithm(&attributes, alg);
4424 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004425
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4427 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4428 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004429 TEST_CALLOC(output1, output1_buffer_size);
4430 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004431
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4433 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004434
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004435 /* The one-shot cipher encryption uses generated iv so validating
4436 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4438 output1_buffer_size, &output1_length));
4439 TEST_LE_U(output1_length,
4440 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4441 TEST_LE_U(output1_length,
4442 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4445 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 PSA_ASSERT(psa_cipher_update(&operation,
4448 input->x, input->len,
4449 output2, output2_buffer_size,
4450 &function_output_length));
4451 TEST_LE_U(function_output_length,
4452 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4453 TEST_LE_U(function_output_length,
4454 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004455 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 PSA_ASSERT(psa_cipher_finish(&operation,
4458 output2 + output2_length,
4459 output2_buffer_size - output2_length,
4460 &function_output_length));
4461 TEST_LE_U(function_output_length,
4462 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4463 TEST_LE_U(function_output_length,
4464 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004465 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004468 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004469 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004470
Gilles Peskine50e586b2018-06-08 14:28:46 +02004471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 psa_cipher_abort(&operation);
4473 mbedtls_free(output1);
4474 mbedtls_free(output2);
4475 psa_destroy_key(key);
4476 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004477}
4478/* END_CASE */
4479
4480/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004481void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4482 data_t *key_data, data_t *iv,
4483 data_t *input,
4484 int first_part_size_arg,
4485 int output1_length_arg, int output2_length_arg,
4486 data_t *expected_output,
4487 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004488{
Ronald Cron5425a212020-08-04 14:58:35 +02004489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004490 psa_key_type_t key_type = key_type_arg;
4491 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004492 psa_status_t status;
4493 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004494 size_t first_part_size = first_part_size_arg;
4495 size_t output1_length = output1_length_arg;
4496 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004497 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004498 size_t output_buffer_size = 0;
4499 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004500 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004501 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4507 psa_set_key_algorithm(&attributes, alg);
4508 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4511 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 if (iv->len > 0) {
4516 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004517 }
4518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4520 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004521 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 TEST_LE_U(first_part_size, input->len);
4524 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4525 output, output_buffer_size,
4526 &function_output_length));
4527 TEST_ASSERT(function_output_length == output1_length);
4528 TEST_LE_U(function_output_length,
4529 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4530 TEST_LE_U(function_output_length,
4531 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004532 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (first_part_size < input->len) {
4535 PSA_ASSERT(psa_cipher_update(&operation,
4536 input->x + first_part_size,
4537 input->len - first_part_size,
4538 (output_buffer_size == 0 ? NULL :
4539 output + total_output_length),
4540 output_buffer_size - total_output_length,
4541 &function_output_length));
4542 TEST_ASSERT(function_output_length == output2_length);
4543 TEST_LE_U(function_output_length,
4544 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4545 alg,
4546 input->len - first_part_size));
4547 TEST_LE_U(function_output_length,
4548 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004549 total_output_length += function_output_length;
4550 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 status = psa_cipher_finish(&operation,
4553 (output_buffer_size == 0 ? NULL :
4554 output + total_output_length),
4555 output_buffer_size - total_output_length,
4556 &function_output_length);
4557 TEST_LE_U(function_output_length,
4558 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4559 TEST_LE_U(function_output_length,
4560 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004561 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004563
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 if (expected_status == PSA_SUCCESS) {
4565 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004566
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004567 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004568 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004569 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004570
4571exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 psa_cipher_abort(&operation);
4573 mbedtls_free(output);
4574 psa_destroy_key(key);
4575 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004576}
4577/* END_CASE */
4578
4579/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004580void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4581 data_t *key_data, data_t *iv,
4582 data_t *input,
4583 int first_part_size_arg,
4584 int output1_length_arg, int output2_length_arg,
4585 data_t *expected_output,
4586 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004587{
Ronald Cron5425a212020-08-04 14:58:35 +02004588 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004589 psa_key_type_t key_type = key_type_arg;
4590 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004591 psa_status_t status;
4592 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004593 size_t first_part_size = first_part_size_arg;
4594 size_t output1_length = output1_length_arg;
4595 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004596 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004597 size_t output_buffer_size = 0;
4598 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004599 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004600 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4606 psa_set_key_algorithm(&attributes, alg);
4607 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4610 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004613
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 if (iv->len > 0) {
4615 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004616 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4619 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004620 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 TEST_LE_U(first_part_size, input->len);
4623 PSA_ASSERT(psa_cipher_update(&operation,
4624 input->x, first_part_size,
4625 output, output_buffer_size,
4626 &function_output_length));
4627 TEST_ASSERT(function_output_length == output1_length);
4628 TEST_LE_U(function_output_length,
4629 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4630 TEST_LE_U(function_output_length,
4631 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004632 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if (first_part_size < input->len) {
4635 PSA_ASSERT(psa_cipher_update(&operation,
4636 input->x + first_part_size,
4637 input->len - first_part_size,
4638 (output_buffer_size == 0 ? NULL :
4639 output + total_output_length),
4640 output_buffer_size - total_output_length,
4641 &function_output_length));
4642 TEST_ASSERT(function_output_length == output2_length);
4643 TEST_LE_U(function_output_length,
4644 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4645 alg,
4646 input->len - first_part_size));
4647 TEST_LE_U(function_output_length,
4648 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004649 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004650 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 status = psa_cipher_finish(&operation,
4653 (output_buffer_size == 0 ? NULL :
4654 output + total_output_length),
4655 output_buffer_size - total_output_length,
4656 &function_output_length);
4657 TEST_LE_U(function_output_length,
4658 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4659 TEST_LE_U(function_output_length,
4660 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004661 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (expected_status == PSA_SUCCESS) {
4665 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004666
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004667 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004668 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004669 }
4670
Gilles Peskine50e586b2018-06-08 14:28:46 +02004671exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 psa_cipher_abort(&operation);
4673 mbedtls_free(output);
4674 psa_destroy_key(key);
4675 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004676}
4677/* END_CASE */
4678
Gilles Peskine50e586b2018-06-08 14:28:46 +02004679/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004680void cipher_decrypt_fail(int alg_arg,
4681 int key_type_arg,
4682 data_t *key_data,
4683 data_t *iv,
4684 data_t *input_arg,
4685 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004686{
4687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4688 psa_status_t status;
4689 psa_key_type_t key_type = key_type_arg;
4690 psa_algorithm_t alg = alg_arg;
4691 psa_status_t expected_status = expected_status_arg;
4692 unsigned char *input = NULL;
4693 size_t input_buffer_size = 0;
4694 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004695 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004696 size_t output_buffer_size = 0;
4697 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004698 size_t function_output_length;
4699 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (PSA_ERROR_BAD_STATE != expected_status) {
4703 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4706 psa_set_key_algorithm(&attributes, alg);
4707 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4710 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004711 }
4712
4713 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4715 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004716 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 memcpy(input, iv->x, iv->len);
4718 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004722 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004723
Neil Armstrong66a479f2022-02-07 15:41:19 +01004724 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4726 output_buffer_size, &output_length);
4727 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004728
Neil Armstrong66a479f2022-02-07 15:41:19 +01004729 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 status = psa_cipher_decrypt_setup(&operation, key, alg);
4731 if (status == PSA_SUCCESS) {
4732 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4733 input_arg->len) +
4734 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004735 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004736
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 if (iv->len > 0) {
4738 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (status != PSA_SUCCESS) {
4741 TEST_EQUAL(status, expected_status);
4742 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004743 }
4744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if (status == PSA_SUCCESS) {
4746 status = psa_cipher_update(&operation,
4747 input_arg->x, input_arg->len,
4748 output_multi, output_buffer_size,
4749 &function_output_length);
4750 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004751 output_length = function_output_length;
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 status = psa_cipher_finish(&operation,
4754 output_multi + output_length,
4755 output_buffer_size - output_length,
4756 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 TEST_EQUAL(status, expected_status);
4759 } else {
4760 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004761 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 } else {
4763 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 } else {
4766 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004767 }
4768
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004769exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 psa_cipher_abort(&operation);
4771 mbedtls_free(input);
4772 mbedtls_free(output);
4773 mbedtls_free(output_multi);
4774 psa_destroy_key(key);
4775 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004776}
4777/* END_CASE */
4778
4779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780void cipher_decrypt(int alg_arg,
4781 int key_type_arg,
4782 data_t *key_data,
4783 data_t *iv,
4784 data_t *input_arg,
4785 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004786{
4787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4788 psa_key_type_t key_type = key_type_arg;
4789 psa_algorithm_t alg = alg_arg;
4790 unsigned char *input = NULL;
4791 size_t input_buffer_size = 0;
4792 unsigned char *output = NULL;
4793 size_t output_buffer_size = 0;
4794 size_t output_length = 0;
4795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4800 psa_set_key_algorithm(&attributes, alg);
4801 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004802
4803 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4805 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004806 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 memcpy(input, iv->x, iv->len);
4808 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004809 }
4810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004812 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4815 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4818 output_buffer_size, &output_length));
4819 TEST_LE_U(output_length,
4820 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4821 TEST_LE_U(output_length,
4822 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004823
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004824 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004825 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004826exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 mbedtls_free(input);
4828 mbedtls_free(output);
4829 psa_destroy_key(key);
4830 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004831}
4832/* END_CASE */
4833
4834/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004835void cipher_verify_output(int alg_arg,
4836 int key_type_arg,
4837 data_t *key_data,
4838 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004839{
Ronald Cron5425a212020-08-04 14:58:35 +02004840 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004841 psa_key_type_t key_type = key_type_arg;
4842 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004843 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004844 size_t output1_size = 0;
4845 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004846 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004847 size_t output2_size = 0;
4848 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004849 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4854 psa_set_key_algorithm(&attributes, alg);
4855 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4858 &key));
4859 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004860 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4863 output1, output1_size,
4864 &output1_length));
4865 TEST_LE_U(output1_length,
4866 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4867 TEST_LE_U(output1_length,
4868 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004869
4870 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004871 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4874 output2, output2_size,
4875 &output2_length));
4876 TEST_LE_U(output2_length,
4877 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4878 TEST_LE_U(output2_length,
4879 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004880
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004881 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004882
4883exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 mbedtls_free(output1);
4885 mbedtls_free(output2);
4886 psa_destroy_key(key);
4887 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004888}
4889/* END_CASE */
4890
4891/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004892void cipher_verify_output_multipart(int alg_arg,
4893 int key_type_arg,
4894 data_t *key_data,
4895 data_t *input,
4896 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004897{
Ronald Cron5425a212020-08-04 14:58:35 +02004898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004899 psa_key_type_t key_type = key_type_arg;
4900 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004901 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004903 size_t iv_size = 16;
4904 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004905 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004906 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004907 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004908 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004909 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004910 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004911 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004912 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4913 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4919 psa_set_key_algorithm(&attributes, alg);
4920 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4923 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4926 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 if (alg != PSA_ALG_ECB_NO_PADDING) {
4929 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4930 iv, iv_size,
4931 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004932 }
4933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4935 TEST_LE_U(output1_buffer_size,
4936 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004937 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004938
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004940
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4942 output1, output1_buffer_size,
4943 &function_output_length));
4944 TEST_LE_U(function_output_length,
4945 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4946 TEST_LE_U(function_output_length,
4947 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004948 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 PSA_ASSERT(psa_cipher_update(&operation1,
4951 input->x + first_part_size,
4952 input->len - first_part_size,
4953 output1, output1_buffer_size,
4954 &function_output_length));
4955 TEST_LE_U(function_output_length,
4956 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4957 alg,
4958 input->len - first_part_size));
4959 TEST_LE_U(function_output_length,
4960 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004961 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 PSA_ASSERT(psa_cipher_finish(&operation1,
4964 output1 + output1_length,
4965 output1_buffer_size - output1_length,
4966 &function_output_length));
4967 TEST_LE_U(function_output_length,
4968 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4969 TEST_LE_U(function_output_length,
4970 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004971 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004974
Gilles Peskine048b7f02018-06-08 14:20:49 +02004975 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 TEST_LE_U(output2_buffer_size,
4977 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4978 TEST_LE_U(output2_buffer_size,
4979 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004980 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 if (iv_length > 0) {
4983 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4984 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004985 }
Moran Pekerded84402018-06-06 16:36:50 +03004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4988 output2, output2_buffer_size,
4989 &function_output_length));
4990 TEST_LE_U(function_output_length,
4991 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4992 TEST_LE_U(function_output_length,
4993 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004994 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 PSA_ASSERT(psa_cipher_update(&operation2,
4997 output1 + first_part_size,
4998 output1_length - first_part_size,
4999 output2, output2_buffer_size,
5000 &function_output_length));
5001 TEST_LE_U(function_output_length,
5002 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
5003 alg,
5004 output1_length - first_part_size));
5005 TEST_LE_U(function_output_length,
5006 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02005007 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03005008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 PSA_ASSERT(psa_cipher_finish(&operation2,
5010 output2 + output2_length,
5011 output2_buffer_size - output2_length,
5012 &function_output_length));
5013 TEST_LE_U(function_output_length,
5014 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
5015 TEST_LE_U(function_output_length,
5016 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02005017 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02005020
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005021 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02005022
5023exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 psa_cipher_abort(&operation1);
5025 psa_cipher_abort(&operation2);
5026 mbedtls_free(output1);
5027 mbedtls_free(output2);
5028 psa_destroy_key(key);
5029 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02005030}
5031/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02005032
Gilles Peskine20035e32018-02-03 22:44:14 +01005033/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005034void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
5035 int alg_arg,
5036 data_t *nonce,
5037 data_t *additional_data,
5038 data_t *input_data,
5039 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005040{
Ronald Cron5425a212020-08-04 14:58:35 +02005041 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005042 psa_key_type_t key_type = key_type_arg;
5043 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005044 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005045 unsigned char *output_data = NULL;
5046 size_t output_size = 0;
5047 size_t output_length = 0;
5048 unsigned char *output_data2 = NULL;
5049 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01005050 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02005051 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005053
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5057 psa_set_key_algorithm(&attributes, alg);
5058 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5061 &key));
5062 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5063 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5066 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005067 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
5068 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
5070 expected_result != PSA_ERROR_NOT_SUPPORTED) {
5071 TEST_EQUAL(output_size,
5072 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5073 TEST_LE_U(output_size,
5074 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01005075 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005076 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 status = psa_aead_encrypt(key, alg,
5079 nonce->x, nonce->len,
5080 additional_data->x,
5081 additional_data->len,
5082 input_data->x, input_data->len,
5083 output_data, output_size,
5084 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01005085
5086 /* If the operation is not supported, just skip and not fail in case the
5087 * encryption involves a common limitation of cryptography hardwares and
5088 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 if (status == PSA_ERROR_NOT_SUPPORTED) {
5090 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5091 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01005092 }
5093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005097 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005098
Gilles Peskine003a4a92019-05-14 16:09:40 +02005099 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
5100 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 TEST_EQUAL(input_data->len,
5102 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02005103
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 TEST_LE_U(input_data->len,
5105 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 TEST_EQUAL(psa_aead_decrypt(key, alg,
5108 nonce->x, nonce->len,
5109 additional_data->x,
5110 additional_data->len,
5111 output_data, output_length,
5112 output_data2, output_length,
5113 &output_length2),
5114 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02005115
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005116 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005117 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005118 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005119
Gilles Peskinea1cac842018-06-11 19:33:02 +02005120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 psa_destroy_key(key);
5122 mbedtls_free(output_data);
5123 mbedtls_free(output_data2);
5124 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005125}
5126/* END_CASE */
5127
5128/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005129void aead_encrypt(int key_type_arg, data_t *key_data,
5130 int alg_arg,
5131 data_t *nonce,
5132 data_t *additional_data,
5133 data_t *input_data,
5134 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005135{
Ronald Cron5425a212020-08-04 14:58:35 +02005136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005137 psa_key_type_t key_type = key_type_arg;
5138 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005139 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005140 unsigned char *output_data = NULL;
5141 size_t output_size = 0;
5142 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01005144 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5149 psa_set_key_algorithm(&attributes, alg);
5150 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5153 &key));
5154 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5155 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5158 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005159 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
5160 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 TEST_EQUAL(output_size,
5162 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5163 TEST_LE_U(output_size,
5164 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005165 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 status = psa_aead_encrypt(key, alg,
5168 nonce->x, nonce->len,
5169 additional_data->x, additional_data->len,
5170 input_data->x, input_data->len,
5171 output_data, output_size,
5172 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005173
Ronald Cron28a45ed2021-02-09 20:35:42 +01005174 /* If the operation is not supported, just skip and not fail in case the
5175 * encryption involves a common limitation of cryptography hardwares and
5176 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (status == PSA_ERROR_NOT_SUPPORTED) {
5178 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5179 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01005180 }
Steven Cooremand588ea12021-01-11 19:36:04 +01005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005183 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005184 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02005185
Gilles Peskinea1cac842018-06-11 19:33:02 +02005186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 psa_destroy_key(key);
5188 mbedtls_free(output_data);
5189 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005190}
5191/* END_CASE */
5192
5193/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005194void aead_decrypt(int key_type_arg, data_t *key_data,
5195 int alg_arg,
5196 data_t *nonce,
5197 data_t *additional_data,
5198 data_t *input_data,
5199 data_t *expected_data,
5200 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02005201{
Ronald Cron5425a212020-08-04 14:58:35 +02005202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005203 psa_key_type_t key_type = key_type_arg;
5204 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01005205 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005206 unsigned char *output_data = NULL;
5207 size_t output_size = 0;
5208 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02005210 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01005211 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02005212
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02005214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5216 psa_set_key_algorithm(&attributes, alg);
5217 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005218
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5220 &key));
5221 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5222 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01005223
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
5225 alg);
5226 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
5227 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01005228 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
5229 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 TEST_EQUAL(output_size,
5231 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
5232 TEST_LE_U(output_size,
5233 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01005234 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005235 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 status = psa_aead_decrypt(key, alg,
5238 nonce->x, nonce->len,
5239 additional_data->x,
5240 additional_data->len,
5241 input_data->x, input_data->len,
5242 output_data, output_size,
5243 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01005244
Ronald Cron28a45ed2021-02-09 20:35:42 +01005245 /* If the operation is not supported, just skip and not fail in case the
5246 * decryption involves a common limitation of cryptography hardwares and
5247 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 if (status == PSA_ERROR_NOT_SUPPORTED) {
5249 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5250 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01005251 }
Steven Cooremand588ea12021-01-11 19:36:04 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005256 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005257 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02005259
Gilles Peskinea1cac842018-06-11 19:33:02 +02005260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 psa_destroy_key(key);
5262 mbedtls_free(output_data);
5263 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005264}
5265/* END_CASE */
5266
5267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005268void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
5269 int alg_arg,
5270 data_t *nonce,
5271 data_t *additional_data,
5272 data_t *input_data,
5273 int do_set_lengths,
5274 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005275{
Paul Elliottd3f82412021-06-16 16:52:21 +01005276 size_t ad_part_len = 0;
5277 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005278 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005279
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
5281 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005282
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (do_set_lengths) {
5284 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005285 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005287 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005289 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005290
5291 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (!aead_multipart_internal_func(key_type_arg, key_data,
5293 alg_arg, nonce,
5294 additional_data,
5295 ad_part_len,
5296 input_data, -1,
5297 set_lengths_method,
5298 expected_output,
5299 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005300 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005301 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 /* length(0) part, length(ad_part_len) part, length(0) part... */
5304 mbedtls_test_set_step(1000 + ad_part_len);
5305
5306 if (!aead_multipart_internal_func(key_type_arg, key_data,
5307 alg_arg, nonce,
5308 additional_data,
5309 ad_part_len,
5310 input_data, -1,
5311 set_lengths_method,
5312 expected_output,
5313 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005314 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 }
5316 }
5317
5318 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5319 /* Split data into length(data_part_len) parts. */
5320 mbedtls_test_set_step(2000 + data_part_len);
5321
5322 if (do_set_lengths) {
5323 if (data_part_len & 0x01) {
5324 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5325 } else {
5326 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5327 }
5328 }
5329
5330 if (!aead_multipart_internal_func(key_type_arg, key_data,
5331 alg_arg, nonce,
5332 additional_data, -1,
5333 input_data, data_part_len,
5334 set_lengths_method,
5335 expected_output,
5336 1, 0)) {
5337 break;
5338 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005339
5340 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005342
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 if (!aead_multipart_internal_func(key_type_arg, key_data,
5344 alg_arg, nonce,
5345 additional_data, -1,
5346 input_data, data_part_len,
5347 set_lengths_method,
5348 expected_output,
5349 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005350 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005352 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005353
Paul Elliott8fc45162021-06-23 16:06:01 +01005354 /* Goto is required to silence warnings about unused labels, as we
5355 * don't actually do any test assertions in this function. */
5356 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005357}
5358/* END_CASE */
5359
5360/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005361void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5362 int alg_arg,
5363 data_t *nonce,
5364 data_t *additional_data,
5365 data_t *input_data,
5366 int do_set_lengths,
5367 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005368{
Paul Elliottd3f82412021-06-16 16:52:21 +01005369 size_t ad_part_len = 0;
5370 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005371 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005374 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if (do_set_lengths) {
5378 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005379 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005381 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005383 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 if (!aead_multipart_internal_func(key_type_arg, key_data,
5386 alg_arg, nonce,
5387 additional_data,
5388 ad_part_len,
5389 input_data, -1,
5390 set_lengths_method,
5391 expected_output,
5392 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005393 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005395
5396 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005398
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 if (!aead_multipart_internal_func(key_type_arg, key_data,
5400 alg_arg, nonce,
5401 additional_data,
5402 ad_part_len,
5403 input_data, -1,
5404 set_lengths_method,
5405 expected_output,
5406 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005407 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005409 }
5410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005412 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (do_set_lengths) {
5416 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005417 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005419 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005421 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 if (!aead_multipart_internal_func(key_type_arg, key_data,
5424 alg_arg, nonce,
5425 additional_data, -1,
5426 input_data, data_part_len,
5427 set_lengths_method,
5428 expected_output,
5429 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005430 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005432
5433 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 if (!aead_multipart_internal_func(key_type_arg, key_data,
5437 alg_arg, nonce,
5438 additional_data, -1,
5439 input_data, data_part_len,
5440 set_lengths_method,
5441 expected_output,
5442 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005443 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005445 }
5446
Paul Elliott8fc45162021-06-23 16:06:01 +01005447 /* Goto is required to silence warnings about unused labels, as we
5448 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005449 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005450}
5451/* END_CASE */
5452
5453/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005454void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5455 int alg_arg,
5456 int nonce_length,
5457 int expected_nonce_length_arg,
5458 data_t *additional_data,
5459 data_t *input_data,
5460 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005461{
5462
5463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5464 psa_key_type_t key_type = key_type_arg;
5465 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005466 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005467 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5469 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005470 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005471 size_t actual_nonce_length = 0;
5472 size_t expected_nonce_length = expected_nonce_length_arg;
5473 unsigned char *output = NULL;
5474 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005475 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005476 size_t ciphertext_size = 0;
5477 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005478 size_t tag_length = 0;
5479 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5484 psa_set_key_algorithm(&attributes, alg);
5485 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5488 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005489
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005491
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005493
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005494 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005495
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005497
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005499
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005500 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005501
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005503
5504 /* If the operation is not supported, just skip and not fail in case the
5505 * encryption involves a common limitation of cryptography hardwares and
5506 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (status == PSA_ERROR_NOT_SUPPORTED) {
5508 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5509 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005510 }
5511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005513
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5515 nonce_length,
5516 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 if (expected_status == PSA_SUCCESS) {
5523 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5524 alg));
5525 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005530 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5532 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5535 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5538 output, output_size,
5539 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5542 &ciphertext_length, tag_buffer,
5543 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005544 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005545
5546exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 psa_destroy_key(key);
5548 mbedtls_free(output);
5549 mbedtls_free(ciphertext);
5550 psa_aead_abort(&operation);
5551 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005552}
5553/* END_CASE */
5554
5555/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005556void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5557 int alg_arg,
5558 int nonce_length_arg,
5559 int set_lengths_method_arg,
5560 data_t *additional_data,
5561 data_t *input_data,
5562 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005563{
5564
5565 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5566 psa_key_type_t key_type = key_type_arg;
5567 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005568 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005569 uint8_t *nonce_buffer = NULL;
5570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5571 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5572 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005573 unsigned char *output = NULL;
5574 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005575 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005576 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005577 size_t ciphertext_size = 0;
5578 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005579 size_t tag_length = 0;
5580 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005581 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005582 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5587 psa_set_key_algorithm(&attributes, alg);
5588 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5591 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005596
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005597 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005600
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005602
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005603 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005606
5607 /* If the operation is not supported, just skip and not fail in case the
5608 * encryption involves a common limitation of cryptography hardwares and
5609 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 if (status == PSA_ERROR_NOT_SUPPORTED) {
5611 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5612 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005613 }
5614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005616
Paul Elliott4023ffd2021-09-10 16:21:22 +01005617 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 if (nonce_length_arg == -1) {
5619 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005620 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 nonce_length = 0;
5622 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005623 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005625 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 if (nonce_buffer) {
5628 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005629 nonce_buffer[index] = 'a' + index;
5630 }
Paul Elliott66696b52021-08-16 18:42:41 +01005631 }
Paul Elliott863864a2021-07-23 17:28:31 +01005632 }
5633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5635 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5636 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005637 }
5638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 if (expected_status == PSA_SUCCESS) {
5644 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5645 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5646 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005647 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005649 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 }
Paul Elliott863864a2021-07-23 17:28:31 +01005651
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005652 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5654 additional_data->len),
5655 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5658 output, output_size,
5659 &ciphertext_length),
5660 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5663 &ciphertext_length, tag_buffer,
5664 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5665 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005666 }
5667
5668exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 psa_destroy_key(key);
5670 mbedtls_free(output);
5671 mbedtls_free(ciphertext);
5672 mbedtls_free(nonce_buffer);
5673 psa_aead_abort(&operation);
5674 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005675}
5676/* END_CASE */
5677
5678/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005679void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005680 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005681 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005682 data_t *nonce,
5683 data_t *additional_data,
5684 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005686{
5687
5688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5689 psa_key_type_t key_type = key_type_arg;
5690 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005691 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5693 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5694 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005695 unsigned char *output = NULL;
5696 unsigned char *ciphertext = NULL;
5697 size_t output_size = output_size_arg;
5698 size_t ciphertext_size = 0;
5699 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005700 size_t tag_length = 0;
5701 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005704
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5706 psa_set_key_algorithm(&attributes, alg);
5707 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005708
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5710 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005711
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005713
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005714 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005717
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005718 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005721
5722 /* If the operation is not supported, just skip and not fail in case the
5723 * encryption involves a common limitation of cryptography hardwares and
5724 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 if (status == PSA_ERROR_NOT_SUPPORTED) {
5726 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5727 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005728 }
5729
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5733 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5738 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 status = psa_aead_update(&operation, input_data->x, input_data->len,
5741 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005746 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5748 &ciphertext_length, tag_buffer,
5749 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005750 }
5751
5752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 psa_destroy_key(key);
5754 mbedtls_free(output);
5755 mbedtls_free(ciphertext);
5756 psa_aead_abort(&operation);
5757 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005758}
5759/* END_CASE */
5760
Paul Elliott91b021e2021-07-23 18:52:31 +01005761/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005762void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5763 int alg_arg,
5764 int finish_ciphertext_size_arg,
5765 int tag_size_arg,
5766 data_t *nonce,
5767 data_t *additional_data,
5768 data_t *input_data,
5769 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005770{
5771
5772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5773 psa_key_type_t key_type = key_type_arg;
5774 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005775 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5777 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5778 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005779 unsigned char *ciphertext = NULL;
5780 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005781 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005782 size_t ciphertext_size = 0;
5783 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5785 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005786 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5791 psa_set_key_algorithm(&attributes, alg);
5792 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5795 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005800
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005801 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005802
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005803 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005804
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005805 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005808
5809 /* If the operation is not supported, just skip and not fail in case the
5810 * encryption involves a common limitation of cryptography hardwares and
5811 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 if (status == PSA_ERROR_NOT_SUPPORTED) {
5813 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5814 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005815 }
5816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5822 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5825 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5828 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005829
5830 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 status = psa_aead_finish(&operation, finish_ciphertext,
5832 finish_ciphertext_size,
5833 &ciphertext_length, tag_buffer,
5834 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005837
5838exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 psa_destroy_key(key);
5840 mbedtls_free(ciphertext);
5841 mbedtls_free(finish_ciphertext);
5842 mbedtls_free(tag_buffer);
5843 psa_aead_abort(&operation);
5844 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005845}
5846/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005847
5848/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005849void aead_multipart_verify(int key_type_arg, data_t *key_data,
5850 int alg_arg,
5851 data_t *nonce,
5852 data_t *additional_data,
5853 data_t *input_data,
5854 data_t *tag,
5855 int tag_usage_arg,
5856 int expected_setup_status_arg,
5857 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005858{
5859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5860 psa_key_type_t key_type = key_type_arg;
5861 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005862 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5864 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5865 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005866 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005867 unsigned char *plaintext = NULL;
5868 unsigned char *finish_plaintext = NULL;
5869 size_t plaintext_size = 0;
5870 size_t plaintext_length = 0;
5871 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005872 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005873 unsigned char *tag_buffer = NULL;
5874 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5879 psa_set_key_algorithm(&attributes, alg);
5880 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5883 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5888 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005889
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005890 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005893
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005894 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005897
5898 /* If the operation is not supported, just skip and not fail in case the
5899 * encryption involves a common limitation of cryptography hardwares and
5900 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if (status == PSA_ERROR_NOT_SUPPORTED) {
5902 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5903 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005904 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005908 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 }
Paul Elliott9961a662021-09-17 19:19:02 +01005910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 status = psa_aead_set_lengths(&operation, additional_data->len,
5916 input_data->len);
5917 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5920 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5923 input_data->len,
5924 plaintext, plaintext_size,
5925 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005928 tag_buffer = tag->x;
5929 tag_size = tag->len;
5930 }
5931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 status = psa_aead_verify(&operation, finish_plaintext,
5933 verify_plaintext_size,
5934 &plaintext_length,
5935 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005938
5939exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 psa_destroy_key(key);
5941 mbedtls_free(plaintext);
5942 mbedtls_free(finish_plaintext);
5943 psa_aead_abort(&operation);
5944 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005945}
5946/* END_CASE */
5947
Paul Elliott9961a662021-09-17 19:19:02 +01005948/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005949void aead_multipart_setup(int key_type_arg, data_t *key_data,
5950 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005951{
5952 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5953 psa_key_type_t key_type = key_type_arg;
5954 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005955 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005956 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5957 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5958 psa_status_t expected_status = expected_status_arg;
5959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 psa_set_key_usage_flags(&attributes,
5963 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5964 psa_set_key_algorithm(&attributes, alg);
5965 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5968 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005979
5980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 psa_destroy_key(key);
5982 psa_aead_abort(&operation);
5983 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005984}
5985/* END_CASE */
5986
5987/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005988void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5989 int alg_arg,
5990 data_t *nonce,
5991 data_t *additional_data,
5992 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005993{
5994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5995 psa_key_type_t key_type = key_type_arg;
5996 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005997 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005998 unsigned char *output_data = NULL;
5999 unsigned char *final_data = NULL;
6000 size_t output_size = 0;
6001 size_t finish_output_size = 0;
6002 size_t output_length = 0;
6003 size_t key_bits = 0;
6004 size_t tag_length = 0;
6005 size_t tag_size = 0;
6006 size_t nonce_length = 0;
6007 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
6008 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
6009 size_t output_part_length = 0;
6010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01006013
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 psa_set_key_usage_flags(&attributes,
6015 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
6016 psa_set_key_algorithm(&attributes, alg);
6017 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6020 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6023 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006030
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006031 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006034
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006036
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006037 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006038
6039 /* Test all operations error without calling setup first. */
6040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6042 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6047 PSA_AEAD_NONCE_MAX_SIZE,
6048 &nonce_length),
6049 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006052
Paul Elliott481be342021-07-16 17:38:47 +01006053 /* ------------------------------------------------------- */
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6056 input_data->len),
6057 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006060
Paul Elliott481be342021-07-16 17:38:47 +01006061 /* ------------------------------------------------------- */
6062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6064 additional_data->len),
6065 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006068
Paul Elliott481be342021-07-16 17:38:47 +01006069 /* ------------------------------------------------------- */
6070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6072 input_data->len, output_data,
6073 output_size, &output_length),
6074 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006077
Paul Elliott481be342021-07-16 17:38:47 +01006078 /* ------------------------------------------------------- */
6079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6081 finish_output_size,
6082 &output_part_length,
6083 tag_buffer, tag_length,
6084 &tag_size),
6085 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006088
Paul Elliott481be342021-07-16 17:38:47 +01006089 /* ------------------------------------------------------- */
6090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6092 finish_output_size,
6093 &output_part_length,
6094 tag_buffer,
6095 tag_length),
6096 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006099
6100 /* Test for double setups. */
6101
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
6105 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006108
Paul Elliott481be342021-07-16 17:38:47 +01006109 /* ------------------------------------------------------- */
6110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
6114 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006117
Paul Elliott374a2be2021-07-16 17:53:40 +01006118 /* ------------------------------------------------------- */
6119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
6123 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006126
6127 /* ------------------------------------------------------- */
6128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
6132 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006135
Paul Elliottc23a9a02021-06-21 18:32:46 +01006136 /* Test for not setting a nonce. */
6137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6141 additional_data->len),
6142 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006145
Paul Elliott7f628422021-09-01 12:08:29 +01006146 /* ------------------------------------------------------- */
6147
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6151 input_data->len, output_data,
6152 output_size, &output_length),
6153 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01006156
Paul Elliottbdc2c682021-09-21 18:37:10 +01006157 /* ------------------------------------------------------- */
6158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6162 finish_output_size,
6163 &output_part_length,
6164 tag_buffer, tag_length,
6165 &tag_size),
6166 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006169
6170 /* ------------------------------------------------------- */
6171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6175 finish_output_size,
6176 &output_part_length,
6177 tag_buffer,
6178 tag_length),
6179 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01006182
Paul Elliottc23a9a02021-06-21 18:32:46 +01006183 /* Test for double setting nonce. */
6184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006186
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6190 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006193
Paul Elliott374a2be2021-07-16 17:53:40 +01006194 /* Test for double generating nonce. */
6195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6199 PSA_AEAD_NONCE_MAX_SIZE,
6200 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6203 PSA_AEAD_NONCE_MAX_SIZE,
6204 &nonce_length),
6205 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006206
6207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006209
6210 /* Test for generate nonce then set and vice versa */
6211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6215 PSA_AEAD_NONCE_MAX_SIZE,
6216 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6219 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006222
Andrzej Kurekad837522021-12-15 15:28:49 +01006223 /* Test for generating nonce after calling set lengths */
6224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6228 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6231 PSA_AEAD_NONCE_MAX_SIZE,
6232 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006235
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006236 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 if (operation.alg == PSA_ALG_CCM) {
6241 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6242 input_data->len),
6243 PSA_ERROR_INVALID_ARGUMENT);
6244 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6245 PSA_AEAD_NONCE_MAX_SIZE,
6246 &nonce_length),
6247 PSA_ERROR_BAD_STATE);
6248 } else {
6249 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6250 input_data->len));
6251 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6252 PSA_AEAD_NONCE_MAX_SIZE,
6253 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006254 }
6255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006257
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006258 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00006259#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6263 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6264 input_data->len),
6265 PSA_ERROR_INVALID_ARGUMENT);
6266 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6267 PSA_AEAD_NONCE_MAX_SIZE,
6268 &nonce_length),
6269 PSA_ERROR_BAD_STATE);
6270 } else {
6271 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6272 input_data->len));
6273 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6274 PSA_AEAD_NONCE_MAX_SIZE,
6275 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006276 }
6277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00006279#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01006280
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006281 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6286 PSA_AEAD_NONCE_MAX_SIZE,
6287 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (operation.alg == PSA_ALG_CCM) {
6290 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6291 input_data->len),
6292 PSA_ERROR_INVALID_ARGUMENT);
6293 } else {
6294 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6295 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006296 }
6297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006299
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006300 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006301 /* Test for setting nonce after calling set lengths */
6302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6306 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006311
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006312 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 if (operation.alg == PSA_ALG_CCM) {
6317 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6318 input_data->len),
6319 PSA_ERROR_INVALID_ARGUMENT);
6320 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6321 PSA_ERROR_BAD_STATE);
6322 } else {
6323 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6324 input_data->len));
6325 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006326 }
6327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006329
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006330 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006331#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6335 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6336 input_data->len),
6337 PSA_ERROR_INVALID_ARGUMENT);
6338 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6339 PSA_ERROR_BAD_STATE);
6340 } else {
6341 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6342 input_data->len));
6343 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006344 }
6345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006347#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006348
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006349 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 if (operation.alg == PSA_ALG_CCM) {
6356 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6357 input_data->len),
6358 PSA_ERROR_INVALID_ARGUMENT);
6359 } else {
6360 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6361 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006362 }
6363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006365
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006366 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006367#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 if (operation.alg == PSA_ALG_GCM) {
6371 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6372 SIZE_MAX),
6373 PSA_ERROR_INVALID_ARGUMENT);
6374 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6375 PSA_ERROR_BAD_STATE);
6376 } else if (operation.alg != PSA_ALG_CCM) {
6377 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6378 SIZE_MAX));
6379 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006380 }
6381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006383#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006384
Tom Cosgrove1797b052022-12-04 17:19:59 +00006385 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006386#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 if (operation.alg == PSA_ALG_GCM) {
6392 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6393 SIZE_MAX),
6394 PSA_ERROR_INVALID_ARGUMENT);
6395 } else if (operation.alg != PSA_ALG_CCM) {
6396 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6397 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006398 }
6399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006401#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006402
6403 /* ------------------------------------------------------- */
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6410 PSA_AEAD_NONCE_MAX_SIZE,
6411 &nonce_length),
6412 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006415
Paul Elliott7220cae2021-06-22 17:25:57 +01006416 /* Test for generating nonce in decrypt setup. */
6417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6421 PSA_AEAD_NONCE_MAX_SIZE,
6422 &nonce_length),
6423 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006426
Paul Elliottc23a9a02021-06-21 18:32:46 +01006427 /* Test for setting lengths twice. */
6428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6434 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6437 input_data->len),
6438 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006441
Andrzej Kurekad837522021-12-15 15:28:49 +01006442 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6451 additional_data->len),
6452 PSA_ERROR_BAD_STATE);
6453 } else {
6454 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6455 additional_data->len));
6456
6457 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6458 input_data->len),
6459 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006460 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006462
6463 /* ------------------------------------------------------- */
6464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 if (operation.alg == PSA_ALG_CCM) {
6470 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6471 input_data->len, output_data,
6472 output_size, &output_length),
6473 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 } else {
6476 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6477 input_data->len, output_data,
6478 output_size, &output_length));
6479
6480 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6481 input_data->len),
6482 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006483 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006485
6486 /* ------------------------------------------------------- */
6487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 if (operation.alg == PSA_ALG_CCM) {
6493 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6494 finish_output_size,
6495 &output_part_length,
6496 tag_buffer, tag_length,
6497 &tag_size));
6498 } else {
6499 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6500 finish_output_size,
6501 &output_part_length,
6502 tag_buffer, tag_length,
6503 &tag_size));
6504
6505 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6506 input_data->len),
6507 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006508 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006510
6511 /* Test for setting lengths after generating nonce + already starting data. */
6512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6516 PSA_AEAD_NONCE_MAX_SIZE,
6517 &nonce_length));
6518 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6521 additional_data->len),
6522 PSA_ERROR_BAD_STATE);
6523 } else {
6524 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6525 additional_data->len));
6526
6527 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6528 input_data->len),
6529 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006530 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006532
6533 /* ------------------------------------------------------- */
6534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006536
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6538 PSA_AEAD_NONCE_MAX_SIZE,
6539 &nonce_length));
6540 if (operation.alg == PSA_ALG_CCM) {
6541 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6542 input_data->len, output_data,
6543 output_size, &output_length),
6544 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006545
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 } else {
6547 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6548 input_data->len, output_data,
6549 output_size, &output_length));
6550
6551 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6552 input_data->len),
6553 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006554 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006556
6557 /* ------------------------------------------------------- */
6558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6562 PSA_AEAD_NONCE_MAX_SIZE,
6563 &nonce_length));
6564 if (operation.alg == PSA_ALG_CCM) {
6565 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6566 finish_output_size,
6567 &output_part_length,
6568 tag_buffer, tag_length,
6569 &tag_size));
6570 } else {
6571 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6572 finish_output_size,
6573 &output_part_length,
6574 tag_buffer, tag_length,
6575 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006576
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6578 input_data->len),
6579 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006580 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006582
Paul Elliott243080c2021-07-21 19:01:17 +01006583 /* Test for not sending any additional data or data after setting non zero
6584 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6591 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6594 finish_output_size,
6595 &output_part_length,
6596 tag_buffer, tag_length,
6597 &tag_size),
6598 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006601
Paul Elliott243080c2021-07-21 19:01:17 +01006602 /* Test for not sending any additional data or data after setting non-zero
6603 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006608
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6610 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6613 finish_output_size,
6614 &output_part_length,
6615 tag_buffer,
6616 tag_length),
6617 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006618
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006620
Paul Elliott243080c2021-07-21 19:01:17 +01006621 /* Test for not sending any additional data after setting a non-zero length
6622 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006625
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6629 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6632 input_data->len, output_data,
6633 output_size, &output_length),
6634 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006635
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006637
Paul Elliottf94bd992021-09-19 18:15:59 +01006638 /* Test for not sending any data after setting a non-zero length for it.*/
6639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6645 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6648 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6651 finish_output_size,
6652 &output_part_length,
6653 tag_buffer, tag_length,
6654 &tag_size),
6655 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006658
Paul Elliottb0450fe2021-09-01 15:06:26 +01006659 /* Test for sending too much additional data after setting lengths. */
6660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006666
6667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6669 additional_data->len),
6670 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006673
Paul Elliotta2a09b02021-09-22 14:56:40 +01006674 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006677
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006679
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6681 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006682
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6684 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6687 1),
6688 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006691
Paul Elliottb0450fe2021-09-01 15:06:26 +01006692 /* Test for sending too much data after setting lengths. */
6693
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006695
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006697
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006699
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6701 input_data->len, output_data,
6702 output_size, &output_length),
6703 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006704
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006706
Paul Elliotta2a09b02021-09-22 14:56:40 +01006707 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006708
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006710
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006712
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6714 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006715
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6717 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6720 input_data->len, output_data,
6721 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006722
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6724 1, output_data,
6725 output_size, &output_length),
6726 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006729
Paul Elliottc23a9a02021-06-21 18:32:46 +01006730 /* Test sending additional data after data. */
6731
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006733
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006735
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 if (operation.alg != PSA_ALG_CCM) {
6737 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6738 input_data->len, output_data,
6739 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006740
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6742 additional_data->len),
6743 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006746
Paul Elliott534d0b42021-06-22 19:15:20 +01006747 /* Test calling finish on decryption. */
6748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006752
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6754 finish_output_size,
6755 &output_part_length,
6756 tag_buffer, tag_length,
6757 &tag_size),
6758 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006759
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006761
6762 /* Test calling verify on encryption. */
6763
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006765
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006767
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6769 finish_output_size,
6770 &output_part_length,
6771 tag_buffer,
6772 tag_length),
6773 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006774
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006776
6777
Paul Elliottc23a9a02021-06-21 18:32:46 +01006778exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 psa_destroy_key(key);
6780 psa_aead_abort(&operation);
6781 mbedtls_free(output_data);
6782 mbedtls_free(final_data);
6783 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006784}
6785/* END_CASE */
6786
6787/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006788void signature_size(int type_arg,
6789 int bits,
6790 int alg_arg,
6791 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006792{
6793 psa_key_type_t type = type_arg;
6794 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006796
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006798
Gilles Peskinee59236f2018-01-27 23:32:46 +01006799exit:
6800 ;
6801}
6802/* END_CASE */
6803
6804/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006805void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6806 int alg_arg, data_t *input_data,
6807 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006808{
Ronald Cron5425a212020-08-04 14:58:35 +02006809 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006810 psa_key_type_t key_type = key_type_arg;
6811 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006812 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006813 unsigned char *signature = NULL;
6814 size_t signature_size;
6815 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006817
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6821 psa_set_key_algorithm(&attributes, alg);
6822 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6825 &key));
6826 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6827 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006828
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006829 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006830 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6832 key_bits, alg);
6833 TEST_ASSERT(signature_size != 0);
6834 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006835 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006836
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006837 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 PSA_ASSERT(psa_sign_hash(key, alg,
6839 input_data->x, input_data->len,
6840 signature, signature_size,
6841 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006842 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006843 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006844 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006845
6846exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006847 /*
6848 * Key attributes may have been returned by psa_get_key_attributes()
6849 * thus reset them as required.
6850 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 psa_destroy_key(key);
6854 mbedtls_free(signature);
6855 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006856}
6857/* END_CASE */
6858
Paul Elliott712d5122022-12-07 14:03:10 +00006859/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006860/**
6861 * sign_hash_interruptible() test intentions:
6862 *
6863 * Note: This test can currently only handle ECDSA.
6864 *
6865 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006866 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006867 *
6868 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6869 * expected for different max_ops values.
6870 *
6871 * 3. Test that the number of ops done prior to start and after abort is zero
6872 * and that each successful stage completes some ops (this is not mandated by
6873 * the PSA specification, but is currently the case).
6874 *
6875 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6876 * complete() calls does not alter the number of ops returned.
6877 */
Paul Elliott712d5122022-12-07 14:03:10 +00006878void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6879 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006880 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006881{
6882 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6883 psa_key_type_t key_type = key_type_arg;
6884 psa_algorithm_t alg = alg_arg;
6885 size_t key_bits;
6886 unsigned char *signature = NULL;
6887 size_t signature_size;
6888 size_t signature_length = 0xdeadbeef;
6889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6890 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006891 uint32_t num_ops = 0;
6892 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006893 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006894 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006895 size_t min_completes = 0;
6896 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006897
Paul Elliott712d5122022-12-07 14:03:10 +00006898 psa_sign_hash_interruptible_operation_t operation =
6899 psa_sign_hash_interruptible_operation_init();
6900
6901 PSA_ASSERT(psa_crypto_init());
6902
6903 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6904 psa_set_key_algorithm(&attributes, alg);
6905 psa_set_key_type(&attributes, key_type);
6906
6907 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6908 &key));
6909 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6910 key_bits = psa_get_key_bits(&attributes);
6911
6912 /* Allocate a buffer which has the size advertised by the
6913 * library. */
6914 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6915 key_bits, alg);
6916 TEST_ASSERT(signature_size != 0);
6917 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006918 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006919
Paul Elliott0c683352022-12-16 19:16:56 +00006920 psa_interruptible_set_max_ops(max_ops);
6921
Paul Elliott6f600372023-02-06 18:41:05 +00006922 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6923 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006924
Paul Elliott712d5122022-12-07 14:03:10 +00006925 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6926 TEST_ASSERT(num_ops_prior == 0);
6927
6928 /* Start performing the signature. */
6929 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6930 input_data->x, input_data->len));
6931
6932 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6933 TEST_ASSERT(num_ops_prior == 0);
6934
6935 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006936 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006937 status = psa_sign_hash_complete(&operation, signature, signature_size,
6938 &signature_length);
6939
Paul Elliott0c683352022-12-16 19:16:56 +00006940 num_completes++;
6941
Paul Elliott712d5122022-12-07 14:03:10 +00006942 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6943 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006944 /* We are asserting here that every complete makes progress
6945 * (completes some ops), which is true of the internal
6946 * implementation and probably any implementation, however this is
6947 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006948 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006949
Paul Elliott712d5122022-12-07 14:03:10 +00006950 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006951
6952 /* Ensure calling get_num_ops() twice still returns the same
6953 * number of ops as previously reported. */
6954 num_ops = psa_sign_hash_get_num_ops(&operation);
6955
6956 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006957 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006958 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006959
6960 TEST_ASSERT(status == PSA_SUCCESS);
6961
Paul Elliott0c683352022-12-16 19:16:56 +00006962 TEST_LE_U(min_completes, num_completes);
6963 TEST_LE_U(num_completes, max_completes);
6964
Paul Elliott712d5122022-12-07 14:03:10 +00006965 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006966 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006967 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006968
6969 PSA_ASSERT(psa_sign_hash_abort(&operation));
6970
Paul Elliott59ad9452022-12-18 15:09:02 +00006971 num_ops = psa_sign_hash_get_num_ops(&operation);
6972 TEST_ASSERT(num_ops == 0);
6973
Paul Elliott712d5122022-12-07 14:03:10 +00006974exit:
6975
6976 /*
6977 * Key attributes may have been returned by psa_get_key_attributes()
6978 * thus reset them as required.
6979 */
6980 psa_reset_key_attributes(&attributes);
6981
6982 psa_destroy_key(key);
6983 mbedtls_free(signature);
6984 PSA_DONE();
6985}
6986/* END_CASE */
6987
Gilles Peskine20035e32018-02-03 22:44:14 +01006988/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006989void sign_hash_fail(int key_type_arg, data_t *key_data,
6990 int alg_arg, data_t *input_data,
6991 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006992{
Ronald Cron5425a212020-08-04 14:58:35 +02006993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006994 psa_key_type_t key_type = key_type_arg;
6995 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006996 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006997 psa_status_t actual_status;
6998 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006999 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01007000 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01007002
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007003 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01007004
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01007006
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
7008 psa_set_key_algorithm(&attributes, alg);
7009 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07007010
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7012 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01007013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 actual_status = psa_sign_hash(key, alg,
7015 input_data->x, input_data->len,
7016 signature, signature_size,
7017 &signature_length);
7018 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02007019 /* The value of *signature_length is unspecified on error, but
7020 * whatever it is, it should be less than signature_size, so that
7021 * if the caller tries to read *signature_length bytes without
7022 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01007024
7025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 psa_reset_key_attributes(&attributes);
7027 psa_destroy_key(key);
7028 mbedtls_free(signature);
7029 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01007030}
7031/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03007032
Paul Elliott91007972022-12-16 12:21:24 +00007033/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007034/**
7035 * sign_hash_fail_interruptible() test intentions:
7036 *
7037 * Note: This test can currently only handle ECDSA.
7038 *
7039 * 1. Test that various failure cases for interruptible sign hash fail with the
7040 * correct error codes, and at the correct point (at start or during
7041 * complete).
7042 *
7043 * 2. Test the number of calls to psa_sign_hash_complete() required are as
7044 * expected for different max_ops values.
7045 *
7046 * 3. Test that the number of ops done prior to start and after abort is zero
7047 * and that each successful stage completes some ops (this is not mandated by
7048 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007049 *
7050 * 4. Check that calling complete() when start() fails and complete()
7051 * after completion results in a BAD_STATE error.
7052 *
7053 * 5. Check that calling start() again after start fails results in a BAD_STATE
7054 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007055 */
Paul Elliott91007972022-12-16 12:21:24 +00007056void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7057 int alg_arg, data_t *input_data,
7058 int signature_size_arg,
7059 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007060 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007061 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007062{
7063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7064 psa_key_type_t key_type = key_type_arg;
7065 psa_algorithm_t alg = alg_arg;
7066 size_t signature_size = signature_size_arg;
7067 psa_status_t actual_status;
7068 psa_status_t expected_start_status = expected_start_status_arg;
7069 psa_status_t expected_complete_status = expected_complete_status_arg;
7070 unsigned char *signature = NULL;
7071 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007072 uint32_t num_ops = 0;
7073 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007074 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007075 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007076 size_t min_completes = 0;
7077 size_t max_completes = 0;
7078
Paul Elliott91007972022-12-16 12:21:24 +00007079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7080 psa_sign_hash_interruptible_operation_t operation =
7081 psa_sign_hash_interruptible_operation_init();
7082
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007083 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00007084
7085 PSA_ASSERT(psa_crypto_init());
7086
7087 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
7088 psa_set_key_algorithm(&attributes, alg);
7089 psa_set_key_type(&attributes, key_type);
7090
7091 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7092 &key));
7093
Paul Elliott0c683352022-12-16 19:16:56 +00007094 psa_interruptible_set_max_ops(max_ops);
7095
Paul Elliott6f600372023-02-06 18:41:05 +00007096 interruptible_signverify_get_minmax_completes(max_ops,
7097 expected_complete_status,
7098 &min_completes,
7099 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007100
Paul Elliott91007972022-12-16 12:21:24 +00007101 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
7102 TEST_ASSERT(num_ops_prior == 0);
7103
7104 /* Start performing the signature. */
7105 actual_status = psa_sign_hash_start(&operation, key, alg,
7106 input_data->x, input_data->len);
7107
7108 TEST_EQUAL(actual_status, expected_start_status);
7109
Paul Elliottc9774412023-02-06 15:14:07 +00007110 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007111 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007112 * start failed. */
7113 actual_status = psa_sign_hash_complete(&operation, signature,
7114 signature_size,
7115 &signature_length);
7116
7117 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7118
7119 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007120 actual_status = psa_sign_hash_start(&operation, key, alg,
7121 input_data->x, input_data->len);
7122
7123 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7124 }
7125
Paul Elliott91007972022-12-16 12:21:24 +00007126 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
7127 TEST_ASSERT(num_ops_prior == 0);
7128
Paul Elliott91007972022-12-16 12:21:24 +00007129 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007130 do {
Paul Elliott91007972022-12-16 12:21:24 +00007131 actual_status = psa_sign_hash_complete(&operation, signature,
7132 signature_size,
7133 &signature_length);
7134
Paul Elliott0c683352022-12-16 19:16:56 +00007135 num_completes++;
7136
Paul Elliott334d7262023-01-20 17:29:41 +00007137 if (actual_status == PSA_SUCCESS ||
7138 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007139 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007140 /* We are asserting here that every complete makes progress
7141 * (completes some ops), which is true of the internal
7142 * implementation and probably any implementation, however this is
7143 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007144 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007145
Paul Elliott91007972022-12-16 12:21:24 +00007146 num_ops_prior = num_ops;
7147 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007148 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007149
Paul Elliottc9774412023-02-06 15:14:07 +00007150 TEST_EQUAL(actual_status, expected_complete_status);
7151
Paul Elliottefebad02023-02-15 16:56:45 +00007152 /* Check that another complete returns BAD_STATE. */
7153 actual_status = psa_sign_hash_complete(&operation, signature,
7154 signature_size,
7155 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00007156
Paul Elliottefebad02023-02-15 16:56:45 +00007157 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007158
Paul Elliott91007972022-12-16 12:21:24 +00007159 PSA_ASSERT(psa_sign_hash_abort(&operation));
7160
Paul Elliott59ad9452022-12-18 15:09:02 +00007161 num_ops = psa_sign_hash_get_num_ops(&operation);
7162 TEST_ASSERT(num_ops == 0);
7163
Paul Elliott91007972022-12-16 12:21:24 +00007164 /* The value of *signature_length is unspecified on error, but
7165 * whatever it is, it should be less than signature_size, so that
7166 * if the caller tries to read *signature_length bytes without
7167 * checking the error code then they don't overflow a buffer. */
7168 TEST_LE_U(signature_length, signature_size);
7169
Paul Elliott0c683352022-12-16 19:16:56 +00007170 TEST_LE_U(min_completes, num_completes);
7171 TEST_LE_U(num_completes, max_completes);
7172
Paul Elliott91007972022-12-16 12:21:24 +00007173exit:
7174 psa_reset_key_attributes(&attributes);
7175 psa_destroy_key(key);
7176 mbedtls_free(signature);
7177 PSA_DONE();
7178}
7179/* END_CASE */
7180
mohammad16038cc1cee2018-03-28 01:21:33 +03007181/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007182void sign_verify_hash(int key_type_arg, data_t *key_data,
7183 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02007184{
Ronald Cron5425a212020-08-04 14:58:35 +02007185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02007186 psa_key_type_t key_type = key_type_arg;
7187 psa_algorithm_t alg = alg_arg;
7188 size_t key_bits;
7189 unsigned char *signature = NULL;
7190 size_t signature_size;
7191 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02007193
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02007195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
7197 psa_set_key_algorithm(&attributes, alg);
7198 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02007199
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7201 &key));
7202 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7203 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02007204
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007205 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02007206 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7208 key_bits, alg);
7209 TEST_ASSERT(signature_size != 0);
7210 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007211 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02007212
7213 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 PSA_ASSERT(psa_sign_hash(key, alg,
7215 input_data->x, input_data->len,
7216 signature, signature_size,
7217 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007218 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 TEST_LE_U(signature_length, signature_size);
7220 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02007221
7222 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 PSA_ASSERT(psa_verify_hash(key, alg,
7224 input_data->x, input_data->len,
7225 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02007226
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02007228 /* Flip a bit in the input and verify that the signature is now
7229 * detected as invalid. Flip a bit at the beginning, not at the end,
7230 * because ECDSA may ignore the last few bits of the input. */
7231 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 TEST_EQUAL(psa_verify_hash(key, alg,
7233 input_data->x, input_data->len,
7234 signature, signature_length),
7235 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02007236 }
7237
7238exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007239 /*
7240 * Key attributes may have been returned by psa_get_key_attributes()
7241 * thus reset them as required.
7242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007244
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 psa_destroy_key(key);
7246 mbedtls_free(signature);
7247 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02007248}
7249/* END_CASE */
7250
Paul Elliott712d5122022-12-07 14:03:10 +00007251/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007252/**
7253 * sign_verify_hash_interruptible() test intentions:
7254 *
7255 * Note: This test can currently only handle ECDSA.
7256 *
Paul Elliott8c092052023-03-06 17:49:14 +00007257 * 1. Test that we can sign an input hash with the given keypair and then
7258 * afterwards verify that signature. This is currently the only way to test
7259 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00007260 *
7261 * 2. Test that after corrupting the hash, the verification detects an invalid
7262 * signature.
7263 *
7264 * 3. Test the number of calls to psa_sign_hash_complete() required are as
7265 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00007266 *
7267 * 4. Test that the number of ops done prior to starting signing and after abort
7268 * is zero and that each successful signing stage completes some ops (this is
7269 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00007270 */
Paul Elliott712d5122022-12-07 14:03:10 +00007271void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00007272 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007273 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007274{
7275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7276 psa_key_type_t key_type = key_type_arg;
7277 psa_algorithm_t alg = alg_arg;
7278 size_t key_bits;
7279 unsigned char *signature = NULL;
7280 size_t signature_size;
7281 size_t signature_length = 0xdeadbeef;
7282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7283 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007284 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00007285 uint32_t num_ops = 0;
7286 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007287 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007288 size_t min_completes = 0;
7289 size_t max_completes = 0;
7290
Paul Elliott712d5122022-12-07 14:03:10 +00007291 psa_sign_hash_interruptible_operation_t sign_operation =
7292 psa_sign_hash_interruptible_operation_init();
7293 psa_verify_hash_interruptible_operation_t verify_operation =
7294 psa_verify_hash_interruptible_operation_init();
7295
7296 PSA_ASSERT(psa_crypto_init());
7297
Paul Elliott0c683352022-12-16 19:16:56 +00007298 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7299 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007300 psa_set_key_algorithm(&attributes, alg);
7301 psa_set_key_type(&attributes, key_type);
7302
7303 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7304 &key));
7305 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7306 key_bits = psa_get_key_bits(&attributes);
7307
7308 /* Allocate a buffer which has the size advertised by the
7309 * library. */
7310 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7311 key_bits, alg);
7312 TEST_ASSERT(signature_size != 0);
7313 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007314 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007315
Paul Elliott0c683352022-12-16 19:16:56 +00007316 psa_interruptible_set_max_ops(max_ops);
7317
Paul Elliott6f600372023-02-06 18:41:05 +00007318 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7319 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007320
Paul Elliott7c173082023-02-26 18:44:45 +00007321 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7322 TEST_ASSERT(num_ops_prior == 0);
7323
Paul Elliott712d5122022-12-07 14:03:10 +00007324 /* Start performing the signature. */
7325 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7326 input_data->x, input_data->len));
7327
Paul Elliott7c173082023-02-26 18:44:45 +00007328 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7329 TEST_ASSERT(num_ops_prior == 0);
7330
Paul Elliott712d5122022-12-07 14:03:10 +00007331 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007332 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007333
Paul Elliott0c683352022-12-16 19:16:56 +00007334 status = psa_sign_hash_complete(&sign_operation, signature,
7335 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007336 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007337
7338 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007339
7340 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7341 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7342 /* We are asserting here that every complete makes progress
7343 * (completes some ops), which is true of the internal
7344 * implementation and probably any implementation, however this is
7345 * not mandated by the PSA specification. */
7346 TEST_ASSERT(num_ops > num_ops_prior);
7347
7348 num_ops_prior = num_ops;
7349 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007350 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007351
7352 TEST_ASSERT(status == PSA_SUCCESS);
7353
Paul Elliott0c683352022-12-16 19:16:56 +00007354 TEST_LE_U(min_completes, num_completes);
7355 TEST_LE_U(num_completes, max_completes);
7356
Paul Elliott712d5122022-12-07 14:03:10 +00007357 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7358
Paul Elliott7c173082023-02-26 18:44:45 +00007359 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7360 TEST_ASSERT(num_ops == 0);
7361
Paul Elliott712d5122022-12-07 14:03:10 +00007362 /* Check that the signature length looks sensible. */
7363 TEST_LE_U(signature_length, signature_size);
7364 TEST_ASSERT(signature_length > 0);
7365
Paul Elliott0c683352022-12-16 19:16:56 +00007366 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007367
7368 /* Start verification. */
7369 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7370 input_data->x, input_data->len,
7371 signature, signature_length));
7372
7373 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007374 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007375 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007376
7377 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007378 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007379
7380 TEST_ASSERT(status == PSA_SUCCESS);
7381
Paul Elliott0c683352022-12-16 19:16:56 +00007382 TEST_LE_U(min_completes, num_completes);
7383 TEST_LE_U(num_completes, max_completes);
7384
Paul Elliott712d5122022-12-07 14:03:10 +00007385 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7386
7387 verify_operation = psa_verify_hash_interruptible_operation_init();
7388
7389 if (input_data->len != 0) {
7390 /* Flip a bit in the input and verify that the signature is now
7391 * detected as invalid. Flip a bit at the beginning, not at the end,
7392 * because ECDSA may ignore the last few bits of the input. */
7393 input_data->x[0] ^= 1;
7394
Paul Elliott712d5122022-12-07 14:03:10 +00007395 /* Start verification. */
7396 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7397 input_data->x, input_data->len,
7398 signature, signature_length));
7399
7400 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007401 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007402 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007403 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007404
7405 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7406 }
7407
7408 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7409
7410exit:
7411 /*
7412 * Key attributes may have been returned by psa_get_key_attributes()
7413 * thus reset them as required.
7414 */
7415 psa_reset_key_attributes(&attributes);
7416
7417 psa_destroy_key(key);
7418 mbedtls_free(signature);
7419 PSA_DONE();
7420}
7421/* END_CASE */
7422
Gilles Peskine9911b022018-06-29 17:30:48 +02007423/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007424void verify_hash(int key_type_arg, data_t *key_data,
7425 int alg_arg, data_t *hash_data,
7426 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007427{
Ronald Cron5425a212020-08-04 14:58:35 +02007428 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007429 psa_key_type_t key_type = key_type_arg;
7430 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007432
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007434
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007436
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7438 psa_set_key_algorithm(&attributes, alg);
7439 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7442 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007443
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 PSA_ASSERT(psa_verify_hash(key, alg,
7445 hash_data->x, hash_data->len,
7446 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007447
itayzafrir5c753392018-05-08 11:18:38 +03007448exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 psa_reset_key_attributes(&attributes);
7450 psa_destroy_key(key);
7451 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007452}
7453/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007454
Paul Elliott712d5122022-12-07 14:03:10 +00007455/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007456/**
7457 * verify_hash_interruptible() test intentions:
7458 *
7459 * Note: This test can currently only handle ECDSA.
7460 *
7461 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007462 * only). Given this test only does verification it can accept public keys as
7463 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007464 *
7465 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7466 * expected for different max_ops values.
7467 *
7468 * 3. Test that the number of ops done prior to start and after abort is zero
7469 * and that each successful stage completes some ops (this is not mandated by
7470 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007471 *
7472 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7473 * complete() calls does not alter the number of ops returned.
7474 *
7475 * 5. Test that after corrupting the hash, the verification detects an invalid
7476 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007477 */
Paul Elliott712d5122022-12-07 14:03:10 +00007478void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7479 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007480 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007481{
7482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7483 psa_key_type_t key_type = key_type_arg;
7484 psa_algorithm_t alg = alg_arg;
7485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7486 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007487 uint32_t num_ops = 0;
7488 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007489 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007490 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007491 size_t min_completes = 0;
7492 size_t max_completes = 0;
7493
Paul Elliott712d5122022-12-07 14:03:10 +00007494 psa_verify_hash_interruptible_operation_t operation =
7495 psa_verify_hash_interruptible_operation_init();
7496
7497 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7498
7499 PSA_ASSERT(psa_crypto_init());
7500
7501 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7502 psa_set_key_algorithm(&attributes, alg);
7503 psa_set_key_type(&attributes, key_type);
7504
7505 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7506 &key));
7507
Paul Elliott0c683352022-12-16 19:16:56 +00007508 psa_interruptible_set_max_ops(max_ops);
7509
Paul Elliott6f600372023-02-06 18:41:05 +00007510 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7511 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007512
Paul Elliott712d5122022-12-07 14:03:10 +00007513 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7514
7515 TEST_ASSERT(num_ops_prior == 0);
7516
7517 /* Start verification. */
7518 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7519 hash_data->x, hash_data->len,
7520 signature_data->x, signature_data->len)
7521 );
7522
7523 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7524
7525 TEST_ASSERT(num_ops_prior == 0);
7526
7527 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007528 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007529 status = psa_verify_hash_complete(&operation);
7530
Paul Elliott0c683352022-12-16 19:16:56 +00007531 num_completes++;
7532
Paul Elliott712d5122022-12-07 14:03:10 +00007533 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7534 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007535 /* We are asserting here that every complete makes progress
7536 * (completes some ops), which is true of the internal
7537 * implementation and probably any implementation, however this is
7538 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007539 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007540
Paul Elliott712d5122022-12-07 14:03:10 +00007541 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007542
7543 /* Ensure calling get_num_ops() twice still returns the same
7544 * number of ops as previously reported. */
7545 num_ops = psa_verify_hash_get_num_ops(&operation);
7546
7547 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007548 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007549 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007550
7551 TEST_ASSERT(status == PSA_SUCCESS);
7552
Paul Elliott0c683352022-12-16 19:16:56 +00007553 TEST_LE_U(min_completes, num_completes);
7554 TEST_LE_U(num_completes, max_completes);
7555
Paul Elliott712d5122022-12-07 14:03:10 +00007556 PSA_ASSERT(psa_verify_hash_abort(&operation));
7557
Paul Elliott59ad9452022-12-18 15:09:02 +00007558 num_ops = psa_verify_hash_get_num_ops(&operation);
7559 TEST_ASSERT(num_ops == 0);
7560
Paul Elliott8359c142023-02-24 18:40:10 +00007561 if (hash_data->len != 0) {
7562 /* Flip a bit in the hash and verify that the signature is now detected
7563 * as invalid. Flip a bit at the beginning, not at the end, because
7564 * ECDSA may ignore the last few bits of the input. */
7565 hash_data->x[0] ^= 1;
7566
7567 /* Start verification. */
7568 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7569 hash_data->x, hash_data->len,
7570 signature_data->x, signature_data->len));
7571
7572 /* Continue performing the signature until complete. */
7573 do {
7574 status = psa_verify_hash_complete(&operation);
7575 } while (status == PSA_OPERATION_INCOMPLETE);
7576
7577 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7578 }
7579
Paul Elliott712d5122022-12-07 14:03:10 +00007580exit:
7581 psa_reset_key_attributes(&attributes);
7582 psa_destroy_key(key);
7583 PSA_DONE();
7584}
7585/* END_CASE */
7586
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007587/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007588void verify_hash_fail(int key_type_arg, data_t *key_data,
7589 int alg_arg, data_t *hash_data,
7590 data_t *signature_data,
7591 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007592{
Ronald Cron5425a212020-08-04 14:58:35 +02007593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007594 psa_key_type_t key_type = key_type_arg;
7595 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007596 psa_status_t actual_status;
7597 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007599
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007601
Gilles Peskine449bd832023-01-11 14:50:10 +01007602 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7603 psa_set_key_algorithm(&attributes, alg);
7604 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007605
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7607 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007608
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 actual_status = psa_verify_hash(key, alg,
7610 hash_data->x, hash_data->len,
7611 signature_data->x, signature_data->len);
7612 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007613
7614exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 psa_reset_key_attributes(&attributes);
7616 psa_destroy_key(key);
7617 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007618}
7619/* END_CASE */
7620
Paul Elliott91007972022-12-16 12:21:24 +00007621/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007622/**
7623 * verify_hash_fail_interruptible() test intentions:
7624 *
7625 * Note: This test can currently only handle ECDSA.
7626 *
7627 * 1. Test that various failure cases for interruptible verify hash fail with
7628 * the correct error codes, and at the correct point (at start or during
7629 * complete).
7630 *
7631 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7632 * expected for different max_ops values.
7633 *
7634 * 3. Test that the number of ops done prior to start and after abort is zero
7635 * and that each successful stage completes some ops (this is not mandated by
7636 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007637 *
7638 * 4. Check that calling complete() when start() fails and complete()
7639 * after completion results in a BAD_STATE error.
7640 *
7641 * 5. Check that calling start() again after start fails results in a BAD_STATE
7642 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007643 */
Paul Elliott91007972022-12-16 12:21:24 +00007644void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7645 int alg_arg, data_t *hash_data,
7646 data_t *signature_data,
7647 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007648 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007649 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007650{
7651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7652 psa_key_type_t key_type = key_type_arg;
7653 psa_algorithm_t alg = alg_arg;
7654 psa_status_t actual_status;
7655 psa_status_t expected_start_status = expected_start_status_arg;
7656 psa_status_t expected_complete_status = expected_complete_status_arg;
7657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007658 uint32_t num_ops = 0;
7659 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007660 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007661 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007662 size_t min_completes = 0;
7663 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007664 psa_verify_hash_interruptible_operation_t operation =
7665 psa_verify_hash_interruptible_operation_init();
7666
7667 PSA_ASSERT(psa_crypto_init());
7668
7669 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7670 psa_set_key_algorithm(&attributes, alg);
7671 psa_set_key_type(&attributes, key_type);
7672
7673 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7674 &key));
7675
Paul Elliott0c683352022-12-16 19:16:56 +00007676 psa_interruptible_set_max_ops(max_ops);
7677
Paul Elliott6f600372023-02-06 18:41:05 +00007678 interruptible_signverify_get_minmax_completes(max_ops,
7679 expected_complete_status,
7680 &min_completes,
7681 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007682
Paul Elliott91007972022-12-16 12:21:24 +00007683 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7684 TEST_ASSERT(num_ops_prior == 0);
7685
7686 /* Start verification. */
7687 actual_status = psa_verify_hash_start(&operation, key, alg,
7688 hash_data->x, hash_data->len,
7689 signature_data->x,
7690 signature_data->len);
7691
7692 TEST_EQUAL(actual_status, expected_start_status);
7693
Paul Elliottc9774412023-02-06 15:14:07 +00007694 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007695 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007696 * start failed. */
7697 actual_status = psa_verify_hash_complete(&operation);
7698
7699 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7700
7701 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007702 actual_status = psa_verify_hash_start(&operation, key, alg,
7703 hash_data->x, hash_data->len,
7704 signature_data->x,
7705 signature_data->len);
7706
7707 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7708 }
7709
Paul Elliott91007972022-12-16 12:21:24 +00007710 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7711 TEST_ASSERT(num_ops_prior == 0);
7712
Paul Elliott91007972022-12-16 12:21:24 +00007713 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007714 do {
Paul Elliott91007972022-12-16 12:21:24 +00007715 actual_status = psa_verify_hash_complete(&operation);
7716
Paul Elliott0c683352022-12-16 19:16:56 +00007717 num_completes++;
7718
Paul Elliott334d7262023-01-20 17:29:41 +00007719 if (actual_status == PSA_SUCCESS ||
7720 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007721 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007722 /* We are asserting here that every complete makes progress
7723 * (completes some ops), which is true of the internal
7724 * implementation and probably any implementation, however this is
7725 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007726 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007727
Paul Elliott91007972022-12-16 12:21:24 +00007728 num_ops_prior = num_ops;
7729 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007730 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007731
Paul Elliottc9774412023-02-06 15:14:07 +00007732 TEST_EQUAL(actual_status, expected_complete_status);
7733
Paul Elliottefebad02023-02-15 16:56:45 +00007734 /* Check that another complete returns BAD_STATE. */
7735 actual_status = psa_verify_hash_complete(&operation);
7736 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007737
Paul Elliott0c683352022-12-16 19:16:56 +00007738 TEST_LE_U(min_completes, num_completes);
7739 TEST_LE_U(num_completes, max_completes);
7740
Paul Elliott91007972022-12-16 12:21:24 +00007741 PSA_ASSERT(psa_verify_hash_abort(&operation));
7742
Paul Elliott59ad9452022-12-18 15:09:02 +00007743 num_ops = psa_verify_hash_get_num_ops(&operation);
7744 TEST_ASSERT(num_ops == 0);
7745
Paul Elliott91007972022-12-16 12:21:24 +00007746exit:
7747 psa_reset_key_attributes(&attributes);
7748 psa_destroy_key(key);
7749 PSA_DONE();
7750}
7751/* END_CASE */
7752
Paul Elliott20a36062022-12-18 13:21:25 +00007753/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007754/**
7755 * interruptible_signverify_hash_state_test() test intentions:
7756 *
7757 * Note: This test can currently only handle ECDSA.
7758 *
7759 * 1. Test that calling the various interruptible sign and verify hash functions
7760 * in incorrect orders returns BAD_STATE errors.
7761 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007762void interruptible_signverify_hash_state_test(int key_type_arg,
7763 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007764{
7765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7766 psa_key_type_t key_type = key_type_arg;
7767 psa_algorithm_t alg = alg_arg;
7768 size_t key_bits;
7769 unsigned char *signature = NULL;
7770 size_t signature_size;
7771 size_t signature_length = 0xdeadbeef;
7772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7773 psa_sign_hash_interruptible_operation_t sign_operation =
7774 psa_sign_hash_interruptible_operation_init();
7775 psa_verify_hash_interruptible_operation_t verify_operation =
7776 psa_verify_hash_interruptible_operation_init();
7777
7778 PSA_ASSERT(psa_crypto_init());
7779
7780 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7781 PSA_KEY_USAGE_VERIFY_HASH);
7782 psa_set_key_algorithm(&attributes, alg);
7783 psa_set_key_type(&attributes, key_type);
7784
7785 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7786 &key));
7787 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7788 key_bits = psa_get_key_bits(&attributes);
7789
7790 /* Allocate a buffer which has the size advertised by the
7791 * library. */
7792 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7793 key_bits, alg);
7794 TEST_ASSERT(signature_size != 0);
7795 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007796 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007797
7798 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7799
7800 /* --- Attempt completes prior to starts --- */
7801 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7802 signature_size,
7803 &signature_length),
7804 PSA_ERROR_BAD_STATE);
7805
7806 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7807
7808 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7809 PSA_ERROR_BAD_STATE);
7810
7811 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7812
7813 /* --- Aborts in all other places. --- */
7814 psa_sign_hash_abort(&sign_operation);
7815
7816 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7817 input_data->x, input_data->len));
7818
7819 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7820
7821 psa_interruptible_set_max_ops(1);
7822
7823 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7824 input_data->x, input_data->len));
7825
7826 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7827 signature_size,
7828 &signature_length),
7829 PSA_OPERATION_INCOMPLETE);
7830
7831 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7832
7833 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7834
7835 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7836 input_data->x, input_data->len));
7837
7838 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7839 signature_size,
7840 &signature_length));
7841
7842 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7843
7844 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7845
7846 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7847 input_data->x, input_data->len,
7848 signature, signature_length));
7849
7850 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7851
7852 psa_interruptible_set_max_ops(1);
7853
7854 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7855 input_data->x, input_data->len,
7856 signature, signature_length));
7857
7858 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7859 PSA_OPERATION_INCOMPLETE);
7860
7861 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7862
7863 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7864
7865 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7866 input_data->x, input_data->len,
7867 signature, signature_length));
7868
7869 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7870
7871 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7872
7873 /* --- Attempt double starts. --- */
7874
7875 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7876 input_data->x, input_data->len));
7877
7878 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7879 input_data->x, input_data->len),
7880 PSA_ERROR_BAD_STATE);
7881
7882 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7883
7884 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7885 input_data->x, input_data->len,
7886 signature, signature_length));
7887
7888 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7889 input_data->x, input_data->len,
7890 signature, signature_length),
7891 PSA_ERROR_BAD_STATE);
7892
7893 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7894
Paul Elliott76d671a2023-02-07 17:45:18 +00007895exit:
7896 /*
7897 * Key attributes may have been returned by psa_get_key_attributes()
7898 * thus reset them as required.
7899 */
7900 psa_reset_key_attributes(&attributes);
7901
7902 psa_destroy_key(key);
7903 mbedtls_free(signature);
7904 PSA_DONE();
7905}
7906/* END_CASE */
7907
7908/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007909/**
Paul Elliottc2033502023-02-26 17:09:14 +00007910 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007911 *
7912 * Note: This test can currently only handle ECDSA.
7913 *
7914 * 1. Test various edge cases in the interruptible sign and verify hash
7915 * interfaces.
7916 */
Paul Elliottc2033502023-02-26 17:09:14 +00007917void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007918 data_t *key_data, int alg_arg, data_t *input_data)
7919{
7920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7921 psa_key_type_t key_type = key_type_arg;
7922 psa_algorithm_t alg = alg_arg;
7923 size_t key_bits;
7924 unsigned char *signature = NULL;
7925 size_t signature_size;
7926 size_t signature_length = 0xdeadbeef;
7927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7928 uint8_t *input_buffer = NULL;
7929 psa_sign_hash_interruptible_operation_t sign_operation =
7930 psa_sign_hash_interruptible_operation_init();
7931 psa_verify_hash_interruptible_operation_t verify_operation =
7932 psa_verify_hash_interruptible_operation_init();
7933
7934 PSA_ASSERT(psa_crypto_init());
7935
7936 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7937 PSA_KEY_USAGE_VERIFY_HASH);
7938 psa_set_key_algorithm(&attributes, alg);
7939 psa_set_key_type(&attributes, key_type);
7940
7941 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7942 &key));
7943 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7944 key_bits = psa_get_key_bits(&attributes);
7945
7946 /* Allocate a buffer which has the size advertised by the
7947 * library. */
7948 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7949 key_bits, alg);
7950 TEST_ASSERT(signature_size != 0);
7951 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007952 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007953
Paul Elliott20a36062022-12-18 13:21:25 +00007954 /* --- Change function inputs mid run, to cause an error (sign only,
7955 * verify passes all inputs to start. --- */
7956
7957 psa_interruptible_set_max_ops(1);
7958
7959 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7960 input_data->x, input_data->len));
7961
7962 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7963 signature_size,
7964 &signature_length),
7965 PSA_OPERATION_INCOMPLETE);
7966
7967 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7968 0,
7969 &signature_length),
7970 PSA_ERROR_BUFFER_TOO_SMALL);
7971
Paul Elliottc9774412023-02-06 15:14:07 +00007972 /* And test that this invalidates the operation. */
7973 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7974 0,
7975 &signature_length),
7976 PSA_ERROR_BAD_STATE);
7977
Paul Elliott20a36062022-12-18 13:21:25 +00007978 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7979
Paul Elliottf9c91a72023-02-05 18:06:38 +00007980 /* Trash the hash buffer in between start and complete, to ensure
7981 * no reliance on external buffers. */
7982 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7983
Paul Elliott6c68df42023-10-23 15:33:37 +01007984 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007985
7986 memcpy(input_buffer, input_data->x, input_data->len);
7987
7988 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7989 input_buffer, input_data->len));
7990
7991 memset(input_buffer, '!', input_data->len);
7992 mbedtls_free(input_buffer);
7993 input_buffer = NULL;
7994
7995 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7996 signature_size,
7997 &signature_length));
7998
7999 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8000
Paul Elliott6c68df42023-10-23 15:33:37 +01008001 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00008002
8003 memcpy(input_buffer, input_data->x, input_data->len);
8004
8005 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8006 input_buffer, input_data->len,
8007 signature, signature_length));
8008
8009 memset(input_buffer, '!', input_data->len);
8010 mbedtls_free(input_buffer);
8011 input_buffer = NULL;
8012
8013 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
8014
8015 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8016
Paul Elliott20a36062022-12-18 13:21:25 +00008017exit:
8018 /*
8019 * Key attributes may have been returned by psa_get_key_attributes()
8020 * thus reset them as required.
8021 */
8022 psa_reset_key_attributes(&attributes);
8023
8024 psa_destroy_key(key);
8025 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01008026 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00008027 PSA_DONE();
8028}
8029/* END_CASE */
8030
Paul Elliotta4cb9092023-02-07 18:01:55 +00008031/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00008032/**
Paul Elliott57702242023-02-26 20:36:10 +00008033 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00008034 *
8035 * Note: This test can currently only handle ECDSA.
8036 *
8037 * 1. Test that setting max ops is reflected in both interruptible sign and
8038 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00008039 * 2. Test that changing the value of max_ops to unlimited during an operation
8040 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00008041 *
8042 * 3. Test that calling get_num_ops() between complete calls gives the same
8043 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00008044 */
Paul Elliott57702242023-02-26 20:36:10 +00008045void interruptible_signverify_hash_ops_tests(int key_type_arg,
8046 data_t *key_data, int alg_arg,
8047 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00008048{
8049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8050 psa_key_type_t key_type = key_type_arg;
8051 psa_algorithm_t alg = alg_arg;
8052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00008053 size_t key_bits;
8054 unsigned char *signature = NULL;
8055 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00008056 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00008057 uint32_t num_ops = 0;
8058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8059
Paul Elliotta4cb9092023-02-07 18:01:55 +00008060 psa_sign_hash_interruptible_operation_t sign_operation =
8061 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00008062 psa_verify_hash_interruptible_operation_t verify_operation =
8063 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00008064
8065 PSA_ASSERT(psa_crypto_init());
8066
8067 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
8068 PSA_KEY_USAGE_VERIFY_HASH);
8069 psa_set_key_algorithm(&attributes, alg);
8070 psa_set_key_type(&attributes, key_type);
8071
Paul Elliottf1743e22023-02-15 18:44:16 +00008072 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
8073 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8074 key_bits = psa_get_key_bits(&attributes);
8075
8076 /* Allocate a buffer which has the size advertised by the
8077 * library. */
8078 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8079
8080 TEST_ASSERT(signature_size != 0);
8081 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008082 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008083
8084 /* Check that default max ops gets set if we don't set it. */
8085 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8086 input_data->x, input_data->len));
8087
8088 TEST_EQUAL(psa_interruptible_get_max_ops(),
8089 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8090
8091 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8092
Paul Elliottf1743e22023-02-15 18:44:16 +00008093 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8094 input_data->x, input_data->len,
8095 signature, signature_size));
8096
8097 TEST_EQUAL(psa_interruptible_get_max_ops(),
8098 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8099
8100 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8101
Paul Elliotta4cb9092023-02-07 18:01:55 +00008102 /* Check that max ops gets set properly. */
8103
8104 psa_interruptible_set_max_ops(0xbeef);
8105
Paul Elliottf1743e22023-02-15 18:44:16 +00008106 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008107
Paul Elliott9e8819f2023-02-26 19:01:35 +00008108 /* --- Ensure changing the max ops mid operation works (operation should
8109 * complete successfully after setting max ops to unlimited --- */
8110 psa_interruptible_set_max_ops(1);
8111
8112 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8113 input_data->x, input_data->len));
8114
8115 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
8116 signature_size,
8117 &signature_length),
8118 PSA_OPERATION_INCOMPLETE);
8119
8120 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8121
8122 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
8123 signature_size,
8124 &signature_length));
8125
8126 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8127
8128 psa_interruptible_set_max_ops(1);
8129
8130 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8131 input_data->x, input_data->len,
8132 signature, signature_length));
8133
8134 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
8135 PSA_OPERATION_INCOMPLETE);
8136
8137 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
8138
8139 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
8140
8141 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8142
Paul Elliottc1e04002023-02-26 20:27:23 +00008143 /* --- Test that not calling get_num_ops inbetween complete calls does not
8144 * result in lost ops. ---*/
8145
8146 psa_interruptible_set_max_ops(1);
8147
8148 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8149 input_data->x, input_data->len));
8150
8151 /* Continue performing the signature until complete. */
8152 do {
8153 status = psa_sign_hash_complete(&sign_operation, signature,
8154 signature_size,
8155 &signature_length);
8156
8157 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
8158
8159 } while (status == PSA_OPERATION_INCOMPLETE);
8160
8161 PSA_ASSERT(status);
8162
8163 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8164
8165 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
8166 input_data->x, input_data->len));
8167
8168 /* Continue performing the signature until complete. */
8169 do {
8170 status = psa_sign_hash_complete(&sign_operation, signature,
8171 signature_size,
8172 &signature_length);
8173 } while (status == PSA_OPERATION_INCOMPLETE);
8174
8175 PSA_ASSERT(status);
8176
8177 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
8178
8179 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
8180
8181 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8182 input_data->x, input_data->len,
8183 signature, signature_length));
8184
8185 /* Continue performing the verification until complete. */
8186 do {
8187 status = psa_verify_hash_complete(&verify_operation);
8188
8189 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
8190
8191 } while (status == PSA_OPERATION_INCOMPLETE);
8192
8193 PSA_ASSERT(status);
8194
8195 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8196
8197 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
8198 input_data->x, input_data->len,
8199 signature, signature_length));
8200
8201 /* Continue performing the verification until complete. */
8202 do {
8203 status = psa_verify_hash_complete(&verify_operation);
8204
8205 } while (status == PSA_OPERATION_INCOMPLETE);
8206
8207 PSA_ASSERT(status);
8208
8209 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
8210
8211 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
8212
Paul Elliotta4cb9092023-02-07 18:01:55 +00008213exit:
8214 /*
8215 * Key attributes may have been returned by psa_get_key_attributes()
8216 * thus reset them as required.
8217 */
8218 psa_reset_key_attributes(&attributes);
8219
8220 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00008221 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00008222 PSA_DONE();
8223}
8224/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00008225
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008226/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008227void sign_message_deterministic(int key_type_arg,
8228 data_t *key_data,
8229 int alg_arg,
8230 data_t *input_data,
8231 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02008232{
8233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8234 psa_key_type_t key_type = key_type_arg;
8235 psa_algorithm_t alg = alg_arg;
8236 size_t key_bits;
8237 unsigned char *signature = NULL;
8238 size_t signature_size;
8239 size_t signature_length = 0xdeadbeef;
8240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8241
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02008243
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8245 psa_set_key_algorithm(&attributes, alg);
8246 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008247
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8249 &key));
8250 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8251 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008252
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8254 TEST_ASSERT(signature_size != 0);
8255 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008256 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008257
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 PSA_ASSERT(psa_sign_message(key, alg,
8259 input_data->x, input_data->len,
8260 signature, signature_size,
8261 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02008262
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008263 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008264 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008265
8266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008268
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 psa_destroy_key(key);
8270 mbedtls_free(signature);
8271 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02008272
8273}
8274/* END_CASE */
8275
8276/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008277void sign_message_fail(int key_type_arg,
8278 data_t *key_data,
8279 int alg_arg,
8280 data_t *input_data,
8281 int signature_size_arg,
8282 int expected_status_arg)
8283{
8284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8285 psa_key_type_t key_type = key_type_arg;
8286 psa_algorithm_t alg = alg_arg;
8287 size_t signature_size = signature_size_arg;
8288 psa_status_t actual_status;
8289 psa_status_t expected_status = expected_status_arg;
8290 unsigned char *signature = NULL;
8291 size_t signature_length = 0xdeadbeef;
8292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8293
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008294 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008295
8296 PSA_ASSERT(psa_crypto_init());
8297
8298 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8299 psa_set_key_algorithm(&attributes, alg);
8300 psa_set_key_type(&attributes, key_type);
8301
8302 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8303 &key));
8304
8305 actual_status = psa_sign_message(key, alg,
8306 input_data->x, input_data->len,
8307 signature, signature_size,
8308 &signature_length);
8309 TEST_EQUAL(actual_status, expected_status);
8310 /* The value of *signature_length is unspecified on error, but
8311 * whatever it is, it should be less than signature_size, so that
8312 * if the caller tries to read *signature_length bytes without
8313 * checking the error code then they don't overflow a buffer. */
8314 TEST_LE_U(signature_length, signature_size);
8315
8316exit:
8317 psa_reset_key_attributes(&attributes);
8318 psa_destroy_key(key);
8319 mbedtls_free(signature);
8320 PSA_DONE();
8321}
8322/* END_CASE */
8323
8324/* BEGIN_CASE */
8325void sign_verify_message(int key_type_arg,
8326 data_t *key_data,
8327 int alg_arg,
8328 data_t *input_data)
8329{
8330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8331 psa_key_type_t key_type = key_type_arg;
8332 psa_algorithm_t alg = alg_arg;
8333 size_t key_bits;
8334 unsigned char *signature = NULL;
8335 size_t signature_size;
8336 size_t signature_length = 0xdeadbeef;
8337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8338
8339 PSA_ASSERT(psa_crypto_init());
8340
8341 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8342 PSA_KEY_USAGE_VERIFY_MESSAGE);
8343 psa_set_key_algorithm(&attributes, alg);
8344 psa_set_key_type(&attributes, key_type);
8345
8346 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8347 &key));
8348 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8349 key_bits = psa_get_key_bits(&attributes);
8350
8351 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8352 TEST_ASSERT(signature_size != 0);
8353 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008354 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008355
8356 PSA_ASSERT(psa_sign_message(key, alg,
8357 input_data->x, input_data->len,
8358 signature, signature_size,
8359 &signature_length));
8360 TEST_LE_U(signature_length, signature_size);
8361 TEST_ASSERT(signature_length > 0);
8362
8363 PSA_ASSERT(psa_verify_message(key, alg,
8364 input_data->x, input_data->len,
8365 signature, signature_length));
8366
8367 if (input_data->len != 0) {
8368 /* Flip a bit in the input and verify that the signature is now
8369 * detected as invalid. Flip a bit at the beginning, not at the end,
8370 * because ECDSA may ignore the last few bits of the input. */
8371 input_data->x[0] ^= 1;
8372 TEST_EQUAL(psa_verify_message(key, alg,
8373 input_data->x, input_data->len,
8374 signature, signature_length),
8375 PSA_ERROR_INVALID_SIGNATURE);
8376 }
8377
8378exit:
8379 psa_reset_key_attributes(&attributes);
8380
8381 psa_destroy_key(key);
8382 mbedtls_free(signature);
8383 PSA_DONE();
8384}
8385/* END_CASE */
8386
8387/* BEGIN_CASE */
8388void verify_message(int key_type_arg,
8389 data_t *key_data,
8390 int alg_arg,
8391 data_t *input_data,
8392 data_t *signature_data)
8393{
8394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8395 psa_key_type_t key_type = key_type_arg;
8396 psa_algorithm_t alg = alg_arg;
8397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8398
8399 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8400
8401 PSA_ASSERT(psa_crypto_init());
8402
8403 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8404 psa_set_key_algorithm(&attributes, alg);
8405 psa_set_key_type(&attributes, key_type);
8406
8407 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8408 &key));
8409
8410 PSA_ASSERT(psa_verify_message(key, alg,
8411 input_data->x, input_data->len,
8412 signature_data->x, signature_data->len));
8413
8414exit:
8415 psa_reset_key_attributes(&attributes);
8416 psa_destroy_key(key);
8417 PSA_DONE();
8418}
8419/* END_CASE */
8420
8421/* BEGIN_CASE */
8422void verify_message_fail(int key_type_arg,
8423 data_t *key_data,
8424 int alg_arg,
8425 data_t *hash_data,
8426 data_t *signature_data,
8427 int expected_status_arg)
8428{
8429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8430 psa_key_type_t key_type = key_type_arg;
8431 psa_algorithm_t alg = alg_arg;
8432 psa_status_t actual_status;
8433 psa_status_t expected_status = expected_status_arg;
8434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8435
8436 PSA_ASSERT(psa_crypto_init());
8437
8438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8439 psa_set_key_algorithm(&attributes, alg);
8440 psa_set_key_type(&attributes, key_type);
8441
8442 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8443 &key));
8444
8445 actual_status = psa_verify_message(key, alg,
8446 hash_data->x, hash_data->len,
8447 signature_data->x,
8448 signature_data->len);
8449 TEST_EQUAL(actual_status, expected_status);
8450
8451exit:
8452 psa_reset_key_attributes(&attributes);
8453 psa_destroy_key(key);
8454 PSA_DONE();
8455}
8456/* END_CASE */
8457
8458/* BEGIN_CASE */
8459void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008460 data_t *key_data,
8461 int alg_arg,
8462 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 data_t *label,
8464 int expected_output_length_arg,
8465 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008466{
Ronald Cron5425a212020-08-04 14:58:35 +02008467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008468 psa_key_type_t key_type = key_type_arg;
8469 psa_algorithm_t alg = alg_arg;
8470 size_t expected_output_length = expected_output_length_arg;
8471 size_t key_bits;
8472 unsigned char *output = NULL;
8473 size_t output_size;
8474 size_t output_length = ~0;
8475 psa_status_t actual_status;
8476 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008478
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008480
Gilles Peskine656896e2018-06-29 19:12:28 +02008481 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8483 psa_set_key_algorithm(&attributes, alg);
8484 psa_set_key_type(&attributes, key_type);
8485 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8486 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008487
8488 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8490 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008491
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8493 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008494 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008495
8496 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 actual_status = psa_asymmetric_encrypt(key, alg,
8498 input_data->x, input_data->len,
8499 label->x, label->len,
8500 output, output_size,
8501 &output_length);
8502 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008503 if (actual_status == PSA_SUCCESS) {
8504 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008505 } else {
8506 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008507 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008508
Gilles Peskine68428122018-06-30 18:42:41 +02008509 /* If the label is empty, the test framework puts a non-null pointer
8510 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008512 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 if (output_size != 0) {
8514 memset(output, 0, output_size);
8515 }
8516 actual_status = psa_asymmetric_encrypt(key, alg,
8517 input_data->x, input_data->len,
8518 NULL, label->len,
8519 output, output_size,
8520 &output_length);
8521 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008522 if (actual_status == PSA_SUCCESS) {
8523 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008524 } else {
8525 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008526 }
Gilles Peskine68428122018-06-30 18:42:41 +02008527 }
8528
Gilles Peskine656896e2018-06-29 19:12:28 +02008529exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008530 /*
8531 * Key attributes may have been returned by psa_get_key_attributes()
8532 * thus reset them as required.
8533 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008535
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 psa_destroy_key(key);
8537 mbedtls_free(output);
8538 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008539}
8540/* END_CASE */
8541
8542/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008543void asymmetric_encrypt_decrypt(int key_type_arg,
8544 data_t *key_data,
8545 int alg_arg,
8546 data_t *input_data,
8547 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008548{
Ronald Cron5425a212020-08-04 14:58:35 +02008549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008550 psa_key_type_t key_type = key_type_arg;
8551 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008552 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008553 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008554 size_t output_size;
8555 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008556 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008557 size_t output2_size;
8558 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008560
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008562
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8564 psa_set_key_algorithm(&attributes, alg);
8565 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008566
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8568 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008569
8570 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8572 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008573
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8575 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008576 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008577
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008578 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 TEST_LE_U(output2_size,
8580 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8581 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008582 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008583
Gilles Peskineeebd7382018-06-08 18:11:54 +02008584 /* We test encryption by checking that encrypt-then-decrypt gives back
8585 * the original plaintext because of the non-optional random
8586 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8588 input_data->x, input_data->len,
8589 label->x, label->len,
8590 output, output_size,
8591 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008592 /* We don't know what ciphertext length to expect, but check that
8593 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008595
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8597 output, output_length,
8598 label->x, label->len,
8599 output2, output2_size,
8600 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008601 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008602 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008603
8604exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008605 /*
8606 * Key attributes may have been returned by psa_get_key_attributes()
8607 * thus reset them as required.
8608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008610
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 psa_destroy_key(key);
8612 mbedtls_free(output);
8613 mbedtls_free(output2);
8614 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008615}
8616/* END_CASE */
8617
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008618/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008619void asymmetric_decrypt(int key_type_arg,
8620 data_t *key_data,
8621 int alg_arg,
8622 data_t *input_data,
8623 data_t *label,
8624 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008625{
Ronald Cron5425a212020-08-04 14:58:35 +02008626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008627 psa_key_type_t key_type = key_type_arg;
8628 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008629 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008630 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008631 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008632 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008634
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8638 psa_set_key_algorithm(&attributes, alg);
8639 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008640
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8642 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008643
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8645 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008646
8647 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8649 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008650 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008651
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8653 input_data->x, input_data->len,
8654 label->x, label->len,
8655 output,
8656 output_size,
8657 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008658 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008659 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008660
Gilles Peskine68428122018-06-30 18:42:41 +02008661 /* If the label is empty, the test framework puts a non-null pointer
8662 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008664 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 if (output_size != 0) {
8666 memset(output, 0, output_size);
8667 }
8668 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8669 input_data->x, input_data->len,
8670 NULL, label->len,
8671 output,
8672 output_size,
8673 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008674 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008675 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008676 }
8677
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008678exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 psa_reset_key_attributes(&attributes);
8680 psa_destroy_key(key);
8681 mbedtls_free(output);
8682 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008683}
8684/* END_CASE */
8685
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008686/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008687void asymmetric_decrypt_fail(int key_type_arg,
8688 data_t *key_data,
8689 int alg_arg,
8690 data_t *input_data,
8691 data_t *label,
8692 int output_size_arg,
8693 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008694{
Ronald Cron5425a212020-08-04 14:58:35 +02008695 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008696 psa_key_type_t key_type = key_type_arg;
8697 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008698 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008699 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008700 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008701 psa_status_t actual_status;
8702 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008704
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008705 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008706
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008708
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8710 psa_set_key_algorithm(&attributes, alg);
8711 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008712
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8714 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 actual_status = psa_asymmetric_decrypt(key, alg,
8717 input_data->x, input_data->len,
8718 label->x, label->len,
8719 output, output_size,
8720 &output_length);
8721 TEST_EQUAL(actual_status, expected_status);
8722 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008723
Gilles Peskine68428122018-06-30 18:42:41 +02008724 /* If the label is empty, the test framework puts a non-null pointer
8725 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008727 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 if (output_size != 0) {
8729 memset(output, 0, output_size);
8730 }
8731 actual_status = psa_asymmetric_decrypt(key, alg,
8732 input_data->x, input_data->len,
8733 NULL, label->len,
8734 output, output_size,
8735 &output_length);
8736 TEST_EQUAL(actual_status, expected_status);
8737 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008738 }
8739
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008740exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 psa_reset_key_attributes(&attributes);
8742 psa_destroy_key(key);
8743 mbedtls_free(output);
8744 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008745}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008746/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008747
8748/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008749void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008750{
8751 /* Test each valid way of initializing the object, except for `= {0}`, as
8752 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8753 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008754 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008755 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008757 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8758 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008761
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008762 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8764 PSA_ERROR_BAD_STATE);
8765 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8766 PSA_ERROR_BAD_STATE);
8767 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8768 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008769
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008770 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 PSA_ASSERT(psa_key_derivation_abort(&func));
8772 PSA_ASSERT(psa_key_derivation_abort(&init));
8773 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008774}
8775/* END_CASE */
8776
Janos Follath16de4a42019-06-13 16:32:24 +01008777/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008778void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008779{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008780 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008781 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008782 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008783
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008785
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8787 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008788
8789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 psa_key_derivation_abort(&operation);
8791 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008792}
8793/* END_CASE */
8794
Janos Follathaf3c2a02019-06-12 12:34:34 +01008795/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308796void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008798{
8799 psa_algorithm_t alg = alg_arg;
8800 size_t capacity = capacity_arg;
8801 psa_status_t expected_status = expected_status_arg;
8802 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8803
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008805
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008807
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8809 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008810
8811exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 psa_key_derivation_abort(&operation);
8813 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008814}
8815/* END_CASE */
8816
8817/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308818void parse_binary_string_test(data_t *input, int output)
8819{
8820 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308821 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308822 TEST_EQUAL(value, output);
8823}
8824/* END_CASE */
8825
8826/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008827void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308828 int step_arg1, int key_type_arg1, data_t *input1,
8829 int expected_status_arg1,
8830 int step_arg2, int key_type_arg2, data_t *input2,
8831 int expected_status_arg2,
8832 int step_arg3, int key_type_arg3, data_t *input3,
8833 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008835{
8836 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308838 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 psa_status_t expected_statuses[] = { expected_status_arg1,
8840 expected_status_arg2,
8841 expected_status_arg3 };
8842 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008843 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8844 MBEDTLS_SVC_KEY_ID_INIT,
8845 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008846 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8848 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008849 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008850 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008851 psa_status_t expected_output_status = expected_output_status_arg;
8852 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008853
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008855
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8857 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8862 mbedtls_test_set_step(i);
8863 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008864 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308865 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8866 key_types[i] != INPUT_INTEGER) {
8867 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 PSA_ASSERT(psa_import_key(&attributes,
8869 inputs[i]->x, inputs[i]->len,
8870 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308871 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008873 // When taking a private key as secret input, use key agreement
8874 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
Ryan Everett73e4ea32024-03-12 16:29:55 +00008876 &operation, keys[i], 0),
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 expected_statuses[i]);
8878 } else {
8879 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8880 keys[i]),
8881 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008882 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308884 if (key_types[i] == INPUT_INTEGER) {
8885 TEST_EQUAL(psa_key_derivation_input_integer(
8886 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308887 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308888 expected_statuses[i]);
8889 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308890 TEST_EQUAL(psa_key_derivation_input_bytes(
8891 &operation, steps[i],
8892 inputs[i]->x, inputs[i]->len),
8893 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308894 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008895 }
8896 }
8897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 if (output_key_type != PSA_KEY_TYPE_NONE) {
8899 psa_reset_key_attributes(&attributes);
8900 psa_set_key_type(&attributes, output_key_type);
8901 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008902 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 psa_key_derivation_output_key(&attributes, &operation,
8904 &output_key);
8905 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008906 uint8_t buffer[1];
8907 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 psa_key_derivation_output_bytes(&operation,
8909 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008910 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008912
Janos Follathaf3c2a02019-06-12 12:34:34 +01008913exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 psa_key_derivation_abort(&operation);
8915 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8916 psa_destroy_key(keys[i]);
8917 }
8918 psa_destroy_key(output_key);
8919 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008920}
8921/* END_CASE */
8922
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308923/* BEGIN_CASE*/
8924void derive_input_invalid_cost(int alg_arg, int64_t cost)
8925{
8926 psa_algorithm_t alg = alg_arg;
8927 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8928
8929 PSA_ASSERT(psa_crypto_init());
8930 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8931
8932 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8933 PSA_KEY_DERIVATION_INPUT_COST,
8934 cost),
8935 PSA_ERROR_NOT_SUPPORTED);
8936
8937exit:
8938 psa_key_derivation_abort(&operation);
8939 PSA_DONE();
8940}
8941/* END_CASE*/
8942
Janos Follathd958bb72019-07-03 15:02:16 +01008943/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008944void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008945{
Janos Follathd958bb72019-07-03 15:02:16 +01008946 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008948 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008949 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008950 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008952 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008954 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008956 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8957 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8964 psa_set_key_algorithm(&attributes, alg);
8965 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008966
Gilles Peskine449bd832023-01-11 14:50:10 +01008967 PSA_ASSERT(psa_import_key(&attributes,
8968 key_data, sizeof(key_data),
8969 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008970
8971 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8973 input1, input1_length,
8974 input2, input2_length,
Ryan Everettc1cc6682024-03-12 16:17:43 +00008975 capacity, 0)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008976 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008978
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008979 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8981 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008982
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008984
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8986 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008987
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 psa_key_derivation_abort(&operation);
8990 psa_destroy_key(key);
8991 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008992}
8993/* END_CASE */
8994
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008995/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008996void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008997{
8998 uint8_t output_buffer[16];
8999 size_t buffer_size = 16;
9000 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009001 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009002
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
9004 output_buffer, buffer_size)
9005 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009006
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
9008 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009009
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009011
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
9013 output_buffer, buffer_size)
9014 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009015
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
9017 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03009018
9019exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03009021}
9022/* END_CASE */
9023
9024/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009025void derive_output(int alg_arg,
9026 int step1_arg, data_t *input1, int expected_status_arg1,
9027 int step2_arg, data_t *input2, int expected_status_arg2,
9028 int step3_arg, data_t *input3, int expected_status_arg3,
9029 int step4_arg, data_t *input4, int expected_status_arg4,
9030 data_t *key_agreement_peer_key,
9031 int requested_capacity_arg,
9032 data_t *expected_output1,
9033 data_t *expected_output2,
9034 int other_key_input_type,
9035 int key_input_type,
9036 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009037{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009038 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
9040 data_t *inputs[] = { input1, input2, input3, input4 };
9041 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
9042 MBEDTLS_SVC_KEY_ID_INIT,
9043 MBEDTLS_SVC_KEY_ID_INIT,
9044 MBEDTLS_SVC_KEY_ID_INIT };
9045 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
9046 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009047 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009048 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009049 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009051 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009053 size_t output_buffer_size = 0;
9054 uint8_t *output_buffer = NULL;
9055 size_t expected_capacity;
9056 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009057 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
9058 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
9059 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
9060 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009061 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009062 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02009063 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009064
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
9066 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009067 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 }
9069 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009070 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009072 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009073 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009075
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009076 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9078 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
9079 requested_capacity));
9080 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
9081 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02009082 case 0:
9083 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309084 case PSA_KEY_DERIVATION_INPUT_COST:
9085 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05309086 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05309087 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05309088 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309089 if (statuses[i] != PSA_SUCCESS) {
9090 goto exit;
9091 }
9092 break;
9093 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02009094 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009096 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 TEST_EQUAL(psa_key_derivation_input_bytes(
9098 &operation, steps[i],
9099 inputs[i]->x, inputs[i]->len),
9100 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02009101
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02009103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009105 break;
9106 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
9108 psa_set_key_algorithm(&attributes1, alg);
9109 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009110
Gilles Peskine449bd832023-01-11 14:50:10 +01009111 PSA_ASSERT(psa_import_key(&attributes1,
9112 inputs[i]->x, inputs[i]->len,
9113 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009114
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
9116 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
9117 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
9118 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009119 }
9120
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309121 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01009122 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05309123 keys[i]),
9124 statuses[i]);
9125
9126 if (statuses[i] != PSA_SUCCESS) {
9127 goto exit;
9128 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009129 break;
9130 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009131 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009132 break;
9133 }
9134 break;
9135 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009137 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9139 steps[i],
9140 inputs[i]->x,
9141 inputs[i]->len),
9142 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009143 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02009144 case 1: // input key, type DERIVE
9145 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
9147 psa_set_key_algorithm(&attributes2, alg);
9148 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009149
9150 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 if (other_key_input_type == 11) {
9152 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
9153 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 PSA_ASSERT(psa_import_key(&attributes2,
9156 inputs[i]->x, inputs[i]->len,
9157 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009158
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 TEST_EQUAL(psa_key_derivation_input_key(&operation,
9160 steps[i],
9161 keys[i]),
9162 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009163 break;
9164 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
9166 psa_set_key_algorithm(&attributes3, alg);
9167 psa_set_key_type(&attributes3,
9168 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009169
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 PSA_ASSERT(psa_import_key(&attributes3,
9171 inputs[i]->x, inputs[i]->len,
9172 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 TEST_EQUAL(psa_key_derivation_key_agreement(
9175 &operation,
9176 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
9177 keys[i], key_agreement_peer_key->x,
9178 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009179 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009180 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009181 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009182 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01009183 }
9184
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009188 break;
9189 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 TEST_EQUAL(psa_key_derivation_input_bytes(
9191 &operation, steps[i],
9192 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02009193
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02009195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009197 break;
9198 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01009199 }
Gilles Peskine1468da72019-05-29 17:35:49 +02009200
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9202 &current_capacity));
9203 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009204 expected_capacity = requested_capacity;
9205
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009207 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
9208
9209 /* For output key derivation secret must be provided using
9210 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02009212 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009214
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
9216 psa_set_key_algorithm(&attributes4, alg);
9217 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
9218 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
9221 &derived_key), expected_status);
9222 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009223 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009225 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 status = psa_key_derivation_output_bytes(&operation,
9227 output_buffer, output_sizes[i]);
9228 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009229 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 TEST_ASSERT(status == PSA_SUCCESS ||
9231 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009232 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 } else if (expected_capacity == 0 ||
9234 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009235 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009237 expected_capacity = 0;
9238 continue;
9239 }
9240 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 PSA_ASSERT(status);
9242 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009243 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009244 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02009246 /* Check the operation status. */
9247 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9249 &current_capacity));
9250 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009251 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009252 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009254
9255exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 mbedtls_free(output_buffer);
9257 psa_key_derivation_abort(&operation);
9258 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
9259 psa_destroy_key(keys[i]);
9260 }
9261 psa_destroy_key(derived_key);
9262 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009263}
9264/* END_CASE */
9265
9266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009267void derive_full(int alg_arg,
9268 data_t *key_data,
9269 data_t *input1,
9270 data_t *input2,
9271 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02009272{
Ronald Cron5425a212020-08-04 14:58:35 +02009273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009274 psa_algorithm_t alg = alg_arg;
9275 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009276 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05309277 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02009278 size_t expected_capacity = requested_capacity;
9279 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009281
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02009283
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9285 psa_set_key_algorithm(&attributes, alg);
9286 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009287
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9289 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009290
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9292 input1->x, input1->len,
9293 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009294 requested_capacity, 0)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009297
Gilles Peskine449bd832023-01-11 14:50:10 +01009298 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9299 &current_capacity));
9300 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009301
9302 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 while (current_capacity > 0) {
9304 size_t read_size = sizeof(output_buffer);
9305 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009306 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009307 }
9308 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9309 output_buffer,
9310 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009311 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9313 &current_capacity));
9314 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009315 }
9316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009317 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9319 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009320
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009322
9323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 psa_key_derivation_abort(&operation);
9325 psa_destroy_key(key);
9326 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009327}
9328/* END_CASE */
9329
Stephan Koch78109f52023-04-12 14:19:36 +02009330/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009331void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9332 int derivation_step,
9333 int capacity, int expected_capacity_status_arg,
9334 data_t *expected_output,
9335 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009336{
9337 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9338 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009339 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009340 uint8_t *output_buffer = NULL;
9341 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009342 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9343 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9344 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009345
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009346 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009347 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009348
Gilles Peskine449bd832023-01-11 14:50:10 +01009349 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9350 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9351 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009352
Gilles Peskine449bd832023-01-11 14:50:10 +01009353 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9354 step, input->x, input->len),
9355 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009356
Gilles Peskine449bd832023-01-11 14:50:10 +01009357 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009360
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9362 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009363
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 TEST_EQUAL(status, expected_output_status);
9365 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009366 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009367 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009369
9370exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 mbedtls_free(output_buffer);
9372 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009373 PSA_DONE();
9374}
9375/* END_CASE */
9376
Janos Follathe60c9052019-07-03 13:51:30 +01009377/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009378void derive_key_exercise(int alg_arg,
9379 data_t *key_data,
9380 data_t *input1,
9381 data_t *input2,
9382 int derived_type_arg,
9383 int derived_bits_arg,
9384 int derived_usage_arg,
9385 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009386{
Ronald Cron5425a212020-08-04 14:58:35 +02009387 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9388 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009389 psa_algorithm_t alg = alg_arg;
9390 psa_key_type_t derived_type = derived_type_arg;
9391 size_t derived_bits = derived_bits_arg;
9392 psa_key_usage_t derived_usage = derived_usage_arg;
9393 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009394 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009395 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009397 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009398
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009400
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9402 psa_set_key_algorithm(&attributes, alg);
9403 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9404 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9405 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009406
9407 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9409 input1->x, input1->len,
9410 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009411 capacity, 0)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009412 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 }
Janos Follathe60c9052019-07-03 13:51:30 +01009414
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 psa_set_key_usage_flags(&attributes, derived_usage);
9416 psa_set_key_algorithm(&attributes, derived_alg);
9417 psa_set_key_type(&attributes, derived_type);
9418 psa_set_key_bits(&attributes, derived_bits);
9419 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9420 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009421
9422 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009423 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9424 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9425 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009426
9427 /* Exercise the derived key. */
Ryan Everett0a271fd2024-03-12 16:34:02 +00009428 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg, 0)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009430 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009431
9432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009433 /*
9434 * Key attributes may have been returned by psa_get_key_attributes()
9435 * thus reset them as required.
9436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009437 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009438
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 psa_key_derivation_abort(&operation);
9440 psa_destroy_key(base_key);
9441 psa_destroy_key(derived_key);
9442 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009443}
9444/* END_CASE */
9445
Janos Follath42fd8882019-07-03 14:17:09 +01009446/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009447void derive_key_export(int alg_arg,
9448 data_t *key_data,
9449 data_t *input1,
9450 data_t *input2,
9451 int bytes1_arg,
9452 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009453{
Ronald Cron5425a212020-08-04 14:58:35 +02009454 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9455 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009456 psa_algorithm_t alg = alg_arg;
9457 size_t bytes1 = bytes1_arg;
9458 size_t bytes2 = bytes2_arg;
9459 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009460 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009461 uint8_t *output_buffer = NULL;
9462 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009463 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9464 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009465 size_t length;
9466
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009467 TEST_CALLOC(output_buffer, capacity);
9468 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9472 psa_set_key_algorithm(&base_attributes, alg);
9473 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9474 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9475 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009476
9477 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009478 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9479 input1->x, input1->len,
9480 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009481 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009482 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 }
Janos Follath42fd8882019-07-03 14:17:09 +01009484
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9486 output_buffer,
9487 capacity));
9488 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009489
9490 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9492 input1->x, input1->len,
9493 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009494 capacity, 0)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009495 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 }
Janos Follath42fd8882019-07-03 14:17:09 +01009497
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9499 psa_set_key_algorithm(&derived_attributes, 0);
9500 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9501 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9502 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9503 &derived_key));
9504 PSA_ASSERT(psa_export_key(derived_key,
9505 export_buffer, bytes1,
9506 &length));
9507 TEST_EQUAL(length, bytes1);
9508 PSA_ASSERT(psa_destroy_key(derived_key));
9509 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9510 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9511 &derived_key));
9512 PSA_ASSERT(psa_export_key(derived_key,
9513 export_buffer + bytes1, bytes2,
9514 &length));
9515 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009516
9517 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009518 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009519 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009520
9521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009522 mbedtls_free(output_buffer);
9523 mbedtls_free(export_buffer);
9524 psa_key_derivation_abort(&operation);
9525 psa_destroy_key(base_key);
9526 psa_destroy_key(derived_key);
9527 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009528}
9529/* END_CASE */
9530
9531/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009532void derive_key_type(int alg_arg,
9533 data_t *key_data,
9534 data_t *input1,
9535 data_t *input2,
9536 int key_type_arg, int bits_arg,
9537 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009538{
9539 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9540 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9541 const psa_algorithm_t alg = alg_arg;
9542 const psa_key_type_t key_type = key_type_arg;
9543 const size_t bits = bits_arg;
9544 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9545 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009547 uint8_t *export_buffer = NULL;
9548 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9549 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9550 size_t export_length;
9551
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009552 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009554
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9556 psa_set_key_algorithm(&base_attributes, alg);
9557 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9558 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9559 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009560
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009562 &operation, base_key, alg,
9563 input1->x, input1->len,
9564 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009565 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009568
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9570 psa_set_key_algorithm(&derived_attributes, 0);
9571 psa_set_key_type(&derived_attributes, key_type);
9572 psa_set_key_bits(&derived_attributes, bits);
9573 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9574 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009575
Gilles Peskine449bd832023-01-11 14:50:10 +01009576 PSA_ASSERT(psa_export_key(derived_key,
9577 export_buffer, export_buffer_size,
9578 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009579 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009580 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009581
9582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009583 mbedtls_free(export_buffer);
9584 psa_key_derivation_abort(&operation);
9585 psa_destroy_key(base_key);
9586 psa_destroy_key(derived_key);
9587 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009588}
9589/* END_CASE */
9590
9591/* BEGIN_CASE */
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009592void derive_key_ext(int alg_arg,
9593 data_t *key_data,
9594 data_t *input1,
9595 data_t *input2,
9596 int key_type_arg, int bits_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009597 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +01009598 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009599 psa_status_t expected_status,
9600 data_t *expected_export)
9601{
9602 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9603 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9604 const psa_algorithm_t alg = alg_arg;
9605 const psa_key_type_t key_type = key_type_arg;
9606 const size_t bits = bits_arg;
Gilles Peskine092ce512024-02-20 12:31:24 +01009607 psa_key_production_parameters_t *params = NULL;
9608 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009609 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9610 const size_t export_buffer_size =
9611 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
9612 uint8_t *export_buffer = NULL;
9613 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9614 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9615 size_t export_length;
9616
9617 TEST_CALLOC(export_buffer, export_buffer_size);
9618 PSA_ASSERT(psa_crypto_init());
9619
9620 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9621 psa_set_key_algorithm(&base_attributes, alg);
9622 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9623 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9624 &base_key));
9625
9626 if (mbedtls_test_psa_setup_key_derivation_wrap(
9627 &operation, base_key, alg,
9628 input1->x, input1->len,
9629 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009630 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009631 goto exit;
9632 }
9633
9634 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9635 psa_set_key_algorithm(&derived_attributes, 0);
9636 psa_set_key_type(&derived_attributes, key_type);
9637 psa_set_key_bits(&derived_attributes, bits);
Gilles Peskine092ce512024-02-20 12:31:24 +01009638 if (!setup_key_production_parameters(&params, &params_data_length,
9639 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009640 goto exit;
9641 }
9642
9643 TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01009644 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009645 &derived_key),
9646 expected_status);
9647
9648 if (expected_status == PSA_SUCCESS) {
9649 PSA_ASSERT(psa_export_key(derived_key,
9650 export_buffer, export_buffer_size,
9651 &export_length));
9652 TEST_MEMORY_COMPARE(export_buffer, export_length,
9653 expected_export->x, expected_export->len);
9654 }
9655
9656exit:
9657 mbedtls_free(export_buffer);
Gilles Peskine092ce512024-02-20 12:31:24 +01009658 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009659 psa_key_derivation_abort(&operation);
9660 psa_destroy_key(base_key);
9661 psa_destroy_key(derived_key);
9662 PSA_DONE();
9663}
9664/* END_CASE */
9665
9666/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009667void derive_key(int alg_arg,
9668 data_t *key_data, data_t *input1, data_t *input2,
9669 int type_arg, int bits_arg,
9670 int expected_status_arg,
9671 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009672{
Ronald Cron5425a212020-08-04 14:58:35 +02009673 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9674 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009675 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009676 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009677 size_t bits = bits_arg;
9678 psa_status_t expected_status = expected_status_arg;
9679 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9680 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9681 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9682
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9686 psa_set_key_algorithm(&base_attributes, alg);
9687 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9688 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9689 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009690
Gilles Peskine449bd832023-01-11 14:50:10 +01009691 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9692 input1->x, input1->len,
9693 input2->x, input2->len,
Ryan Everettc1cc6682024-03-12 16:17:43 +00009694 SIZE_MAX, 0)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009695 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009696 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009697
Gilles Peskine449bd832023-01-11 14:50:10 +01009698 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9699 psa_set_key_algorithm(&derived_attributes, 0);
9700 psa_set_key_type(&derived_attributes, type);
9701 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009702
9703 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009704 psa_key_derivation_output_key(&derived_attributes,
9705 &operation,
9706 &derived_key);
9707 if (is_large_output > 0) {
9708 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9709 }
9710 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009711
9712exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009713 psa_key_derivation_abort(&operation);
9714 psa_destroy_key(base_key);
9715 psa_destroy_key(derived_key);
9716 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009717}
9718/* END_CASE */
9719
9720/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009721void key_agreement_setup(int alg_arg,
9722 int our_key_type_arg, int our_key_alg_arg,
9723 data_t *our_key_data, data_t *peer_key_data,
9724 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009725{
Ronald Cron5425a212020-08-04 14:58:35 +02009726 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009727 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009728 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009729 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009730 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009732 psa_status_t expected_status = expected_status_arg;
9733 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009734
Gilles Peskine449bd832023-01-11 14:50:10 +01009735 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009736
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9738 psa_set_key_algorithm(&attributes, our_key_alg);
9739 psa_set_key_type(&attributes, our_key_type);
9740 PSA_ASSERT(psa_import_key(&attributes,
9741 our_key_data->x, our_key_data->len,
9742 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009743
Gilles Peskine77f40d82019-04-11 21:27:06 +02009744 /* The tests currently include inputs that should fail at either step.
9745 * Test cases that fail at the setup step should be changed to call
9746 * key_derivation_setup instead, and this function should be renamed
9747 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009748 status = psa_key_derivation_setup(&operation, alg);
9749 if (status == PSA_SUCCESS) {
9750 TEST_EQUAL(psa_key_derivation_key_agreement(
9751 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9752 our_key,
9753 peer_key_data->x, peer_key_data->len),
9754 expected_status);
9755 } else {
9756 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009757 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009758
9759exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009760 psa_key_derivation_abort(&operation);
9761 psa_destroy_key(our_key);
9762 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009763}
9764/* END_CASE */
9765
9766/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009767void raw_key_agreement(int alg_arg,
9768 int our_key_type_arg, data_t *our_key_data,
9769 data_t *peer_key_data,
9770 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009771{
Ronald Cron5425a212020-08-04 14:58:35 +02009772 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009773 psa_algorithm_t alg = alg_arg;
9774 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009776 unsigned char *output = NULL;
9777 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009778 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009779
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009781
Gilles Peskine449bd832023-01-11 14:50:10 +01009782 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9783 psa_set_key_algorithm(&attributes, alg);
9784 psa_set_key_type(&attributes, our_key_type);
9785 PSA_ASSERT(psa_import_key(&attributes,
9786 our_key_data->x, our_key_data->len,
9787 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009788
Gilles Peskine449bd832023-01-11 14:50:10 +01009789 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9790 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009791
Gilles Peskine992bee82022-04-13 23:25:52 +02009792 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009793 TEST_LE_U(expected_output->len,
9794 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9795 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9796 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009797
9798 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009799 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009800 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9801 peer_key_data->x, peer_key_data->len,
9802 output, expected_output->len,
9803 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009804 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009805 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009806 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009807 output = NULL;
9808 output_length = ~0;
9809
9810 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009811 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009812 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9813 peer_key_data->x, peer_key_data->len,
9814 output, expected_output->len + 1,
9815 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009816 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009817 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009818 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009819 output = NULL;
9820 output_length = ~0;
9821
9822 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009823 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009824 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9825 peer_key_data->x, peer_key_data->len,
9826 output, expected_output->len - 1,
9827 &output_length),
9828 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009829 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009830 TEST_LE_U(output_length, expected_output->len - 1);
9831 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009832 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009833
9834exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 mbedtls_free(output);
9836 psa_destroy_key(our_key);
9837 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009838}
9839/* END_CASE */
9840
9841/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009842void key_agreement_capacity(int alg_arg,
9843 int our_key_type_arg, data_t *our_key_data,
9844 data_t *peer_key_data,
9845 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009846{
Ronald Cron5425a212020-08-04 14:58:35 +02009847 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009848 psa_algorithm_t alg = alg_arg;
9849 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009850 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009852 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009853 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009854
Gilles Peskine449bd832023-01-11 14:50:10 +01009855 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009856
Gilles Peskine449bd832023-01-11 14:50:10 +01009857 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9858 psa_set_key_algorithm(&attributes, alg);
9859 psa_set_key_type(&attributes, our_key_type);
9860 PSA_ASSERT(psa_import_key(&attributes,
9861 our_key_data->x, our_key_data->len,
9862 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009863
Gilles Peskine449bd832023-01-11 14:50:10 +01009864 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9865 PSA_ASSERT(psa_key_derivation_key_agreement(
9866 &operation,
9867 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9868 peer_key_data->x, peer_key_data->len));
9869 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009870 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009871 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9872 PSA_KEY_DERIVATION_INPUT_INFO,
9873 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009874 }
Gilles Peskine59685592018-09-18 12:11:34 +02009875
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009876 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009877 PSA_ASSERT(psa_key_derivation_get_capacity(
9878 &operation, &actual_capacity));
9879 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009880
Gilles Peskinebf491972018-10-25 22:36:12 +02009881 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009882 while (actual_capacity > sizeof(output)) {
9883 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9884 output, sizeof(output)));
9885 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009886 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009887 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9888 output, actual_capacity));
9889 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9890 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009891
Gilles Peskine59685592018-09-18 12:11:34 +02009892exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009893 psa_key_derivation_abort(&operation);
9894 psa_destroy_key(our_key);
9895 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009896}
9897/* END_CASE */
9898
Valerio Settiad819672023-12-29 12:14:41 +01009899/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9900void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009901{
9902 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009903 psa_ecc_family_t ecc_family = psa_family_arg;
9904 size_t bits = bits_arg;
9905 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009906
Valerio Settiad819672023-12-29 12:14:41 +01009907 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9908 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009909 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009910}
9911/* END_CASE */
9912
Valerio Settiac739522024-01-04 10:22:01 +01009913/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9914void ecc_conversion_functions_fail()
9915{
9916 size_t bits;
9917
Valerio Settidb6e0292024-01-05 10:15:45 +01009918 /* Invalid legacy curve identifiers. */
9919 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9920 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009921 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9922 TEST_EQUAL(0, bits);
9923
9924 /* Invalid PSA EC family. */
9925 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9926 /* Invalid bit-size for a valid EC family. */
9927 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9928
9929 /* Twisted-Edward curves are not supported yet. */
9930 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9931 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9932 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9933 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9934}
9935/* END_CASE */
9936
9937
Valerio Settibf999cb2023-12-28 17:48:13 +01009938/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009939void key_agreement_output(int alg_arg,
9940 int our_key_type_arg, data_t *our_key_data,
9941 data_t *peer_key_data,
9942 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009943{
Ronald Cron5425a212020-08-04 14:58:35 +02009944 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009945 psa_algorithm_t alg = alg_arg;
9946 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009947 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009949 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009950
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009951 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009952 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009953
Gilles Peskine449bd832023-01-11 14:50:10 +01009954 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009955
Gilles Peskine449bd832023-01-11 14:50:10 +01009956 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9957 psa_set_key_algorithm(&attributes, alg);
9958 psa_set_key_type(&attributes, our_key_type);
9959 PSA_ASSERT(psa_import_key(&attributes,
9960 our_key_data->x, our_key_data->len,
9961 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009962
Gilles Peskine449bd832023-01-11 14:50:10 +01009963 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9964 PSA_ASSERT(psa_key_derivation_key_agreement(
9965 &operation,
9966 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9967 peer_key_data->x, peer_key_data->len));
9968 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009969 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009970 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9971 PSA_KEY_DERIVATION_INPUT_INFO,
9972 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009973 }
Gilles Peskine59685592018-09-18 12:11:34 +02009974
Gilles Peskine449bd832023-01-11 14:50:10 +01009975 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9976 actual_output,
9977 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009978 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009979 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009980 if (expected_output2->len != 0) {
9981 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9982 actual_output,
9983 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009984 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009985 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009986 }
Gilles Peskine59685592018-09-18 12:11:34 +02009987
9988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009989 psa_key_derivation_abort(&operation);
9990 psa_destroy_key(our_key);
9991 PSA_DONE();
9992 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009993}
9994/* END_CASE */
9995
9996/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009997void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009998{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009999 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +020010000 unsigned char *output = NULL;
10001 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +020010002 size_t i;
10003 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +020010004
Gilles Peskine449bd832023-01-11 14:50:10 +010010005 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +000010006
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010007 TEST_CALLOC(output, bytes);
10008 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +020010009
Gilles Peskine449bd832023-01-11 14:50:10 +010010010 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +020010011
Gilles Peskinea50d7392018-06-21 10:22:13 +020010012 /* Run several times, to ensure that every output byte will be
10013 * nonzero at least once with overwhelming probability
10014 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +010010015 for (run = 0; run < 10; run++) {
10016 if (bytes != 0) {
10017 memset(output, 0, bytes);
10018 }
10019 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +020010020
Gilles Peskine449bd832023-01-11 14:50:10 +010010021 for (i = 0; i < bytes; i++) {
10022 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +020010023 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010010024 }
Gilles Peskinea50d7392018-06-21 10:22:13 +020010025 }
Gilles Peskine05d69892018-06-19 22:00:52 +020010026 }
Gilles Peskinea50d7392018-06-21 10:22:13 +020010027
10028 /* Check that every byte was changed to nonzero at least once. This
10029 * validates that psa_generate_random is overwriting every byte of
10030 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010031 for (i = 0; i < bytes; i++) {
10032 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +020010033 }
Gilles Peskine05d69892018-06-19 22:00:52 +020010034
10035exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010036 PSA_DONE();
10037 mbedtls_free(output);
10038 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +020010039}
10040/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +020010041
Ryan3a1b7862024-03-01 17:24:04 +000010042#if defined MBEDTLS_THREADING_PTHREAD
10043
10044/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */
10045void concurrently_generate_keys(int type_arg,
10046 int bits_arg,
10047 int usage_arg,
10048 int alg_arg,
10049 int expected_status_arg,
10050 int is_large_key_arg,
10051 int arg_thread_count,
10052 int reps_arg)
10053{
10054 size_t thread_count = (size_t) arg_thread_count;
10055 mbedtls_test_thread_t *threads = NULL;
10056 generate_key_context gkc;
10057 gkc.type = type_arg;
10058 gkc.usage = usage_arg;
10059 gkc.bits = bits_arg;
10060 gkc.alg = alg_arg;
10061 gkc.expected_status = expected_status_arg;
10062 gkc.is_large_key = is_large_key_arg;
10063 gkc.reps = reps_arg;
10064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10065
10066 PSA_ASSERT(psa_crypto_init());
10067
10068 psa_set_key_usage_flags(&attributes, usage_arg);
10069 psa_set_key_algorithm(&attributes, alg_arg);
10070 psa_set_key_type(&attributes, type_arg);
10071 psa_set_key_bits(&attributes, bits_arg);
10072 gkc.attributes = &attributes;
10073
10074 TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count);
10075
10076 /* Split threads to generate key then destroy key. */
10077 for (size_t i = 0; i < thread_count; i++) {
10078 TEST_EQUAL(
10079 mbedtls_test_thread_create(&threads[i], thread_generate_key,
10080 (void *) &gkc), 0);
10081 }
10082
10083 /* Join threads. */
10084 for (size_t i = 0; i < thread_count; i++) {
10085 TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0);
10086 }
10087
10088exit:
10089 mbedtls_free(threads);
10090 PSA_DONE();
10091}
10092/* END_CASE */
10093#endif
10094
Gilles Peskine12313cd2018-06-20 00:20:32 +020010095/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010096void generate_key(int type_arg,
10097 int bits_arg,
10098 int usage_arg,
10099 int alg_arg,
10100 int expected_status_arg,
10101 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +020010102{
Ronald Cron5425a212020-08-04 14:58:35 +020010103 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +020010104 psa_key_type_t type = type_arg;
10105 psa_key_usage_t usage = usage_arg;
10106 size_t bits = bits_arg;
10107 psa_algorithm_t alg = alg_arg;
10108 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +020010109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +020010110 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +020010111
Gilles Peskine449bd832023-01-11 14:50:10 +010010112 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +020010113
Gilles Peskine449bd832023-01-11 14:50:10 +010010114 psa_set_key_usage_flags(&attributes, usage);
10115 psa_set_key_algorithm(&attributes, alg);
10116 psa_set_key_type(&attributes, type);
10117 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +020010118
10119 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010120 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +010010121
Gilles Peskine449bd832023-01-11 14:50:10 +010010122 if (is_large_key > 0) {
10123 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
10124 }
10125 TEST_EQUAL(status, expected_status);
10126 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +020010127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010128 }
Gilles Peskine12313cd2018-06-20 00:20:32 +020010129
10130 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +010010131 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
10132 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
10133 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +020010134
Gilles Peskine818ca122018-06-20 18:16:48 +020010135 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010136 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskine02b75072018-07-01 22:31:34 +020010137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010138 }
Gilles Peskine12313cd2018-06-20 00:20:32 +020010139
10140exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010141 /*
10142 * Key attributes may have been returned by psa_get_key_attributes()
10143 * thus reset them as required.
10144 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010145 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010146
Gilles Peskine449bd832023-01-11 14:50:10 +010010147 psa_destroy_key(key);
10148 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +020010149}
10150/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +030010151
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010152/* BEGIN_CASE */
10153void generate_key_ext(int type_arg,
10154 int bits_arg,
10155 int usage_arg,
10156 int alg_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +010010157 int flags_arg,
Gilles Peskine092ce512024-02-20 12:31:24 +010010158 data_t *params_data,
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010159 int expected_status_arg)
10160{
10161 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10162 psa_key_type_t type = type_arg;
10163 psa_key_usage_t usage = usage_arg;
10164 size_t bits = bits_arg;
10165 psa_algorithm_t alg = alg_arg;
10166 psa_status_t expected_status = expected_status_arg;
10167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine092ce512024-02-20 12:31:24 +010010168 psa_key_production_parameters_t *params = NULL;
10169 size_t params_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010170 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
10171
10172 PSA_ASSERT(psa_crypto_init());
10173
10174 psa_set_key_usage_flags(&attributes, usage);
10175 psa_set_key_algorithm(&attributes, alg);
10176 psa_set_key_type(&attributes, type);
10177 psa_set_key_bits(&attributes, bits);
10178
Gilles Peskine092ce512024-02-20 12:31:24 +010010179 if (!setup_key_production_parameters(&params, &params_data_length,
10180 flags_arg, params_data)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010181 goto exit;
10182 }
10183
10184 /* Generate a key */
10185 psa_status_t status = psa_generate_key_ext(&attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +010010186 params, params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010187 &key);
10188
10189 TEST_EQUAL(status, expected_status);
10190 if (expected_status != PSA_SUCCESS) {
10191 goto exit;
10192 }
10193
10194 /* Test the key information */
10195 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
10196 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
10197 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
10198
Gilles Peskine7a18f962024-02-12 16:48:11 +010010199#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
10200 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +010010201 TEST_ASSERT(rsa_test_e(key, bits, params_data));
Gilles Peskine7a18f962024-02-12 16:48:11 +010010202 }
10203#endif
10204
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010205 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010206 if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010207 goto exit;
10208 }
10209
10210exit:
10211 /*
10212 * Key attributes may have been returned by psa_get_key_attributes()
10213 * thus reset them as required.
10214 */
10215 psa_reset_key_attributes(&got_attributes);
Gilles Peskine092ce512024-02-20 12:31:24 +010010216 mbedtls_free(params);
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010217 psa_destroy_key(key);
10218 PSA_DONE();
10219}
10220/* END_CASE */
10221
10222/* BEGIN_CASE */
Gilles Peskine092ce512024-02-20 12:31:24 +010010223void key_production_parameters_init()
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010224{
Gilles Peskine092ce512024-02-20 12:31:24 +010010225 psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT;
10226 psa_key_production_parameters_t zero;
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010227 memset(&zero, 0, sizeof(zero));
10228
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010229 TEST_EQUAL(init.flags, 0);
10230 TEST_EQUAL(zero.flags, 0);
10231}
10232/* END_CASE */
10233
Darryl Greend49a4992018-06-18 17:27:26 +010010234/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010010235void persistent_key_load_key_from_storage(data_t *data,
10236 int type_arg, int bits_arg,
10237 int usage_flags_arg, int alg_arg,
10238 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +010010239{
Gilles Peskine449bd832023-01-11 14:50:10 +010010240 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +020010242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10243 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010244 psa_key_type_t type = type_arg;
10245 size_t bits = bits_arg;
10246 psa_key_usage_t usage_flags = usage_flags_arg;
10247 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +020010248 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +010010249 unsigned char *first_export = NULL;
10250 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010010251 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -070010252 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +010010253 size_t second_exported_length;
10254
Gilles Peskine449bd832023-01-11 14:50:10 +010010255 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010256 TEST_CALLOC(first_export, export_size);
10257 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010258 }
Darryl Greend49a4992018-06-18 17:27:26 +010010259
Gilles Peskine449bd832023-01-11 14:50:10 +010010260 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010261
Gilles Peskine449bd832023-01-11 14:50:10 +010010262 psa_set_key_id(&attributes, key_id);
10263 psa_set_key_usage_flags(&attributes, usage_flags);
10264 psa_set_key_algorithm(&attributes, alg);
10265 psa_set_key_type(&attributes, type);
10266 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +010010267
Gilles Peskine449bd832023-01-11 14:50:10 +010010268 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010269 case IMPORT_KEY:
10270 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010271 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
10272 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010273 break;
Darryl Greend49a4992018-06-18 17:27:26 +010010274
Darryl Green0c6575a2018-11-07 16:05:30 +000010275 case GENERATE_KEY:
10276 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010277 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010278 break;
10279
10280 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +010010281#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +010010282 {
10283 /* Create base key */
10284 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
10285 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
10286 psa_set_key_usage_flags(&base_attributes,
10287 PSA_KEY_USAGE_DERIVE);
10288 psa_set_key_algorithm(&base_attributes, derive_alg);
10289 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
10290 PSA_ASSERT(psa_import_key(&base_attributes,
10291 data->x, data->len,
10292 &base_key));
10293 /* Derive a key. */
10294 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
10295 PSA_ASSERT(psa_key_derivation_input_key(
10296 &operation,
10297 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
10298 PSA_ASSERT(psa_key_derivation_input_bytes(
10299 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
10300 NULL, 0));
10301 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
10302 &operation,
10303 &key));
10304 PSA_ASSERT(psa_key_derivation_abort(&operation));
10305 PSA_ASSERT(psa_destroy_key(base_key));
10306 base_key = MBEDTLS_SVC_KEY_ID_INIT;
10307 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010308#else
Gilles Peskine449bd832023-01-11 14:50:10 +010010309 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010310#endif
10311 break;
10312
10313 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010010314 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010315 break;
Darryl Green0c6575a2018-11-07 16:05:30 +000010316 }
Gilles Peskine449bd832023-01-11 14:50:10 +010010317 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +010010318
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010319 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010320 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10321 PSA_ASSERT(psa_export_key(key,
10322 first_export, export_size,
10323 &first_exported_length));
10324 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010325 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010326 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +010010327 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010328 }
Darryl Greend49a4992018-06-18 17:27:26 +010010329
10330 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +010010331 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010332 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +010010333 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010334
Darryl Greend49a4992018-06-18 17:27:26 +010010335 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +010010336 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
10337 TEST_ASSERT(mbedtls_svc_key_id_equal(
10338 psa_get_key_id(&attributes), key_id));
10339 TEST_EQUAL(psa_get_key_lifetime(&attributes),
10340 PSA_KEY_LIFETIME_PERSISTENT);
10341 TEST_EQUAL(psa_get_key_type(&attributes), type);
10342 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
10343 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
10344 mbedtls_test_update_key_usage_flags(usage_flags));
10345 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +010010346
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010347 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010348 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10349 PSA_ASSERT(psa_export_key(key,
10350 second_export, export_size,
10351 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010352 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010353 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +000010354 }
10355
10356 /* Do something with the key according to its type and permitted usage. */
Ryan Everett0a271fd2024-03-12 16:34:02 +000010357 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010359 }
Darryl Greend49a4992018-06-18 17:27:26 +010010360
10361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010362 /*
10363 * Key attributes may have been returned by psa_get_key_attributes()
10364 * thus reset them as required.
10365 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010366 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010367
Gilles Peskine449bd832023-01-11 14:50:10 +010010368 mbedtls_free(first_export);
10369 mbedtls_free(second_export);
10370 psa_key_derivation_abort(&operation);
10371 psa_destroy_key(base_key);
10372 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010373 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +010010374}
10375/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +020010376
Neil Armstronga557cb82022-06-10 08:58:32 +020010377/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010378void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
10379 int primitive_arg, int hash_arg, int role_arg,
10380 int test_input, data_t *pw_data,
10381 int inj_err_type_arg,
10382 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010383{
10384 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10385 psa_pake_operation_t operation = psa_pake_operation_init();
10386 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010387 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +020010388 psa_key_type_t key_type_pw = key_type_pw_arg;
10389 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010390 psa_algorithm_t hash_alg = hash_arg;
10391 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010392 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010394 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
10395 psa_status_t expected_error = expected_error_arg;
10396 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010397 unsigned char *output_buffer = NULL;
10398 size_t output_len = 0;
10399
Gilles Peskine449bd832023-01-11 14:50:10 +010010400 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010401
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010402 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +010010403 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010404 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010405
Gilles Peskine449bd832023-01-11 14:50:10 +010010406 if (pw_data->len > 0) {
10407 psa_set_key_usage_flags(&attributes, key_usage_pw);
10408 psa_set_key_algorithm(&attributes, alg);
10409 psa_set_key_type(&attributes, key_type_pw);
10410 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10411 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010412 }
10413
Gilles Peskine449bd832023-01-11 14:50:10 +010010414 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10415 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10416 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010417
Gilles Peskine449bd832023-01-11 14:50:10 +010010418 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010419
Gilles Peskine449bd832023-01-11 14:50:10 +010010420 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10421 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10422 expected_error);
10423 PSA_ASSERT(psa_pake_abort(&operation));
10424 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10425 expected_error);
10426 PSA_ASSERT(psa_pake_abort(&operation));
10427 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10428 expected_error);
10429 PSA_ASSERT(psa_pake_abort(&operation));
10430 TEST_EQUAL(psa_pake_set_role(&operation, role),
10431 expected_error);
10432 PSA_ASSERT(psa_pake_abort(&operation));
10433 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10434 NULL, 0, NULL),
10435 expected_error);
10436 PSA_ASSERT(psa_pake_abort(&operation));
10437 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10438 expected_error);
10439 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010440 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010441 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010442
Gilles Peskine449bd832023-01-11 14:50:10 +010010443 status = psa_pake_setup(&operation, &cipher_suite);
10444 if (status != PSA_SUCCESS) {
10445 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010446 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010447 }
10448
Gilles Peskine449bd832023-01-11 14:50:10 +010010449 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10450 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10451 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010452 goto exit;
10453 }
10454
Gilles Peskine449bd832023-01-11 14:50:10 +010010455 status = psa_pake_set_role(&operation, role);
10456 if (status != PSA_SUCCESS) {
10457 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010458 goto exit;
10459 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010460
Gilles Peskine449bd832023-01-11 14:50:10 +010010461 if (pw_data->len > 0) {
10462 status = psa_pake_set_password_key(&operation, key);
10463 if (status != PSA_SUCCESS) {
10464 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010465 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010466 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010467 }
10468
Gilles Peskine449bd832023-01-11 14:50:10 +010010469 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10470 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10471 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010472 goto exit;
10473 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010474
Gilles Peskine449bd832023-01-11 14:50:10 +010010475 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10476 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10477 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010478 goto exit;
10479 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010480
Gilles Peskine449bd832023-01-11 14:50:10 +010010481 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010482 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010483 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10484 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010485 goto exit;
10486 }
10487
Gilles Peskine449bd832023-01-11 14:50:10 +010010488 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010489 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010490 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10491 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010492 goto exit;
10493 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010494
Gilles Peskine449bd832023-01-11 14:50:10 +010010495 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10496 PSA_PAKE_STEP_KEY_SHARE);
10497 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10498 PSA_PAKE_STEP_ZK_PUBLIC);
10499 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10500 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010501
Gilles Peskine449bd832023-01-11 14:50:10 +010010502 if (test_input) {
10503 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10504 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10505 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010506 goto exit;
10507 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010508
Gilles Peskine449bd832023-01-11 14:50:10 +010010509 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10510 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10511 output_buffer, size_zk_proof),
10512 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010513 goto exit;
10514 }
10515
Gilles Peskine449bd832023-01-11 14:50:10 +010010516 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10517 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10518 output_buffer, size_zk_proof),
10519 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010520 goto exit;
10521 }
10522
Gilles Peskine449bd832023-01-11 14:50:10 +010010523 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10524 output_buffer, size_key_share);
10525 if (status != PSA_SUCCESS) {
10526 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010527 goto exit;
10528 }
10529
Gilles Peskine449bd832023-01-11 14:50:10 +010010530 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10531 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10532 output_buffer, size_zk_public + 1),
10533 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010534 goto exit;
10535 }
10536
Gilles Peskine449bd832023-01-11 14:50:10 +010010537 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010538 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010539 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10540 output_buffer, size_zk_public + 1);
10541 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10542 output_buffer, size_zk_public),
10543 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010544 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010545 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010546 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010547 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10548 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10549 NULL, 0, NULL),
10550 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010551 goto exit;
10552 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010553
Gilles Peskine449bd832023-01-11 14:50:10 +010010554 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10555 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10556 output_buffer, buf_size, &output_len),
10557 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010558 goto exit;
10559 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010560
Gilles Peskine449bd832023-01-11 14:50:10 +010010561 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10562 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10563 output_buffer, buf_size, &output_len),
10564 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010565 goto exit;
10566 }
10567
Gilles Peskine449bd832023-01-11 14:50:10 +010010568 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10569 output_buffer, buf_size, &output_len);
10570 if (status != PSA_SUCCESS) {
10571 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010572 goto exit;
10573 }
10574
Gilles Peskine449bd832023-01-11 14:50:10 +010010575 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010576
Gilles Peskine449bd832023-01-11 14:50:10 +010010577 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10578 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10579 output_buffer, size_zk_public - 1, &output_len),
10580 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010581 goto exit;
10582 }
10583
Gilles Peskine449bd832023-01-11 14:50:10 +010010584 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010585 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010586 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10587 output_buffer, size_zk_public - 1, &output_len);
10588 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10589 output_buffer, buf_size, &output_len),
10590 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010591 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010592 }
10593 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010594
10595exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010596 PSA_ASSERT(psa_destroy_key(key));
10597 PSA_ASSERT(psa_pake_abort(&operation));
10598 mbedtls_free(output_buffer);
10599 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010600}
10601/* END_CASE */
10602
Neil Armstronga557cb82022-06-10 08:58:32 +020010603/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010604void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10605 int client_input_first, int inject_error,
10606 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010607{
10608 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10609 psa_pake_operation_t server = psa_pake_operation_init();
10610 psa_pake_operation_t client = psa_pake_operation_init();
10611 psa_algorithm_t alg = alg_arg;
10612 psa_algorithm_t hash_alg = hash_arg;
10613 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10615
Gilles Peskine449bd832023-01-11 14:50:10 +010010616 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010617
Gilles Peskine449bd832023-01-11 14:50:10 +010010618 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10619 psa_set_key_algorithm(&attributes, alg);
10620 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10621 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10622 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010623
Gilles Peskine449bd832023-01-11 14:50:10 +010010624 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10625 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10626 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010627
10628
Gilles Peskine449bd832023-01-11 14:50:10 +010010629 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10630 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010631
Gilles Peskine449bd832023-01-11 14:50:10 +010010632 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10633 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010634
Gilles Peskine449bd832023-01-11 14:50:10 +010010635 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10636 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010637
Gilles Peskine449bd832023-01-11 14:50:10 +010010638 ecjpake_do_round(alg, primitive_arg, &server, &client,
10639 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010640
Gilles Peskine449bd832023-01-11 14:50:10 +010010641 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010643 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010644
Gilles Peskine449bd832023-01-11 14:50:10 +010010645 ecjpake_do_round(alg, primitive_arg, &server, &client,
10646 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010647
10648exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010649 psa_destroy_key(key);
10650 psa_pake_abort(&server);
10651 psa_pake_abort(&client);
10652 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010653}
10654/* END_CASE */
10655
10656/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010657void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10658 int derive_alg_arg, data_t *pw_data,
10659 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010660{
10661 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10662 psa_pake_operation_t server = psa_pake_operation_init();
10663 psa_pake_operation_t client = psa_pake_operation_init();
10664 psa_algorithm_t alg = alg_arg;
10665 psa_algorithm_t hash_alg = hash_arg;
10666 psa_algorithm_t derive_alg = derive_alg_arg;
10667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10669 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010670 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010671 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010672 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010673 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010674
Gilles Peskine449bd832023-01-11 14:50:10 +010010675 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010676
Gilles Peskine449bd832023-01-11 14:50:10 +010010677 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10678 psa_set_key_algorithm(&attributes, alg);
10679 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10680 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10681 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010682
Gilles Peskine449bd832023-01-11 14:50:10 +010010683 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10684 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10685 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010686
Neil Armstrong1e855602022-06-15 11:32:11 +020010687 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010688 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10689 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010690
Gilles Peskine449bd832023-01-11 14:50:10 +010010691 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10692 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10693 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10694 PSA_KEY_DERIVATION_INPUT_SEED,
10695 (const uint8_t *) "", 0));
10696 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10697 PSA_KEY_DERIVATION_INPUT_SEED,
10698 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010699 }
10700
Gilles Peskine449bd832023-01-11 14:50:10 +010010701 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10702 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010703
Gilles Peskine449bd832023-01-11 14:50:10 +010010704 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10705 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010706
Gilles Peskine449bd832023-01-11 14:50:10 +010010707 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10708 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010709
Gilles Peskine449bd832023-01-11 14:50:10 +010010710 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10711 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10712 PSA_ERROR_BAD_STATE);
10713 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10714 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010715 goto exit;
10716 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010717
Neil Armstrongf983caf2022-06-15 15:27:48 +020010718 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010719 ecjpake_do_round(alg, primitive_arg, &server, &client,
10720 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010721
Gilles Peskine449bd832023-01-11 14:50:10 +010010722 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10723 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10724 PSA_ERROR_BAD_STATE);
10725 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10726 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010727 goto exit;
10728 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010729
Neil Armstrongf983caf2022-06-15 15:27:48 +020010730 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010731 ecjpake_do_round(alg, primitive_arg, &server, &client,
10732 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010733
Gilles Peskine449bd832023-01-11 14:50:10 +010010734 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10735 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010736
10737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010738 psa_key_derivation_abort(&server_derive);
10739 psa_key_derivation_abort(&client_derive);
10740 psa_destroy_key(key);
10741 psa_pake_abort(&server);
10742 psa_pake_abort(&client);
10743 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010744}
10745/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010746
10747/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010748void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010749{
10750 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10751 const size_t bits = 256;
10752 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010753 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010754 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010755 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010756
10757 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10758 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010759 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10760 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10761 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10762 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010763 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010764 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10765 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010766
10767 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010768 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10769 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10770 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10771 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10772 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10773 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010774
10775 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010776 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10777 PSA_PAKE_OUTPUT_MAX_SIZE);
10778 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10779 PSA_PAKE_OUTPUT_MAX_SIZE);
10780 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10781 PSA_PAKE_OUTPUT_MAX_SIZE);
10782 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10783 PSA_PAKE_INPUT_MAX_SIZE);
10784 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10785 PSA_PAKE_INPUT_MAX_SIZE);
10786 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10787 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010788}
10789/* END_CASE */