blob: ec8afe705ffb4e5eb0b57da1854e49ce3f521dce [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Valerio Settibf999cb2023-12-28 17:48:13 +01009#include "mbedtls/psa_util.h"
10
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020011/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
12 * uses mbedtls_ctr_drbg internally. */
13#include "mbedtls/ctr_drbg.h"
14
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020016#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020017
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010018/* For psa_can_do_hash() */
19#include "psa_crypto_core.h"
20
Gilles Peskine8e94efe2021-02-13 00:25:53 +010021#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010022#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010023#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053024#if defined(PSA_CRYPTO_DRIVER_TEST)
25#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053026#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
27#else
28#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053029#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010030
Gilles Peskine4023c012021-05-27 13:21:20 +020031/* If this comes up, it's a bug in the test code or in the test data. */
32#define UNUSED 0xdeadbeef
33
Dave Rodgman647791d2021-06-23 12:49:59 +010034/* Assert that an operation is (not) active.
35 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010036#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
37#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010038
Przemek Stekiel7c795482022-11-15 22:26:12 +010039#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010040int ecjpake_operation_setup(psa_pake_operation_t *operation,
41 psa_pake_cipher_suite_t *cipher_suite,
42 psa_pake_role_t role,
43 mbedtls_svc_key_id_t key,
44 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010045{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (key_available) {
53 PSA_ASSERT(psa_pake_set_password_key(operation, key));
54 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010055 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010056exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010057 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010058}
59#endif
60
Jaeden Amerof24c7f82018-06-27 17:20:43 +010061/** An invalid export length that will never be set by psa_export_key(). */
62static const size_t INVALID_EXPORT_LENGTH = ~0U;
63
Gilles Peskinea7aa4422018-08-14 15:17:54 +020064/** Test if a buffer contains a constant byte value.
65 *
66 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020067 *
68 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020069 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 * \param size Size of the buffer in bytes.
71 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020072 * \return 1 if the buffer is all-bits-zero.
73 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020076{
77 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 for (i = 0; i < size; i++) {
79 if (((unsigned char *) buffer)[i] != c) {
80 return 0;
81 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020082 }
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020084}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010085#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020086/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010087static int asn1_write_10x(unsigned char **p,
88 unsigned char *start,
89 size_t bits,
90 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020091{
92 int ret;
93 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (bits == 0) {
95 return MBEDTLS_ERR_ASN1_INVALID_DATA;
96 }
97 if (bits <= 8 && x >= 1 << (bits - 1)) {
98 return MBEDTLS_ERR_ASN1_INVALID_DATA;
99 }
100 if (*p < start || *p - start < (ptrdiff_t) len) {
101 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
102 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200103 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 (*p)[len-1] = x;
105 if (bits % 8 == 0) {
106 (*p)[1] |= 1;
107 } else {
108 (*p)[0] |= 1 << (bits % 8);
109 }
110 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
111 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
112 MBEDTLS_ASN1_INTEGER));
113 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200114}
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116static int construct_fake_rsa_key(unsigned char *buffer,
117 size_t buffer_size,
118 unsigned char **p,
119 size_t bits,
120 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200121{
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200123 int ret;
124 int len = 0;
125 /* Construct something that looks like a DER encoding of
126 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
127 * RSAPrivateKey ::= SEQUENCE {
128 * version Version,
129 * modulus INTEGER, -- n
130 * publicExponent INTEGER, -- e
131 * privateExponent INTEGER, -- d
132 * prime1 INTEGER, -- p
133 * prime2 INTEGER, -- q
134 * exponent1 INTEGER, -- d mod (p-1)
135 * exponent2 INTEGER, -- d mod (q-1)
136 * coefficient INTEGER, -- (inverse of q) mod p
137 * otherPrimeInfos OtherPrimeInfos OPTIONAL
138 * }
139 * Or, for a public key, the same structure with only
140 * version, modulus and publicExponent.
141 */
142 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (keypair) {
144 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* q */
151 asn1_write_10x(p, buffer, half_bits, 1));
152 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
153 asn1_write_10x(p, buffer, half_bits, 3));
154 MBEDTLS_ASN1_CHK_ADD(len, /* d */
155 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200156 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
158 asn1_write_10x(p, buffer, 17, 1));
159 MBEDTLS_ASN1_CHK_ADD(len, /* n */
160 asn1_write_10x(p, buffer, bits, 1));
161 if (keypair) {
162 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
163 mbedtls_asn1_write_int(p, buffer, 0));
164 }
165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 {
167 const unsigned char tag =
168 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200172}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100173#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int exercise_mac_setup(psa_key_type_t key_type,
176 const unsigned char *key_bytes,
177 size_t key_length,
178 psa_algorithm_t alg,
179 psa_mac_operation_t *operation,
180 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100181{
Ronald Cron5425a212020-08-04 14:58:35 +0200182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
186 psa_set_key_algorithm(&attributes, alg);
187 psa_set_key_type(&attributes, key_type);
188 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 /* If setup failed, reproduce the failure, so that the caller can
194 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (*status != PSA_SUCCESS) {
196 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 }
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201
202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 psa_destroy_key(key);
204 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100205}
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int exercise_cipher_setup(psa_key_type_t key_type,
208 const unsigned char *key_bytes,
209 size_t key_length,
210 psa_algorithm_t alg,
211 psa_cipher_operation_t *operation,
212 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100213{
Ronald Cron5425a212020-08-04 14:58:35 +0200214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
218 psa_set_key_algorithm(&attributes, alg);
219 psa_set_key_type(&attributes, key_type);
220 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100225 /* If setup failed, reproduce the failure, so that the caller can
226 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (*status != PSA_SUCCESS) {
228 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
229 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234
235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 psa_destroy_key(key);
237 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241{
242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200244 uint8_t buffer[1];
245 size_t length;
246 int ok = 0;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 psa_set_key_id(&attributes, key_id);
249 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
250 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
251 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
252 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
253 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200254 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200256 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
258 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
259 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
260 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
261 TEST_EQUAL(psa_get_key_type(&attributes), 0);
262 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
265 PSA_ERROR_INVALID_HANDLE);
266 TEST_EQUAL(psa_export_public_key(key,
267 buffer, sizeof(buffer), &length),
268 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200270 ok = 1;
271
272exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100273 /*
274 * Key attributes may have been returned by psa_get_key_attributes()
275 * thus reset them as required.
276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200280}
281
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200282/* Assert that a key isn't reported as having a slot number. */
283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200285 do \
286 { \
287 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 TEST_EQUAL(psa_get_key_slot_number( \
289 attributes, \
290 &ASSERT_NO_SLOT_NUMBER_slot_number), \
291 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200292 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200294#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295#define ASSERT_NO_SLOT_NUMBER(attributes) \
296 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
298
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530299#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
300
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301/* An overapproximation of the amount of storage needed for a key of the
302 * given type and with the given content. The API doesn't make it easy
303 * to find a good value for the size. The current implementation doesn't
304 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305#define KEY_BITS_FROM_DATA(type, data) \
306 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100307
Darryl Green0c6575a2018-11-07 16:05:30 +0000308typedef enum {
309 IMPORT_KEY = 0,
310 GENERATE_KEY = 1,
311 DERIVE_KEY = 2
312} generate_method;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100315 DO_NOT_SET_LENGTHS = 0,
316 SET_LENGTHS_BEFORE_NONCE = 1,
317 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100318} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100321 USE_NULL_TAG = 0,
322 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100323} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100324
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530325
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326/*!
327 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 * \param key_type_arg Type of key passed in
329 * \param key_data The encryption / decryption key data
330 * \param alg_arg The type of algorithm used
331 * \param nonce Nonce data
332 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100333 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100334 * feed additional data in to be encrypted /
335 * decrypted. If -1, no chunking.
336 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100337 * \param data_part_len_arg If not -1, the length of chunks to feed
338 * the data in to be encrypted / decrypted. If
339 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100340 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100341 * expected here, this controls whether or not
342 * to set lengths, and in what order with
343 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100346 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100347 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
351 int alg_arg,
352 data_t *nonce,
353 data_t *additional_data,
354 int ad_part_len_arg,
355 data_t *input_data,
356 int data_part_len_arg,
357 set_lengths_method_t set_lengths_method,
358 data_t *expected_output,
359 int is_encrypt,
360 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100361{
362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
363 psa_key_type_t key_type = key_type_arg;
364 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100365 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 unsigned char *output_data = NULL;
367 unsigned char *part_data = NULL;
368 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100371 size_t output_size = 0;
372 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100373 size_t output_length = 0;
374 size_t key_bits = 0;
375 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100376 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100377 size_t part_length = 0;
378 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t ad_part_len = 0;
381 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
384 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100387 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (is_encrypt) {
392 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
393 } else {
394 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100396
397 psa_set_key_algorithm(&attributes, alg);
398 psa_set_key_type(&attributes, key_type);
399
400 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
401 &key));
402
403 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
404 key_bits = psa_get_key_bits(&attributes);
405
406 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
407
408 if (is_encrypt) {
409 /* Tag gets written at end of buffer. */
410 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
411 (input_data->len +
412 tag_length));
413 data_true_size = input_data->len;
414 } else {
415 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
416 (input_data->len -
417 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100418
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100419 /* Do not want to attempt to decrypt tag. */
420 data_true_size = input_data->len - tag_length;
421 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100422
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100423 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (is_encrypt) {
426 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
427 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
428 } else {
429 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
430 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100431 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100433 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (is_encrypt) {
436 status = psa_aead_encrypt_setup(&operation, key, alg);
437 } else {
438 status = psa_aead_decrypt_setup(&operation, key, alg);
439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
441 /* If the operation is not supported, just skip and not fail in case the
442 * encryption involves a common limitation of cryptography hardwares and
443 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (status == PSA_ERROR_NOT_SUPPORTED) {
445 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
446 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
452 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
453 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
454 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
455 data_true_size));
456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
457 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
458 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
461 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100469 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 part_offset += part_length, part_count++) {
471 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100472 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100474 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100476 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation,
480 additional_data->x + part_offset,
481 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
487 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 data_part_len = (size_t) data_part_len_arg;
493 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
494 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100495
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100496 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100499 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 part_offset += part_length, part_count++) {
501 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100502 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 } else if ((data_true_size - part_offset) < data_part_len) {
504 part_length = (data_true_size - part_offset);
505 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100506 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 }
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 PSA_ASSERT(psa_aead_update(&operation,
510 (input_data->x + part_offset),
511 part_length, part_data,
512 part_data_size,
513 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (output_data && output_part_length) {
516 memcpy((output_data + output_length), part_data,
517 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100518 }
519
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 output_length += output_part_length;
521 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100523 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
525 data_true_size, output_data,
526 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100527 }
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (is_encrypt) {
530 PSA_ASSERT(psa_aead_finish(&operation, final_data,
531 final_output_size,
532 &output_part_length,
533 tag_buffer, tag_length,
534 &tag_size));
535 } else {
536 PSA_ASSERT(psa_aead_verify(&operation, final_data,
537 final_output_size,
538 &output_part_length,
539 (input_data->x + data_true_size),
540 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100541 }
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (output_data && output_part_length) {
544 memcpy((output_data + output_length), final_data,
545 output_part_length);
546 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100547
548 output_length += output_part_length;
549
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100550
551 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
552 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (is_encrypt) {
554 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (output_data && tag_length) {
557 memcpy((output_data + output_length), tag_buffer,
558 tag_length);
559 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100560
561 output_length += tag_length;
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 TEST_EQUAL(output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
565 input_data->len));
566 TEST_LE_U(output_length,
567 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
568 } else {
569 TEST_EQUAL(output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
571 input_data->len));
572 TEST_LE_U(output_length,
573 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 }
575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100577 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100578 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100582
583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 psa_destroy_key(key);
585 psa_aead_abort(&operation);
586 mbedtls_free(output_data);
587 mbedtls_free(part_data);
588 mbedtls_free(final_data);
589 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100592}
593
Neil Armstrong4766f992022-02-28 16:23:59 +0100594/*!
595 * \brief Internal Function for MAC multipart tests.
596 * \param key_type_arg Type of key passed in
597 * \param key_data The encryption / decryption key data
598 * \param alg_arg The type of algorithm used
599 * \param input_data Data to encrypt / decrypt
600 * \param data_part_len_arg If not -1, the length of chunks to feed
601 * the data in to be encrypted / decrypted. If
602 * -1, no chunking
603 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000604 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100605 * \param do_zero_parts If non-zero, interleave zero length chunks
606 * with normal length chunks.
607 * \return int Zero on failure, non-zero on success.
608 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
610 int alg_arg,
611 data_t *input_data,
612 int data_part_len_arg,
613 data_t *expected_output,
614 int is_verify,
615 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100616{
617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
618 psa_key_type_t key_type = key_type_arg;
619 psa_algorithm_t alg = alg_arg;
620 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
621 unsigned char mac[PSA_MAC_MAX_SIZE];
622 size_t part_offset = 0;
623 size_t part_length = 0;
624 size_t data_part_len = 0;
625 size_t mac_len = 0;
626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
628
629 int test_ok = 0;
630 size_t part_count = 0;
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (is_verify) {
635 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
636 } else {
637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
638 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 psa_set_key_algorithm(&attributes, alg);
641 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
644 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (is_verify) {
647 status = psa_mac_verify_setup(&operation, key, alg);
648 } else {
649 status = psa_mac_sign_setup(&operation, key, alg);
650 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 part_offset += part_length, part_count++) {
661 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100662 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 } else if ((input_data->len - part_offset) < data_part_len) {
664 part_length = (input_data->len - part_offset);
665 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100666 part_length = data_part_len;
667 }
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 PSA_ASSERT(psa_mac_update(&operation,
670 (input_data->x + part_offset),
671 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100672 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100674 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
676 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (is_verify) {
680 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
681 expected_output->len));
682 } else {
683 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
684 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100685
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100686 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100687 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100688 }
689
690 test_ok = 1;
691
692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 psa_destroy_key(key);
694 psa_mac_abort(&operation);
695 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100698}
699
Neil Armstrong75673ab2022-06-15 17:39:01 +0200700#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100701static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
702 psa_pake_operation_t *server,
703 psa_pake_operation_t *client,
704 int client_input_first,
705 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200706{
707 unsigned char *buffer0 = NULL, *buffer1 = NULL;
708 size_t buffer_length = (
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
711 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200712 /* The output should be exactly this size according to the spec */
713 const size_t expected_size_key_share =
714 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
715 /* The output should be exactly this size according to the spec */
716 const size_t expected_size_zk_public =
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
718 /* The output can be smaller: the spec allows stripping leading zeroes */
719 const size_t max_expected_size_zk_proof =
720 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200721 size_t buffer0_off = 0;
722 size_t buffer1_off = 0;
723 size_t s_g1_len, s_g2_len, s_a_len;
724 size_t s_g1_off, s_g2_off, s_a_off;
725 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
726 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
727 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
728 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
729 size_t c_g1_len, c_g2_len, c_a_len;
730 size_t c_g1_off, c_g2_off, c_a_off;
731 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
732 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
733 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
734 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
735 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200736 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100738 TEST_CALLOC(buffer0, buffer_length);
739 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200742 case 1:
743 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
745 buffer0 + buffer0_off,
746 512 - buffer0_off, &s_g1_len));
747 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200748 s_g1_off = buffer0_off;
749 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
751 buffer0 + buffer0_off,
752 512 - buffer0_off, &s_x1_pk_len));
753 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200754 s_x1_pk_off = buffer0_off;
755 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
757 buffer0 + buffer0_off,
758 512 - buffer0_off, &s_x1_pr_len));
759 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200760 s_x1_pr_off = buffer0_off;
761 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
763 buffer0 + buffer0_off,
764 512 - buffer0_off, &s_g2_len));
765 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200766 s_g2_off = buffer0_off;
767 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
769 buffer0 + buffer0_off,
770 512 - buffer0_off, &s_x2_pk_len));
771 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200772 s_x2_pk_off = buffer0_off;
773 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
775 buffer0 + buffer0_off,
776 512 - buffer0_off, &s_x2_pr_len));
777 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200778 s_x2_pr_off = buffer0_off;
779 buffer0_off += s_x2_pr_len;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500782 buffer0[s_x1_pr_off + 8] ^= 1;
783 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200784 expected_status = PSA_ERROR_DATA_INVALID;
785 }
786
Neil Armstrong51009d72022-09-05 17:59:54 +0200787 /*
788 * When injecting errors in inputs, the implementation is
789 * free to detect it right away of with a delay.
790 * This permits delaying the error until the end of the input
791 * sequence, if no error appears then, this will be treated
792 * as an error.
793 */
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200796 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
798 buffer0 + s_g1_off, s_g1_len);
799 if (inject_error == 1 && status != PSA_SUCCESS) {
800 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 } else {
803 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 }
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
807 buffer0 + s_x1_pk_off,
808 s_x1_pk_len);
809 if (inject_error == 1 && status != PSA_SUCCESS) {
810 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200811 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 } else {
813 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 }
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
817 buffer0 + s_x1_pr_off,
818 s_x1_pr_len);
819 if (inject_error == 1 && status != PSA_SUCCESS) {
820 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200821 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 } else {
823 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
827 buffer0 + s_g2_off,
828 s_g2_len);
829 if (inject_error == 1 && status != PSA_SUCCESS) {
830 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200831 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 } else {
833 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 }
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
837 buffer0 + s_x2_pk_off,
838 s_x2_pk_len);
839 if (inject_error == 1 && status != PSA_SUCCESS) {
840 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200841 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 } else {
843 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
847 buffer0 + s_x2_pr_off,
848 s_x2_pr_len);
849 if (inject_error == 1 && status != PSA_SUCCESS) {
850 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200851 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 } else {
853 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200854 }
855
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200856 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (inject_error == 1) {
858 TEST_ASSERT(
859 !"One of the last psa_pake_input() calls should have returned the expected error.");
860 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 }
862
863 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
865 buffer1 + buffer1_off,
866 512 - buffer1_off, &c_g1_len));
867 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200868 c_g1_off = buffer1_off;
869 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
871 buffer1 + buffer1_off,
872 512 - buffer1_off, &c_x1_pk_len));
873 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200874 c_x1_pk_off = buffer1_off;
875 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
877 buffer1 + buffer1_off,
878 512 - buffer1_off, &c_x1_pr_len));
879 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200880 c_x1_pr_off = buffer1_off;
881 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
883 buffer1 + buffer1_off,
884 512 - buffer1_off, &c_g2_len));
885 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200886 c_g2_off = buffer1_off;
887 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
889 buffer1 + buffer1_off,
890 512 - buffer1_off, &c_x2_pk_len));
891 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200892 c_x2_pk_off = buffer1_off;
893 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
895 buffer1 + buffer1_off,
896 512 - buffer1_off, &c_x2_pr_len));
897 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200898 c_x2_pr_off = buffer1_off;
899 buffer1_off += c_x2_pr_len;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
904 buffer0 + s_g1_off, s_g1_len);
905 if (inject_error == 1 && status != PSA_SUCCESS) {
906 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200907 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 } else {
909 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 }
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
913 buffer0 + s_x1_pk_off,
914 s_x1_pk_len);
915 if (inject_error == 1 && status != PSA_SUCCESS) {
916 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 } else {
919 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 }
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
923 buffer0 + s_x1_pr_off,
924 s_x1_pr_len);
925 if (inject_error == 1 && status != PSA_SUCCESS) {
926 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200927 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 } else {
929 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 }
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
933 buffer0 + s_g2_off,
934 s_g2_len);
935 if (inject_error == 1 && status != PSA_SUCCESS) {
936 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200937 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 } else {
939 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 }
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
943 buffer0 + s_x2_pk_off,
944 s_x2_pk_len);
945 if (inject_error == 1 && status != PSA_SUCCESS) {
946 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200947 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 } else {
949 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 }
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
953 buffer0 + s_x2_pr_off,
954 s_x2_pr_len);
955 if (inject_error == 1 && status != PSA_SUCCESS) {
956 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200957 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 } else {
959 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200960 }
961
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200962 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (inject_error == 1) {
964 TEST_ASSERT(
965 !"One of the last psa_pake_input() calls should have returned the expected error.");
966 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500970 buffer1[c_x1_pr_off + 12] ^= 1;
971 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200972 expected_status = PSA_ERROR_DATA_INVALID;
973 }
974
975 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
977 buffer1 + c_g1_off, c_g1_len);
978 if (inject_error == 2 && status != PSA_SUCCESS) {
979 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200980 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 } else {
982 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200983 }
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
986 buffer1 + c_x1_pk_off, c_x1_pk_len);
987 if (inject_error == 2 && status != PSA_SUCCESS) {
988 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 } else {
991 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200992 }
993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
995 buffer1 + c_x1_pr_off, c_x1_pr_len);
996 if (inject_error == 2 && status != PSA_SUCCESS) {
997 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200998 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 } else {
1000 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001001 }
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1004 buffer1 + c_g2_off, c_g2_len);
1005 if (inject_error == 2 && status != PSA_SUCCESS) {
1006 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 } else {
1009 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001010 }
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1013 buffer1 + c_x2_pk_off, c_x2_pk_len);
1014 if (inject_error == 2 && status != PSA_SUCCESS) {
1015 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001016 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 } else {
1018 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001019 }
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1022 buffer1 + c_x2_pr_off, c_x2_pr_len);
1023 if (inject_error == 2 && status != PSA_SUCCESS) {
1024 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001025 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 } else {
1027 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001028 }
1029
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001030 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (inject_error == 2) {
1032 TEST_ASSERT(
1033 !"One of the last psa_pake_input() calls should have returned the expected error.");
1034 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001035
1036 break;
1037
1038 case 2:
1039 /* Server second round Output */
1040 buffer0_off = 0;
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1043 buffer0 + buffer0_off,
1044 512 - buffer0_off, &s_a_len));
1045 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001046 s_a_off = buffer0_off;
1047 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1049 buffer0 + buffer0_off,
1050 512 - buffer0_off, &s_x2s_pk_len));
1051 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001052 s_x2s_pk_off = buffer0_off;
1053 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1055 buffer0 + buffer0_off,
1056 512 - buffer0_off, &s_x2s_pr_len));
1057 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001058 s_x2s_pr_off = buffer0_off;
1059 buffer0_off += s_x2s_pr_len;
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001062 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001063 expected_status = PSA_ERROR_DATA_INVALID;
1064 }
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1069 buffer0 + s_a_off, s_a_len);
1070 if (inject_error == 3 && status != PSA_SUCCESS) {
1071 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001072 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 } else {
1074 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 }
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1078 buffer0 + s_x2s_pk_off,
1079 s_x2s_pk_len);
1080 if (inject_error == 3 && status != PSA_SUCCESS) {
1081 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001082 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 } else {
1084 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 }
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1088 buffer0 + s_x2s_pr_off,
1089 s_x2s_pr_len);
1090 if (inject_error == 3 && status != PSA_SUCCESS) {
1091 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001092 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 } else {
1094 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001095 }
1096
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001097 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (inject_error == 3) {
1099 TEST_ASSERT(
1100 !"One of the last psa_pake_input() calls should have returned the expected error.");
1101 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001102 }
1103
1104 /* Client second round Output */
1105 buffer1_off = 0;
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1108 buffer1 + buffer1_off,
1109 512 - buffer1_off, &c_a_len));
1110 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001111 c_a_off = buffer1_off;
1112 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1114 buffer1 + buffer1_off,
1115 512 - buffer1_off, &c_x2s_pk_len));
1116 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001117 c_x2s_pk_off = buffer1_off;
1118 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1120 buffer1 + buffer1_off,
1121 512 - buffer1_off, &c_x2s_pr_len));
1122 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001123 c_x2s_pr_off = buffer1_off;
1124 buffer1_off += c_x2s_pr_len;
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001127 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1129 buffer0 + s_a_off, s_a_len);
1130 if (inject_error == 3 && status != PSA_SUCCESS) {
1131 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001132 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else {
1134 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 }
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1138 buffer0 + s_x2s_pk_off,
1139 s_x2s_pk_len);
1140 if (inject_error == 3 && status != PSA_SUCCESS) {
1141 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001142 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 } else {
1144 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 }
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1148 buffer0 + s_x2s_pr_off,
1149 s_x2s_pr_len);
1150 if (inject_error == 3 && status != PSA_SUCCESS) {
1151 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001152 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 } else {
1154 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001155 }
1156
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001157 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (inject_error == 3) {
1159 TEST_ASSERT(
1160 !"One of the last psa_pake_input() calls should have returned the expected error.");
1161 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001165 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001166 expected_status = PSA_ERROR_DATA_INVALID;
1167 }
1168
1169 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1171 buffer1 + c_a_off, c_a_len);
1172 if (inject_error == 4 && status != PSA_SUCCESS) {
1173 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001174 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 } else {
1176 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001177 }
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1180 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1181 if (inject_error == 4 && status != PSA_SUCCESS) {
1182 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001183 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 } else {
1185 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001186 }
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1189 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1190 if (inject_error == 4 && status != PSA_SUCCESS) {
1191 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 } else {
1194 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001195 }
1196
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001197 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (inject_error == 4) {
1199 TEST_ASSERT(
1200 !"One of the last psa_pake_input() calls should have returned the expected error.");
1201 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001202
1203 break;
1204
1205 }
1206
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 mbedtls_free(buffer0);
1209 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001210}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001211#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001214 INJECT_ERR_NONE = 0,
1215 INJECT_ERR_UNINITIALIZED_ACCESS,
1216 INJECT_ERR_DUPLICATE_SETUP,
1217 INJECT_ERR_INVALID_USER,
1218 INJECT_ERR_INVALID_PEER,
1219 INJECT_ERR_SET_USER,
1220 INJECT_ERR_SET_PEER,
1221 INJECT_EMPTY_IO_BUFFER,
1222 INJECT_UNKNOWN_STEP,
1223 INJECT_INVALID_FIRST_STEP,
1224 INJECT_WRONG_BUFFER_SIZE,
1225 INJECT_VALID_OPERATION_AFTER_FAILURE,
1226 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1227 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1228} ecjpake_injected_failure_t;
1229
Paul Elliott01885fa2023-02-09 12:07:30 +00001230#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001231
Paul Elliott6f600372023-02-06 18:41:05 +00001232static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1233 psa_status_t expected_status,
1234 size_t *min_completes,
1235 size_t *max_completes)
1236{
1237
1238 /* This is slightly contrived, but we only really know that with a minimum
1239 value of max_ops that a successful operation should take more than one op
1240 to complete, and likewise that with a max_ops of
1241 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1242 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001243
Paul Elliott6f600372023-02-06 18:41:05 +00001244 if (expected_status == PSA_SUCCESS) {
1245 *min_completes = 2;
1246 } else {
1247 *min_completes = 1;
1248 }
1249
1250 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1251 } else {
1252 *min_completes = 1;
1253 *max_completes = 1;
1254 }
1255}
Paul Elliott01885fa2023-02-09 12:07:30 +00001256#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001257
Gilles Peskinee59236f2018-01-27 23:32:46 +01001258/* END_HEADER */
1259
1260/* BEGIN_DEPENDENCIES
1261 * depends_on:MBEDTLS_PSA_CRYPTO_C
1262 * END_DEPENDENCIES
1263 */
1264
1265/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001266void psa_can_do_hash()
1267{
1268 /* We can't test that this is specific to drivers until partial init has
1269 * been implemented, but we can at least test before/after full init. */
1270 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1271 PSA_INIT();
1272 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1273 PSA_DONE();
1274}
1275/* END_CASE */
1276
1277/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001278void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001279{
1280 size_t max_truncated_mac_size =
1281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1282
1283 /* Check that the length for a truncated MAC always fits in the algorithm
1284 * encoding. The shifted mask is the maximum truncated value. The
1285 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001287}
1288/* END_CASE */
1289
1290/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291void import_with_policy(int type_arg,
1292 int usage_arg, int alg_arg,
1293 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001294{
1295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1296 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001298 psa_key_type_t type = type_arg;
1299 psa_key_usage_t usage = usage_arg;
1300 psa_algorithm_t alg = alg_arg;
1301 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001303 psa_status_t status;
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 psa_set_key_type(&attributes, type);
1308 psa_set_key_usage_flags(&attributes, usage);
1309 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 status = psa_import_key(&attributes,
1312 key_material, sizeof(key_material),
1313 &key);
1314 TEST_EQUAL(status, expected_status);
1315 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1320 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1321 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1322 mbedtls_test_update_key_usage_flags(usage));
1323 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1324 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 PSA_ASSERT(psa_destroy_key(key));
1327 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001328
1329exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001330 /*
1331 * Key attributes may have been returned by psa_get_key_attributes()
1332 * thus reset them as required.
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 psa_destroy_key(key);
1337 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001338}
1339/* END_CASE */
1340
1341/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342void import_with_data(data_t *data, int type_arg,
1343 int attr_bits_arg,
1344 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001345{
1346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1347 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001349 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001350 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001351 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 psa_set_key_type(&attributes, type);
1357 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001360 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1361 *
1362 * This can happen with a type supported only by a driver:
1363 * - the driver sees the invalid data (for example wrong size) and thinks
1364 * "well perhaps this is a key size I don't support" so it returns
1365 * NOT_SUPPORTED which is correct at this point;
1366 * - we fallback to built-ins, which don't support this type, so return
1367 * NOT_SUPPORTED which again is correct at this point.
1368 */
1369 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1370 status == PSA_ERROR_NOT_SUPPORTED) {
1371 ; // OK
1372 } else {
1373 TEST_EQUAL(status, expected_status);
1374 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1380 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1381 if (attr_bits != 0) {
1382 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1383 }
1384 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 PSA_ASSERT(psa_destroy_key(key));
1387 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388
1389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001390 /*
1391 * Key attributes may have been returned by psa_get_key_attributes()
1392 * thus reset them as required.
1393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 psa_destroy_key(key);
1397 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398}
1399/* END_CASE */
1400
1401/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001402/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403void import_large_key(int type_arg, int byte_size_arg,
1404 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001405{
1406 psa_key_type_t type = type_arg;
1407 size_t byte_size = byte_size_arg;
1408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1409 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001410 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001411 psa_status_t status;
1412 uint8_t *buffer = NULL;
1413 size_t buffer_size = byte_size + 1;
1414 size_t n;
1415
Steven Cooreman69967ce2021-01-18 18:01:08 +01001416 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001417 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001418 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001422
1423 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1425 psa_set_key_type(&attributes, type);
1426 status = psa_import_key(&attributes, buffer, byte_size, &key);
1427 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1428 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (status == PSA_SUCCESS) {
1431 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1432 TEST_EQUAL(psa_get_key_type(&attributes), type);
1433 TEST_EQUAL(psa_get_key_bits(&attributes),
1434 PSA_BYTES_TO_BITS(byte_size));
1435 ASSERT_NO_SLOT_NUMBER(&attributes);
1436 memset(buffer, 0, byte_size + 1);
1437 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1438 for (n = 0; n < byte_size; n++) {
1439 TEST_EQUAL(buffer[n], 'K');
1440 }
1441 for (n = byte_size; n < buffer_size; n++) {
1442 TEST_EQUAL(buffer[n], 0);
1443 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001444 }
1445
1446exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001447 /*
1448 * Key attributes may have been returned by psa_get_key_attributes()
1449 * thus reset them as required.
1450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 psa_destroy_key(key);
1454 PSA_DONE();
1455 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001456}
1457/* END_CASE */
1458
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001459/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001460/* Import an RSA key with a valid structure (but not valid numbers
1461 * inside, beyond having sensible size and parity). This is expected to
1462 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001464{
Ronald Cron5425a212020-08-04 14:58:35 +02001465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001466 size_t bits = bits_arg;
1467 psa_status_t expected_status = expected_status_arg;
1468 psa_status_t status;
1469 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001470 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001473 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001474 unsigned char *p;
1475 int ret;
1476 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001480 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1483 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001484 length = ret;
1485
1486 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 psa_set_key_type(&attributes, type);
1488 status = psa_import_key(&attributes, p, length, &key);
1489 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (status == PSA_SUCCESS) {
1492 PSA_ASSERT(psa_destroy_key(key));
1493 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001494
1495exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 mbedtls_free(buffer);
1497 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001498}
1499/* END_CASE */
1500
1501/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502void import_export(data_t *data,
1503 int type_arg,
1504 int usage_arg, int alg_arg,
1505 int lifetime_arg,
1506 int expected_bits,
1507 int export_size_delta,
1508 int expected_export_status_arg,
1509 /*whether reexport must give the original input exactly*/
1510 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511{
Ronald Cron5425a212020-08-04 14:58:35 +02001512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001514 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001515 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301517 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518 unsigned char *exported = NULL;
1519 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001521 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001524 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525
Moran Pekercb088e72018-07-17 17:36:59 +03001526 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001527 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001529 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 }
1531 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 psa_set_key_lifetime(&attributes, lifetime);
1534 psa_set_key_usage_flags(&attributes, usage_arg);
1535 psa_set_key_algorithm(&attributes, alg);
1536 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001537
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001538 if (PSA_KEY_TYPE_IS_DH(type) &&
1539 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001540 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1541 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001542 }
1543
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001545 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001546 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547
1548 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1550 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1551 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1552 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553
1554 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 status = psa_export_key(key, exported, export_size, &exported_length);
1556 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001557
1558 /* The exported length must be set by psa_export_key() to a value between 0
1559 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1561 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1562 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1565 export_size - exported_length));
1566 if (status != PSA_SUCCESS) {
1567 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001569 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570
Gilles Peskineea38a922021-02-13 00:05:16 +01001571 /* Run sanity checks on the exported key. For non-canonical inputs,
1572 * this validates the canonical representations. For canonical inputs,
1573 * this doesn't directly validate the implementation, but it still helps
1574 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (!psa_key_lifetime_is_external(lifetime)) {
1576 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 }
Archana4d7ae1d2021-07-07 02:50:22 +05301579 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001582 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001584 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1586 &key2));
1587 PSA_ASSERT(psa_export_key(key2,
1588 reexported,
1589 export_size,
1590 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001591 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001592 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 TEST_LE_U(exported_length,
1596 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1597 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001598 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1599 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1600 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1601 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1602 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603
1604destroy:
1605 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 PSA_ASSERT(psa_destroy_key(key));
1607 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608
1609exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001610 /*
1611 * Key attributes may have been returned by psa_get_key_attributes()
1612 * thus reset them as required.
1613 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 psa_reset_key_attributes(&got_attributes);
1615 psa_destroy_key(key);
1616 mbedtls_free(exported);
1617 mbedtls_free(reexported);
1618 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619}
1620/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001621
Moran Pekerf709f4a2018-06-06 17:26:04 +03001622/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623void import_export_public_key(data_t *data,
1624 int type_arg, // key pair or public key
1625 int alg_arg,
1626 int lifetime_arg,
1627 int export_size_delta,
1628 int expected_export_status_arg,
1629 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001630{
Ronald Cron5425a212020-08-04 14:58:35 +02001631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001633 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001634 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001635 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301636 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001637 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001638 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001639 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 psa_set_key_lifetime(&attributes, lifetime);
1645 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1646 psa_set_key_algorithm(&attributes, alg);
1647 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001648
1649 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001651
Gilles Peskine49c25912018-10-29 15:15:31 +01001652 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001653 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 status = psa_export_public_key(key,
1655 exported, export_size,
1656 &exported_length);
1657 TEST_EQUAL(status, expected_export_status);
1658 if (status == PSA_SUCCESS) {
1659 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001660 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1662 bits = psa_get_key_bits(&attributes);
1663 TEST_LE_U(expected_public_key->len,
1664 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1665 TEST_LE_U(expected_public_key->len,
1666 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1667 TEST_LE_U(expected_public_key->len,
1668 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001669 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001670 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001671 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001672exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001673 /*
1674 * Key attributes may have been returned by psa_get_key_attributes()
1675 * thus reset them as required.
1676 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 mbedtls_free(exported);
1680 psa_destroy_key(key);
1681 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001682}
1683/* END_CASE */
1684
Gilles Peskine20035e32018-02-03 22:44:14 +01001685/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686void import_and_exercise_key(data_t *data,
1687 int type_arg,
1688 int bits_arg,
1689 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001690{
Ronald Cron5425a212020-08-04 14:58:35 +02001691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001692 psa_key_type_t type = type_arg;
1693 size_t bits = bits_arg;
1694 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001697 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 psa_set_key_usage_flags(&attributes, usage);
1702 psa_set_key_algorithm(&attributes, alg);
1703 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001704
1705 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001707
1708 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1710 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1711 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712
1713 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 PSA_ASSERT(psa_destroy_key(key));
1719 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001720
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001721exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001722 /*
1723 * Key attributes may have been returned by psa_get_key_attributes()
1724 * thus reset them as required.
1725 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 psa_reset_key_attributes(&attributes);
1729 psa_destroy_key(key);
1730 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001731}
1732/* END_CASE */
1733
1734/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735void effective_key_attributes(int type_arg, int expected_type_arg,
1736 int bits_arg, int expected_bits_arg,
1737 int usage_arg, int expected_usage_arg,
1738 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001739{
Ronald Cron5425a212020-08-04 14:58:35 +02001740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001741 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001742 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001743 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001744 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001745 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001746 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001747 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001748 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 psa_set_key_usage_flags(&attributes, usage);
1754 psa_set_key_algorithm(&attributes, alg);
1755 psa_set_key_type(&attributes, key_type);
1756 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 PSA_ASSERT(psa_generate_key(&attributes, &key));
1759 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1762 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1763 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1764 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1765 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001766
1767exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001768 /*
1769 * Key attributes may have been returned by psa_get_key_attributes()
1770 * thus reset them as required.
1771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_destroy_key(key);
1775 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001776}
1777/* END_CASE */
1778
1779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001780void check_key_policy(int type_arg, int bits_arg,
1781 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001782{
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1784 usage_arg,
1785 mbedtls_test_update_key_usage_flags(usage_arg),
1786 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001787 goto exit;
1788}
1789/* END_CASE */
1790
1791/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001792void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001793{
1794 /* Test each valid way of initializing the object, except for `= {0}`, as
1795 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1796 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001797 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1800 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1805 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1806 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 TEST_EQUAL(psa_get_key_type(&func), 0);
1809 TEST_EQUAL(psa_get_key_type(&init), 0);
1810 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 TEST_EQUAL(psa_get_key_bits(&func), 0);
1813 TEST_EQUAL(psa_get_key_bits(&init), 0);
1814 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1817 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1818 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1821 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1822 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001823}
1824/* END_CASE */
1825
1826/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827void mac_key_policy(int policy_usage_arg,
1828 int policy_alg_arg,
1829 int key_type_arg,
1830 data_t *key_data,
1831 int exercise_alg_arg,
1832 int expected_status_sign_arg,
1833 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001834{
Ronald Cron5425a212020-08-04 14:58:35 +02001835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001837 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001838 psa_key_type_t key_type = key_type_arg;
1839 psa_algorithm_t policy_alg = policy_alg_arg;
1840 psa_algorithm_t exercise_alg = exercise_alg_arg;
1841 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001843 psa_status_t expected_status_sign = expected_status_sign_arg;
1844 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 psa_set_key_usage_flags(&attributes, policy_usage);
1850 psa_set_key_algorithm(&attributes, policy_alg);
1851 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1854 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1857 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1860 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001861
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001862 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001864 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1866 input, 128,
1867 mac, PSA_MAC_MAX_SIZE, &mac_len),
1868 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001869
Neil Armstrong3af9b972022-02-07 12:20:21 +01001870 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 PSA_ASSERT(psa_mac_abort(&operation));
1872 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1873 if (status == PSA_SUCCESS) {
1874 status = psa_mac_update(&operation, input, 128);
1875 if (status == PSA_SUCCESS) {
1876 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1877 &mac_len),
1878 expected_status_sign);
1879 } else {
1880 TEST_EQUAL(status, expected_status_sign);
1881 }
1882 } else {
1883 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001886
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001887 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 status = psa_mac_verify(key, exercise_alg, input, 128,
1889 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1892 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1893 } else {
1894 TEST_EQUAL(status, expected_status_verify);
1895 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001896
Neil Armstrong3af9b972022-02-07 12:20:21 +01001897 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1899 if (status == PSA_SUCCESS) {
1900 status = psa_mac_update(&operation, input, 128);
1901 if (status == PSA_SUCCESS) {
1902 status = psa_mac_verify_finish(&operation, mac, mac_len);
1903 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1904 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1905 } else {
1906 TEST_EQUAL(status, expected_status_verify);
1907 }
1908 } else {
1909 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001910 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 } else {
1912 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001913 }
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 memset(mac, 0, sizeof(mac));
1918 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1919 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920
1921exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 psa_mac_abort(&operation);
1923 psa_destroy_key(key);
1924 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925}
1926/* END_CASE */
1927
1928/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929void cipher_key_policy(int policy_usage_arg,
1930 int policy_alg,
1931 int key_type,
1932 data_t *key_data,
1933 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934{
Ronald Cron5425a212020-08-04 14:58:35 +02001935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001937 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001938 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001939 size_t output_buffer_size = 0;
1940 size_t input_buffer_size = 0;
1941 size_t output_length = 0;
1942 uint8_t *output = NULL;
1943 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944 psa_status_t status;
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1947 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1948 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001949
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001950 TEST_CALLOC(input, input_buffer_size);
1951 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 psa_set_key_usage_flags(&attributes, policy_usage);
1956 psa_set_key_algorithm(&attributes, policy_alg);
1957 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1960 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001962 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 TEST_EQUAL(policy_usage,
1964 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001965
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001966 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1968 output, output_buffer_size,
1969 &output_length);
1970 if (policy_alg == exercise_alg &&
1971 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1972 PSA_ASSERT(status);
1973 } else {
1974 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1975 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001976
1977 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1979 if (policy_alg == exercise_alg &&
1980 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1981 PSA_ASSERT(status);
1982 } else {
1983 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1984 }
1985 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001987 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1989 input, input_buffer_size,
1990 &output_length);
1991 if (policy_alg == exercise_alg &&
1992 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1993 PSA_ASSERT(status);
1994 } else {
1995 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1996 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001997
1998 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2000 if (policy_alg == exercise_alg &&
2001 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2002 PSA_ASSERT(status);
2003 } else {
2004 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2005 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
2007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 psa_cipher_abort(&operation);
2009 mbedtls_free(input);
2010 mbedtls_free(output);
2011 psa_destroy_key(key);
2012 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013}
2014/* END_CASE */
2015
2016/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002017void aead_key_policy(int policy_usage_arg,
2018 int policy_alg,
2019 int key_type,
2020 data_t *key_data,
2021 int nonce_length_arg,
2022 int tag_length_arg,
2023 int exercise_alg,
2024 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025{
Ronald Cron5425a212020-08-04 14:58:35 +02002026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002028 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002029 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002031 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 size_t nonce_length = nonce_length_arg;
2034 unsigned char tag[16];
2035 size_t tag_length = tag_length_arg;
2036 size_t output_length;
2037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 TEST_LE_U(nonce_length, sizeof(nonce));
2039 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 psa_set_key_usage_flags(&attributes, policy_usage);
2044 psa_set_key_algorithm(&attributes, policy_alg);
2045 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2048 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002049
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002050 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 TEST_EQUAL(policy_usage,
2052 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002053
Neil Armstrong752d8112022-02-07 14:51:11 +01002054 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 status = psa_aead_encrypt(key, exercise_alg,
2056 nonce, nonce_length,
2057 NULL, 0,
2058 NULL, 0,
2059 tag, tag_length,
2060 &output_length);
2061 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2062 TEST_EQUAL(status, expected_status);
2063 } else {
2064 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2065 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
Neil Armstrong752d8112022-02-07 14:51:11 +01002067 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2069 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2070 TEST_EQUAL(status, expected_status);
2071 } else {
2072 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2073 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002074
2075 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 memset(tag, 0, sizeof(tag));
2077 status = psa_aead_decrypt(key, exercise_alg,
2078 nonce, nonce_length,
2079 NULL, 0,
2080 tag, tag_length,
2081 NULL, 0,
2082 &output_length);
2083 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2084 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2085 } else if (expected_status == PSA_SUCCESS) {
2086 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2087 } else {
2088 TEST_EQUAL(status, expected_status);
2089 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
Neil Armstrong752d8112022-02-07 14:51:11 +01002091 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 PSA_ASSERT(psa_aead_abort(&operation));
2093 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2094 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2095 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2096 } else {
2097 TEST_EQUAL(status, expected_status);
2098 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002099
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 PSA_ASSERT(psa_aead_abort(&operation));
2102 psa_destroy_key(key);
2103 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104}
2105/* END_CASE */
2106
2107/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108void asymmetric_encryption_key_policy(int policy_usage_arg,
2109 int policy_alg,
2110 int key_type,
2111 data_t *key_data,
2112 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113{
Ronald Cron5425a212020-08-04 14:58:35 +02002114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002116 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117 psa_status_t status;
2118 size_t key_bits;
2119 size_t buffer_length;
2120 unsigned char *buffer = NULL;
2121 size_t output_length;
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 psa_set_key_usage_flags(&attributes, policy_usage);
2126 psa_set_key_algorithm(&attributes, policy_alg);
2127 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2130 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002131
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002132 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 TEST_EQUAL(policy_usage,
2134 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2137 key_bits = psa_get_key_bits(&attributes);
2138 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2139 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002140 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 status = psa_asymmetric_encrypt(key, exercise_alg,
2143 NULL, 0,
2144 NULL, 0,
2145 buffer, buffer_length,
2146 &output_length);
2147 if (policy_alg == exercise_alg &&
2148 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2149 PSA_ASSERT(status);
2150 } else {
2151 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2152 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (buffer_length != 0) {
2155 memset(buffer, 0, buffer_length);
2156 }
2157 status = psa_asymmetric_decrypt(key, exercise_alg,
2158 buffer, buffer_length,
2159 NULL, 0,
2160 buffer, buffer_length,
2161 &output_length);
2162 if (policy_alg == exercise_alg &&
2163 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2164 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2165 } else {
2166 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2167 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168
2169exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002170 /*
2171 * Key attributes may have been returned by psa_get_key_attributes()
2172 * thus reset them as required.
2173 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 psa_destroy_key(key);
2177 PSA_DONE();
2178 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179}
2180/* END_CASE */
2181
2182/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183void asymmetric_signature_key_policy(int policy_usage_arg,
2184 int policy_alg,
2185 int key_type,
2186 data_t *key_data,
2187 int exercise_alg,
2188 int payload_length_arg,
2189 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002190{
Ronald Cron5425a212020-08-04 14:58:35 +02002191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002193 psa_key_usage_t policy_usage = policy_usage_arg;
2194 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002197 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2198 * compatible with the policy and `payload_length_arg` is supposed to be
2199 * a valid input length to sign. If `payload_length_arg <= 0`,
2200 * `exercise_alg` is supposed to be forbidden by the policy. */
2201 int compatible_alg = payload_length_arg > 0;
2202 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204 size_t signature_length;
2205
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002206 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002207 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 TEST_EQUAL(expected_usage,
2209 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 psa_set_key_usage_flags(&attributes, policy_usage);
2214 psa_set_key_algorithm(&attributes, policy_alg);
2215 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2218 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 status = psa_sign_hash(key, exercise_alg,
2223 payload, payload_length,
2224 signature, sizeof(signature),
2225 &signature_length);
2226 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2227 PSA_ASSERT(status);
2228 } else {
2229 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2230 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 memset(signature, 0, sizeof(signature));
2233 status = psa_verify_hash(key, exercise_alg,
2234 payload, payload_length,
2235 signature, sizeof(signature));
2236 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2237 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2238 } else {
2239 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2240 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2243 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2244 status = psa_sign_message(key, exercise_alg,
2245 payload, payload_length,
2246 signature, sizeof(signature),
2247 &signature_length);
2248 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2249 PSA_ASSERT(status);
2250 } else {
2251 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2252 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 memset(signature, 0, sizeof(signature));
2255 status = psa_verify_message(key, exercise_alg,
2256 payload, payload_length,
2257 signature, sizeof(signature));
2258 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2259 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2260 } else {
2261 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2262 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002263 }
2264
Gilles Peskined5b33222018-06-18 22:20:03 +02002265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 psa_destroy_key(key);
2267 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002268}
2269/* END_CASE */
2270
Janos Follathba3fab92019-06-11 14:50:16 +01002271/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272void derive_key_policy(int policy_usage,
2273 int policy_alg,
2274 int key_type,
2275 data_t *key_data,
2276 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002277{
Ronald Cron5425a212020-08-04 14:58:35 +02002278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002280 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002281 psa_status_t status;
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 psa_set_key_usage_flags(&attributes, policy_usage);
2286 psa_set_key_algorithm(&attributes, policy_alg);
2287 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2290 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2295 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2296 PSA_ASSERT(psa_key_derivation_input_bytes(
2297 &operation,
2298 PSA_KEY_DERIVATION_INPUT_SEED,
2299 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002300 }
Janos Follathba3fab92019-06-11 14:50:16 +01002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 status = psa_key_derivation_input_key(&operation,
2303 PSA_KEY_DERIVATION_INPUT_SECRET,
2304 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (policy_alg == exercise_alg &&
2307 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2308 PSA_ASSERT(status);
2309 } else {
2310 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2311 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002312
2313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 psa_key_derivation_abort(&operation);
2315 psa_destroy_key(key);
2316 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002317}
2318/* END_CASE */
2319
2320/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321void agreement_key_policy(int policy_usage,
2322 int policy_alg,
2323 int key_type_arg,
2324 data_t *key_data,
2325 int exercise_alg,
2326 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327{
Ronald Cron5425a212020-08-04 14:58:35 +02002328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002330 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002331 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002332 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002333 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 psa_set_key_usage_flags(&attributes, policy_usage);
2338 psa_set_key_algorithm(&attributes, policy_alg);
2339 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2342 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2345 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002348
2349exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 psa_key_derivation_abort(&operation);
2351 psa_destroy_key(key);
2352 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002353}
2354/* END_CASE */
2355
2356/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002357void key_policy_alg2(int key_type_arg, data_t *key_data,
2358 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002359{
Ronald Cron5425a212020-08-04 14:58:35 +02002360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002361 psa_key_type_t key_type = key_type_arg;
2362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2363 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2364 psa_key_usage_t usage = usage_arg;
2365 psa_algorithm_t alg = alg_arg;
2366 psa_algorithm_t alg2 = alg2_arg;
2367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 psa_set_key_usage_flags(&attributes, usage);
2371 psa_set_key_algorithm(&attributes, alg);
2372 psa_set_key_enrollment_algorithm(&attributes, alg2);
2373 psa_set_key_type(&attributes, key_type);
2374 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2375 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002376
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002377 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 usage = mbedtls_test_update_key_usage_flags(usage);
2379 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2380 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2381 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2382 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 }
2387 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002388 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002390
2391exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002392 /*
2393 * Key attributes may have been returned by psa_get_key_attributes()
2394 * thus reset them as required.
2395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 psa_destroy_key(key);
2399 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002400}
2401/* END_CASE */
2402
2403/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002404void raw_agreement_key_policy(int policy_usage,
2405 int policy_alg,
2406 int key_type_arg,
2407 data_t *key_data,
2408 int exercise_alg,
2409 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002410{
Ronald Cron5425a212020-08-04 14:58:35 +02002411 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002413 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002415 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 psa_set_key_usage_flags(&attributes, policy_usage);
2421 psa_set_key_algorithm(&attributes, policy_alg);
2422 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2425 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002430
2431exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 psa_key_derivation_abort(&operation);
2433 psa_destroy_key(key);
2434 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002435}
2436/* END_CASE */
2437
2438/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439void copy_success(int source_usage_arg,
2440 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002441 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 int type_arg, data_t *material,
2443 int copy_attributes,
2444 int target_usage_arg,
2445 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002446 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 int expected_usage_arg,
2448 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449{
Gilles Peskineca25db92019-04-19 11:43:08 +02002450 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2451 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002452 psa_key_usage_t expected_usage = expected_usage_arg;
2453 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002454 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302455 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2456 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002457 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2458 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459 uint8_t *export_buffer = NULL;
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002462
Gilles Peskineca25db92019-04-19 11:43:08 +02002463 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2465 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2466 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2467 psa_set_key_type(&source_attributes, type_arg);
2468 psa_set_key_lifetime(&source_attributes, source_lifetime);
2469 PSA_ASSERT(psa_import_key(&source_attributes,
2470 material->x, material->len,
2471 &source_key));
2472 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002473
Gilles Peskineca25db92019-04-19 11:43:08 +02002474 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002476 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002477 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (target_usage_arg != -1) {
2481 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2482 }
2483 if (target_alg_arg != -1) {
2484 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2485 }
2486 if (target_alg2_arg != -1) {
2487 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2488 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002489
Archana8a180362021-07-05 02:18:48 +05302490
Gilles Peskine57ab7212019-01-28 13:03:09 +01002491 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 PSA_ASSERT(psa_copy_key(source_key,
2493 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002494
2495 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497
2498 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2500 TEST_EQUAL(psa_get_key_type(&source_attributes),
2501 psa_get_key_type(&target_attributes));
2502 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2503 psa_get_key_bits(&target_attributes));
2504 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2505 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2506 TEST_EQUAL(expected_alg2,
2507 psa_get_key_enrollment_algorithm(&target_attributes));
2508 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002509 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002510 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2512 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002513 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002514 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002515 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (!psa_key_lifetime_is_external(target_lifetime)) {
2518 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302519 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 }
2521 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302522 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 }
Archana8a180362021-07-05 02:18:48 +05302524 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002527
2528exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002529 /*
2530 * Source and target key attributes may have been returned by
2531 * psa_get_key_attributes() thus reset them as required.
2532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 psa_reset_key_attributes(&source_attributes);
2534 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 PSA_DONE();
2537 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002538}
2539/* END_CASE */
2540
2541/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542void copy_fail(int source_usage_arg,
2543 int source_alg_arg, int source_alg2_arg,
2544 int source_lifetime_arg,
2545 int type_arg, data_t *material,
2546 int target_type_arg, int target_bits_arg,
2547 int target_usage_arg,
2548 int target_alg_arg, int target_alg2_arg,
2549 int target_id_arg, int target_lifetime_arg,
2550 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002551{
2552 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2553 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002554 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2555 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002559
2560 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2562 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2563 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2564 psa_set_key_type(&source_attributes, type_arg);
2565 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2566 PSA_ASSERT(psa_import_key(&source_attributes,
2567 material->x, material->len,
2568 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002569
2570 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 psa_set_key_id(&target_attributes, key_id);
2572 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2573 psa_set_key_type(&target_attributes, target_type_arg);
2574 psa_set_key_bits(&target_attributes, target_bits_arg);
2575 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2576 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2577 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002578
2579 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 TEST_EQUAL(psa_copy_key(source_key,
2581 &target_attributes, &target_key),
2582 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002585
Gilles Peskine4a644642019-05-03 17:14:08 +02002586exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 psa_reset_key_attributes(&source_attributes);
2588 psa_reset_key_attributes(&target_attributes);
2589 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002594void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002595{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002596 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002597 /* Test each valid way of initializing the object, except for `= {0}`, as
2598 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2599 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002600 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002602 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2603 psa_hash_operation_t zero;
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002606
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002607 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2609 PSA_ERROR_BAD_STATE);
2610 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2611 PSA_ERROR_BAD_STATE);
2612 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2613 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002614
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002615 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 PSA_ASSERT(psa_hash_abort(&func));
2617 PSA_ASSERT(psa_hash_abort(&init));
2618 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002619}
2620/* END_CASE */
2621
2622/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623void hash_setup(int alg_arg,
2624 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625{
2626 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002627 uint8_t *output = NULL;
2628 size_t output_size = 0;
2629 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002630 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002631 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632 psa_status_t status;
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002635
Neil Armstrongedb20862022-02-07 15:47:44 +01002636 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002638 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 status = psa_hash_compute(alg, NULL, 0,
2641 output, output_size, &output_length);
2642 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002643
2644 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 status = psa_hash_setup(&operation, alg);
2646 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002648 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002650
2651 /* If setup failed, reproduce the failure, so as to
2652 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (status != PSA_SUCCESS) {
2654 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2655 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002656
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002657 /* Now the operation object should be reusable. */
2658#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2660 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002661#endif
2662
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 mbedtls_free(output);
2665 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002666}
2667/* END_CASE */
2668
2669/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670void hash_compute_fail(int alg_arg, data_t *input,
2671 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002672{
2673 psa_algorithm_t alg = alg_arg;
2674 uint8_t *output = NULL;
2675 size_t output_size = output_size_arg;
2676 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002677 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002678 psa_status_t expected_status = expected_status_arg;
2679 psa_status_t status;
2680
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002681 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002684
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002685 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 status = psa_hash_compute(alg, input->x, input->len,
2687 output, output_size, &output_length);
2688 TEST_EQUAL(status, expected_status);
2689 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002690
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002691 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 status = psa_hash_setup(&operation, alg);
2693 if (status == PSA_SUCCESS) {
2694 status = psa_hash_update(&operation, input->x, input->len);
2695 if (status == PSA_SUCCESS) {
2696 status = psa_hash_finish(&operation, output, output_size,
2697 &output_length);
2698 if (status == PSA_SUCCESS) {
2699 TEST_LE_U(output_length, output_size);
2700 } else {
2701 TEST_EQUAL(status, expected_status);
2702 }
2703 } else {
2704 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002705 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 } else {
2707 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002708 }
2709
Gilles Peskine0a749c82019-11-28 19:33:58 +01002710exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 PSA_ASSERT(psa_hash_abort(&operation));
2712 mbedtls_free(output);
2713 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002714}
2715/* END_CASE */
2716
2717/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002718void hash_compare_fail(int alg_arg, data_t *input,
2719 data_t *reference_hash,
2720 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002721{
2722 psa_algorithm_t alg = alg_arg;
2723 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002724 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002725 psa_status_t status;
2726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002728
Neil Armstrong55a1be12022-02-07 11:23:20 +01002729 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 status = psa_hash_compare(alg, input->x, input->len,
2731 reference_hash->x, reference_hash->len);
2732 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002733
Neil Armstrong55a1be12022-02-07 11:23:20 +01002734 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 status = psa_hash_setup(&operation, alg);
2736 if (status == PSA_SUCCESS) {
2737 status = psa_hash_update(&operation, input->x, input->len);
2738 if (status == PSA_SUCCESS) {
2739 status = psa_hash_verify(&operation, reference_hash->x,
2740 reference_hash->len);
2741 TEST_EQUAL(status, expected_status);
2742 } else {
2743 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 } else {
2746 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002747 }
2748
Gilles Peskine88e08462020-01-28 20:43:00 +01002749exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 PSA_ASSERT(psa_hash_abort(&operation));
2751 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002752}
2753/* END_CASE */
2754
2755/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756void hash_compute_compare(int alg_arg, data_t *input,
2757 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002758{
2759 psa_algorithm_t alg = alg_arg;
2760 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2761 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002762 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002763 size_t i;
2764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002766
Neil Armstrongca30a002022-02-07 11:40:23 +01002767 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2769 output, PSA_HASH_LENGTH(alg),
2770 &output_length));
2771 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002772 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002773 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002774
Neil Armstrongca30a002022-02-07 11:40:23 +01002775 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 PSA_ASSERT(psa_hash_setup(&operation, alg));
2777 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2778 PSA_ASSERT(psa_hash_finish(&operation, output,
2779 PSA_HASH_LENGTH(alg),
2780 &output_length));
2781 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002782 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002783 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002784
2785 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2787 output, sizeof(output),
2788 &output_length));
2789 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002790 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002791 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002792
Neil Armstrongca30a002022-02-07 11:40:23 +01002793 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 PSA_ASSERT(psa_hash_setup(&operation, alg));
2795 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2796 PSA_ASSERT(psa_hash_finish(&operation, output,
2797 sizeof(output), &output_length));
2798 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002799 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002800 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002801
2802 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2804 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002805
Neil Armstrongca30a002022-02-07 11:40:23 +01002806 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 PSA_ASSERT(psa_hash_setup(&operation, alg));
2808 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2809 PSA_ASSERT(psa_hash_verify(&operation, output,
2810 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002811
2812 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2814 output, output_length + 1),
2815 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002816
Neil Armstrongca30a002022-02-07 11:40:23 +01002817 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 PSA_ASSERT(psa_hash_setup(&operation, alg));
2819 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2820 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2821 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002822
2823 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2825 output, output_length - 1),
2826 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002827
Neil Armstrongca30a002022-02-07 11:40:23 +01002828 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 PSA_ASSERT(psa_hash_setup(&operation, alg));
2830 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2831 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2832 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002833
Gilles Peskine0a749c82019-11-28 19:33:58 +01002834 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 for (i = 0; i < output_length; i++) {
2836 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002837 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002838
2839 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2841 output, output_length),
2842 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002843
2844 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 PSA_ASSERT(psa_hash_setup(&operation, alg));
2846 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2847 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2848 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002849
Gilles Peskine0a749c82019-11-28 19:33:58 +01002850 output[i] ^= 1;
2851 }
2852
2853exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 PSA_ASSERT(psa_hash_abort(&operation));
2855 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002856}
2857/* END_CASE */
2858
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002859/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002860void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002861{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002863 unsigned char input[] = "";
2864 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002865 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002866 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2867 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2869 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002870 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002871 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002872 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002875
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002876 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 PSA_ASSERT(psa_hash_setup(&operation, alg));
2878 ASSERT_OPERATION_IS_ACTIVE(operation);
2879 TEST_EQUAL(psa_hash_setup(&operation, alg),
2880 PSA_ERROR_BAD_STATE);
2881 ASSERT_OPERATION_IS_INACTIVE(operation);
2882 PSA_ASSERT(psa_hash_abort(&operation));
2883 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002884
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002885 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2887 PSA_ERROR_BAD_STATE);
2888 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002889
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002890 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002892 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 ASSERT_OPERATION_IS_ACTIVE(operation);
2894 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2895 PSA_ERROR_BAD_STATE);
2896 ASSERT_OPERATION_IS_INACTIVE(operation);
2897 PSA_ASSERT(psa_hash_abort(&operation));
2898 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002899
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002900 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 PSA_ASSERT(psa_hash_setup(&operation, alg));
2902 PSA_ASSERT(psa_hash_finish(&operation,
2903 hash, sizeof(hash), &hash_len));
2904 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2905 PSA_ERROR_BAD_STATE);
2906 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002907
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002908 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 TEST_EQUAL(psa_hash_verify(&operation,
2910 valid_hash, sizeof(valid_hash)),
2911 PSA_ERROR_BAD_STATE);
2912 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002913
2914 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 PSA_ASSERT(psa_hash_setup(&operation, alg));
2916 PSA_ASSERT(psa_hash_finish(&operation,
2917 hash, sizeof(hash), &hash_len));
2918 TEST_EQUAL(psa_hash_verify(&operation,
2919 valid_hash, sizeof(valid_hash)),
2920 PSA_ERROR_BAD_STATE);
2921 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002922
2923 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 PSA_ASSERT(psa_hash_setup(&operation, alg));
2925 ASSERT_OPERATION_IS_ACTIVE(operation);
2926 PSA_ASSERT(psa_hash_verify(&operation,
2927 valid_hash, sizeof(valid_hash)));
2928 ASSERT_OPERATION_IS_INACTIVE(operation);
2929 TEST_EQUAL(psa_hash_verify(&operation,
2930 valid_hash, sizeof(valid_hash)),
2931 PSA_ERROR_BAD_STATE);
2932 ASSERT_OPERATION_IS_INACTIVE(operation);
2933 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002934
2935 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 TEST_EQUAL(psa_hash_finish(&operation,
2937 hash, sizeof(hash), &hash_len),
2938 PSA_ERROR_BAD_STATE);
2939 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002940
2941 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 PSA_ASSERT(psa_hash_setup(&operation, alg));
2943 PSA_ASSERT(psa_hash_finish(&operation,
2944 hash, sizeof(hash), &hash_len));
2945 TEST_EQUAL(psa_hash_finish(&operation,
2946 hash, sizeof(hash), &hash_len),
2947 PSA_ERROR_BAD_STATE);
2948 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002949
2950 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 PSA_ASSERT(psa_hash_setup(&operation, alg));
2952 PSA_ASSERT(psa_hash_verify(&operation,
2953 valid_hash, sizeof(valid_hash)));
2954 TEST_EQUAL(psa_hash_finish(&operation,
2955 hash, sizeof(hash), &hash_len),
2956 PSA_ERROR_BAD_STATE);
2957 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002958
2959exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002961}
2962/* END_CASE */
2963
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002964/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002966{
2967 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002968 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2969 * appended to it */
2970 unsigned char hash[] = {
2971 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2972 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2974 };
2975 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002976 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002979
itayzafrir27e69452018-11-01 14:26:34 +02002980 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 PSA_ASSERT(psa_hash_setup(&operation, alg));
2982 ASSERT_OPERATION_IS_ACTIVE(operation);
2983 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2984 PSA_ERROR_INVALID_SIGNATURE);
2985 ASSERT_OPERATION_IS_INACTIVE(operation);
2986 PSA_ASSERT(psa_hash_abort(&operation));
2987 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002988
itayzafrir27e69452018-11-01 14:26:34 +02002989 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 PSA_ASSERT(psa_hash_setup(&operation, alg));
2991 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2992 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002993
itayzafrir27e69452018-11-01 14:26:34 +02002994 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 PSA_ASSERT(psa_hash_setup(&operation, alg));
2996 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2997 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002998
itayzafrirec93d302018-10-18 18:01:10 +03002999exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003001}
3002/* END_CASE */
3003
Ronald Cronee414c72021-03-18 18:50:08 +01003004/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003005void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003006{
3007 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003008 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003010 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003011 size_t hash_len;
3012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003014
itayzafrir58028322018-10-25 10:22:01 +03003015 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 PSA_ASSERT(psa_hash_setup(&operation, alg));
3017 TEST_EQUAL(psa_hash_finish(&operation,
3018 hash, expected_size - 1, &hash_len),
3019 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003020
3021exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003023}
3024/* END_CASE */
3025
Ronald Cronee414c72021-03-18 18:50:08 +01003026/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003028{
3029 psa_algorithm_t alg = PSA_ALG_SHA_256;
3030 unsigned char hash[PSA_HASH_MAX_SIZE];
3031 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3032 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3033 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3034 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3035 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3036 size_t hash_len;
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 PSA_ASSERT(psa_crypto_init());
3039 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3042 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3043 PSA_ASSERT(psa_hash_finish(&op_finished,
3044 hash, sizeof(hash), &hash_len));
3045 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3046 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3049 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3052 PSA_ASSERT(psa_hash_finish(&op_init,
3053 hash, sizeof(hash), &hash_len));
3054 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3055 PSA_ASSERT(psa_hash_finish(&op_finished,
3056 hash, sizeof(hash), &hash_len));
3057 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3058 PSA_ASSERT(psa_hash_finish(&op_aborted,
3059 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003060
3061exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 psa_hash_abort(&op_source);
3063 psa_hash_abort(&op_init);
3064 psa_hash_abort(&op_setup);
3065 psa_hash_abort(&op_finished);
3066 psa_hash_abort(&op_aborted);
3067 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003068}
3069/* END_CASE */
3070
Ronald Cronee414c72021-03-18 18:50:08 +01003071/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003072void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003073{
3074 psa_algorithm_t alg = PSA_ALG_SHA_256;
3075 unsigned char hash[PSA_HASH_MAX_SIZE];
3076 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3077 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3078 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3079 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3080 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3081 size_t hash_len;
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3086 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3087 PSA_ASSERT(psa_hash_finish(&op_finished,
3088 hash, sizeof(hash), &hash_len));
3089 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3090 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3093 PSA_ASSERT(psa_hash_finish(&op_target,
3094 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3097 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3098 PSA_ERROR_BAD_STATE);
3099 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3100 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003101
3102exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 psa_hash_abort(&op_target);
3104 psa_hash_abort(&op_init);
3105 psa_hash_abort(&op_setup);
3106 psa_hash_abort(&op_finished);
3107 psa_hash_abort(&op_aborted);
3108 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003109}
3110/* END_CASE */
3111
itayzafrir58028322018-10-25 10:22:01 +03003112/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003114{
Jaeden Amero252ef282019-02-15 14:05:35 +00003115 const uint8_t input[1] = { 0 };
3116
Jaeden Amero769ce272019-01-04 11:48:03 +00003117 /* Test each valid way of initializing the object, except for `= {0}`, as
3118 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3119 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003120 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003122 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3123 psa_mac_operation_t zero;
3124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003126
Jaeden Amero252ef282019-02-15 14:05:35 +00003127 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 TEST_EQUAL(psa_mac_update(&func,
3129 input, sizeof(input)),
3130 PSA_ERROR_BAD_STATE);
3131 TEST_EQUAL(psa_mac_update(&init,
3132 input, sizeof(input)),
3133 PSA_ERROR_BAD_STATE);
3134 TEST_EQUAL(psa_mac_update(&zero,
3135 input, sizeof(input)),
3136 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003137
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003138 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 PSA_ASSERT(psa_mac_abort(&func));
3140 PSA_ASSERT(psa_mac_abort(&init));
3141 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003142}
3143/* END_CASE */
3144
3145/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003146void mac_setup(int key_type_arg,
3147 data_t *key,
3148 int alg_arg,
3149 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003151 psa_key_type_t key_type = key_type_arg;
3152 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003153 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003154 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003155 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3156#if defined(KNOWN_SUPPORTED_MAC_ALG)
3157 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3158#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003161
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3163 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 }
3166 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003168 /* The operation object should be reusable. */
3169#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3171 smoke_test_key_data,
3172 sizeof(smoke_test_key_data),
3173 KNOWN_SUPPORTED_MAC_ALG,
3174 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 }
3177 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003178#endif
3179
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003182}
3183/* END_CASE */
3184
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003185/* 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 +01003186void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003187{
Ronald Cron5425a212020-08-04 14:58:35 +02003188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003189 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3190 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003191 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003192 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3193 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3195 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003197 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3198 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3199 size_t sign_mac_length = 0;
3200 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3201 const uint8_t verify_mac[] = {
3202 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3203 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3205 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 PSA_ASSERT(psa_crypto_init());
3208 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3209 psa_set_key_algorithm(&attributes, alg);
3210 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3213 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003214
Jaeden Amero252ef282019-02-15 14:05:35 +00003215 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3217 PSA_ERROR_BAD_STATE);
3218 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003219
3220 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3222 &sign_mac_length),
3223 PSA_ERROR_BAD_STATE);
3224 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003225
3226 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 TEST_EQUAL(psa_mac_verify_finish(&operation,
3228 verify_mac, sizeof(verify_mac)),
3229 PSA_ERROR_BAD_STATE);
3230 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003231
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003232 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3234 ASSERT_OPERATION_IS_ACTIVE(operation);
3235 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3236 PSA_ERROR_BAD_STATE);
3237 ASSERT_OPERATION_IS_INACTIVE(operation);
3238 PSA_ASSERT(psa_mac_abort(&operation));
3239 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003240
Jaeden Amero252ef282019-02-15 14:05:35 +00003241 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3243 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3244 PSA_ASSERT(psa_mac_sign_finish(&operation,
3245 sign_mac, sizeof(sign_mac),
3246 &sign_mac_length));
3247 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3248 PSA_ERROR_BAD_STATE);
3249 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003250
3251 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3253 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3254 PSA_ASSERT(psa_mac_verify_finish(&operation,
3255 verify_mac, sizeof(verify_mac)));
3256 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3257 PSA_ERROR_BAD_STATE);
3258 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003259
3260 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3262 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3263 PSA_ASSERT(psa_mac_sign_finish(&operation,
3264 sign_mac, sizeof(sign_mac),
3265 &sign_mac_length));
3266 TEST_EQUAL(psa_mac_sign_finish(&operation,
3267 sign_mac, sizeof(sign_mac),
3268 &sign_mac_length),
3269 PSA_ERROR_BAD_STATE);
3270 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003271
3272 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3274 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3275 PSA_ASSERT(psa_mac_verify_finish(&operation,
3276 verify_mac, sizeof(verify_mac)));
3277 TEST_EQUAL(psa_mac_verify_finish(&operation,
3278 verify_mac, sizeof(verify_mac)),
3279 PSA_ERROR_BAD_STATE);
3280 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003281
3282 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3284 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3285 ASSERT_OPERATION_IS_ACTIVE(operation);
3286 TEST_EQUAL(psa_mac_verify_finish(&operation,
3287 verify_mac, sizeof(verify_mac)),
3288 PSA_ERROR_BAD_STATE);
3289 ASSERT_OPERATION_IS_INACTIVE(operation);
3290 PSA_ASSERT(psa_mac_abort(&operation));
3291 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003292
3293 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3295 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3296 ASSERT_OPERATION_IS_ACTIVE(operation);
3297 TEST_EQUAL(psa_mac_sign_finish(&operation,
3298 sign_mac, sizeof(sign_mac),
3299 &sign_mac_length),
3300 PSA_ERROR_BAD_STATE);
3301 ASSERT_OPERATION_IS_INACTIVE(operation);
3302 PSA_ASSERT(psa_mac_abort(&operation));
3303 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003304
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003306
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003309}
3310/* END_CASE */
3311
3312/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003313void mac_sign_verify_multi(int key_type_arg,
3314 data_t *key_data,
3315 int alg_arg,
3316 data_t *input,
3317 int is_verify,
3318 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003319{
3320 size_t data_part_len = 0;
3321
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003323 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003325
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 if (mac_multipart_internal_func(key_type_arg, key_data,
3327 alg_arg,
3328 input, data_part_len,
3329 expected_mac,
3330 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003331 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003333
3334 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003336
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 if (mac_multipart_internal_func(key_type_arg, key_data,
3338 alg_arg,
3339 input, data_part_len,
3340 expected_mac,
3341 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003342 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003344 }
3345
3346 /* Goto is required to silence warnings about unused labels, as we
3347 * don't actually do any test assertions in this function. */
3348 goto exit;
3349}
3350/* END_CASE */
3351
3352/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003353void mac_sign(int key_type_arg,
3354 data_t *key_data,
3355 int alg_arg,
3356 data_t *input,
3357 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358{
Ronald Cron5425a212020-08-04 14:58:35 +02003359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003360 psa_key_type_t key_type = key_type_arg;
3361 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003362 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003364 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003365 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003367 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003368 const size_t output_sizes_to_test[] = {
3369 0,
3370 1,
3371 expected_mac->len - 1,
3372 expected_mac->len,
3373 expected_mac->len + 1,
3374 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003375
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003377 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3383 psa_set_key_algorithm(&attributes, alg);
3384 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3387 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003390 const size_t output_size = output_sizes_to_test[i];
3391 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 (output_size >= expected_mac->len ? PSA_SUCCESS :
3393 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003396 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003397
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003398 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 TEST_EQUAL(psa_mac_compute(key, alg,
3400 input->x, input->len,
3401 actual_mac, output_size, &mac_length),
3402 expected_status);
3403 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003404 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003405 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003406 }
3407
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (output_size > 0) {
3409 memset(actual_mac, 0, output_size);
3410 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003411
3412 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3414 PSA_ASSERT(psa_mac_update(&operation,
3415 input->x, input->len));
3416 TEST_EQUAL(psa_mac_sign_finish(&operation,
3417 actual_mac, output_size,
3418 &mac_length),
3419 expected_status);
3420 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003423 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003424 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003425 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003427 actual_mac = NULL;
3428 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003429
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003430exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 psa_mac_abort(&operation);
3432 psa_destroy_key(key);
3433 PSA_DONE();
3434 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003435}
3436/* END_CASE */
3437
3438/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003439void mac_verify(int key_type_arg,
3440 data_t *key_data,
3441 int alg_arg,
3442 data_t *input,
3443 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003444{
Ronald Cron5425a212020-08-04 14:58:35 +02003445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003446 psa_key_type_t key_type = key_type_arg;
3447 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003448 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003450 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003453
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003455
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3457 psa_set_key_algorithm(&attributes, alg);
3458 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003459
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3461 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003462
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003463 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3465 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003466
3467 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3469 PSA_ASSERT(psa_mac_update(&operation,
3470 input->x, input->len));
3471 PSA_ASSERT(psa_mac_verify_finish(&operation,
3472 expected_mac->x,
3473 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003474
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003475 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 TEST_EQUAL(psa_mac_verify(key, alg,
3477 input->x, input->len,
3478 expected_mac->x,
3479 expected_mac->len - 1),
3480 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003481
3482 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3484 PSA_ASSERT(psa_mac_update(&operation,
3485 input->x, input->len));
3486 TEST_EQUAL(psa_mac_verify_finish(&operation,
3487 expected_mac->x,
3488 expected_mac->len - 1),
3489 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003490
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003491 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003492 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3494 TEST_EQUAL(psa_mac_verify(key, alg,
3495 input->x, input->len,
3496 perturbed_mac, expected_mac->len + 1),
3497 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003498
3499 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3501 PSA_ASSERT(psa_mac_update(&operation,
3502 input->x, input->len));
3503 TEST_EQUAL(psa_mac_verify_finish(&operation,
3504 perturbed_mac,
3505 expected_mac->len + 1),
3506 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003507
3508 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 for (size_t i = 0; i < expected_mac->len; i++) {
3510 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003511 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003512
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 TEST_EQUAL(psa_mac_verify(key, alg,
3514 input->x, input->len,
3515 perturbed_mac, expected_mac->len),
3516 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003517
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3519 PSA_ASSERT(psa_mac_update(&operation,
3520 input->x, input->len));
3521 TEST_EQUAL(psa_mac_verify_finish(&operation,
3522 perturbed_mac,
3523 expected_mac->len),
3524 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003525 perturbed_mac[i] ^= 1;
3526 }
3527
Gilles Peskine8c9def32018-02-08 10:02:12 +01003528exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 psa_mac_abort(&operation);
3530 psa_destroy_key(key);
3531 PSA_DONE();
3532 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003533}
3534/* END_CASE */
3535
3536/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003537void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003538{
Jaeden Ameroab439972019-02-15 14:12:05 +00003539 const uint8_t input[1] = { 0 };
3540 unsigned char output[1] = { 0 };
3541 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003542 /* Test each valid way of initializing the object, except for `= {0}`, as
3543 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3544 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003545 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003547 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3548 psa_cipher_operation_t zero;
3549
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003551
Jaeden Ameroab439972019-02-15 14:12:05 +00003552 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 TEST_EQUAL(psa_cipher_update(&func,
3554 input, sizeof(input),
3555 output, sizeof(output),
3556 &output_length),
3557 PSA_ERROR_BAD_STATE);
3558 TEST_EQUAL(psa_cipher_update(&init,
3559 input, sizeof(input),
3560 output, sizeof(output),
3561 &output_length),
3562 PSA_ERROR_BAD_STATE);
3563 TEST_EQUAL(psa_cipher_update(&zero,
3564 input, sizeof(input),
3565 output, sizeof(output),
3566 &output_length),
3567 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003568
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003569 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 PSA_ASSERT(psa_cipher_abort(&func));
3571 PSA_ASSERT(psa_cipher_abort(&init));
3572 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003573}
3574/* END_CASE */
3575
3576/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003577void cipher_setup(int key_type_arg,
3578 data_t *key,
3579 int alg_arg,
3580 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003581{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003582 psa_key_type_t key_type = key_type_arg;
3583 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003584 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003585 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003586 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003587#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003588 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3589#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003590
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003592
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3594 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003595 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 }
3597 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003598
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003599 /* The operation object should be reusable. */
3600#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3602 smoke_test_key_data,
3603 sizeof(smoke_test_key_data),
3604 KNOWN_SUPPORTED_CIPHER_ALG,
3605 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 }
3608 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003609#endif
3610
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 psa_cipher_abort(&operation);
3613 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003614}
3615/* END_CASE */
3616
Ronald Cronee414c72021-03-18 18:50:08 +01003617/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003618void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003619{
Ronald Cron5425a212020-08-04 14:58:35 +02003620 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003621 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3622 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003624 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003625 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003626 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003627 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 0xaa, 0xaa, 0xaa, 0xaa
3629 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003630 const uint8_t text[] = {
3631 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003632 0xbb, 0xbb, 0xbb, 0xbb
3633 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003634 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003635 size_t length = 0;
3636
Gilles Peskine449bd832023-01-11 14:50:10 +01003637 PSA_ASSERT(psa_crypto_init());
3638 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3639 psa_set_key_algorithm(&attributes, alg);
3640 psa_set_key_type(&attributes, key_type);
3641 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3642 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003643
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003644 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003645 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3646 ASSERT_OPERATION_IS_ACTIVE(operation);
3647 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3648 PSA_ERROR_BAD_STATE);
3649 ASSERT_OPERATION_IS_INACTIVE(operation);
3650 PSA_ASSERT(psa_cipher_abort(&operation));
3651 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003652
3653 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3655 ASSERT_OPERATION_IS_ACTIVE(operation);
3656 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3657 PSA_ERROR_BAD_STATE);
3658 ASSERT_OPERATION_IS_INACTIVE(operation);
3659 PSA_ASSERT(psa_cipher_abort(&operation));
3660 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003661
Jaeden Ameroab439972019-02-15 14:12:05 +00003662 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3664 buffer, sizeof(buffer),
3665 &length),
3666 PSA_ERROR_BAD_STATE);
3667 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003668
3669 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3671 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3672 buffer, sizeof(buffer),
3673 &length));
3674 ASSERT_OPERATION_IS_ACTIVE(operation);
3675 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3676 buffer, sizeof(buffer),
3677 &length),
3678 PSA_ERROR_BAD_STATE);
3679 ASSERT_OPERATION_IS_INACTIVE(operation);
3680 PSA_ASSERT(psa_cipher_abort(&operation));
3681 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003682
3683 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003684 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3685 PSA_ASSERT(psa_cipher_set_iv(&operation,
3686 iv, sizeof(iv)));
3687 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3688 buffer, sizeof(buffer),
3689 &length),
3690 PSA_ERROR_BAD_STATE);
3691 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003692
3693 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 TEST_EQUAL(psa_cipher_set_iv(&operation,
3695 iv, sizeof(iv)),
3696 PSA_ERROR_BAD_STATE);
3697 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003698
3699 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3701 PSA_ASSERT(psa_cipher_set_iv(&operation,
3702 iv, sizeof(iv)));
3703 ASSERT_OPERATION_IS_ACTIVE(operation);
3704 TEST_EQUAL(psa_cipher_set_iv(&operation,
3705 iv, sizeof(iv)),
3706 PSA_ERROR_BAD_STATE);
3707 ASSERT_OPERATION_IS_INACTIVE(operation);
3708 PSA_ASSERT(psa_cipher_abort(&operation));
3709 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003710
3711 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003712 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3713 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3714 buffer, sizeof(buffer),
3715 &length));
3716 TEST_EQUAL(psa_cipher_set_iv(&operation,
3717 iv, sizeof(iv)),
3718 PSA_ERROR_BAD_STATE);
3719 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003720
3721 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 TEST_EQUAL(psa_cipher_update(&operation,
3723 text, sizeof(text),
3724 buffer, sizeof(buffer),
3725 &length),
3726 PSA_ERROR_BAD_STATE);
3727 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003728
3729 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3731 ASSERT_OPERATION_IS_ACTIVE(operation);
3732 TEST_EQUAL(psa_cipher_update(&operation,
3733 text, sizeof(text),
3734 buffer, sizeof(buffer),
3735 &length),
3736 PSA_ERROR_BAD_STATE);
3737 ASSERT_OPERATION_IS_INACTIVE(operation);
3738 PSA_ASSERT(psa_cipher_abort(&operation));
3739 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003740
3741 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3743 PSA_ASSERT(psa_cipher_set_iv(&operation,
3744 iv, sizeof(iv)));
3745 PSA_ASSERT(psa_cipher_finish(&operation,
3746 buffer, sizeof(buffer), &length));
3747 TEST_EQUAL(psa_cipher_update(&operation,
3748 text, sizeof(text),
3749 buffer, sizeof(buffer),
3750 &length),
3751 PSA_ERROR_BAD_STATE);
3752 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003753
3754 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 TEST_EQUAL(psa_cipher_finish(&operation,
3756 buffer, sizeof(buffer), &length),
3757 PSA_ERROR_BAD_STATE);
3758 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003759
3760 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003762 /* Not calling update means we are encrypting an empty buffer, which is OK
3763 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003764 ASSERT_OPERATION_IS_ACTIVE(operation);
3765 TEST_EQUAL(psa_cipher_finish(&operation,
3766 buffer, sizeof(buffer), &length),
3767 PSA_ERROR_BAD_STATE);
3768 ASSERT_OPERATION_IS_INACTIVE(operation);
3769 PSA_ASSERT(psa_cipher_abort(&operation));
3770 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003771
3772 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3774 PSA_ASSERT(psa_cipher_set_iv(&operation,
3775 iv, sizeof(iv)));
3776 PSA_ASSERT(psa_cipher_finish(&operation,
3777 buffer, sizeof(buffer), &length));
3778 TEST_EQUAL(psa_cipher_finish(&operation,
3779 buffer, sizeof(buffer), &length),
3780 PSA_ERROR_BAD_STATE);
3781 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003782
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003784
Jaeden Ameroab439972019-02-15 14:12:05 +00003785exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 psa_cipher_abort(&operation);
3787 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003788}
3789/* END_CASE */
3790
3791/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003792void cipher_encrypt_fail(int alg_arg,
3793 int key_type_arg,
3794 data_t *key_data,
3795 data_t *input,
3796 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003797{
Ronald Cron5425a212020-08-04 14:58:35 +02003798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003799 psa_status_t status;
3800 psa_key_type_t key_type = key_type_arg;
3801 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003802 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003804 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3805 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003806 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003807 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003808 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003809 size_t function_output_length;
3810 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 if (PSA_ERROR_BAD_STATE != expected_status) {
3814 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003815
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3817 psa_set_key_algorithm(&attributes, alg);
3818 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003819
Gilles Peskine449bd832023-01-11 14:50:10 +01003820 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3821 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003822 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003823
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3825 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003826 }
3827
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003828 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3830 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003831
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003833
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003834 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003835 status = psa_cipher_encrypt_setup(&operation, key, alg);
3836 if (status == PSA_SUCCESS) {
3837 if (alg != PSA_ALG_ECB_NO_PADDING) {
3838 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3839 iv, iv_size,
3840 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003841 }
3842
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 status = psa_cipher_update(&operation, input->x, input->len,
3844 output, output_buffer_size,
3845 &function_output_length);
3846 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003847 output_length += function_output_length;
3848
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 status = psa_cipher_finish(&operation, output + output_length,
3850 output_buffer_size - output_length,
3851 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 TEST_EQUAL(status, expected_status);
3854 } else {
3855 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003856 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 } else {
3858 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003859 }
3860
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003861exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 psa_cipher_abort(&operation);
3863 mbedtls_free(output);
3864 psa_destroy_key(key);
3865 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003866}
3867/* END_CASE */
3868
3869/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003870void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3871 data_t *input, int iv_length,
3872 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003873{
3874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3875 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3877 size_t output_buffer_size = 0;
3878 unsigned char *output = NULL;
3879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003881 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003882
Gilles Peskine449bd832023-01-11 14:50:10 +01003883 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003884
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3886 psa_set_key_algorithm(&attributes, alg);
3887 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003888
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3890 &key));
3891 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3892 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3893 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003894
3895exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 psa_cipher_abort(&operation);
3897 mbedtls_free(output);
3898 psa_destroy_key(key);
3899 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003900}
3901/* END_CASE */
3902
3903/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003904void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3905 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906{
3907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3908 psa_key_type_t key_type = key_type_arg;
3909 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003910 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3911 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003912 unsigned char *output = NULL;
3913 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003914 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3916
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003918
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003919 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 TEST_LE_U(ciphertext->len,
3921 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3922 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3923 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3924 TEST_LE_U(plaintext->len,
3925 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3926 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3927 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003928
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003929
3930 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 psa_set_key_usage_flags(&attributes,
3932 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3933 psa_set_key_algorithm(&attributes, alg);
3934 psa_set_key_type(&attributes, key_type);
3935 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3936 &key));
3937 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3938 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003939 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003940
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003941 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3943 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3944 PSA_ERROR_BAD_STATE);
3945 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3946 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3947 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003948
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003949 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3951 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3952 &length),
3953 PSA_ERROR_BAD_STATE);
3954 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3955 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3956 &length),
3957 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003958
Gilles Peskine286c3142022-04-20 17:09:38 +02003959 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003961 output_length = 0;
3962 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 PSA_ASSERT(psa_cipher_update(&operation,
3964 plaintext->x, plaintext->len,
3965 output, output_buffer_size,
3966 &length));
3967 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 PSA_ASSERT(psa_cipher_finish(&operation,
3970 mbedtls_buffer_offset(output, output_length),
3971 output_buffer_size - output_length,
3972 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003973 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003974 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003975 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003976
Gilles Peskine286c3142022-04-20 17:09:38 +02003977 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003979 output_length = 0;
3980 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 PSA_ASSERT(psa_cipher_update(&operation,
3982 ciphertext->x, ciphertext->len,
3983 output, output_buffer_size,
3984 &length));
3985 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003986 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 PSA_ASSERT(psa_cipher_finish(&operation,
3988 mbedtls_buffer_offset(output, output_length),
3989 output_buffer_size - output_length,
3990 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003991 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003992 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003993 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003994
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003995 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003996 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3998 output, output_buffer_size,
3999 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004000 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004001 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004002
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004003 /* One-shot decryption */
4004 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4006 output, output_buffer_size,
4007 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004008 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004009 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004010
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004011exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004012 PSA_ASSERT(psa_cipher_abort(&operation));
4013 mbedtls_free(output);
4014 psa_cipher_abort(&operation);
4015 psa_destroy_key(key);
4016 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004017}
4018/* END_CASE */
4019
4020/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004021void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004022{
4023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4024 psa_algorithm_t alg = alg_arg;
4025 psa_key_type_t key_type = key_type_arg;
4026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4027 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4028 psa_status_t status;
4029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4033 psa_set_key_algorithm(&attributes, alg);
4034 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004035
4036 /* Usage of either of these two size macros would cause divide by zero
4037 * with incorrect key types previously. Input length should be irrelevant
4038 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4040 0);
4041 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004042
4043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4045 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004046
4047 /* Should fail due to invalid alg type (to support invalid key type).
4048 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004052
4053exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 psa_cipher_abort(&operation);
4055 psa_destroy_key(key);
4056 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004057}
4058/* END_CASE */
4059
4060/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004061void cipher_encrypt_validation(int alg_arg,
4062 int key_type_arg,
4063 data_t *key_data,
4064 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004065{
4066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4067 psa_key_type_t key_type = key_type_arg;
4068 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004070 unsigned char *output1 = NULL;
4071 size_t output1_buffer_size = 0;
4072 size_t output1_length = 0;
4073 unsigned char *output2 = NULL;
4074 size_t output2_buffer_size = 0;
4075 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004077 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004081
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4083 psa_set_key_algorithm(&attributes, alg);
4084 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4087 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4088 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004089 TEST_CALLOC(output1, output1_buffer_size);
4090 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4093 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004094
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004095 /* The one-shot cipher encryption uses generated iv so validating
4096 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4098 output1_buffer_size, &output1_length));
4099 TEST_LE_U(output1_length,
4100 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4101 TEST_LE_U(output1_length,
4102 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4105 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 PSA_ASSERT(psa_cipher_update(&operation,
4108 input->x, input->len,
4109 output2, output2_buffer_size,
4110 &function_output_length));
4111 TEST_LE_U(function_output_length,
4112 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4113 TEST_LE_U(function_output_length,
4114 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004115 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 PSA_ASSERT(psa_cipher_finish(&operation,
4118 output2 + output2_length,
4119 output2_buffer_size - output2_length,
4120 &function_output_length));
4121 TEST_LE_U(function_output_length,
4122 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4123 TEST_LE_U(function_output_length,
4124 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004125 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004128 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004129 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004130
Gilles Peskine50e586b2018-06-08 14:28:46 +02004131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 psa_cipher_abort(&operation);
4133 mbedtls_free(output1);
4134 mbedtls_free(output2);
4135 psa_destroy_key(key);
4136 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004137}
4138/* END_CASE */
4139
4140/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004141void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4142 data_t *key_data, data_t *iv,
4143 data_t *input,
4144 int first_part_size_arg,
4145 int output1_length_arg, int output2_length_arg,
4146 data_t *expected_output,
4147 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004148{
Ronald Cron5425a212020-08-04 14:58:35 +02004149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004150 psa_key_type_t key_type = key_type_arg;
4151 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004152 psa_status_t status;
4153 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004154 size_t first_part_size = first_part_size_arg;
4155 size_t output1_length = output1_length_arg;
4156 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004157 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158 size_t output_buffer_size = 0;
4159 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004160 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004161 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4167 psa_set_key_algorithm(&attributes, alg);
4168 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4171 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (iv->len > 0) {
4176 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004177 }
4178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4180 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004181 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 TEST_LE_U(first_part_size, input->len);
4184 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4185 output, output_buffer_size,
4186 &function_output_length));
4187 TEST_ASSERT(function_output_length == output1_length);
4188 TEST_LE_U(function_output_length,
4189 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4190 TEST_LE_U(function_output_length,
4191 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004192 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 if (first_part_size < input->len) {
4195 PSA_ASSERT(psa_cipher_update(&operation,
4196 input->x + first_part_size,
4197 input->len - first_part_size,
4198 (output_buffer_size == 0 ? NULL :
4199 output + total_output_length),
4200 output_buffer_size - total_output_length,
4201 &function_output_length));
4202 TEST_ASSERT(function_output_length == output2_length);
4203 TEST_LE_U(function_output_length,
4204 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4205 alg,
4206 input->len - first_part_size));
4207 TEST_LE_U(function_output_length,
4208 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004209 total_output_length += function_output_length;
4210 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 status = psa_cipher_finish(&operation,
4213 (output_buffer_size == 0 ? NULL :
4214 output + total_output_length),
4215 output_buffer_size - total_output_length,
4216 &function_output_length);
4217 TEST_LE_U(function_output_length,
4218 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4219 TEST_LE_U(function_output_length,
4220 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004221 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 if (expected_status == PSA_SUCCESS) {
4225 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004226
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004227 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004228 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004229 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004230
4231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 psa_cipher_abort(&operation);
4233 mbedtls_free(output);
4234 psa_destroy_key(key);
4235 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004236}
4237/* END_CASE */
4238
4239/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004240void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4241 data_t *key_data, data_t *iv,
4242 data_t *input,
4243 int first_part_size_arg,
4244 int output1_length_arg, int output2_length_arg,
4245 data_t *expected_output,
4246 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004247{
Ronald Cron5425a212020-08-04 14:58:35 +02004248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004249 psa_key_type_t key_type = key_type_arg;
4250 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004251 psa_status_t status;
4252 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004253 size_t first_part_size = first_part_size_arg;
4254 size_t output1_length = output1_length_arg;
4255 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004256 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257 size_t output_buffer_size = 0;
4258 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004259 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004260 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4266 psa_set_key_algorithm(&attributes, alg);
4267 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4270 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (iv->len > 0) {
4275 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004276 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4279 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004280 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 TEST_LE_U(first_part_size, input->len);
4283 PSA_ASSERT(psa_cipher_update(&operation,
4284 input->x, first_part_size,
4285 output, output_buffer_size,
4286 &function_output_length));
4287 TEST_ASSERT(function_output_length == output1_length);
4288 TEST_LE_U(function_output_length,
4289 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4290 TEST_LE_U(function_output_length,
4291 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004292 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 if (first_part_size < input->len) {
4295 PSA_ASSERT(psa_cipher_update(&operation,
4296 input->x + first_part_size,
4297 input->len - first_part_size,
4298 (output_buffer_size == 0 ? NULL :
4299 output + total_output_length),
4300 output_buffer_size - total_output_length,
4301 &function_output_length));
4302 TEST_ASSERT(function_output_length == output2_length);
4303 TEST_LE_U(function_output_length,
4304 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4305 alg,
4306 input->len - first_part_size));
4307 TEST_LE_U(function_output_length,
4308 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004309 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004310 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 status = psa_cipher_finish(&operation,
4313 (output_buffer_size == 0 ? NULL :
4314 output + total_output_length),
4315 output_buffer_size - total_output_length,
4316 &function_output_length);
4317 TEST_LE_U(function_output_length,
4318 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4319 TEST_LE_U(function_output_length,
4320 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004321 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004323
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 if (expected_status == PSA_SUCCESS) {
4325 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004326
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004327 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004328 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004329 }
4330
Gilles Peskine50e586b2018-06-08 14:28:46 +02004331exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 psa_cipher_abort(&operation);
4333 mbedtls_free(output);
4334 psa_destroy_key(key);
4335 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004336}
4337/* END_CASE */
4338
Gilles Peskine50e586b2018-06-08 14:28:46 +02004339/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340void cipher_decrypt_fail(int alg_arg,
4341 int key_type_arg,
4342 data_t *key_data,
4343 data_t *iv,
4344 data_t *input_arg,
4345 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004346{
4347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4348 psa_status_t status;
4349 psa_key_type_t key_type = key_type_arg;
4350 psa_algorithm_t alg = alg_arg;
4351 psa_status_t expected_status = expected_status_arg;
4352 unsigned char *input = NULL;
4353 size_t input_buffer_size = 0;
4354 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004356 size_t output_buffer_size = 0;
4357 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004358 size_t function_output_length;
4359 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 if (PSA_ERROR_BAD_STATE != expected_status) {
4363 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4366 psa_set_key_algorithm(&attributes, alg);
4367 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4370 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004371 }
4372
4373 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4375 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004376 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 memcpy(input, iv->x, iv->len);
4378 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004379 }
4380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004382 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004383
Neil Armstrong66a479f2022-02-07 15:41:19 +01004384 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4386 output_buffer_size, &output_length);
4387 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004388
Neil Armstrong66a479f2022-02-07 15:41:19 +01004389 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 status = psa_cipher_decrypt_setup(&operation, key, alg);
4391 if (status == PSA_SUCCESS) {
4392 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4393 input_arg->len) +
4394 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004395 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (iv->len > 0) {
4398 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (status != PSA_SUCCESS) {
4401 TEST_EQUAL(status, expected_status);
4402 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004403 }
4404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (status == PSA_SUCCESS) {
4406 status = psa_cipher_update(&operation,
4407 input_arg->x, input_arg->len,
4408 output_multi, output_buffer_size,
4409 &function_output_length);
4410 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004411 output_length = function_output_length;
4412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 status = psa_cipher_finish(&operation,
4414 output_multi + output_length,
4415 output_buffer_size - output_length,
4416 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 TEST_EQUAL(status, expected_status);
4419 } else {
4420 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004421 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 } else {
4423 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004424 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 } else {
4426 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004427 }
4428
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004429exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 psa_cipher_abort(&operation);
4431 mbedtls_free(input);
4432 mbedtls_free(output);
4433 mbedtls_free(output_multi);
4434 psa_destroy_key(key);
4435 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004436}
4437/* END_CASE */
4438
4439/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004440void cipher_decrypt(int alg_arg,
4441 int key_type_arg,
4442 data_t *key_data,
4443 data_t *iv,
4444 data_t *input_arg,
4445 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446{
4447 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4448 psa_key_type_t key_type = key_type_arg;
4449 psa_algorithm_t alg = alg_arg;
4450 unsigned char *input = NULL;
4451 size_t input_buffer_size = 0;
4452 unsigned char *output = NULL;
4453 size_t output_buffer_size = 0;
4454 size_t output_length = 0;
4455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4460 psa_set_key_algorithm(&attributes, alg);
4461 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004462
4463 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4465 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004466 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 memcpy(input, iv->x, iv->len);
4468 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004469 }
4470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004472 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4475 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4478 output_buffer_size, &output_length));
4479 TEST_LE_U(output_length,
4480 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4481 TEST_LE_U(output_length,
4482 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004483
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004484 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004485 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004486exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 mbedtls_free(input);
4488 mbedtls_free(output);
4489 psa_destroy_key(key);
4490 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004491}
4492/* END_CASE */
4493
4494/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004495void cipher_verify_output(int alg_arg,
4496 int key_type_arg,
4497 data_t *key_data,
4498 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004499{
Ronald Cron5425a212020-08-04 14:58:35 +02004500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004501 psa_key_type_t key_type = key_type_arg;
4502 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004503 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004504 size_t output1_size = 0;
4505 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004506 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004507 size_t output2_size = 0;
4508 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4514 psa_set_key_algorithm(&attributes, alg);
4515 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4518 &key));
4519 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004520 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4523 output1, output1_size,
4524 &output1_length));
4525 TEST_LE_U(output1_length,
4526 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4527 TEST_LE_U(output1_length,
4528 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004529
4530 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004531 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4534 output2, output2_size,
4535 &output2_length));
4536 TEST_LE_U(output2_length,
4537 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4538 TEST_LE_U(output2_length,
4539 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004540
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004541 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004542
4543exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 mbedtls_free(output1);
4545 mbedtls_free(output2);
4546 psa_destroy_key(key);
4547 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004548}
4549/* END_CASE */
4550
4551/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004552void cipher_verify_output_multipart(int alg_arg,
4553 int key_type_arg,
4554 data_t *key_data,
4555 data_t *input,
4556 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004557{
Ronald Cron5425a212020-08-04 14:58:35 +02004558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004559 psa_key_type_t key_type = key_type_arg;
4560 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004561 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004563 size_t iv_size = 16;
4564 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004565 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004566 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004567 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004568 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004569 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004570 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004571 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004572 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4573 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4579 psa_set_key_algorithm(&attributes, alg);
4580 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4583 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4586 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 if (alg != PSA_ALG_ECB_NO_PADDING) {
4589 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4590 iv, iv_size,
4591 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004592 }
4593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4595 TEST_LE_U(output1_buffer_size,
4596 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004597 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4602 output1, output1_buffer_size,
4603 &function_output_length));
4604 TEST_LE_U(function_output_length,
4605 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4606 TEST_LE_U(function_output_length,
4607 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004608 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 PSA_ASSERT(psa_cipher_update(&operation1,
4611 input->x + first_part_size,
4612 input->len - first_part_size,
4613 output1, output1_buffer_size,
4614 &function_output_length));
4615 TEST_LE_U(function_output_length,
4616 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4617 alg,
4618 input->len - first_part_size));
4619 TEST_LE_U(function_output_length,
4620 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004621 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 PSA_ASSERT(psa_cipher_finish(&operation1,
4624 output1 + output1_length,
4625 output1_buffer_size - output1_length,
4626 &function_output_length));
4627 TEST_LE_U(function_output_length,
4628 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4629 TEST_LE_U(function_output_length,
4630 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004631 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004634
Gilles Peskine048b7f02018-06-08 14:20:49 +02004635 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 TEST_LE_U(output2_buffer_size,
4637 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4638 TEST_LE_U(output2_buffer_size,
4639 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004640 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 if (iv_length > 0) {
4643 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4644 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004645 }
Moran Pekerded84402018-06-06 16:36:50 +03004646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4648 output2, output2_buffer_size,
4649 &function_output_length));
4650 TEST_LE_U(function_output_length,
4651 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4652 TEST_LE_U(function_output_length,
4653 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004654 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 PSA_ASSERT(psa_cipher_update(&operation2,
4657 output1 + first_part_size,
4658 output1_length - first_part_size,
4659 output2, output2_buffer_size,
4660 &function_output_length));
4661 TEST_LE_U(function_output_length,
4662 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4663 alg,
4664 output1_length - first_part_size));
4665 TEST_LE_U(function_output_length,
4666 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004667 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 PSA_ASSERT(psa_cipher_finish(&operation2,
4670 output2 + output2_length,
4671 output2_buffer_size - output2_length,
4672 &function_output_length));
4673 TEST_LE_U(function_output_length,
4674 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4675 TEST_LE_U(function_output_length,
4676 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004677 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004680
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004681 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004682
4683exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 psa_cipher_abort(&operation1);
4685 psa_cipher_abort(&operation2);
4686 mbedtls_free(output1);
4687 mbedtls_free(output2);
4688 psa_destroy_key(key);
4689 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004690}
4691/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004692
Gilles Peskine20035e32018-02-03 22:44:14 +01004693/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004694void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4695 int alg_arg,
4696 data_t *nonce,
4697 data_t *additional_data,
4698 data_t *input_data,
4699 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004700{
Ronald Cron5425a212020-08-04 14:58:35 +02004701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004702 psa_key_type_t key_type = key_type_arg;
4703 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004704 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004705 unsigned char *output_data = NULL;
4706 size_t output_size = 0;
4707 size_t output_length = 0;
4708 unsigned char *output_data2 = NULL;
4709 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004710 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004711 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4717 psa_set_key_algorithm(&attributes, alg);
4718 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4721 &key));
4722 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4723 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4726 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004727 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4728 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4730 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4731 TEST_EQUAL(output_size,
4732 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4733 TEST_LE_U(output_size,
4734 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004735 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004736 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 status = psa_aead_encrypt(key, alg,
4739 nonce->x, nonce->len,
4740 additional_data->x,
4741 additional_data->len,
4742 input_data->x, input_data->len,
4743 output_data, output_size,
4744 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004745
4746 /* If the operation is not supported, just skip and not fail in case the
4747 * encryption involves a common limitation of cryptography hardwares and
4748 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 if (status == PSA_ERROR_NOT_SUPPORTED) {
4750 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4751 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004752 }
4753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004757 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004758
Gilles Peskine003a4a92019-05-14 16:09:40 +02004759 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4760 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 TEST_EQUAL(input_data->len,
4762 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 TEST_LE_U(input_data->len,
4765 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 TEST_EQUAL(psa_aead_decrypt(key, alg,
4768 nonce->x, nonce->len,
4769 additional_data->x,
4770 additional_data->len,
4771 output_data, output_length,
4772 output_data2, output_length,
4773 &output_length2),
4774 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004775
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004776 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004777 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004778 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004779
Gilles Peskinea1cac842018-06-11 19:33:02 +02004780exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 psa_destroy_key(key);
4782 mbedtls_free(output_data);
4783 mbedtls_free(output_data2);
4784 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004785}
4786/* END_CASE */
4787
4788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004789void aead_encrypt(int key_type_arg, data_t *key_data,
4790 int alg_arg,
4791 data_t *nonce,
4792 data_t *additional_data,
4793 data_t *input_data,
4794 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795{
Ronald Cron5425a212020-08-04 14:58:35 +02004796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797 psa_key_type_t key_type = key_type_arg;
4798 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004799 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800 unsigned char *output_data = NULL;
4801 size_t output_size = 0;
4802 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004804 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4809 psa_set_key_algorithm(&attributes, alg);
4810 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4813 &key));
4814 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4815 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4818 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004819 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4820 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 TEST_EQUAL(output_size,
4822 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4823 TEST_LE_U(output_size,
4824 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004825 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 status = psa_aead_encrypt(key, alg,
4828 nonce->x, nonce->len,
4829 additional_data->x, additional_data->len,
4830 input_data->x, input_data->len,
4831 output_data, output_size,
4832 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004833
Ronald Cron28a45ed2021-02-09 20:35:42 +01004834 /* If the operation is not supported, just skip and not fail in case the
4835 * encryption involves a common limitation of cryptography hardwares and
4836 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (status == PSA_ERROR_NOT_SUPPORTED) {
4838 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4839 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004840 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004843 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004844 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004845
Gilles Peskinea1cac842018-06-11 19:33:02 +02004846exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 psa_destroy_key(key);
4848 mbedtls_free(output_data);
4849 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850}
4851/* END_CASE */
4852
4853/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004854void aead_decrypt(int key_type_arg, data_t *key_data,
4855 int alg_arg,
4856 data_t *nonce,
4857 data_t *additional_data,
4858 data_t *input_data,
4859 data_t *expected_data,
4860 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861{
Ronald Cron5425a212020-08-04 14:58:35 +02004862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863 psa_key_type_t key_type = key_type_arg;
4864 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004865 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004866 unsigned char *output_data = NULL;
4867 size_t output_size = 0;
4868 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004870 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004871 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4876 psa_set_key_algorithm(&attributes, alg);
4877 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4880 &key));
4881 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4882 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4885 alg);
4886 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4887 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004888 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4889 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 TEST_EQUAL(output_size,
4891 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4892 TEST_LE_U(output_size,
4893 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004894 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004895 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 status = psa_aead_decrypt(key, alg,
4898 nonce->x, nonce->len,
4899 additional_data->x,
4900 additional_data->len,
4901 input_data->x, input_data->len,
4902 output_data, output_size,
4903 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004904
Ronald Cron28a45ed2021-02-09 20:35:42 +01004905 /* If the operation is not supported, just skip and not fail in case the
4906 * decryption involves a common limitation of cryptography hardwares and
4907 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (status == PSA_ERROR_NOT_SUPPORTED) {
4909 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4910 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004911 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004916 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004917 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004919
Gilles Peskinea1cac842018-06-11 19:33:02 +02004920exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 psa_destroy_key(key);
4922 mbedtls_free(output_data);
4923 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004924}
4925/* END_CASE */
4926
4927/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004928void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4929 int alg_arg,
4930 data_t *nonce,
4931 data_t *additional_data,
4932 data_t *input_data,
4933 int do_set_lengths,
4934 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004935{
Paul Elliottd3f82412021-06-16 16:52:21 +01004936 size_t ad_part_len = 0;
4937 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004938 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4941 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (do_set_lengths) {
4944 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004945 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004947 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004949 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004950
4951 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 if (!aead_multipart_internal_func(key_type_arg, key_data,
4953 alg_arg, nonce,
4954 additional_data,
4955 ad_part_len,
4956 input_data, -1,
4957 set_lengths_method,
4958 expected_output,
4959 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004960 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004961 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 /* length(0) part, length(ad_part_len) part, length(0) part... */
4964 mbedtls_test_set_step(1000 + ad_part_len);
4965
4966 if (!aead_multipart_internal_func(key_type_arg, key_data,
4967 alg_arg, nonce,
4968 additional_data,
4969 ad_part_len,
4970 input_data, -1,
4971 set_lengths_method,
4972 expected_output,
4973 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004974 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 }
4976 }
4977
4978 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4979 /* Split data into length(data_part_len) parts. */
4980 mbedtls_test_set_step(2000 + data_part_len);
4981
4982 if (do_set_lengths) {
4983 if (data_part_len & 0x01) {
4984 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4985 } else {
4986 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4987 }
4988 }
4989
4990 if (!aead_multipart_internal_func(key_type_arg, key_data,
4991 alg_arg, nonce,
4992 additional_data, -1,
4993 input_data, data_part_len,
4994 set_lengths_method,
4995 expected_output,
4996 1, 0)) {
4997 break;
4998 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999
5000 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (!aead_multipart_internal_func(key_type_arg, key_data,
5004 alg_arg, nonce,
5005 additional_data, -1,
5006 input_data, data_part_len,
5007 set_lengths_method,
5008 expected_output,
5009 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005012 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005013
Paul Elliott8fc45162021-06-23 16:06:01 +01005014 /* Goto is required to silence warnings about unused labels, as we
5015 * don't actually do any test assertions in this function. */
5016 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005017}
5018/* END_CASE */
5019
5020/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005021void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5022 int alg_arg,
5023 data_t *nonce,
5024 data_t *additional_data,
5025 data_t *input_data,
5026 int do_set_lengths,
5027 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005028{
Paul Elliottd3f82412021-06-16 16:52:21 +01005029 size_t ad_part_len = 0;
5030 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005031 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005034 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005036
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (do_set_lengths) {
5038 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005041 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005043 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 if (!aead_multipart_internal_func(key_type_arg, key_data,
5046 alg_arg, nonce,
5047 additional_data,
5048 ad_part_len,
5049 input_data, -1,
5050 set_lengths_method,
5051 expected_output,
5052 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005053 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005055
5056 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 if (!aead_multipart_internal_func(key_type_arg, key_data,
5060 alg_arg, nonce,
5061 additional_data,
5062 ad_part_len,
5063 input_data, -1,
5064 set_lengths_method,
5065 expected_output,
5066 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005067 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005069 }
5070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005072 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (do_set_lengths) {
5076 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005077 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005081 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (!aead_multipart_internal_func(key_type_arg, key_data,
5084 alg_arg, nonce,
5085 additional_data, -1,
5086 input_data, data_part_len,
5087 set_lengths_method,
5088 expected_output,
5089 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005092
5093 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (!aead_multipart_internal_func(key_type_arg, key_data,
5097 alg_arg, nonce,
5098 additional_data, -1,
5099 input_data, data_part_len,
5100 set_lengths_method,
5101 expected_output,
5102 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005103 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005105 }
5106
Paul Elliott8fc45162021-06-23 16:06:01 +01005107 /* Goto is required to silence warnings about unused labels, as we
5108 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005109 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005110}
5111/* END_CASE */
5112
5113/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005114void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5115 int alg_arg,
5116 int nonce_length,
5117 int expected_nonce_length_arg,
5118 data_t *additional_data,
5119 data_t *input_data,
5120 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005121{
5122
5123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5124 psa_key_type_t key_type = key_type_arg;
5125 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005126 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005127 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5129 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005130 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005131 size_t actual_nonce_length = 0;
5132 size_t expected_nonce_length = expected_nonce_length_arg;
5133 unsigned char *output = NULL;
5134 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005135 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005136 size_t ciphertext_size = 0;
5137 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005138 size_t tag_length = 0;
5139 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5144 psa_set_key_algorithm(&attributes, alg);
5145 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5148 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005153
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005154 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005159
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005160 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005161
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005163
5164 /* If the operation is not supported, just skip and not fail in case the
5165 * encryption involves a common limitation of cryptography hardwares and
5166 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 if (status == PSA_ERROR_NOT_SUPPORTED) {
5168 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5169 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005170 }
5171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5175 nonce_length,
5176 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 if (expected_status == PSA_SUCCESS) {
5183 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5184 alg));
5185 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005190 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5192 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5195 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5198 output, output_size,
5199 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005200
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5202 &ciphertext_length, tag_buffer,
5203 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005204 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005205
5206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 psa_destroy_key(key);
5208 mbedtls_free(output);
5209 mbedtls_free(ciphertext);
5210 psa_aead_abort(&operation);
5211 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005212}
5213/* END_CASE */
5214
5215/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005216void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5217 int alg_arg,
5218 int nonce_length_arg,
5219 int set_lengths_method_arg,
5220 data_t *additional_data,
5221 data_t *input_data,
5222 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005223{
5224
5225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5226 psa_key_type_t key_type = key_type_arg;
5227 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005228 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005229 uint8_t *nonce_buffer = NULL;
5230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5231 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5232 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005233 unsigned char *output = NULL;
5234 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005235 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005236 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005237 size_t ciphertext_size = 0;
5238 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005239 size_t tag_length = 0;
5240 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005241 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005242 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005245
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5247 psa_set_key_algorithm(&attributes, alg);
5248 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5251 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005256
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005257 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005262
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005263 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005266
5267 /* If the operation is not supported, just skip and not fail in case the
5268 * encryption involves a common limitation of cryptography hardwares and
5269 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (status == PSA_ERROR_NOT_SUPPORTED) {
5271 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5272 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005273 }
5274
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005276
Paul Elliott4023ffd2021-09-10 16:21:22 +01005277 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if (nonce_length_arg == -1) {
5279 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005280 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 nonce_length = 0;
5282 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005283 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005285 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 if (nonce_buffer) {
5288 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005289 nonce_buffer[index] = 'a' + index;
5290 }
Paul Elliott66696b52021-08-16 18:42:41 +01005291 }
Paul Elliott863864a2021-07-23 17:28:31 +01005292 }
5293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5295 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5296 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005297 }
5298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if (expected_status == PSA_SUCCESS) {
5304 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5305 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5306 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005307 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005309 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 }
Paul Elliott863864a2021-07-23 17:28:31 +01005311
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005312 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5314 additional_data->len),
5315 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005316
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5318 output, output_size,
5319 &ciphertext_length),
5320 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5323 &ciphertext_length, tag_buffer,
5324 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5325 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005326 }
5327
5328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 psa_destroy_key(key);
5330 mbedtls_free(output);
5331 mbedtls_free(ciphertext);
5332 mbedtls_free(nonce_buffer);
5333 psa_aead_abort(&operation);
5334 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005335}
5336/* END_CASE */
5337
5338/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005339void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005340 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005341 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005342 data_t *nonce,
5343 data_t *additional_data,
5344 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005346{
5347
5348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5349 psa_key_type_t key_type = key_type_arg;
5350 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005351 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5354 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005355 unsigned char *output = NULL;
5356 unsigned char *ciphertext = NULL;
5357 size_t output_size = output_size_arg;
5358 size_t ciphertext_size = 0;
5359 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005360 size_t tag_length = 0;
5361 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5366 psa_set_key_algorithm(&attributes, alg);
5367 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5370 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005373
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005374 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005377
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005378 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005381
5382 /* If the operation is not supported, just skip and not fail in case the
5383 * encryption involves a common limitation of cryptography hardwares and
5384 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 if (status == PSA_ERROR_NOT_SUPPORTED) {
5386 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5387 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005388 }
5389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5393 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5398 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 status = psa_aead_update(&operation, input_data->x, input_data->len,
5401 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005402
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005406 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5408 &ciphertext_length, tag_buffer,
5409 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005410 }
5411
5412exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 psa_destroy_key(key);
5414 mbedtls_free(output);
5415 mbedtls_free(ciphertext);
5416 psa_aead_abort(&operation);
5417 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005418}
5419/* END_CASE */
5420
Paul Elliott91b021e2021-07-23 18:52:31 +01005421/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005422void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5423 int alg_arg,
5424 int finish_ciphertext_size_arg,
5425 int tag_size_arg,
5426 data_t *nonce,
5427 data_t *additional_data,
5428 data_t *input_data,
5429 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005430{
5431
5432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5433 psa_key_type_t key_type = key_type_arg;
5434 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005435 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5437 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5438 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005439 unsigned char *ciphertext = NULL;
5440 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005441 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005442 size_t ciphertext_size = 0;
5443 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5445 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005446 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5451 psa_set_key_algorithm(&attributes, alg);
5452 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5455 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005460
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005461 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005462
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005463 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005464
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005465 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005468
5469 /* If the operation is not supported, just skip and not fail in case the
5470 * encryption involves a common limitation of cryptography hardwares and
5471 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 if (status == PSA_ERROR_NOT_SUPPORTED) {
5473 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5474 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005475 }
5476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5482 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5485 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5488 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005489
5490 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 status = psa_aead_finish(&operation, finish_ciphertext,
5492 finish_ciphertext_size,
5493 &ciphertext_length, tag_buffer,
5494 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005495
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005497
5498exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 psa_destroy_key(key);
5500 mbedtls_free(ciphertext);
5501 mbedtls_free(finish_ciphertext);
5502 mbedtls_free(tag_buffer);
5503 psa_aead_abort(&operation);
5504 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005505}
5506/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005507
5508/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005509void aead_multipart_verify(int key_type_arg, data_t *key_data,
5510 int alg_arg,
5511 data_t *nonce,
5512 data_t *additional_data,
5513 data_t *input_data,
5514 data_t *tag,
5515 int tag_usage_arg,
5516 int expected_setup_status_arg,
5517 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005518{
5519 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5520 psa_key_type_t key_type = key_type_arg;
5521 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005522 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5524 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5525 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005526 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005527 unsigned char *plaintext = NULL;
5528 unsigned char *finish_plaintext = NULL;
5529 size_t plaintext_size = 0;
5530 size_t plaintext_length = 0;
5531 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005532 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005533 unsigned char *tag_buffer = NULL;
5534 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5539 psa_set_key_algorithm(&attributes, alg);
5540 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5543 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5548 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005549
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005550 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005553
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005554 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005557
5558 /* If the operation is not supported, just skip and not fail in case the
5559 * encryption involves a common limitation of cryptography hardwares and
5560 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 if (status == PSA_ERROR_NOT_SUPPORTED) {
5562 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5563 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005564 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 }
Paul Elliott9961a662021-09-17 19:19:02 +01005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 status = psa_aead_set_lengths(&operation, additional_data->len,
5576 input_data->len);
5577 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5580 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5583 input_data->len,
5584 plaintext, plaintext_size,
5585 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005588 tag_buffer = tag->x;
5589 tag_size = tag->len;
5590 }
5591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 status = psa_aead_verify(&operation, finish_plaintext,
5593 verify_plaintext_size,
5594 &plaintext_length,
5595 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005598
5599exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 psa_destroy_key(key);
5601 mbedtls_free(plaintext);
5602 mbedtls_free(finish_plaintext);
5603 psa_aead_abort(&operation);
5604 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005605}
5606/* END_CASE */
5607
Paul Elliott9961a662021-09-17 19:19:02 +01005608/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005609void aead_multipart_setup(int key_type_arg, data_t *key_data,
5610 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005611{
5612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5613 psa_key_type_t key_type = key_type_arg;
5614 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005615 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5617 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5618 psa_status_t expected_status = expected_status_arg;
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 psa_set_key_usage_flags(&attributes,
5623 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5624 psa_set_key_algorithm(&attributes, alg);
5625 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5628 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005629
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005637
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005639
5640exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 psa_destroy_key(key);
5642 psa_aead_abort(&operation);
5643 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005644}
5645/* END_CASE */
5646
5647/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005648void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5649 int alg_arg,
5650 data_t *nonce,
5651 data_t *additional_data,
5652 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005653{
5654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5655 psa_key_type_t key_type = key_type_arg;
5656 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005657 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005658 unsigned char *output_data = NULL;
5659 unsigned char *final_data = NULL;
5660 size_t output_size = 0;
5661 size_t finish_output_size = 0;
5662 size_t output_length = 0;
5663 size_t key_bits = 0;
5664 size_t tag_length = 0;
5665 size_t tag_size = 0;
5666 size_t nonce_length = 0;
5667 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5668 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5669 size_t output_part_length = 0;
5670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 psa_set_key_usage_flags(&attributes,
5675 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5676 psa_set_key_algorithm(&attributes, alg);
5677 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5680 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5683 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005691 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005697 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
5699 /* Test all operations error without calling setup first. */
5700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5702 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5707 PSA_AEAD_NONCE_MAX_SIZE,
5708 &nonce_length),
5709 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712
Paul Elliott481be342021-07-16 17:38:47 +01005713 /* ------------------------------------------------------- */
5714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5716 input_data->len),
5717 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005720
Paul Elliott481be342021-07-16 17:38:47 +01005721 /* ------------------------------------------------------- */
5722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5724 additional_data->len),
5725 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728
Paul Elliott481be342021-07-16 17:38:47 +01005729 /* ------------------------------------------------------- */
5730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5732 input_data->len, output_data,
5733 output_size, &output_length),
5734 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737
Paul Elliott481be342021-07-16 17:38:47 +01005738 /* ------------------------------------------------------- */
5739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5741 finish_output_size,
5742 &output_part_length,
5743 tag_buffer, tag_length,
5744 &tag_size),
5745 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005748
Paul Elliott481be342021-07-16 17:38:47 +01005749 /* ------------------------------------------------------- */
5750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5752 finish_output_size,
5753 &output_part_length,
5754 tag_buffer,
5755 tag_length),
5756 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
5760 /* Test for double setups. */
5761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5765 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005768
Paul Elliott481be342021-07-16 17:38:47 +01005769 /* ------------------------------------------------------- */
5770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5774 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005777
Paul Elliott374a2be2021-07-16 17:53:40 +01005778 /* ------------------------------------------------------- */
5779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5783 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005786
5787 /* ------------------------------------------------------- */
5788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5792 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005795
Paul Elliottc23a9a02021-06-21 18:32:46 +01005796 /* Test for not setting a nonce. */
5797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5801 additional_data->len),
5802 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005805
Paul Elliott7f628422021-09-01 12:08:29 +01005806 /* ------------------------------------------------------- */
5807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5811 input_data->len, output_data,
5812 output_size, &output_length),
5813 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005816
Paul Elliottbdc2c682021-09-21 18:37:10 +01005817 /* ------------------------------------------------------- */
5818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5822 finish_output_size,
5823 &output_part_length,
5824 tag_buffer, tag_length,
5825 &tag_size),
5826 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005829
5830 /* ------------------------------------------------------- */
5831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5835 finish_output_size,
5836 &output_part_length,
5837 tag_buffer,
5838 tag_length),
5839 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005842
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843 /* Test for double setting nonce. */
5844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5850 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853
Paul Elliott374a2be2021-07-16 17:53:40 +01005854 /* Test for double generating nonce. */
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5859 PSA_AEAD_NONCE_MAX_SIZE,
5860 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5863 PSA_AEAD_NONCE_MAX_SIZE,
5864 &nonce_length),
5865 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
5867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005869
5870 /* Test for generate nonce then set and vice versa */
5871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5875 PSA_AEAD_NONCE_MAX_SIZE,
5876 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5879 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005882
Andrzej Kurekad837522021-12-15 15:28:49 +01005883 /* Test for generating nonce after calling set lengths */
5884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5888 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5891 PSA_AEAD_NONCE_MAX_SIZE,
5892 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005895
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005896 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 if (operation.alg == PSA_ALG_CCM) {
5901 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5902 input_data->len),
5903 PSA_ERROR_INVALID_ARGUMENT);
5904 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5905 PSA_AEAD_NONCE_MAX_SIZE,
5906 &nonce_length),
5907 PSA_ERROR_BAD_STATE);
5908 } else {
5909 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5910 input_data->len));
5911 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5912 PSA_AEAD_NONCE_MAX_SIZE,
5913 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005914 }
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005917
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005918 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005919#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5923 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5924 input_data->len),
5925 PSA_ERROR_INVALID_ARGUMENT);
5926 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5927 PSA_AEAD_NONCE_MAX_SIZE,
5928 &nonce_length),
5929 PSA_ERROR_BAD_STATE);
5930 } else {
5931 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5932 input_data->len));
5933 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5934 PSA_AEAD_NONCE_MAX_SIZE,
5935 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005936 }
5937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005939#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005940
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005941 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5946 PSA_AEAD_NONCE_MAX_SIZE,
5947 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if (operation.alg == PSA_ALG_CCM) {
5950 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5951 input_data->len),
5952 PSA_ERROR_INVALID_ARGUMENT);
5953 } else {
5954 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5955 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005956 }
5957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005959
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005960 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005961 /* Test for setting nonce after calling set lengths */
5962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5966 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005971
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005972 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (operation.alg == PSA_ALG_CCM) {
5977 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5978 input_data->len),
5979 PSA_ERROR_INVALID_ARGUMENT);
5980 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5981 PSA_ERROR_BAD_STATE);
5982 } else {
5983 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5984 input_data->len));
5985 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005986 }
5987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005989
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005990 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005991#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5995 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5996 input_data->len),
5997 PSA_ERROR_INVALID_ARGUMENT);
5998 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5999 PSA_ERROR_BAD_STATE);
6000 } else {
6001 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6002 input_data->len));
6003 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006004 }
6005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006007#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006008
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006009 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 if (operation.alg == PSA_ALG_CCM) {
6016 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6017 input_data->len),
6018 PSA_ERROR_INVALID_ARGUMENT);
6019 } else {
6020 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6021 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006022 }
6023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006025
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006026 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006027#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 if (operation.alg == PSA_ALG_GCM) {
6031 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6032 SIZE_MAX),
6033 PSA_ERROR_INVALID_ARGUMENT);
6034 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6035 PSA_ERROR_BAD_STATE);
6036 } else if (operation.alg != PSA_ALG_CCM) {
6037 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6038 SIZE_MAX));
6039 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006040 }
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006043#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006044
Tom Cosgrove1797b052022-12-04 17:19:59 +00006045 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006046#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 if (operation.alg == PSA_ALG_GCM) {
6052 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6053 SIZE_MAX),
6054 PSA_ERROR_INVALID_ARGUMENT);
6055 } else if (operation.alg != PSA_ALG_CCM) {
6056 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6057 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006058 }
6059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006061#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006062
6063 /* ------------------------------------------------------- */
6064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6070 PSA_AEAD_NONCE_MAX_SIZE,
6071 &nonce_length),
6072 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006075
Paul Elliott7220cae2021-06-22 17:25:57 +01006076 /* Test for generating nonce in decrypt setup. */
6077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6081 PSA_AEAD_NONCE_MAX_SIZE,
6082 &nonce_length),
6083 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006086
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087 /* Test for setting lengths twice. */
6088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6094 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6097 input_data->len),
6098 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006101
Andrzej Kurekad837522021-12-15 15:28:49 +01006102 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6111 additional_data->len),
6112 PSA_ERROR_BAD_STATE);
6113 } else {
6114 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6115 additional_data->len));
6116
6117 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6118 input_data->len),
6119 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006122
6123 /* ------------------------------------------------------- */
6124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (operation.alg == PSA_ALG_CCM) {
6130 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6131 input_data->len, output_data,
6132 output_size, &output_length),
6133 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 } else {
6136 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6137 input_data->len, output_data,
6138 output_size, &output_length));
6139
6140 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6141 input_data->len),
6142 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006143 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006145
6146 /* ------------------------------------------------------- */
6147
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006151
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if (operation.alg == PSA_ALG_CCM) {
6153 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6154 finish_output_size,
6155 &output_part_length,
6156 tag_buffer, tag_length,
6157 &tag_size));
6158 } else {
6159 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6160 finish_output_size,
6161 &output_part_length,
6162 tag_buffer, tag_length,
6163 &tag_size));
6164
6165 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6166 input_data->len),
6167 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006168 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006170
6171 /* Test for setting lengths after generating nonce + already starting data. */
6172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6176 PSA_AEAD_NONCE_MAX_SIZE,
6177 &nonce_length));
6178 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6181 additional_data->len),
6182 PSA_ERROR_BAD_STATE);
6183 } else {
6184 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6185 additional_data->len));
6186
6187 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6188 input_data->len),
6189 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006190 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006192
6193 /* ------------------------------------------------------- */
6194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6198 PSA_AEAD_NONCE_MAX_SIZE,
6199 &nonce_length));
6200 if (operation.alg == PSA_ALG_CCM) {
6201 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6202 input_data->len, output_data,
6203 output_size, &output_length),
6204 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 } else {
6207 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6208 input_data->len, output_data,
6209 output_size, &output_length));
6210
6211 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6212 input_data->len),
6213 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006214 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006216
6217 /* ------------------------------------------------------- */
6218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6222 PSA_AEAD_NONCE_MAX_SIZE,
6223 &nonce_length));
6224 if (operation.alg == PSA_ALG_CCM) {
6225 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6226 finish_output_size,
6227 &output_part_length,
6228 tag_buffer, tag_length,
6229 &tag_size));
6230 } else {
6231 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6232 finish_output_size,
6233 &output_part_length,
6234 tag_buffer, tag_length,
6235 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6238 input_data->len),
6239 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006240 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006242
Paul Elliott243080c2021-07-21 19:01:17 +01006243 /* Test for not sending any additional data or data after setting non zero
6244 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6251 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6254 finish_output_size,
6255 &output_part_length,
6256 tag_buffer, tag_length,
6257 &tag_size),
6258 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006261
Paul Elliott243080c2021-07-21 19:01:17 +01006262 /* Test for not sending any additional data or data after setting non-zero
6263 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6270 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6273 finish_output_size,
6274 &output_part_length,
6275 tag_buffer,
6276 tag_length),
6277 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006280
Paul Elliott243080c2021-07-21 19:01:17 +01006281 /* Test for not sending any additional data after setting a non-zero length
6282 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6289 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6292 input_data->len, output_data,
6293 output_size, &output_length),
6294 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006297
Paul Elliottf94bd992021-09-19 18:15:59 +01006298 /* Test for not sending any data after setting a non-zero length for it.*/
6299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6305 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6308 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6311 finish_output_size,
6312 &output_part_length,
6313 tag_buffer, tag_length,
6314 &tag_size),
6315 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006318
Paul Elliottb0450fe2021-09-01 15:06:26 +01006319 /* Test for sending too much additional data after setting lengths. */
6320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006326
6327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6329 additional_data->len),
6330 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006333
Paul Elliotta2a09b02021-09-22 14:56:40 +01006334 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6341 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6344 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6347 1),
6348 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006351
Paul Elliottb0450fe2021-09-01 15:06:26 +01006352 /* Test for sending too much data after setting lengths. */
6353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6361 input_data->len, output_data,
6362 output_size, &output_length),
6363 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006366
Paul Elliotta2a09b02021-09-22 14:56:40 +01006367 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6374 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6377 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6380 input_data->len, output_data,
6381 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6384 1, output_data,
6385 output_size, &output_length),
6386 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006389
Paul Elliottc23a9a02021-06-21 18:32:46 +01006390 /* Test sending additional data after data. */
6391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (operation.alg != PSA_ALG_CCM) {
6397 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6398 input_data->len, output_data,
6399 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6402 additional_data->len),
6403 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006404 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006406
Paul Elliott534d0b42021-06-22 19:15:20 +01006407 /* Test calling finish on decryption. */
6408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6414 finish_output_size,
6415 &output_part_length,
6416 tag_buffer, tag_length,
6417 &tag_size),
6418 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006421
6422 /* Test calling verify on encryption. */
6423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6429 finish_output_size,
6430 &output_part_length,
6431 tag_buffer,
6432 tag_length),
6433 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006436
6437
Paul Elliottc23a9a02021-06-21 18:32:46 +01006438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 psa_destroy_key(key);
6440 psa_aead_abort(&operation);
6441 mbedtls_free(output_data);
6442 mbedtls_free(final_data);
6443 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006444}
6445/* END_CASE */
6446
6447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006448void signature_size(int type_arg,
6449 int bits,
6450 int alg_arg,
6451 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006452{
6453 psa_key_type_t type = type_arg;
6454 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006458
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459exit:
6460 ;
6461}
6462/* END_CASE */
6463
6464/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006465void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6466 int alg_arg, data_t *input_data,
6467 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468{
Ronald Cron5425a212020-08-04 14:58:35 +02006469 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006470 psa_key_type_t key_type = key_type_arg;
6471 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006472 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006473 unsigned char *signature = NULL;
6474 size_t signature_size;
6475 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6481 psa_set_key_algorithm(&attributes, alg);
6482 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6485 &key));
6486 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6487 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006488
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006489 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006490 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6492 key_bits, alg);
6493 TEST_ASSERT(signature_size != 0);
6494 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006495 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006496
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006497 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 PSA_ASSERT(psa_sign_hash(key, alg,
6499 input_data->x, input_data->len,
6500 signature, signature_size,
6501 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006502 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006503 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006504 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006505
6506exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006507 /*
6508 * Key attributes may have been returned by psa_get_key_attributes()
6509 * thus reset them as required.
6510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 psa_destroy_key(key);
6514 mbedtls_free(signature);
6515 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006516}
6517/* END_CASE */
6518
Paul Elliott712d5122022-12-07 14:03:10 +00006519/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006520/**
6521 * sign_hash_interruptible() test intentions:
6522 *
6523 * Note: This test can currently only handle ECDSA.
6524 *
6525 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006526 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006527 *
6528 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6529 * expected for different max_ops values.
6530 *
6531 * 3. Test that the number of ops done prior to start and after abort is zero
6532 * and that each successful stage completes some ops (this is not mandated by
6533 * the PSA specification, but is currently the case).
6534 *
6535 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6536 * complete() calls does not alter the number of ops returned.
6537 */
Paul Elliott712d5122022-12-07 14:03:10 +00006538void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6539 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006540 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006541{
6542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6543 psa_key_type_t key_type = key_type_arg;
6544 psa_algorithm_t alg = alg_arg;
6545 size_t key_bits;
6546 unsigned char *signature = NULL;
6547 size_t signature_size;
6548 size_t signature_length = 0xdeadbeef;
6549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6550 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006551 uint32_t num_ops = 0;
6552 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006553 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006554 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006555 size_t min_completes = 0;
6556 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006557
Paul Elliott712d5122022-12-07 14:03:10 +00006558 psa_sign_hash_interruptible_operation_t operation =
6559 psa_sign_hash_interruptible_operation_init();
6560
6561 PSA_ASSERT(psa_crypto_init());
6562
6563 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6564 psa_set_key_algorithm(&attributes, alg);
6565 psa_set_key_type(&attributes, key_type);
6566
6567 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6568 &key));
6569 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6570 key_bits = psa_get_key_bits(&attributes);
6571
6572 /* Allocate a buffer which has the size advertised by the
6573 * library. */
6574 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6575 key_bits, alg);
6576 TEST_ASSERT(signature_size != 0);
6577 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006578 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006579
Paul Elliott0c683352022-12-16 19:16:56 +00006580 psa_interruptible_set_max_ops(max_ops);
6581
Paul Elliott6f600372023-02-06 18:41:05 +00006582 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6583 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006584
Paul Elliott712d5122022-12-07 14:03:10 +00006585 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6586 TEST_ASSERT(num_ops_prior == 0);
6587
6588 /* Start performing the signature. */
6589 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6590 input_data->x, input_data->len));
6591
6592 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6593 TEST_ASSERT(num_ops_prior == 0);
6594
6595 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006596 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006597 status = psa_sign_hash_complete(&operation, signature, signature_size,
6598 &signature_length);
6599
Paul Elliott0c683352022-12-16 19:16:56 +00006600 num_completes++;
6601
Paul Elliott712d5122022-12-07 14:03:10 +00006602 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6603 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006604 /* We are asserting here that every complete makes progress
6605 * (completes some ops), which is true of the internal
6606 * implementation and probably any implementation, however this is
6607 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006608 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006609
Paul Elliott712d5122022-12-07 14:03:10 +00006610 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006611
6612 /* Ensure calling get_num_ops() twice still returns the same
6613 * number of ops as previously reported. */
6614 num_ops = psa_sign_hash_get_num_ops(&operation);
6615
6616 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006617 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006618 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006619
6620 TEST_ASSERT(status == PSA_SUCCESS);
6621
Paul Elliott0c683352022-12-16 19:16:56 +00006622 TEST_LE_U(min_completes, num_completes);
6623 TEST_LE_U(num_completes, max_completes);
6624
Paul Elliott712d5122022-12-07 14:03:10 +00006625 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006626 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006627 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006628
6629 PSA_ASSERT(psa_sign_hash_abort(&operation));
6630
Paul Elliott59ad9452022-12-18 15:09:02 +00006631 num_ops = psa_sign_hash_get_num_ops(&operation);
6632 TEST_ASSERT(num_ops == 0);
6633
Paul Elliott712d5122022-12-07 14:03:10 +00006634exit:
6635
6636 /*
6637 * Key attributes may have been returned by psa_get_key_attributes()
6638 * thus reset them as required.
6639 */
6640 psa_reset_key_attributes(&attributes);
6641
6642 psa_destroy_key(key);
6643 mbedtls_free(signature);
6644 PSA_DONE();
6645}
6646/* END_CASE */
6647
Gilles Peskine20035e32018-02-03 22:44:14 +01006648/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006649void sign_hash_fail(int key_type_arg, data_t *key_data,
6650 int alg_arg, data_t *input_data,
6651 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006652{
Ronald Cron5425a212020-08-04 14:58:35 +02006653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006654 psa_key_type_t key_type = key_type_arg;
6655 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006656 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006657 psa_status_t actual_status;
6658 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006659 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006660 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006662
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006663 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006666
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6668 psa_set_key_algorithm(&attributes, alg);
6669 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6672 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 actual_status = psa_sign_hash(key, alg,
6675 input_data->x, input_data->len,
6676 signature, signature_size,
6677 &signature_length);
6678 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006679 /* The value of *signature_length is unspecified on error, but
6680 * whatever it is, it should be less than signature_size, so that
6681 * if the caller tries to read *signature_length bytes without
6682 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006684
6685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 psa_reset_key_attributes(&attributes);
6687 psa_destroy_key(key);
6688 mbedtls_free(signature);
6689 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006690}
6691/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006692
Paul Elliott91007972022-12-16 12:21:24 +00006693/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006694/**
6695 * sign_hash_fail_interruptible() test intentions:
6696 *
6697 * Note: This test can currently only handle ECDSA.
6698 *
6699 * 1. Test that various failure cases for interruptible sign hash fail with the
6700 * correct error codes, and at the correct point (at start or during
6701 * complete).
6702 *
6703 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6704 * expected for different max_ops values.
6705 *
6706 * 3. Test that the number of ops done prior to start and after abort is zero
6707 * and that each successful stage completes some ops (this is not mandated by
6708 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006709 *
6710 * 4. Check that calling complete() when start() fails and complete()
6711 * after completion results in a BAD_STATE error.
6712 *
6713 * 5. Check that calling start() again after start fails results in a BAD_STATE
6714 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006715 */
Paul Elliott91007972022-12-16 12:21:24 +00006716void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6717 int alg_arg, data_t *input_data,
6718 int signature_size_arg,
6719 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006720 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006721 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006722{
6723 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6724 psa_key_type_t key_type = key_type_arg;
6725 psa_algorithm_t alg = alg_arg;
6726 size_t signature_size = signature_size_arg;
6727 psa_status_t actual_status;
6728 psa_status_t expected_start_status = expected_start_status_arg;
6729 psa_status_t expected_complete_status = expected_complete_status_arg;
6730 unsigned char *signature = NULL;
6731 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006732 uint32_t num_ops = 0;
6733 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006734 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006735 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006736 size_t min_completes = 0;
6737 size_t max_completes = 0;
6738
Paul Elliott91007972022-12-16 12:21:24 +00006739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6740 psa_sign_hash_interruptible_operation_t operation =
6741 psa_sign_hash_interruptible_operation_init();
6742
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006743 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006744
6745 PSA_ASSERT(psa_crypto_init());
6746
6747 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6748 psa_set_key_algorithm(&attributes, alg);
6749 psa_set_key_type(&attributes, key_type);
6750
6751 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6752 &key));
6753
Paul Elliott0c683352022-12-16 19:16:56 +00006754 psa_interruptible_set_max_ops(max_ops);
6755
Paul Elliott6f600372023-02-06 18:41:05 +00006756 interruptible_signverify_get_minmax_completes(max_ops,
6757 expected_complete_status,
6758 &min_completes,
6759 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006760
Paul Elliott91007972022-12-16 12:21:24 +00006761 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6762 TEST_ASSERT(num_ops_prior == 0);
6763
6764 /* Start performing the signature. */
6765 actual_status = psa_sign_hash_start(&operation, key, alg,
6766 input_data->x, input_data->len);
6767
6768 TEST_EQUAL(actual_status, expected_start_status);
6769
Paul Elliottc9774412023-02-06 15:14:07 +00006770 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006771 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006772 * start failed. */
6773 actual_status = psa_sign_hash_complete(&operation, signature,
6774 signature_size,
6775 &signature_length);
6776
6777 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6778
6779 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006780 actual_status = psa_sign_hash_start(&operation, key, alg,
6781 input_data->x, input_data->len);
6782
6783 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6784 }
6785
Paul Elliott91007972022-12-16 12:21:24 +00006786 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6787 TEST_ASSERT(num_ops_prior == 0);
6788
Paul Elliott91007972022-12-16 12:21:24 +00006789 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006790 do {
Paul Elliott91007972022-12-16 12:21:24 +00006791 actual_status = psa_sign_hash_complete(&operation, signature,
6792 signature_size,
6793 &signature_length);
6794
Paul Elliott0c683352022-12-16 19:16:56 +00006795 num_completes++;
6796
Paul Elliott334d7262023-01-20 17:29:41 +00006797 if (actual_status == PSA_SUCCESS ||
6798 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006799 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006800 /* We are asserting here that every complete makes progress
6801 * (completes some ops), which is true of the internal
6802 * implementation and probably any implementation, however this is
6803 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006804 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006805
Paul Elliott91007972022-12-16 12:21:24 +00006806 num_ops_prior = num_ops;
6807 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006808 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006809
Paul Elliottc9774412023-02-06 15:14:07 +00006810 TEST_EQUAL(actual_status, expected_complete_status);
6811
Paul Elliottefebad02023-02-15 16:56:45 +00006812 /* Check that another complete returns BAD_STATE. */
6813 actual_status = psa_sign_hash_complete(&operation, signature,
6814 signature_size,
6815 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006816
Paul Elliottefebad02023-02-15 16:56:45 +00006817 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006818
Paul Elliott91007972022-12-16 12:21:24 +00006819 PSA_ASSERT(psa_sign_hash_abort(&operation));
6820
Paul Elliott59ad9452022-12-18 15:09:02 +00006821 num_ops = psa_sign_hash_get_num_ops(&operation);
6822 TEST_ASSERT(num_ops == 0);
6823
Paul Elliott91007972022-12-16 12:21:24 +00006824 /* The value of *signature_length is unspecified on error, but
6825 * whatever it is, it should be less than signature_size, so that
6826 * if the caller tries to read *signature_length bytes without
6827 * checking the error code then they don't overflow a buffer. */
6828 TEST_LE_U(signature_length, signature_size);
6829
Paul Elliott0c683352022-12-16 19:16:56 +00006830 TEST_LE_U(min_completes, num_completes);
6831 TEST_LE_U(num_completes, max_completes);
6832
Paul Elliott91007972022-12-16 12:21:24 +00006833exit:
6834 psa_reset_key_attributes(&attributes);
6835 psa_destroy_key(key);
6836 mbedtls_free(signature);
6837 PSA_DONE();
6838}
6839/* END_CASE */
6840
mohammad16038cc1cee2018-03-28 01:21:33 +03006841/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006842void sign_verify_hash(int key_type_arg, data_t *key_data,
6843 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006844{
Ronald Cron5425a212020-08-04 14:58:35 +02006845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006846 psa_key_type_t key_type = key_type_arg;
6847 psa_algorithm_t alg = alg_arg;
6848 size_t key_bits;
6849 unsigned char *signature = NULL;
6850 size_t signature_size;
6851 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006855
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6857 psa_set_key_algorithm(&attributes, alg);
6858 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006859
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6861 &key));
6862 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6863 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006864
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006865 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006866 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6868 key_bits, alg);
6869 TEST_ASSERT(signature_size != 0);
6870 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006871 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006872
6873 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 PSA_ASSERT(psa_sign_hash(key, alg,
6875 input_data->x, input_data->len,
6876 signature, signature_size,
6877 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006878 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 TEST_LE_U(signature_length, signature_size);
6880 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006881
6882 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 PSA_ASSERT(psa_verify_hash(key, alg,
6884 input_data->x, input_data->len,
6885 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006886
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006888 /* Flip a bit in the input and verify that the signature is now
6889 * detected as invalid. Flip a bit at the beginning, not at the end,
6890 * because ECDSA may ignore the last few bits of the input. */
6891 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 TEST_EQUAL(psa_verify_hash(key, alg,
6893 input_data->x, input_data->len,
6894 signature, signature_length),
6895 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006896 }
6897
6898exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006899 /*
6900 * Key attributes may have been returned by psa_get_key_attributes()
6901 * thus reset them as required.
6902 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 psa_destroy_key(key);
6906 mbedtls_free(signature);
6907 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006908}
6909/* END_CASE */
6910
Paul Elliott712d5122022-12-07 14:03:10 +00006911/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006912/**
6913 * sign_verify_hash_interruptible() test intentions:
6914 *
6915 * Note: This test can currently only handle ECDSA.
6916 *
Paul Elliott8c092052023-03-06 17:49:14 +00006917 * 1. Test that we can sign an input hash with the given keypair and then
6918 * afterwards verify that signature. This is currently the only way to test
6919 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006920 *
6921 * 2. Test that after corrupting the hash, the verification detects an invalid
6922 * signature.
6923 *
6924 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6925 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006926 *
6927 * 4. Test that the number of ops done prior to starting signing and after abort
6928 * is zero and that each successful signing stage completes some ops (this is
6929 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006930 */
Paul Elliott712d5122022-12-07 14:03:10 +00006931void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006932 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006933 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006934{
6935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6936 psa_key_type_t key_type = key_type_arg;
6937 psa_algorithm_t alg = alg_arg;
6938 size_t key_bits;
6939 unsigned char *signature = NULL;
6940 size_t signature_size;
6941 size_t signature_length = 0xdeadbeef;
6942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6943 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006944 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006945 uint32_t num_ops = 0;
6946 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006947 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006948 size_t min_completes = 0;
6949 size_t max_completes = 0;
6950
Paul Elliott712d5122022-12-07 14:03:10 +00006951 psa_sign_hash_interruptible_operation_t sign_operation =
6952 psa_sign_hash_interruptible_operation_init();
6953 psa_verify_hash_interruptible_operation_t verify_operation =
6954 psa_verify_hash_interruptible_operation_init();
6955
6956 PSA_ASSERT(psa_crypto_init());
6957
Paul Elliott0c683352022-12-16 19:16:56 +00006958 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6959 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006960 psa_set_key_algorithm(&attributes, alg);
6961 psa_set_key_type(&attributes, key_type);
6962
6963 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6964 &key));
6965 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6966 key_bits = psa_get_key_bits(&attributes);
6967
6968 /* Allocate a buffer which has the size advertised by the
6969 * library. */
6970 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6971 key_bits, alg);
6972 TEST_ASSERT(signature_size != 0);
6973 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006974 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006975
Paul Elliott0c683352022-12-16 19:16:56 +00006976 psa_interruptible_set_max_ops(max_ops);
6977
Paul Elliott6f600372023-02-06 18:41:05 +00006978 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6979 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006980
Paul Elliott7c173082023-02-26 18:44:45 +00006981 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6982 TEST_ASSERT(num_ops_prior == 0);
6983
Paul Elliott712d5122022-12-07 14:03:10 +00006984 /* Start performing the signature. */
6985 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6986 input_data->x, input_data->len));
6987
Paul Elliott7c173082023-02-26 18:44:45 +00006988 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6989 TEST_ASSERT(num_ops_prior == 0);
6990
Paul Elliott712d5122022-12-07 14:03:10 +00006991 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006992 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006993
Paul Elliott0c683352022-12-16 19:16:56 +00006994 status = psa_sign_hash_complete(&sign_operation, signature,
6995 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006996 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006997
6998 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006999
7000 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7001 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7002 /* We are asserting here that every complete makes progress
7003 * (completes some ops), which is true of the internal
7004 * implementation and probably any implementation, however this is
7005 * not mandated by the PSA specification. */
7006 TEST_ASSERT(num_ops > num_ops_prior);
7007
7008 num_ops_prior = num_ops;
7009 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007010 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007011
7012 TEST_ASSERT(status == PSA_SUCCESS);
7013
Paul Elliott0c683352022-12-16 19:16:56 +00007014 TEST_LE_U(min_completes, num_completes);
7015 TEST_LE_U(num_completes, max_completes);
7016
Paul Elliott712d5122022-12-07 14:03:10 +00007017 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7018
Paul Elliott7c173082023-02-26 18:44:45 +00007019 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7020 TEST_ASSERT(num_ops == 0);
7021
Paul Elliott712d5122022-12-07 14:03:10 +00007022 /* Check that the signature length looks sensible. */
7023 TEST_LE_U(signature_length, signature_size);
7024 TEST_ASSERT(signature_length > 0);
7025
Paul Elliott0c683352022-12-16 19:16:56 +00007026 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007027
7028 /* Start verification. */
7029 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7030 input_data->x, input_data->len,
7031 signature, signature_length));
7032
7033 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007034 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007035 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007036
7037 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007038 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007039
7040 TEST_ASSERT(status == PSA_SUCCESS);
7041
Paul Elliott0c683352022-12-16 19:16:56 +00007042 TEST_LE_U(min_completes, num_completes);
7043 TEST_LE_U(num_completes, max_completes);
7044
Paul Elliott712d5122022-12-07 14:03:10 +00007045 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7046
7047 verify_operation = psa_verify_hash_interruptible_operation_init();
7048
7049 if (input_data->len != 0) {
7050 /* Flip a bit in the input and verify that the signature is now
7051 * detected as invalid. Flip a bit at the beginning, not at the end,
7052 * because ECDSA may ignore the last few bits of the input. */
7053 input_data->x[0] ^= 1;
7054
Paul Elliott712d5122022-12-07 14:03:10 +00007055 /* Start verification. */
7056 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7057 input_data->x, input_data->len,
7058 signature, signature_length));
7059
7060 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007061 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007062 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007063 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007064
7065 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7066 }
7067
7068 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7069
7070exit:
7071 /*
7072 * Key attributes may have been returned by psa_get_key_attributes()
7073 * thus reset them as required.
7074 */
7075 psa_reset_key_attributes(&attributes);
7076
7077 psa_destroy_key(key);
7078 mbedtls_free(signature);
7079 PSA_DONE();
7080}
7081/* END_CASE */
7082
Gilles Peskine9911b022018-06-29 17:30:48 +02007083/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007084void verify_hash(int key_type_arg, data_t *key_data,
7085 int alg_arg, data_t *hash_data,
7086 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007087{
Ronald Cron5425a212020-08-04 14:58:35 +02007088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007089 psa_key_type_t key_type = key_type_arg;
7090 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007091 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007092
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007096
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7098 psa_set_key_algorithm(&attributes, alg);
7099 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7102 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 PSA_ASSERT(psa_verify_hash(key, alg,
7105 hash_data->x, hash_data->len,
7106 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007107
itayzafrir5c753392018-05-08 11:18:38 +03007108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 psa_reset_key_attributes(&attributes);
7110 psa_destroy_key(key);
7111 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007112}
7113/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007114
Paul Elliott712d5122022-12-07 14:03:10 +00007115/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007116/**
7117 * verify_hash_interruptible() test intentions:
7118 *
7119 * Note: This test can currently only handle ECDSA.
7120 *
7121 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007122 * only). Given this test only does verification it can accept public keys as
7123 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007124 *
7125 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7126 * expected for different max_ops values.
7127 *
7128 * 3. Test that the number of ops done prior to start and after abort is zero
7129 * and that each successful stage completes some ops (this is not mandated by
7130 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007131 *
7132 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7133 * complete() calls does not alter the number of ops returned.
7134 *
7135 * 5. Test that after corrupting the hash, the verification detects an invalid
7136 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007137 */
Paul Elliott712d5122022-12-07 14:03:10 +00007138void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7139 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007140 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007141{
7142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7143 psa_key_type_t key_type = key_type_arg;
7144 psa_algorithm_t alg = alg_arg;
7145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7146 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007147 uint32_t num_ops = 0;
7148 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007149 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007150 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007151 size_t min_completes = 0;
7152 size_t max_completes = 0;
7153
Paul Elliott712d5122022-12-07 14:03:10 +00007154 psa_verify_hash_interruptible_operation_t operation =
7155 psa_verify_hash_interruptible_operation_init();
7156
7157 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7158
7159 PSA_ASSERT(psa_crypto_init());
7160
7161 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7162 psa_set_key_algorithm(&attributes, alg);
7163 psa_set_key_type(&attributes, key_type);
7164
7165 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7166 &key));
7167
Paul Elliott0c683352022-12-16 19:16:56 +00007168 psa_interruptible_set_max_ops(max_ops);
7169
Paul Elliott6f600372023-02-06 18:41:05 +00007170 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7171 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007172
Paul Elliott712d5122022-12-07 14:03:10 +00007173 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7174
7175 TEST_ASSERT(num_ops_prior == 0);
7176
7177 /* Start verification. */
7178 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7179 hash_data->x, hash_data->len,
7180 signature_data->x, signature_data->len)
7181 );
7182
7183 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7184
7185 TEST_ASSERT(num_ops_prior == 0);
7186
7187 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007188 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007189 status = psa_verify_hash_complete(&operation);
7190
Paul Elliott0c683352022-12-16 19:16:56 +00007191 num_completes++;
7192
Paul Elliott712d5122022-12-07 14:03:10 +00007193 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7194 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007195 /* We are asserting here that every complete makes progress
7196 * (completes some ops), which is true of the internal
7197 * implementation and probably any implementation, however this is
7198 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007199 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007200
Paul Elliott712d5122022-12-07 14:03:10 +00007201 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007202
7203 /* Ensure calling get_num_ops() twice still returns the same
7204 * number of ops as previously reported. */
7205 num_ops = psa_verify_hash_get_num_ops(&operation);
7206
7207 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007208 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007209 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007210
7211 TEST_ASSERT(status == PSA_SUCCESS);
7212
Paul Elliott0c683352022-12-16 19:16:56 +00007213 TEST_LE_U(min_completes, num_completes);
7214 TEST_LE_U(num_completes, max_completes);
7215
Paul Elliott712d5122022-12-07 14:03:10 +00007216 PSA_ASSERT(psa_verify_hash_abort(&operation));
7217
Paul Elliott59ad9452022-12-18 15:09:02 +00007218 num_ops = psa_verify_hash_get_num_ops(&operation);
7219 TEST_ASSERT(num_ops == 0);
7220
Paul Elliott8359c142023-02-24 18:40:10 +00007221 if (hash_data->len != 0) {
7222 /* Flip a bit in the hash and verify that the signature is now detected
7223 * as invalid. Flip a bit at the beginning, not at the end, because
7224 * ECDSA may ignore the last few bits of the input. */
7225 hash_data->x[0] ^= 1;
7226
7227 /* Start verification. */
7228 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7229 hash_data->x, hash_data->len,
7230 signature_data->x, signature_data->len));
7231
7232 /* Continue performing the signature until complete. */
7233 do {
7234 status = psa_verify_hash_complete(&operation);
7235 } while (status == PSA_OPERATION_INCOMPLETE);
7236
7237 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7238 }
7239
Paul Elliott712d5122022-12-07 14:03:10 +00007240exit:
7241 psa_reset_key_attributes(&attributes);
7242 psa_destroy_key(key);
7243 PSA_DONE();
7244}
7245/* END_CASE */
7246
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007247/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007248void verify_hash_fail(int key_type_arg, data_t *key_data,
7249 int alg_arg, data_t *hash_data,
7250 data_t *signature_data,
7251 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007252{
Ronald Cron5425a212020-08-04 14:58:35 +02007253 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007254 psa_key_type_t key_type = key_type_arg;
7255 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256 psa_status_t actual_status;
7257 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007259
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7263 psa_set_key_algorithm(&attributes, alg);
7264 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7267 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 actual_status = psa_verify_hash(key, alg,
7270 hash_data->x, hash_data->len,
7271 signature_data->x, signature_data->len);
7272 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007273
7274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 psa_reset_key_attributes(&attributes);
7276 psa_destroy_key(key);
7277 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007278}
7279/* END_CASE */
7280
Paul Elliott91007972022-12-16 12:21:24 +00007281/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007282/**
7283 * verify_hash_fail_interruptible() test intentions:
7284 *
7285 * Note: This test can currently only handle ECDSA.
7286 *
7287 * 1. Test that various failure cases for interruptible verify hash fail with
7288 * the correct error codes, and at the correct point (at start or during
7289 * complete).
7290 *
7291 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7292 * expected for different max_ops values.
7293 *
7294 * 3. Test that the number of ops done prior to start and after abort is zero
7295 * and that each successful stage completes some ops (this is not mandated by
7296 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007297 *
7298 * 4. Check that calling complete() when start() fails and complete()
7299 * after completion results in a BAD_STATE error.
7300 *
7301 * 5. Check that calling start() again after start fails results in a BAD_STATE
7302 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007303 */
Paul Elliott91007972022-12-16 12:21:24 +00007304void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7305 int alg_arg, data_t *hash_data,
7306 data_t *signature_data,
7307 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007308 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007309 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007310{
7311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7312 psa_key_type_t key_type = key_type_arg;
7313 psa_algorithm_t alg = alg_arg;
7314 psa_status_t actual_status;
7315 psa_status_t expected_start_status = expected_start_status_arg;
7316 psa_status_t expected_complete_status = expected_complete_status_arg;
7317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007318 uint32_t num_ops = 0;
7319 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007320 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007321 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007322 size_t min_completes = 0;
7323 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007324 psa_verify_hash_interruptible_operation_t operation =
7325 psa_verify_hash_interruptible_operation_init();
7326
7327 PSA_ASSERT(psa_crypto_init());
7328
7329 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7330 psa_set_key_algorithm(&attributes, alg);
7331 psa_set_key_type(&attributes, key_type);
7332
7333 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7334 &key));
7335
Paul Elliott0c683352022-12-16 19:16:56 +00007336 psa_interruptible_set_max_ops(max_ops);
7337
Paul Elliott6f600372023-02-06 18:41:05 +00007338 interruptible_signverify_get_minmax_completes(max_ops,
7339 expected_complete_status,
7340 &min_completes,
7341 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007342
Paul Elliott91007972022-12-16 12:21:24 +00007343 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7344 TEST_ASSERT(num_ops_prior == 0);
7345
7346 /* Start verification. */
7347 actual_status = psa_verify_hash_start(&operation, key, alg,
7348 hash_data->x, hash_data->len,
7349 signature_data->x,
7350 signature_data->len);
7351
7352 TEST_EQUAL(actual_status, expected_start_status);
7353
Paul Elliottc9774412023-02-06 15:14:07 +00007354 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007355 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007356 * start failed. */
7357 actual_status = psa_verify_hash_complete(&operation);
7358
7359 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7360
7361 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007362 actual_status = psa_verify_hash_start(&operation, key, alg,
7363 hash_data->x, hash_data->len,
7364 signature_data->x,
7365 signature_data->len);
7366
7367 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7368 }
7369
Paul Elliott91007972022-12-16 12:21:24 +00007370 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7371 TEST_ASSERT(num_ops_prior == 0);
7372
Paul Elliott91007972022-12-16 12:21:24 +00007373 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007374 do {
Paul Elliott91007972022-12-16 12:21:24 +00007375 actual_status = psa_verify_hash_complete(&operation);
7376
Paul Elliott0c683352022-12-16 19:16:56 +00007377 num_completes++;
7378
Paul Elliott334d7262023-01-20 17:29:41 +00007379 if (actual_status == PSA_SUCCESS ||
7380 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007381 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007382 /* We are asserting here that every complete makes progress
7383 * (completes some ops), which is true of the internal
7384 * implementation and probably any implementation, however this is
7385 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007386 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007387
Paul Elliott91007972022-12-16 12:21:24 +00007388 num_ops_prior = num_ops;
7389 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007390 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007391
Paul Elliottc9774412023-02-06 15:14:07 +00007392 TEST_EQUAL(actual_status, expected_complete_status);
7393
Paul Elliottefebad02023-02-15 16:56:45 +00007394 /* Check that another complete returns BAD_STATE. */
7395 actual_status = psa_verify_hash_complete(&operation);
7396 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007397
Paul Elliott0c683352022-12-16 19:16:56 +00007398 TEST_LE_U(min_completes, num_completes);
7399 TEST_LE_U(num_completes, max_completes);
7400
Paul Elliott91007972022-12-16 12:21:24 +00007401 PSA_ASSERT(psa_verify_hash_abort(&operation));
7402
Paul Elliott59ad9452022-12-18 15:09:02 +00007403 num_ops = psa_verify_hash_get_num_ops(&operation);
7404 TEST_ASSERT(num_ops == 0);
7405
Paul Elliott91007972022-12-16 12:21:24 +00007406exit:
7407 psa_reset_key_attributes(&attributes);
7408 psa_destroy_key(key);
7409 PSA_DONE();
7410}
7411/* END_CASE */
7412
Paul Elliott20a36062022-12-18 13:21:25 +00007413/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007414/**
7415 * interruptible_signverify_hash_state_test() test intentions:
7416 *
7417 * Note: This test can currently only handle ECDSA.
7418 *
7419 * 1. Test that calling the various interruptible sign and verify hash functions
7420 * in incorrect orders returns BAD_STATE errors.
7421 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007422void interruptible_signverify_hash_state_test(int key_type_arg,
7423 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007424{
7425 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7426 psa_key_type_t key_type = key_type_arg;
7427 psa_algorithm_t alg = alg_arg;
7428 size_t key_bits;
7429 unsigned char *signature = NULL;
7430 size_t signature_size;
7431 size_t signature_length = 0xdeadbeef;
7432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7433 psa_sign_hash_interruptible_operation_t sign_operation =
7434 psa_sign_hash_interruptible_operation_init();
7435 psa_verify_hash_interruptible_operation_t verify_operation =
7436 psa_verify_hash_interruptible_operation_init();
7437
7438 PSA_ASSERT(psa_crypto_init());
7439
7440 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7441 PSA_KEY_USAGE_VERIFY_HASH);
7442 psa_set_key_algorithm(&attributes, alg);
7443 psa_set_key_type(&attributes, key_type);
7444
7445 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7446 &key));
7447 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7448 key_bits = psa_get_key_bits(&attributes);
7449
7450 /* Allocate a buffer which has the size advertised by the
7451 * library. */
7452 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7453 key_bits, alg);
7454 TEST_ASSERT(signature_size != 0);
7455 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007456 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007457
7458 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7459
7460 /* --- Attempt completes prior to starts --- */
7461 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7462 signature_size,
7463 &signature_length),
7464 PSA_ERROR_BAD_STATE);
7465
7466 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7467
7468 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7469 PSA_ERROR_BAD_STATE);
7470
7471 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7472
7473 /* --- Aborts in all other places. --- */
7474 psa_sign_hash_abort(&sign_operation);
7475
7476 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7477 input_data->x, input_data->len));
7478
7479 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7480
7481 psa_interruptible_set_max_ops(1);
7482
7483 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7484 input_data->x, input_data->len));
7485
7486 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7487 signature_size,
7488 &signature_length),
7489 PSA_OPERATION_INCOMPLETE);
7490
7491 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7492
7493 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7494
7495 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7496 input_data->x, input_data->len));
7497
7498 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7499 signature_size,
7500 &signature_length));
7501
7502 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7503
7504 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7505
7506 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7507 input_data->x, input_data->len,
7508 signature, signature_length));
7509
7510 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7511
7512 psa_interruptible_set_max_ops(1);
7513
7514 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7515 input_data->x, input_data->len,
7516 signature, signature_length));
7517
7518 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7519 PSA_OPERATION_INCOMPLETE);
7520
7521 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7522
7523 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7524
7525 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7526 input_data->x, input_data->len,
7527 signature, signature_length));
7528
7529 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7530
7531 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7532
7533 /* --- Attempt double starts. --- */
7534
7535 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7536 input_data->x, input_data->len));
7537
7538 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7539 input_data->x, input_data->len),
7540 PSA_ERROR_BAD_STATE);
7541
7542 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7543
7544 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7545 input_data->x, input_data->len,
7546 signature, signature_length));
7547
7548 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7549 input_data->x, input_data->len,
7550 signature, signature_length),
7551 PSA_ERROR_BAD_STATE);
7552
7553 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7554
Paul Elliott76d671a2023-02-07 17:45:18 +00007555exit:
7556 /*
7557 * Key attributes may have been returned by psa_get_key_attributes()
7558 * thus reset them as required.
7559 */
7560 psa_reset_key_attributes(&attributes);
7561
7562 psa_destroy_key(key);
7563 mbedtls_free(signature);
7564 PSA_DONE();
7565}
7566/* END_CASE */
7567
7568/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007569/**
Paul Elliottc2033502023-02-26 17:09:14 +00007570 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007571 *
7572 * Note: This test can currently only handle ECDSA.
7573 *
7574 * 1. Test various edge cases in the interruptible sign and verify hash
7575 * interfaces.
7576 */
Paul Elliottc2033502023-02-26 17:09:14 +00007577void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007578 data_t *key_data, int alg_arg, data_t *input_data)
7579{
7580 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7581 psa_key_type_t key_type = key_type_arg;
7582 psa_algorithm_t alg = alg_arg;
7583 size_t key_bits;
7584 unsigned char *signature = NULL;
7585 size_t signature_size;
7586 size_t signature_length = 0xdeadbeef;
7587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7588 uint8_t *input_buffer = NULL;
7589 psa_sign_hash_interruptible_operation_t sign_operation =
7590 psa_sign_hash_interruptible_operation_init();
7591 psa_verify_hash_interruptible_operation_t verify_operation =
7592 psa_verify_hash_interruptible_operation_init();
7593
7594 PSA_ASSERT(psa_crypto_init());
7595
7596 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7597 PSA_KEY_USAGE_VERIFY_HASH);
7598 psa_set_key_algorithm(&attributes, alg);
7599 psa_set_key_type(&attributes, key_type);
7600
7601 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7602 &key));
7603 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7604 key_bits = psa_get_key_bits(&attributes);
7605
7606 /* Allocate a buffer which has the size advertised by the
7607 * library. */
7608 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7609 key_bits, alg);
7610 TEST_ASSERT(signature_size != 0);
7611 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007612 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007613
Paul Elliott20a36062022-12-18 13:21:25 +00007614 /* --- Change function inputs mid run, to cause an error (sign only,
7615 * verify passes all inputs to start. --- */
7616
7617 psa_interruptible_set_max_ops(1);
7618
7619 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7620 input_data->x, input_data->len));
7621
7622 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7623 signature_size,
7624 &signature_length),
7625 PSA_OPERATION_INCOMPLETE);
7626
7627 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7628 0,
7629 &signature_length),
7630 PSA_ERROR_BUFFER_TOO_SMALL);
7631
Paul Elliottc9774412023-02-06 15:14:07 +00007632 /* And test that this invalidates the operation. */
7633 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7634 0,
7635 &signature_length),
7636 PSA_ERROR_BAD_STATE);
7637
Paul Elliott20a36062022-12-18 13:21:25 +00007638 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7639
Paul Elliottf9c91a72023-02-05 18:06:38 +00007640 /* Trash the hash buffer in between start and complete, to ensure
7641 * no reliance on external buffers. */
7642 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7643
Paul Elliott6c68df42023-10-23 15:33:37 +01007644 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007645
7646 memcpy(input_buffer, input_data->x, input_data->len);
7647
7648 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7649 input_buffer, input_data->len));
7650
7651 memset(input_buffer, '!', input_data->len);
7652 mbedtls_free(input_buffer);
7653 input_buffer = NULL;
7654
7655 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7656 signature_size,
7657 &signature_length));
7658
7659 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7660
Paul Elliott6c68df42023-10-23 15:33:37 +01007661 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007662
7663 memcpy(input_buffer, input_data->x, input_data->len);
7664
7665 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7666 input_buffer, input_data->len,
7667 signature, signature_length));
7668
7669 memset(input_buffer, '!', input_data->len);
7670 mbedtls_free(input_buffer);
7671 input_buffer = NULL;
7672
7673 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7674
7675 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7676
Paul Elliott20a36062022-12-18 13:21:25 +00007677exit:
7678 /*
7679 * Key attributes may have been returned by psa_get_key_attributes()
7680 * thus reset them as required.
7681 */
7682 psa_reset_key_attributes(&attributes);
7683
7684 psa_destroy_key(key);
7685 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007686 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007687 PSA_DONE();
7688}
7689/* END_CASE */
7690
Paul Elliotta4cb9092023-02-07 18:01:55 +00007691/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007692/**
Paul Elliott57702242023-02-26 20:36:10 +00007693 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007694 *
7695 * Note: This test can currently only handle ECDSA.
7696 *
7697 * 1. Test that setting max ops is reflected in both interruptible sign and
7698 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007699 * 2. Test that changing the value of max_ops to unlimited during an operation
7700 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007701 *
7702 * 3. Test that calling get_num_ops() between complete calls gives the same
7703 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007704 */
Paul Elliott57702242023-02-26 20:36:10 +00007705void interruptible_signverify_hash_ops_tests(int key_type_arg,
7706 data_t *key_data, int alg_arg,
7707 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007708{
7709 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7710 psa_key_type_t key_type = key_type_arg;
7711 psa_algorithm_t alg = alg_arg;
7712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007713 size_t key_bits;
7714 unsigned char *signature = NULL;
7715 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007716 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007717 uint32_t num_ops = 0;
7718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7719
Paul Elliotta4cb9092023-02-07 18:01:55 +00007720 psa_sign_hash_interruptible_operation_t sign_operation =
7721 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007722 psa_verify_hash_interruptible_operation_t verify_operation =
7723 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007724
7725 PSA_ASSERT(psa_crypto_init());
7726
7727 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7728 PSA_KEY_USAGE_VERIFY_HASH);
7729 psa_set_key_algorithm(&attributes, alg);
7730 psa_set_key_type(&attributes, key_type);
7731
Paul Elliottf1743e22023-02-15 18:44:16 +00007732 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7733 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7734 key_bits = psa_get_key_bits(&attributes);
7735
7736 /* Allocate a buffer which has the size advertised by the
7737 * library. */
7738 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7739
7740 TEST_ASSERT(signature_size != 0);
7741 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007742 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007743
7744 /* Check that default max ops gets set if we don't set it. */
7745 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7746 input_data->x, input_data->len));
7747
7748 TEST_EQUAL(psa_interruptible_get_max_ops(),
7749 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7750
7751 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7752
Paul Elliottf1743e22023-02-15 18:44:16 +00007753 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7754 input_data->x, input_data->len,
7755 signature, signature_size));
7756
7757 TEST_EQUAL(psa_interruptible_get_max_ops(),
7758 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7759
7760 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7761
Paul Elliotta4cb9092023-02-07 18:01:55 +00007762 /* Check that max ops gets set properly. */
7763
7764 psa_interruptible_set_max_ops(0xbeef);
7765
Paul Elliottf1743e22023-02-15 18:44:16 +00007766 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007767
Paul Elliott9e8819f2023-02-26 19:01:35 +00007768 /* --- Ensure changing the max ops mid operation works (operation should
7769 * complete successfully after setting max ops to unlimited --- */
7770 psa_interruptible_set_max_ops(1);
7771
7772 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7773 input_data->x, input_data->len));
7774
7775 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7776 signature_size,
7777 &signature_length),
7778 PSA_OPERATION_INCOMPLETE);
7779
7780 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7781
7782 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7783 signature_size,
7784 &signature_length));
7785
7786 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7787
7788 psa_interruptible_set_max_ops(1);
7789
7790 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7791 input_data->x, input_data->len,
7792 signature, signature_length));
7793
7794 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7795 PSA_OPERATION_INCOMPLETE);
7796
7797 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7798
7799 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7800
7801 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7802
Paul Elliottc1e04002023-02-26 20:27:23 +00007803 /* --- Test that not calling get_num_ops inbetween complete calls does not
7804 * result in lost ops. ---*/
7805
7806 psa_interruptible_set_max_ops(1);
7807
7808 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7809 input_data->x, input_data->len));
7810
7811 /* Continue performing the signature until complete. */
7812 do {
7813 status = psa_sign_hash_complete(&sign_operation, signature,
7814 signature_size,
7815 &signature_length);
7816
7817 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7818
7819 } while (status == PSA_OPERATION_INCOMPLETE);
7820
7821 PSA_ASSERT(status);
7822
7823 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7824
7825 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7826 input_data->x, input_data->len));
7827
7828 /* Continue performing the signature until complete. */
7829 do {
7830 status = psa_sign_hash_complete(&sign_operation, signature,
7831 signature_size,
7832 &signature_length);
7833 } while (status == PSA_OPERATION_INCOMPLETE);
7834
7835 PSA_ASSERT(status);
7836
7837 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7838
7839 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7840
7841 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7842 input_data->x, input_data->len,
7843 signature, signature_length));
7844
7845 /* Continue performing the verification until complete. */
7846 do {
7847 status = psa_verify_hash_complete(&verify_operation);
7848
7849 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7850
7851 } while (status == PSA_OPERATION_INCOMPLETE);
7852
7853 PSA_ASSERT(status);
7854
7855 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7856
7857 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7858 input_data->x, input_data->len,
7859 signature, signature_length));
7860
7861 /* Continue performing the verification until complete. */
7862 do {
7863 status = psa_verify_hash_complete(&verify_operation);
7864
7865 } while (status == PSA_OPERATION_INCOMPLETE);
7866
7867 PSA_ASSERT(status);
7868
7869 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7870
7871 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7872
Paul Elliotta4cb9092023-02-07 18:01:55 +00007873exit:
7874 /*
7875 * Key attributes may have been returned by psa_get_key_attributes()
7876 * thus reset them as required.
7877 */
7878 psa_reset_key_attributes(&attributes);
7879
7880 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007881 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007882 PSA_DONE();
7883}
7884/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007885
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007886/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007887void sign_message_deterministic(int key_type_arg,
7888 data_t *key_data,
7889 int alg_arg,
7890 data_t *input_data,
7891 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007892{
7893 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7894 psa_key_type_t key_type = key_type_arg;
7895 psa_algorithm_t alg = alg_arg;
7896 size_t key_bits;
7897 unsigned char *signature = NULL;
7898 size_t signature_size;
7899 size_t signature_length = 0xdeadbeef;
7900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7905 psa_set_key_algorithm(&attributes, alg);
7906 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7909 &key));
7910 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7911 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007912
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7914 TEST_ASSERT(signature_size != 0);
7915 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007916 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 PSA_ASSERT(psa_sign_message(key, alg,
7919 input_data->x, input_data->len,
7920 signature, signature_size,
7921 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007922
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007923 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007924 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007925
7926exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007928
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 psa_destroy_key(key);
7930 mbedtls_free(signature);
7931 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007932
7933}
7934/* END_CASE */
7935
7936/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007937void sign_message_fail(int key_type_arg,
7938 data_t *key_data,
7939 int alg_arg,
7940 data_t *input_data,
7941 int signature_size_arg,
7942 int expected_status_arg)
7943{
7944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7945 psa_key_type_t key_type = key_type_arg;
7946 psa_algorithm_t alg = alg_arg;
7947 size_t signature_size = signature_size_arg;
7948 psa_status_t actual_status;
7949 psa_status_t expected_status = expected_status_arg;
7950 unsigned char *signature = NULL;
7951 size_t signature_length = 0xdeadbeef;
7952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7953
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007954 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007955
7956 PSA_ASSERT(psa_crypto_init());
7957
7958 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7959 psa_set_key_algorithm(&attributes, alg);
7960 psa_set_key_type(&attributes, key_type);
7961
7962 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7963 &key));
7964
7965 actual_status = psa_sign_message(key, alg,
7966 input_data->x, input_data->len,
7967 signature, signature_size,
7968 &signature_length);
7969 TEST_EQUAL(actual_status, expected_status);
7970 /* The value of *signature_length is unspecified on error, but
7971 * whatever it is, it should be less than signature_size, so that
7972 * if the caller tries to read *signature_length bytes without
7973 * checking the error code then they don't overflow a buffer. */
7974 TEST_LE_U(signature_length, signature_size);
7975
7976exit:
7977 psa_reset_key_attributes(&attributes);
7978 psa_destroy_key(key);
7979 mbedtls_free(signature);
7980 PSA_DONE();
7981}
7982/* END_CASE */
7983
7984/* BEGIN_CASE */
7985void sign_verify_message(int key_type_arg,
7986 data_t *key_data,
7987 int alg_arg,
7988 data_t *input_data)
7989{
7990 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7991 psa_key_type_t key_type = key_type_arg;
7992 psa_algorithm_t alg = alg_arg;
7993 size_t key_bits;
7994 unsigned char *signature = NULL;
7995 size_t signature_size;
7996 size_t signature_length = 0xdeadbeef;
7997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7998
7999 PSA_ASSERT(psa_crypto_init());
8000
8001 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8002 PSA_KEY_USAGE_VERIFY_MESSAGE);
8003 psa_set_key_algorithm(&attributes, alg);
8004 psa_set_key_type(&attributes, key_type);
8005
8006 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8007 &key));
8008 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8009 key_bits = psa_get_key_bits(&attributes);
8010
8011 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8012 TEST_ASSERT(signature_size != 0);
8013 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008014 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008015
8016 PSA_ASSERT(psa_sign_message(key, alg,
8017 input_data->x, input_data->len,
8018 signature, signature_size,
8019 &signature_length));
8020 TEST_LE_U(signature_length, signature_size);
8021 TEST_ASSERT(signature_length > 0);
8022
8023 PSA_ASSERT(psa_verify_message(key, alg,
8024 input_data->x, input_data->len,
8025 signature, signature_length));
8026
8027 if (input_data->len != 0) {
8028 /* Flip a bit in the input and verify that the signature is now
8029 * detected as invalid. Flip a bit at the beginning, not at the end,
8030 * because ECDSA may ignore the last few bits of the input. */
8031 input_data->x[0] ^= 1;
8032 TEST_EQUAL(psa_verify_message(key, alg,
8033 input_data->x, input_data->len,
8034 signature, signature_length),
8035 PSA_ERROR_INVALID_SIGNATURE);
8036 }
8037
8038exit:
8039 psa_reset_key_attributes(&attributes);
8040
8041 psa_destroy_key(key);
8042 mbedtls_free(signature);
8043 PSA_DONE();
8044}
8045/* END_CASE */
8046
8047/* BEGIN_CASE */
8048void verify_message(int key_type_arg,
8049 data_t *key_data,
8050 int alg_arg,
8051 data_t *input_data,
8052 data_t *signature_data)
8053{
8054 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8055 psa_key_type_t key_type = key_type_arg;
8056 psa_algorithm_t alg = alg_arg;
8057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8058
8059 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8060
8061 PSA_ASSERT(psa_crypto_init());
8062
8063 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8064 psa_set_key_algorithm(&attributes, alg);
8065 psa_set_key_type(&attributes, key_type);
8066
8067 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8068 &key));
8069
8070 PSA_ASSERT(psa_verify_message(key, alg,
8071 input_data->x, input_data->len,
8072 signature_data->x, signature_data->len));
8073
8074exit:
8075 psa_reset_key_attributes(&attributes);
8076 psa_destroy_key(key);
8077 PSA_DONE();
8078}
8079/* END_CASE */
8080
8081/* BEGIN_CASE */
8082void verify_message_fail(int key_type_arg,
8083 data_t *key_data,
8084 int alg_arg,
8085 data_t *hash_data,
8086 data_t *signature_data,
8087 int expected_status_arg)
8088{
8089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8090 psa_key_type_t key_type = key_type_arg;
8091 psa_algorithm_t alg = alg_arg;
8092 psa_status_t actual_status;
8093 psa_status_t expected_status = expected_status_arg;
8094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8095
8096 PSA_ASSERT(psa_crypto_init());
8097
8098 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8099 psa_set_key_algorithm(&attributes, alg);
8100 psa_set_key_type(&attributes, key_type);
8101
8102 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8103 &key));
8104
8105 actual_status = psa_verify_message(key, alg,
8106 hash_data->x, hash_data->len,
8107 signature_data->x,
8108 signature_data->len);
8109 TEST_EQUAL(actual_status, expected_status);
8110
8111exit:
8112 psa_reset_key_attributes(&attributes);
8113 psa_destroy_key(key);
8114 PSA_DONE();
8115}
8116/* END_CASE */
8117
8118/* BEGIN_CASE */
8119void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008120 data_t *key_data,
8121 int alg_arg,
8122 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 data_t *label,
8124 int expected_output_length_arg,
8125 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008126{
Ronald Cron5425a212020-08-04 14:58:35 +02008127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008128 psa_key_type_t key_type = key_type_arg;
8129 psa_algorithm_t alg = alg_arg;
8130 size_t expected_output_length = expected_output_length_arg;
8131 size_t key_bits;
8132 unsigned char *output = NULL;
8133 size_t output_size;
8134 size_t output_length = ~0;
8135 psa_status_t actual_status;
8136 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008138
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008140
Gilles Peskine656896e2018-06-29 19:12:28 +02008141 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8143 psa_set_key_algorithm(&attributes, alg);
8144 psa_set_key_type(&attributes, key_type);
8145 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8146 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008147
8148 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8150 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008151
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8153 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008154 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008155
8156 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 actual_status = psa_asymmetric_encrypt(key, alg,
8158 input_data->x, input_data->len,
8159 label->x, label->len,
8160 output, output_size,
8161 &output_length);
8162 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008163 if (actual_status == PSA_SUCCESS) {
8164 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008165 } else {
8166 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008167 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008168
Gilles Peskine68428122018-06-30 18:42:41 +02008169 /* If the label is empty, the test framework puts a non-null pointer
8170 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008171 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008172 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008173 if (output_size != 0) {
8174 memset(output, 0, output_size);
8175 }
8176 actual_status = psa_asymmetric_encrypt(key, alg,
8177 input_data->x, input_data->len,
8178 NULL, label->len,
8179 output, output_size,
8180 &output_length);
8181 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008182 if (actual_status == PSA_SUCCESS) {
8183 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008184 } else {
8185 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008186 }
Gilles Peskine68428122018-06-30 18:42:41 +02008187 }
8188
Gilles Peskine656896e2018-06-29 19:12:28 +02008189exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008190 /*
8191 * Key attributes may have been returned by psa_get_key_attributes()
8192 * thus reset them as required.
8193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008195
Gilles Peskine449bd832023-01-11 14:50:10 +01008196 psa_destroy_key(key);
8197 mbedtls_free(output);
8198 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008199}
8200/* END_CASE */
8201
8202/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008203void asymmetric_encrypt_decrypt(int key_type_arg,
8204 data_t *key_data,
8205 int alg_arg,
8206 data_t *input_data,
8207 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008208{
Ronald Cron5425a212020-08-04 14:58:35 +02008209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008210 psa_key_type_t key_type = key_type_arg;
8211 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008212 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008213 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008214 size_t output_size;
8215 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008216 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008217 size_t output2_size;
8218 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008220
Gilles Peskine449bd832023-01-11 14:50:10 +01008221 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8224 psa_set_key_algorithm(&attributes, alg);
8225 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8228 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008229
8230 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8232 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008233
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8235 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008236 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008237
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008238 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 TEST_LE_U(output2_size,
8240 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8241 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008242 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008243
Gilles Peskineeebd7382018-06-08 18:11:54 +02008244 /* We test encryption by checking that encrypt-then-decrypt gives back
8245 * the original plaintext because of the non-optional random
8246 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8248 input_data->x, input_data->len,
8249 label->x, label->len,
8250 output, output_size,
8251 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008252 /* We don't know what ciphertext length to expect, but check that
8253 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008255
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8257 output, output_length,
8258 label->x, label->len,
8259 output2, output2_size,
8260 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008261 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008262 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008263
8264exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008265 /*
8266 * Key attributes may have been returned by psa_get_key_attributes()
8267 * thus reset them as required.
8268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008270
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 psa_destroy_key(key);
8272 mbedtls_free(output);
8273 mbedtls_free(output2);
8274 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008275}
8276/* END_CASE */
8277
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008278/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279void asymmetric_decrypt(int key_type_arg,
8280 data_t *key_data,
8281 int alg_arg,
8282 data_t *input_data,
8283 data_t *label,
8284 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008285{
Ronald Cron5425a212020-08-04 14:58:35 +02008286 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008287 psa_key_type_t key_type = key_type_arg;
8288 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008289 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008290 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008291 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008292 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008294
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008296
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8298 psa_set_key_algorithm(&attributes, alg);
8299 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008300
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8302 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008303
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8305 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008306
8307 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8309 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008310 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008311
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8313 input_data->x, input_data->len,
8314 label->x, label->len,
8315 output,
8316 output_size,
8317 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008318 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008319 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008320
Gilles Peskine68428122018-06-30 18:42:41 +02008321 /* If the label is empty, the test framework puts a non-null pointer
8322 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008324 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008325 if (output_size != 0) {
8326 memset(output, 0, output_size);
8327 }
8328 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8329 input_data->x, input_data->len,
8330 NULL, label->len,
8331 output,
8332 output_size,
8333 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008334 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008335 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008336 }
8337
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 psa_reset_key_attributes(&attributes);
8340 psa_destroy_key(key);
8341 mbedtls_free(output);
8342 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008343}
8344/* END_CASE */
8345
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008346/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008347void asymmetric_decrypt_fail(int key_type_arg,
8348 data_t *key_data,
8349 int alg_arg,
8350 data_t *input_data,
8351 data_t *label,
8352 int output_size_arg,
8353 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008354{
Ronald Cron5425a212020-08-04 14:58:35 +02008355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356 psa_key_type_t key_type = key_type_arg;
8357 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008358 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008359 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008360 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008361 psa_status_t actual_status;
8362 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008364
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008365 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008366
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008368
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8370 psa_set_key_algorithm(&attributes, alg);
8371 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008372
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8374 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008375
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 actual_status = psa_asymmetric_decrypt(key, alg,
8377 input_data->x, input_data->len,
8378 label->x, label->len,
8379 output, output_size,
8380 &output_length);
8381 TEST_EQUAL(actual_status, expected_status);
8382 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008383
Gilles Peskine68428122018-06-30 18:42:41 +02008384 /* If the label is empty, the test framework puts a non-null pointer
8385 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008387 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008388 if (output_size != 0) {
8389 memset(output, 0, output_size);
8390 }
8391 actual_status = psa_asymmetric_decrypt(key, alg,
8392 input_data->x, input_data->len,
8393 NULL, label->len,
8394 output, output_size,
8395 &output_length);
8396 TEST_EQUAL(actual_status, expected_status);
8397 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008398 }
8399
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008400exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 psa_reset_key_attributes(&attributes);
8402 psa_destroy_key(key);
8403 mbedtls_free(output);
8404 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008405}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008406/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008407
8408/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008409void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008410{
8411 /* Test each valid way of initializing the object, except for `= {0}`, as
8412 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8413 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008414 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008415 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008417 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8418 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008419
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008421
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008422 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8424 PSA_ERROR_BAD_STATE);
8425 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8426 PSA_ERROR_BAD_STATE);
8427 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8428 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008429
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008430 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 PSA_ASSERT(psa_key_derivation_abort(&func));
8432 PSA_ASSERT(psa_key_derivation_abort(&init));
8433 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008434}
8435/* END_CASE */
8436
Janos Follath16de4a42019-06-13 16:32:24 +01008437/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008438void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008439{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008440 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008441 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008442 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008443
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008445
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8447 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008448
8449exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 psa_key_derivation_abort(&operation);
8451 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008452}
8453/* END_CASE */
8454
Janos Follathaf3c2a02019-06-12 12:34:34 +01008455/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008456void derive_set_capacity(int alg_arg, int capacity_arg,
8457 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008458{
8459 psa_algorithm_t alg = alg_arg;
8460 size_t capacity = capacity_arg;
8461 psa_status_t expected_status = expected_status_arg;
8462 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008465
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008467
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8469 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008470
8471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 psa_key_derivation_abort(&operation);
8473 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008474}
8475/* END_CASE */
8476
8477/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308478void parse_binary_string_test(data_t *input, int output)
8479{
8480 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308481 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308482 TEST_EQUAL(value, output);
8483}
8484/* END_CASE */
8485
8486/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008487void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308488 int step_arg1, int key_type_arg1, data_t *input1,
8489 int expected_status_arg1,
8490 int step_arg2, int key_type_arg2, data_t *input2,
8491 int expected_status_arg2,
8492 int step_arg3, int key_type_arg3, data_t *input3,
8493 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008495{
8496 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308498 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 psa_status_t expected_statuses[] = { expected_status_arg1,
8500 expected_status_arg2,
8501 expected_status_arg3 };
8502 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008503 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8504 MBEDTLS_SVC_KEY_ID_INIT,
8505 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008506 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8508 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008509 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008510 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008511 psa_status_t expected_output_status = expected_output_status_arg;
8512 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008513
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008515
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8517 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008520
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8522 mbedtls_test_set_step(i);
8523 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008524 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308525 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8526 key_types[i] != INPUT_INTEGER) {
8527 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 PSA_ASSERT(psa_import_key(&attributes,
8529 inputs[i]->x, inputs[i]->len,
8530 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308531 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008533 // When taking a private key as secret input, use key agreement
8534 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8536 &operation, keys[i]),
8537 expected_statuses[i]);
8538 } else {
8539 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8540 keys[i]),
8541 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008542 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308544 if (key_types[i] == INPUT_INTEGER) {
8545 TEST_EQUAL(psa_key_derivation_input_integer(
8546 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308547 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308548 expected_statuses[i]);
8549 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308550 TEST_EQUAL(psa_key_derivation_input_bytes(
8551 &operation, steps[i],
8552 inputs[i]->x, inputs[i]->len),
8553 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308554 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008555 }
8556 }
8557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 if (output_key_type != PSA_KEY_TYPE_NONE) {
8559 psa_reset_key_attributes(&attributes);
8560 psa_set_key_type(&attributes, output_key_type);
8561 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008562 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 psa_key_derivation_output_key(&attributes, &operation,
8564 &output_key);
8565 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008566 uint8_t buffer[1];
8567 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 psa_key_derivation_output_bytes(&operation,
8569 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008570 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008572
Janos Follathaf3c2a02019-06-12 12:34:34 +01008573exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 psa_key_derivation_abort(&operation);
8575 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8576 psa_destroy_key(keys[i]);
8577 }
8578 psa_destroy_key(output_key);
8579 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008580}
8581/* END_CASE */
8582
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308583/* BEGIN_CASE*/
8584void derive_input_invalid_cost(int alg_arg, int64_t cost)
8585{
8586 psa_algorithm_t alg = alg_arg;
8587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8588
8589 PSA_ASSERT(psa_crypto_init());
8590 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8591
8592 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8593 PSA_KEY_DERIVATION_INPUT_COST,
8594 cost),
8595 PSA_ERROR_NOT_SUPPORTED);
8596
8597exit:
8598 psa_key_derivation_abort(&operation);
8599 PSA_DONE();
8600}
8601/* END_CASE*/
8602
Janos Follathd958bb72019-07-03 15:02:16 +01008603/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008604void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008605{
Janos Follathd958bb72019-07-03 15:02:16 +01008606 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008608 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008609 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008610 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008612 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008614 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008616 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8617 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008622
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8624 psa_set_key_algorithm(&attributes, alg);
8625 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008626
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 PSA_ASSERT(psa_import_key(&attributes,
8628 key_data, sizeof(key_data),
8629 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008630
8631 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8633 input1, input1_length,
8634 input2, input2_length,
8635 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008636 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008638
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008639 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8641 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008642
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008644
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8646 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008647
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008648exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 psa_key_derivation_abort(&operation);
8650 psa_destroy_key(key);
8651 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008652}
8653/* END_CASE */
8654
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008655/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008656void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008657{
8658 uint8_t output_buffer[16];
8659 size_t buffer_size = 16;
8660 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008661 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008662
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8664 output_buffer, buffer_size)
8665 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008666
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8668 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008669
Gilles Peskine449bd832023-01-11 14:50:10 +01008670 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008671
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8673 output_buffer, buffer_size)
8674 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008675
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8677 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008678
8679exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008681}
8682/* END_CASE */
8683
8684/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008685void derive_output(int alg_arg,
8686 int step1_arg, data_t *input1, int expected_status_arg1,
8687 int step2_arg, data_t *input2, int expected_status_arg2,
8688 int step3_arg, data_t *input3, int expected_status_arg3,
8689 int step4_arg, data_t *input4, int expected_status_arg4,
8690 data_t *key_agreement_peer_key,
8691 int requested_capacity_arg,
8692 data_t *expected_output1,
8693 data_t *expected_output2,
8694 int other_key_input_type,
8695 int key_input_type,
8696 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008697{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008698 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8700 data_t *inputs[] = { input1, input2, input3, input4 };
8701 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8702 MBEDTLS_SVC_KEY_ID_INIT,
8703 MBEDTLS_SVC_KEY_ID_INIT,
8704 MBEDTLS_SVC_KEY_ID_INIT };
8705 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8706 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008707 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008709 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008711 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008713 size_t output_buffer_size = 0;
8714 uint8_t *output_buffer = NULL;
8715 size_t expected_capacity;
8716 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008717 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8718 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8719 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8720 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008721 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008722 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008723 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008724
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8726 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008727 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 }
8729 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008730 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008732 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008733 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008735
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008736 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8738 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8739 requested_capacity));
8740 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8741 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008742 case 0:
8743 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308744 case PSA_KEY_DERIVATION_INPUT_COST:
8745 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308746 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308747 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308748 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308749 if (statuses[i] != PSA_SUCCESS) {
8750 goto exit;
8751 }
8752 break;
8753 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008754 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008756 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 TEST_EQUAL(psa_key_derivation_input_bytes(
8758 &operation, steps[i],
8759 inputs[i]->x, inputs[i]->len),
8760 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008761
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008763 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008765 break;
8766 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8768 psa_set_key_algorithm(&attributes1, alg);
8769 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008770
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 PSA_ASSERT(psa_import_key(&attributes1,
8772 inputs[i]->x, inputs[i]->len,
8773 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008774
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8776 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8777 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8778 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008779 }
8780
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308781 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308783 keys[i]),
8784 statuses[i]);
8785
8786 if (statuses[i] != PSA_SUCCESS) {
8787 goto exit;
8788 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008789 break;
8790 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008791 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008792 break;
8793 }
8794 break;
8795 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008797 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8799 steps[i],
8800 inputs[i]->x,
8801 inputs[i]->len),
8802 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008803 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008804 case 1: // input key, type DERIVE
8805 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8807 psa_set_key_algorithm(&attributes2, alg);
8808 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008809
8810 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 if (other_key_input_type == 11) {
8812 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8813 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 PSA_ASSERT(psa_import_key(&attributes2,
8816 inputs[i]->x, inputs[i]->len,
8817 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008818
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8820 steps[i],
8821 keys[i]),
8822 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008823 break;
8824 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8826 psa_set_key_algorithm(&attributes3, alg);
8827 psa_set_key_type(&attributes3,
8828 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008829
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 PSA_ASSERT(psa_import_key(&attributes3,
8831 inputs[i]->x, inputs[i]->len,
8832 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008833
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 TEST_EQUAL(psa_key_derivation_key_agreement(
8835 &operation,
8836 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8837 keys[i], key_agreement_peer_key->x,
8838 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008839 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008840 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008841 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008842 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008843 }
8844
Gilles Peskine449bd832023-01-11 14:50:10 +01008845 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008846 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008848 break;
8849 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 TEST_EQUAL(psa_key_derivation_input_bytes(
8851 &operation, steps[i],
8852 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008853
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008855 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008857 break;
8858 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008859 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8862 &current_capacity));
8863 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008864 expected_capacity = requested_capacity;
8865
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008867 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8868
8869 /* For output key derivation secret must be provided using
8870 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008872 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008874
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8876 psa_set_key_algorithm(&attributes4, alg);
8877 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8878 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008879
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8881 &derived_key), expected_status);
8882 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008883 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008885 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 status = psa_key_derivation_output_bytes(&operation,
8887 output_buffer, output_sizes[i]);
8888 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008889 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 TEST_ASSERT(status == PSA_SUCCESS ||
8891 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008892 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 } else if (expected_capacity == 0 ||
8894 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008895 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008897 expected_capacity = 0;
8898 continue;
8899 }
8900 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 PSA_ASSERT(status);
8902 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008903 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008904 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008906 /* Check the operation status. */
8907 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8909 &current_capacity));
8910 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008911 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008912 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008914
8915exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008916 mbedtls_free(output_buffer);
8917 psa_key_derivation_abort(&operation);
8918 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8919 psa_destroy_key(keys[i]);
8920 }
8921 psa_destroy_key(derived_key);
8922 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008923}
8924/* END_CASE */
8925
8926/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008927void derive_full(int alg_arg,
8928 data_t *key_data,
8929 data_t *input1,
8930 data_t *input2,
8931 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008932{
Ronald Cron5425a212020-08-04 14:58:35 +02008933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008934 psa_algorithm_t alg = alg_arg;
8935 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008936 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008937 unsigned char output_buffer[16];
8938 size_t expected_capacity = requested_capacity;
8939 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008941
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008943
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8945 psa_set_key_algorithm(&attributes, alg);
8946 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008947
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8949 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008950
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8952 input1->x, input1->len,
8953 input2->x, input2->len,
8954 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008955 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008957
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8959 &current_capacity));
8960 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008961
8962 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 while (current_capacity > 0) {
8964 size_t read_size = sizeof(output_buffer);
8965 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008966 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008967 }
8968 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8969 output_buffer,
8970 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008971 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8973 &current_capacity));
8974 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008975 }
8976
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008977 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8979 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008980
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008982
8983exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 psa_key_derivation_abort(&operation);
8985 psa_destroy_key(key);
8986 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008987}
8988/* END_CASE */
8989
Stephan Koch78109f52023-04-12 14:19:36 +02008990/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008991void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8992 int derivation_step,
8993 int capacity, int expected_capacity_status_arg,
8994 data_t *expected_output,
8995 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008996{
8997 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8998 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008999 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009000 uint8_t *output_buffer = NULL;
9001 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009002 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9003 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9004 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009005
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009006 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009008
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9010 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9011 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009012
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9014 step, input->x, input->len),
9015 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009016
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009018 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009020
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9022 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009023
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 TEST_EQUAL(status, expected_output_status);
9025 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009026 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009027 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009029
9030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 mbedtls_free(output_buffer);
9032 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009033 PSA_DONE();
9034}
9035/* END_CASE */
9036
Janos Follathe60c9052019-07-03 13:51:30 +01009037/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009038void derive_key_exercise(int alg_arg,
9039 data_t *key_data,
9040 data_t *input1,
9041 data_t *input2,
9042 int derived_type_arg,
9043 int derived_bits_arg,
9044 int derived_usage_arg,
9045 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009046{
Ronald Cron5425a212020-08-04 14:58:35 +02009047 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9048 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009049 psa_algorithm_t alg = alg_arg;
9050 psa_key_type_t derived_type = derived_type_arg;
9051 size_t derived_bits = derived_bits_arg;
9052 psa_key_usage_t derived_usage = derived_usage_arg;
9053 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009055 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009057 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009058
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009060
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9062 psa_set_key_algorithm(&attributes, alg);
9063 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9064 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9065 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009066
9067 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9069 input1->x, input1->len,
9070 input2->x, input2->len,
9071 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 }
Janos Follathe60c9052019-07-03 13:51:30 +01009074
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 psa_set_key_usage_flags(&attributes, derived_usage);
9076 psa_set_key_algorithm(&attributes, derived_alg);
9077 psa_set_key_type(&attributes, derived_type);
9078 psa_set_key_bits(&attributes, derived_bits);
9079 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9080 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009081
9082 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9084 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9085 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009086
9087 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009089 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009091
9092exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009093 /*
9094 * Key attributes may have been returned by psa_get_key_attributes()
9095 * thus reset them as required.
9096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 psa_key_derivation_abort(&operation);
9100 psa_destroy_key(base_key);
9101 psa_destroy_key(derived_key);
9102 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009103}
9104/* END_CASE */
9105
Janos Follath42fd8882019-07-03 14:17:09 +01009106/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009107void derive_key_export(int alg_arg,
9108 data_t *key_data,
9109 data_t *input1,
9110 data_t *input2,
9111 int bytes1_arg,
9112 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009113{
Ronald Cron5425a212020-08-04 14:58:35 +02009114 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9115 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009116 psa_algorithm_t alg = alg_arg;
9117 size_t bytes1 = bytes1_arg;
9118 size_t bytes2 = bytes2_arg;
9119 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009120 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009121 uint8_t *output_buffer = NULL;
9122 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009123 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9124 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009125 size_t length;
9126
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009127 TEST_CALLOC(output_buffer, capacity);
9128 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009130
Gilles Peskine449bd832023-01-11 14:50:10 +01009131 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9132 psa_set_key_algorithm(&base_attributes, alg);
9133 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9134 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9135 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009136
9137 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9139 input1->x, input1->len,
9140 input2->x, input2->len,
9141 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 }
Janos Follath42fd8882019-07-03 14:17:09 +01009144
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9146 output_buffer,
9147 capacity));
9148 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009149
9150 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9152 input1->x, input1->len,
9153 input2->x, input2->len,
9154 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 }
Janos Follath42fd8882019-07-03 14:17:09 +01009157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9159 psa_set_key_algorithm(&derived_attributes, 0);
9160 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9161 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9162 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9163 &derived_key));
9164 PSA_ASSERT(psa_export_key(derived_key,
9165 export_buffer, bytes1,
9166 &length));
9167 TEST_EQUAL(length, bytes1);
9168 PSA_ASSERT(psa_destroy_key(derived_key));
9169 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9170 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9171 &derived_key));
9172 PSA_ASSERT(psa_export_key(derived_key,
9173 export_buffer + bytes1, bytes2,
9174 &length));
9175 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009176
9177 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009178 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009179 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009180
9181exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 mbedtls_free(output_buffer);
9183 mbedtls_free(export_buffer);
9184 psa_key_derivation_abort(&operation);
9185 psa_destroy_key(base_key);
9186 psa_destroy_key(derived_key);
9187 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009188}
9189/* END_CASE */
9190
9191/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009192void derive_key_type(int alg_arg,
9193 data_t *key_data,
9194 data_t *input1,
9195 data_t *input2,
9196 int key_type_arg, int bits_arg,
9197 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009198{
9199 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9200 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9201 const psa_algorithm_t alg = alg_arg;
9202 const psa_key_type_t key_type = key_type_arg;
9203 const size_t bits = bits_arg;
9204 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9205 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009207 uint8_t *export_buffer = NULL;
9208 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9209 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9210 size_t export_length;
9211
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009212 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009214
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9216 psa_set_key_algorithm(&base_attributes, alg);
9217 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9218 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9219 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009220
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009222 &operation, base_key, alg,
9223 input1->x, input1->len,
9224 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9230 psa_set_key_algorithm(&derived_attributes, 0);
9231 psa_set_key_type(&derived_attributes, key_type);
9232 psa_set_key_bits(&derived_attributes, bits);
9233 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9234 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009235
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 PSA_ASSERT(psa_export_key(derived_key,
9237 export_buffer, export_buffer_size,
9238 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009239 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009240 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009241
9242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 mbedtls_free(export_buffer);
9244 psa_key_derivation_abort(&operation);
9245 psa_destroy_key(base_key);
9246 psa_destroy_key(derived_key);
9247 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009248}
9249/* END_CASE */
9250
9251/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009252void derive_key(int alg_arg,
9253 data_t *key_data, data_t *input1, data_t *input2,
9254 int type_arg, int bits_arg,
9255 int expected_status_arg,
9256 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009257{
Ronald Cron5425a212020-08-04 14:58:35 +02009258 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9259 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009260 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009261 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009262 size_t bits = bits_arg;
9263 psa_status_t expected_status = expected_status_arg;
9264 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9265 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9266 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9267
Gilles Peskine449bd832023-01-11 14:50:10 +01009268 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9271 psa_set_key_algorithm(&base_attributes, alg);
9272 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9273 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9274 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009275
Gilles Peskine449bd832023-01-11 14:50:10 +01009276 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9277 input1->x, input1->len,
9278 input2->x, input2->len,
9279 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009282
Gilles Peskine449bd832023-01-11 14:50:10 +01009283 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9284 psa_set_key_algorithm(&derived_attributes, 0);
9285 psa_set_key_type(&derived_attributes, type);
9286 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009287
9288 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009289 psa_key_derivation_output_key(&derived_attributes,
9290 &operation,
9291 &derived_key);
9292 if (is_large_output > 0) {
9293 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9294 }
9295 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009296
9297exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009298 psa_key_derivation_abort(&operation);
9299 psa_destroy_key(base_key);
9300 psa_destroy_key(derived_key);
9301 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009302}
9303/* END_CASE */
9304
9305/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009306void key_agreement_setup(int alg_arg,
9307 int our_key_type_arg, int our_key_alg_arg,
9308 data_t *our_key_data, data_t *peer_key_data,
9309 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009310{
Ronald Cron5425a212020-08-04 14:58:35 +02009311 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009312 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009313 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009314 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009315 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009317 psa_status_t expected_status = expected_status_arg;
9318 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009319
Gilles Peskine449bd832023-01-11 14:50:10 +01009320 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009321
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9323 psa_set_key_algorithm(&attributes, our_key_alg);
9324 psa_set_key_type(&attributes, our_key_type);
9325 PSA_ASSERT(psa_import_key(&attributes,
9326 our_key_data->x, our_key_data->len,
9327 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009328
Gilles Peskine77f40d82019-04-11 21:27:06 +02009329 /* The tests currently include inputs that should fail at either step.
9330 * Test cases that fail at the setup step should be changed to call
9331 * key_derivation_setup instead, and this function should be renamed
9332 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009333 status = psa_key_derivation_setup(&operation, alg);
9334 if (status == PSA_SUCCESS) {
9335 TEST_EQUAL(psa_key_derivation_key_agreement(
9336 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9337 our_key,
9338 peer_key_data->x, peer_key_data->len),
9339 expected_status);
9340 } else {
9341 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009342 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009343
9344exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009345 psa_key_derivation_abort(&operation);
9346 psa_destroy_key(our_key);
9347 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009348}
9349/* END_CASE */
9350
9351/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009352void raw_key_agreement(int alg_arg,
9353 int our_key_type_arg, data_t *our_key_data,
9354 data_t *peer_key_data,
9355 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009356{
Ronald Cron5425a212020-08-04 14:58:35 +02009357 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009358 psa_algorithm_t alg = alg_arg;
9359 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009361 unsigned char *output = NULL;
9362 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009363 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009364
Gilles Peskine449bd832023-01-11 14:50:10 +01009365 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009366
Gilles Peskine449bd832023-01-11 14:50:10 +01009367 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9368 psa_set_key_algorithm(&attributes, alg);
9369 psa_set_key_type(&attributes, our_key_type);
9370 PSA_ASSERT(psa_import_key(&attributes,
9371 our_key_data->x, our_key_data->len,
9372 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009373
Gilles Peskine449bd832023-01-11 14:50:10 +01009374 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9375 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009376
Gilles Peskine992bee82022-04-13 23:25:52 +02009377 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009378 TEST_LE_U(expected_output->len,
9379 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9380 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9381 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009382
9383 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009384 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9386 peer_key_data->x, peer_key_data->len,
9387 output, expected_output->len,
9388 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009389 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009390 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009392 output = NULL;
9393 output_length = ~0;
9394
9395 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009396 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9398 peer_key_data->x, peer_key_data->len,
9399 output, expected_output->len + 1,
9400 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009401 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009402 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009404 output = NULL;
9405 output_length = ~0;
9406
9407 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009408 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9410 peer_key_data->x, peer_key_data->len,
9411 output, expected_output->len - 1,
9412 &output_length),
9413 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009414 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 TEST_LE_U(output_length, expected_output->len - 1);
9416 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009417 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009418
9419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 mbedtls_free(output);
9421 psa_destroy_key(our_key);
9422 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009423}
9424/* END_CASE */
9425
9426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009427void key_agreement_capacity(int alg_arg,
9428 int our_key_type_arg, data_t *our_key_data,
9429 data_t *peer_key_data,
9430 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009431{
Ronald Cron5425a212020-08-04 14:58:35 +02009432 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009433 psa_algorithm_t alg = alg_arg;
9434 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009435 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009437 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009438 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009439
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009441
Gilles Peskine449bd832023-01-11 14:50:10 +01009442 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9443 psa_set_key_algorithm(&attributes, alg);
9444 psa_set_key_type(&attributes, our_key_type);
9445 PSA_ASSERT(psa_import_key(&attributes,
9446 our_key_data->x, our_key_data->len,
9447 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9450 PSA_ASSERT(psa_key_derivation_key_agreement(
9451 &operation,
9452 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9453 peer_key_data->x, peer_key_data->len));
9454 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009455 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9457 PSA_KEY_DERIVATION_INPUT_INFO,
9458 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009459 }
Gilles Peskine59685592018-09-18 12:11:34 +02009460
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009461 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009462 PSA_ASSERT(psa_key_derivation_get_capacity(
9463 &operation, &actual_capacity));
9464 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009465
Gilles Peskinebf491972018-10-25 22:36:12 +02009466 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 while (actual_capacity > sizeof(output)) {
9468 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9469 output, sizeof(output)));
9470 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009471 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9473 output, actual_capacity));
9474 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9475 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009476
Gilles Peskine59685592018-09-18 12:11:34 +02009477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009478 psa_key_derivation_abort(&operation);
9479 psa_destroy_key(our_key);
9480 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009481}
9482/* END_CASE */
9483
Valerio Settiad819672023-12-29 12:14:41 +01009484/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9485void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009486{
9487 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009488 psa_ecc_family_t ecc_family = psa_family_arg;
9489 size_t bits = bits_arg;
9490 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009491
Valerio Settiad819672023-12-29 12:14:41 +01009492 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9493 TEST_EQUAL(bits, bits_tmp);
Valerio Settibf999cb2023-12-28 17:48:13 +01009494
Valerio Settiad819672023-12-29 12:14:41 +01009495 if (grp_id != MBEDTLS_ECP_DP_MAX) {
9496 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
9497 } else {
9498 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(ecc_family, bits));
9499 }
Valerio Settibf999cb2023-12-28 17:48:13 +01009500}
9501/* END_CASE */
9502
9503/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009504void key_agreement_output(int alg_arg,
9505 int our_key_type_arg, data_t *our_key_data,
9506 data_t *peer_key_data,
9507 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009508{
Ronald Cron5425a212020-08-04 14:58:35 +02009509 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009510 psa_algorithm_t alg = alg_arg;
9511 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009512 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009514 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009515
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009516 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009517 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009518
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009520
Gilles Peskine449bd832023-01-11 14:50:10 +01009521 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9522 psa_set_key_algorithm(&attributes, alg);
9523 psa_set_key_type(&attributes, our_key_type);
9524 PSA_ASSERT(psa_import_key(&attributes,
9525 our_key_data->x, our_key_data->len,
9526 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009527
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9529 PSA_ASSERT(psa_key_derivation_key_agreement(
9530 &operation,
9531 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9532 peer_key_data->x, peer_key_data->len));
9533 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009534 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009535 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9536 PSA_KEY_DERIVATION_INPUT_INFO,
9537 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009538 }
Gilles Peskine59685592018-09-18 12:11:34 +02009539
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9541 actual_output,
9542 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009543 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009544 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 if (expected_output2->len != 0) {
9546 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9547 actual_output,
9548 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009549 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009550 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009551 }
Gilles Peskine59685592018-09-18 12:11:34 +02009552
9553exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 psa_key_derivation_abort(&operation);
9555 psa_destroy_key(our_key);
9556 PSA_DONE();
9557 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009558}
9559/* END_CASE */
9560
9561/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009562void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009563{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009564 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009565 unsigned char *output = NULL;
9566 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009567 size_t i;
9568 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009569
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009571
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009572 TEST_CALLOC(output, bytes);
9573 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009574
Gilles Peskine449bd832023-01-11 14:50:10 +01009575 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009576
Gilles Peskinea50d7392018-06-21 10:22:13 +02009577 /* Run several times, to ensure that every output byte will be
9578 * nonzero at least once with overwhelming probability
9579 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009580 for (run = 0; run < 10; run++) {
9581 if (bytes != 0) {
9582 memset(output, 0, bytes);
9583 }
9584 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009585
Gilles Peskine449bd832023-01-11 14:50:10 +01009586 for (i = 0; i < bytes; i++) {
9587 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009588 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009589 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009590 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009591 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009592
9593 /* Check that every byte was changed to nonzero at least once. This
9594 * validates that psa_generate_random is overwriting every byte of
9595 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009596 for (i = 0; i < bytes; i++) {
9597 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009598 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009599
9600exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 PSA_DONE();
9602 mbedtls_free(output);
9603 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009604}
9605/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009606
9607/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009608void generate_key(int type_arg,
9609 int bits_arg,
9610 int usage_arg,
9611 int alg_arg,
9612 int expected_status_arg,
9613 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009614{
Ronald Cron5425a212020-08-04 14:58:35 +02009615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009616 psa_key_type_t type = type_arg;
9617 psa_key_usage_t usage = usage_arg;
9618 size_t bits = bits_arg;
9619 psa_algorithm_t alg = alg_arg;
9620 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009622 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009623
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009625
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 psa_set_key_usage_flags(&attributes, usage);
9627 psa_set_key_algorithm(&attributes, alg);
9628 psa_set_key_type(&attributes, type);
9629 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009630
9631 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009632 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009633
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 if (is_large_key > 0) {
9635 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9636 }
9637 TEST_EQUAL(status, expected_status);
9638 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009639 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009640 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009641
9642 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9644 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9645 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009646
Gilles Peskine818ca122018-06-20 18:16:48 +02009647 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009651
9652exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009653 /*
9654 * Key attributes may have been returned by psa_get_key_attributes()
9655 * thus reset them as required.
9656 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009657 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009658
Gilles Peskine449bd832023-01-11 14:50:10 +01009659 psa_destroy_key(key);
9660 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009661}
9662/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009663
Valerio Setti19fec542023-07-25 12:31:50 +02009664/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine449bd832023-01-11 14:50:10 +01009665void generate_key_rsa(int bits_arg,
9666 data_t *e_arg,
9667 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009668{
Ronald Cron5425a212020-08-04 14:58:35 +02009669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009670 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009671 size_t bits = bits_arg;
9672 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9673 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9674 psa_status_t expected_status = expected_status_arg;
9675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9676 uint8_t *exported = NULL;
9677 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009678 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009679 size_t exported_length = SIZE_MAX;
9680 uint8_t *e_read_buffer = NULL;
9681 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009682 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009683 size_t e_read_length = SIZE_MAX;
9684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 if (e_arg->len == 0 ||
9686 (e_arg->len == 3 &&
9687 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009688 is_default_public_exponent = 1;
9689 e_read_size = 0;
9690 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009691 TEST_CALLOC(e_read_buffer, e_read_size);
9692 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009693
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009695
Gilles Peskine449bd832023-01-11 14:50:10 +01009696 psa_set_key_usage_flags(&attributes, usage);
9697 psa_set_key_algorithm(&attributes, alg);
9698 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9699 e_arg->x, e_arg->len));
9700 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009701
9702 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009703 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9704 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009707
9708 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009709 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9710 TEST_EQUAL(psa_get_key_type(&attributes), type);
9711 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009712 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9713 e_read_buffer, e_read_size,
9714 &e_read_length);
9715
9716
9717#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9718 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9719 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009720 if (is_default_public_exponent) {
9721 TEST_EQUAL(e_read_length, 0);
9722 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009723 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009724 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009725 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009726#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009727 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009728 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009729#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009730
9731 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009732 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009733 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009734 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009735
9736 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 PSA_ASSERT(psa_export_public_key(key,
9738 exported, exported_size,
9739 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009740 {
9741 uint8_t *p = exported;
9742 uint8_t *end = exported + exported_length;
9743 size_t len;
9744 /* RSAPublicKey ::= SEQUENCE {
9745 * modulus INTEGER, -- n
9746 * publicExponent INTEGER } -- e
9747 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009748 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9749 MBEDTLS_ASN1_SEQUENCE |
9750 MBEDTLS_ASN1_CONSTRUCTED));
9751 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9752 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9753 MBEDTLS_ASN1_INTEGER));
9754 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009755 ++p;
9756 --len;
9757 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009758 if (e_arg->len == 0) {
9759 TEST_EQUAL(len, 3);
9760 TEST_EQUAL(p[0], 1);
9761 TEST_EQUAL(p[1], 0);
9762 TEST_EQUAL(p[2], 1);
9763 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009764 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009765 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009766 }
9767
9768exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009769 /*
9770 * Key attributes may have been returned by psa_get_key_attributes() or
9771 * set by psa_set_key_domain_parameters() thus reset them as required.
9772 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009773 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009774
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 psa_destroy_key(key);
9776 PSA_DONE();
9777 mbedtls_free(e_read_buffer);
9778 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009779}
9780/* END_CASE */
9781
Darryl Greend49a4992018-06-18 17:27:26 +01009782/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009783void persistent_key_load_key_from_storage(data_t *data,
9784 int type_arg, int bits_arg,
9785 int usage_flags_arg, int alg_arg,
9786 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009787{
Gilles Peskine449bd832023-01-11 14:50:10 +01009788 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9791 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009792 psa_key_type_t type = type_arg;
9793 size_t bits = bits_arg;
9794 psa_key_usage_t usage_flags = usage_flags_arg;
9795 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009796 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009797 unsigned char *first_export = NULL;
9798 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009799 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009800 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009801 size_t second_exported_length;
9802
Gilles Peskine449bd832023-01-11 14:50:10 +01009803 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009804 TEST_CALLOC(first_export, export_size);
9805 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009806 }
Darryl Greend49a4992018-06-18 17:27:26 +01009807
Gilles Peskine449bd832023-01-11 14:50:10 +01009808 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009809
Gilles Peskine449bd832023-01-11 14:50:10 +01009810 psa_set_key_id(&attributes, key_id);
9811 psa_set_key_usage_flags(&attributes, usage_flags);
9812 psa_set_key_algorithm(&attributes, alg);
9813 psa_set_key_type(&attributes, type);
9814 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009815
Gilles Peskine449bd832023-01-11 14:50:10 +01009816 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009817 case IMPORT_KEY:
9818 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009819 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9820 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009821 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009822
Darryl Green0c6575a2018-11-07 16:05:30 +00009823 case GENERATE_KEY:
9824 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009825 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009826 break;
9827
9828 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009829#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009830 {
9831 /* Create base key */
9832 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9833 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9834 psa_set_key_usage_flags(&base_attributes,
9835 PSA_KEY_USAGE_DERIVE);
9836 psa_set_key_algorithm(&base_attributes, derive_alg);
9837 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9838 PSA_ASSERT(psa_import_key(&base_attributes,
9839 data->x, data->len,
9840 &base_key));
9841 /* Derive a key. */
9842 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9843 PSA_ASSERT(psa_key_derivation_input_key(
9844 &operation,
9845 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9846 PSA_ASSERT(psa_key_derivation_input_bytes(
9847 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9848 NULL, 0));
9849 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9850 &operation,
9851 &key));
9852 PSA_ASSERT(psa_key_derivation_abort(&operation));
9853 PSA_ASSERT(psa_destroy_key(base_key));
9854 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9855 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009856#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009857 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009858#endif
9859 break;
9860
9861 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009862 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009863 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009864 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009865 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009866
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009867 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009868 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9869 PSA_ASSERT(psa_export_key(key,
9870 first_export, export_size,
9871 &first_exported_length));
9872 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009873 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009874 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009875 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009876 }
Darryl Greend49a4992018-06-18 17:27:26 +01009877
9878 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009879 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009880 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009881 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009882
Darryl Greend49a4992018-06-18 17:27:26 +01009883 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9885 TEST_ASSERT(mbedtls_svc_key_id_equal(
9886 psa_get_key_id(&attributes), key_id));
9887 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9888 PSA_KEY_LIFETIME_PERSISTENT);
9889 TEST_EQUAL(psa_get_key_type(&attributes), type);
9890 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9891 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9892 mbedtls_test_update_key_usage_flags(usage_flags));
9893 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009894
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009895 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009896 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9897 PSA_ASSERT(psa_export_key(key,
9898 second_export, export_size,
9899 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009900 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009901 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009902 }
9903
9904 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009905 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009906 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009907 }
Darryl Greend49a4992018-06-18 17:27:26 +01009908
9909exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009910 /*
9911 * Key attributes may have been returned by psa_get_key_attributes()
9912 * thus reset them as required.
9913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009914 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009915
Gilles Peskine449bd832023-01-11 14:50:10 +01009916 mbedtls_free(first_export);
9917 mbedtls_free(second_export);
9918 psa_key_derivation_abort(&operation);
9919 psa_destroy_key(base_key);
9920 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009921 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009922}
9923/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009924
Neil Armstronga557cb82022-06-10 08:58:32 +02009925/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009926void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9927 int primitive_arg, int hash_arg, int role_arg,
9928 int test_input, data_t *pw_data,
9929 int inj_err_type_arg,
9930 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009931{
9932 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9933 psa_pake_operation_t operation = psa_pake_operation_init();
9934 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009935 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009936 psa_key_type_t key_type_pw = key_type_pw_arg;
9937 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009938 psa_algorithm_t hash_alg = hash_arg;
9939 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009942 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9943 psa_status_t expected_error = expected_error_arg;
9944 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009945 unsigned char *output_buffer = NULL;
9946 size_t output_len = 0;
9947
Gilles Peskine449bd832023-01-11 14:50:10 +01009948 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009949
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009950 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009951 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009952 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009953
Gilles Peskine449bd832023-01-11 14:50:10 +01009954 if (pw_data->len > 0) {
9955 psa_set_key_usage_flags(&attributes, key_usage_pw);
9956 psa_set_key_algorithm(&attributes, alg);
9957 psa_set_key_type(&attributes, key_type_pw);
9958 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9959 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009960 }
9961
Gilles Peskine449bd832023-01-11 14:50:10 +01009962 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9963 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9964 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009965
Gilles Peskine449bd832023-01-11 14:50:10 +01009966 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009967
Gilles Peskine449bd832023-01-11 14:50:10 +01009968 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9969 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9970 expected_error);
9971 PSA_ASSERT(psa_pake_abort(&operation));
9972 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9973 expected_error);
9974 PSA_ASSERT(psa_pake_abort(&operation));
9975 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9976 expected_error);
9977 PSA_ASSERT(psa_pake_abort(&operation));
9978 TEST_EQUAL(psa_pake_set_role(&operation, role),
9979 expected_error);
9980 PSA_ASSERT(psa_pake_abort(&operation));
9981 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9982 NULL, 0, NULL),
9983 expected_error);
9984 PSA_ASSERT(psa_pake_abort(&operation));
9985 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9986 expected_error);
9987 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009988 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009989 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009990
Gilles Peskine449bd832023-01-11 14:50:10 +01009991 status = psa_pake_setup(&operation, &cipher_suite);
9992 if (status != PSA_SUCCESS) {
9993 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009994 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009995 }
9996
Gilles Peskine449bd832023-01-11 14:50:10 +01009997 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9998 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9999 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010000 goto exit;
10001 }
10002
Gilles Peskine449bd832023-01-11 14:50:10 +010010003 status = psa_pake_set_role(&operation, role);
10004 if (status != PSA_SUCCESS) {
10005 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010006 goto exit;
10007 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010008
Gilles Peskine449bd832023-01-11 14:50:10 +010010009 if (pw_data->len > 0) {
10010 status = psa_pake_set_password_key(&operation, key);
10011 if (status != PSA_SUCCESS) {
10012 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010013 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010014 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010015 }
10016
Gilles Peskine449bd832023-01-11 14:50:10 +010010017 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10018 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10019 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010020 goto exit;
10021 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010022
Gilles Peskine449bd832023-01-11 14:50:10 +010010023 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10024 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10025 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010026 goto exit;
10027 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010028
Gilles Peskine449bd832023-01-11 14:50:10 +010010029 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010030 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010031 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10032 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010033 goto exit;
10034 }
10035
Gilles Peskine449bd832023-01-11 14:50:10 +010010036 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010037 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010038 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10039 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010040 goto exit;
10041 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010042
Gilles Peskine449bd832023-01-11 14:50:10 +010010043 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10044 PSA_PAKE_STEP_KEY_SHARE);
10045 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10046 PSA_PAKE_STEP_ZK_PUBLIC);
10047 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10048 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010049
Gilles Peskine449bd832023-01-11 14:50:10 +010010050 if (test_input) {
10051 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10052 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10053 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010054 goto exit;
10055 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010056
Gilles Peskine449bd832023-01-11 14:50:10 +010010057 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10058 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10059 output_buffer, size_zk_proof),
10060 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010061 goto exit;
10062 }
10063
Gilles Peskine449bd832023-01-11 14:50:10 +010010064 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10065 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10066 output_buffer, size_zk_proof),
10067 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010068 goto exit;
10069 }
10070
Gilles Peskine449bd832023-01-11 14:50:10 +010010071 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10072 output_buffer, size_key_share);
10073 if (status != PSA_SUCCESS) {
10074 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010075 goto exit;
10076 }
10077
Gilles Peskine449bd832023-01-11 14:50:10 +010010078 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10079 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10080 output_buffer, size_zk_public + 1),
10081 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010082 goto exit;
10083 }
10084
Gilles Peskine449bd832023-01-11 14:50:10 +010010085 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010086 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010087 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10088 output_buffer, size_zk_public + 1);
10089 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10090 output_buffer, size_zk_public),
10091 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010092 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010093 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010094 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010095 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10096 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10097 NULL, 0, NULL),
10098 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010099 goto exit;
10100 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010101
Gilles Peskine449bd832023-01-11 14:50:10 +010010102 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10103 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10104 output_buffer, buf_size, &output_len),
10105 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010106 goto exit;
10107 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010108
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10110 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10111 output_buffer, buf_size, &output_len),
10112 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010113 goto exit;
10114 }
10115
Gilles Peskine449bd832023-01-11 14:50:10 +010010116 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10117 output_buffer, buf_size, &output_len);
10118 if (status != PSA_SUCCESS) {
10119 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010120 goto exit;
10121 }
10122
Gilles Peskine449bd832023-01-11 14:50:10 +010010123 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010124
Gilles Peskine449bd832023-01-11 14:50:10 +010010125 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10126 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10127 output_buffer, size_zk_public - 1, &output_len),
10128 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010129 goto exit;
10130 }
10131
Gilles Peskine449bd832023-01-11 14:50:10 +010010132 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010133 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010134 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10135 output_buffer, size_zk_public - 1, &output_len);
10136 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10137 output_buffer, buf_size, &output_len),
10138 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010139 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010140 }
10141 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010142
10143exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010144 PSA_ASSERT(psa_destroy_key(key));
10145 PSA_ASSERT(psa_pake_abort(&operation));
10146 mbedtls_free(output_buffer);
10147 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010148}
10149/* END_CASE */
10150
Neil Armstronga557cb82022-06-10 08:58:32 +020010151/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010152void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10153 int client_input_first, int inject_error,
10154 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010155{
10156 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10157 psa_pake_operation_t server = psa_pake_operation_init();
10158 psa_pake_operation_t client = psa_pake_operation_init();
10159 psa_algorithm_t alg = alg_arg;
10160 psa_algorithm_t hash_alg = hash_arg;
10161 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10163
Gilles Peskine449bd832023-01-11 14:50:10 +010010164 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010165
Gilles Peskine449bd832023-01-11 14:50:10 +010010166 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10167 psa_set_key_algorithm(&attributes, alg);
10168 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10169 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10170 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010171
Gilles Peskine449bd832023-01-11 14:50:10 +010010172 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10173 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10174 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010175
10176
Gilles Peskine449bd832023-01-11 14:50:10 +010010177 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10178 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010179
Gilles Peskine449bd832023-01-11 14:50:10 +010010180 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10181 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010182
Gilles Peskine449bd832023-01-11 14:50:10 +010010183 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10184 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010185
Gilles Peskine449bd832023-01-11 14:50:10 +010010186 ecjpake_do_round(alg, primitive_arg, &server, &client,
10187 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010188
Gilles Peskine449bd832023-01-11 14:50:10 +010010189 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010192
Gilles Peskine449bd832023-01-11 14:50:10 +010010193 ecjpake_do_round(alg, primitive_arg, &server, &client,
10194 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010195
10196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010197 psa_destroy_key(key);
10198 psa_pake_abort(&server);
10199 psa_pake_abort(&client);
10200 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010201}
10202/* END_CASE */
10203
10204/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010205void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10206 int derive_alg_arg, data_t *pw_data,
10207 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010208{
10209 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10210 psa_pake_operation_t server = psa_pake_operation_init();
10211 psa_pake_operation_t client = psa_pake_operation_init();
10212 psa_algorithm_t alg = alg_arg;
10213 psa_algorithm_t hash_alg = hash_arg;
10214 psa_algorithm_t derive_alg = derive_alg_arg;
10215 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10217 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010218 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010219 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010220 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010221 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010222
Gilles Peskine449bd832023-01-11 14:50:10 +010010223 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010224
Gilles Peskine449bd832023-01-11 14:50:10 +010010225 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10226 psa_set_key_algorithm(&attributes, alg);
10227 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10228 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10229 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010230
Gilles Peskine449bd832023-01-11 14:50:10 +010010231 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10232 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10233 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010234
Neil Armstrong1e855602022-06-15 11:32:11 +020010235 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010236 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10237 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010238
Gilles Peskine449bd832023-01-11 14:50:10 +010010239 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10240 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10241 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10242 PSA_KEY_DERIVATION_INPUT_SEED,
10243 (const uint8_t *) "", 0));
10244 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10245 PSA_KEY_DERIVATION_INPUT_SEED,
10246 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010247 }
10248
Gilles Peskine449bd832023-01-11 14:50:10 +010010249 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10250 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010251
Gilles Peskine449bd832023-01-11 14:50:10 +010010252 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10253 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010254
Gilles Peskine449bd832023-01-11 14:50:10 +010010255 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10256 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010257
Gilles Peskine449bd832023-01-11 14:50:10 +010010258 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10259 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10260 PSA_ERROR_BAD_STATE);
10261 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10262 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010263 goto exit;
10264 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010265
Neil Armstrongf983caf2022-06-15 15:27:48 +020010266 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010267 ecjpake_do_round(alg, primitive_arg, &server, &client,
10268 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010269
Gilles Peskine449bd832023-01-11 14:50:10 +010010270 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10271 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10272 PSA_ERROR_BAD_STATE);
10273 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10274 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010275 goto exit;
10276 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010277
Neil Armstrongf983caf2022-06-15 15:27:48 +020010278 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010279 ecjpake_do_round(alg, primitive_arg, &server, &client,
10280 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010281
Gilles Peskine449bd832023-01-11 14:50:10 +010010282 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10283 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010284
10285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010286 psa_key_derivation_abort(&server_derive);
10287 psa_key_derivation_abort(&client_derive);
10288 psa_destroy_key(key);
10289 psa_pake_abort(&server);
10290 psa_pake_abort(&client);
10291 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010292}
10293/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010294
10295/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010296void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010297{
10298 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10299 const size_t bits = 256;
10300 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010301 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010302 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010303 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010304
10305 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10306 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010307 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10308 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10309 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10310 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010311 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010312 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10313 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010314
10315 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010316 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10317 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10318 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10319 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10320 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10321 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010322
10323 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010324 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10325 PSA_PAKE_OUTPUT_MAX_SIZE);
10326 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10327 PSA_PAKE_OUTPUT_MAX_SIZE);
10328 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10329 PSA_PAKE_OUTPUT_MAX_SIZE);
10330 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10331 PSA_PAKE_INPUT_MAX_SIZE);
10332 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10333 PSA_PAKE_INPUT_MAX_SIZE);
10334 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10335 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010336}
10337/* END_CASE */