blob: 182443a5e9f3b1a889f6b90574ea947c09a701ae [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
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
Gilles Peskine8e94efe2021-02-13 00:25:53 +010016#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010017#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010018#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053019#if defined(PSA_CRYPTO_DRIVER_TEST)
20#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053021#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
22#else
23#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053024#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020025#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010026
Gilles Peskine4023c012021-05-27 13:21:20 +020027/* If this comes up, it's a bug in the test code or in the test data. */
28#define UNUSED 0xdeadbeef
29
Dave Rodgman647791d2021-06-23 12:49:59 +010030/* Assert that an operation is (not) active.
31 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010032#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
33#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010034
Przemek Stekiel7c795482022-11-15 22:26:12 +010035#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010036int ecjpake_operation_setup(psa_pake_operation_t *operation,
37 psa_pake_cipher_suite_t *cipher_suite,
38 psa_pake_role_t role,
39 mbedtls_svc_key_id_t key,
40 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010041{
Gilles Peskine449bd832023-01-11 14:50:10 +010042 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (key_available) {
49 PSA_ASSERT(psa_pake_set_password_key(operation, key));
50 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010051 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010052exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010053 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010054}
55#endif
56
Jaeden Amerof24c7f82018-06-27 17:20:43 +010057/** An invalid export length that will never be set by psa_export_key(). */
58static const size_t INVALID_EXPORT_LENGTH = ~0U;
59
Gilles Peskinea7aa4422018-08-14 15:17:54 +020060/** Test if a buffer contains a constant byte value.
61 *
62 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020063 *
64 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020065 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 * \param size Size of the buffer in bytes.
67 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020068 * \return 1 if the buffer is all-bits-zero.
69 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072{
73 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 for (i = 0; i < size; i++) {
75 if (((unsigned char *) buffer)[i] != c) {
76 return 0;
77 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020078 }
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010081#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020082/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010083static int asn1_write_10x(unsigned char **p,
84 unsigned char *start,
85 size_t bits,
86 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020087{
88 int ret;
89 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (bits == 0) {
91 return MBEDTLS_ERR_ASN1_INVALID_DATA;
92 }
93 if (bits <= 8 && x >= 1 << (bits - 1)) {
94 return MBEDTLS_ERR_ASN1_INVALID_DATA;
95 }
96 if (*p < start || *p - start < (ptrdiff_t) len) {
97 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
98 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020099 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 (*p)[len-1] = x;
101 if (bits % 8 == 0) {
102 (*p)[1] |= 1;
103 } else {
104 (*p)[0] |= 1 << (bits % 8);
105 }
106 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
108 MBEDTLS_ASN1_INTEGER));
109 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200110}
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112static int construct_fake_rsa_key(unsigned char *buffer,
113 size_t buffer_size,
114 unsigned char **p,
115 size_t bits,
116 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200117{
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200119 int ret;
120 int len = 0;
121 /* Construct something that looks like a DER encoding of
122 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
123 * RSAPrivateKey ::= SEQUENCE {
124 * version Version,
125 * modulus INTEGER, -- n
126 * publicExponent INTEGER, -- e
127 * privateExponent INTEGER, -- d
128 * prime1 INTEGER, -- p
129 * prime2 INTEGER, -- q
130 * exponent1 INTEGER, -- d mod (p-1)
131 * exponent2 INTEGER, -- d mod (q-1)
132 * coefficient INTEGER, -- (inverse of q) mod p
133 * otherPrimeInfos OtherPrimeInfos OPTIONAL
134 * }
135 * Or, for a public key, the same structure with only
136 * version, modulus and publicExponent.
137 */
138 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (keypair) {
140 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
141 asn1_write_10x(p, buffer, half_bits, 1));
142 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
143 asn1_write_10x(p, buffer, half_bits, 1));
144 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* q */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
149 asn1_write_10x(p, buffer, half_bits, 3));
150 MBEDTLS_ASN1_CHK_ADD(len, /* d */
151 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
154 asn1_write_10x(p, buffer, 17, 1));
155 MBEDTLS_ASN1_CHK_ADD(len, /* n */
156 asn1_write_10x(p, buffer, bits, 1));
157 if (keypair) {
158 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
159 mbedtls_asn1_write_int(p, buffer, 0));
160 }
161 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200162 {
163 const unsigned char tag =
164 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200168}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100169#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int exercise_mac_setup(psa_key_type_t key_type,
172 const unsigned char *key_bytes,
173 size_t key_length,
174 psa_algorithm_t alg,
175 psa_mac_operation_t *operation,
176 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100177{
Ronald Cron5425a212020-08-04 14:58:35 +0200178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
182 psa_set_key_algorithm(&attributes, alg);
183 psa_set_key_type(&attributes, key_type);
184 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100189 /* If setup failed, reproduce the failure, so that the caller can
190 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (*status != PSA_SUCCESS) {
192 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 psa_destroy_key(key);
196 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197
198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201}
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203int exercise_cipher_setup(psa_key_type_t key_type,
204 const unsigned char *key_bytes,
205 size_t key_length,
206 psa_algorithm_t alg,
207 psa_cipher_operation_t *operation,
208 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100209{
Ronald Cron5425a212020-08-04 14:58:35 +0200210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
214 psa_set_key_algorithm(&attributes, alg);
215 psa_set_key_type(&attributes, key_type);
216 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100219 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 /* If setup failed, reproduce the failure, so that the caller can
222 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (*status != PSA_SUCCESS) {
224 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
225 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 psa_destroy_key(key);
229 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230
231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234}
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237{
238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 uint8_t buffer[1];
241 size_t length;
242 int ok = 0;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 psa_set_key_id(&attributes, key_id);
245 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
246 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
247 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
248 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
249 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
254 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
255 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
256 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
257 TEST_EQUAL(psa_get_key_type(&attributes), 0);
258 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
261 PSA_ERROR_INVALID_HANDLE);
262 TEST_EQUAL(psa_export_public_key(key,
263 buffer, sizeof(buffer), &length),
264 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200265
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200266 ok = 1;
267
268exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100269 /*
270 * Key attributes may have been returned by psa_get_key_attributes()
271 * thus reset them as required.
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200276}
277
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200278/* Assert that a key isn't reported as having a slot number. */
279#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200281 do \
282 { \
283 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 TEST_EQUAL(psa_get_key_slot_number( \
285 attributes, \
286 &ASSERT_NO_SLOT_NUMBER_slot_number), \
287 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200288 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200290#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291#define ASSERT_NO_SLOT_NUMBER(attributes) \
292 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
294
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100295/* An overapproximation of the amount of storage needed for a key of the
296 * given type and with the given content. The API doesn't make it easy
297 * to find a good value for the size. The current implementation doesn't
298 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299#define KEY_BITS_FROM_DATA(type, data) \
300 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301
Darryl Green0c6575a2018-11-07 16:05:30 +0000302typedef enum {
303 IMPORT_KEY = 0,
304 GENERATE_KEY = 1,
305 DERIVE_KEY = 2
306} generate_method;
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100309 DO_NOT_SET_LENGTHS = 0,
310 SET_LENGTHS_BEFORE_NONCE = 1,
311 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100312} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100315 USE_NULL_TAG = 0,
316 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100317} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319/*!
320 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321 * \param key_type_arg Type of key passed in
322 * \param key_data The encryption / decryption key data
323 * \param alg_arg The type of algorithm used
324 * \param nonce Nonce data
325 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100326 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 * feed additional data in to be encrypted /
328 * decrypted. If -1, no chunking.
329 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100330 * \param data_part_len_arg If not -1, the length of chunks to feed
331 * the data in to be encrypted / decrypted. If
332 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100333 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100334 * expected here, this controls whether or not
335 * to set lengths, and in what order with
336 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100339 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100340 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
344 int alg_arg,
345 data_t *nonce,
346 data_t *additional_data,
347 int ad_part_len_arg,
348 data_t *input_data,
349 int data_part_len_arg,
350 set_lengths_method_t set_lengths_method,
351 data_t *expected_output,
352 int is_encrypt,
353 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100354{
355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
356 psa_key_type_t key_type = key_type_arg;
357 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100358 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100359 unsigned char *output_data = NULL;
360 unsigned char *part_data = NULL;
361 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100363 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100364 size_t output_size = 0;
365 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 size_t output_length = 0;
367 size_t key_bits = 0;
368 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100369 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_length = 0;
371 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100372 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100373 size_t ad_part_len = 0;
374 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
377 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
378
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (is_encrypt) {
385 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
386 } else {
387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100389
390 psa_set_key_algorithm(&attributes, alg);
391 psa_set_key_type(&attributes, key_type);
392
393 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
394 &key));
395
396 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
397 key_bits = psa_get_key_bits(&attributes);
398
399 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
400
401 if (is_encrypt) {
402 /* Tag gets written at end of buffer. */
403 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
404 (input_data->len +
405 tag_length));
406 data_true_size = input_data->len;
407 } else {
408 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
409 (input_data->len -
410 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100411
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100412 /* Do not want to attempt to decrypt tag. */
413 data_true_size = input_data->len - tag_length;
414 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (is_encrypt) {
419 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
420 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
421 } else {
422 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
423 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100424 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (is_encrypt) {
429 status = psa_aead_encrypt_setup(&operation, key, alg);
430 } else {
431 status = psa_aead_decrypt_setup(&operation, key, alg);
432 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
434 /* If the operation is not supported, just skip and not fail in case the
435 * encryption involves a common limitation of cryptography hardwares and
436 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (status == PSA_ERROR_NOT_SUPPORTED) {
438 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
439 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
445 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
446 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
447 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
448 data_true_size));
449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
450 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
451 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
454 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100459 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100462 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 part_offset += part_length, part_count++) {
464 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100465 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100467 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100469 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 }
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 PSA_ASSERT(psa_aead_update_ad(&operation,
473 additional_data->x + part_offset,
474 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100475
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
480 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 data_part_len = (size_t) data_part_len_arg;
486 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
487 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100492 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 part_offset += part_length, part_count++) {
494 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100495 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if ((data_true_size - part_offset) < data_part_len) {
497 part_length = (data_true_size - part_offset);
498 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 PSA_ASSERT(psa_aead_update(&operation,
503 (input_data->x + part_offset),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (output_data && output_part_length) {
509 memcpy((output_data + output_length), part_data,
510 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 }
512
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 output_length += output_part_length;
514 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100516 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
518 data_true_size, output_data,
519 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (is_encrypt) {
523 PSA_ASSERT(psa_aead_finish(&operation, final_data,
524 final_output_size,
525 &output_part_length,
526 tag_buffer, tag_length,
527 &tag_size));
528 } else {
529 PSA_ASSERT(psa_aead_verify(&operation, final_data,
530 final_output_size,
531 &output_part_length,
532 (input_data->x + data_true_size),
533 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (output_data && output_part_length) {
537 memcpy((output_data + output_length), final_data,
538 output_part_length);
539 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100540
541 output_length += output_part_length;
542
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100543
544 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
545 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (is_encrypt) {
547 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (output_data && tag_length) {
550 memcpy((output_data + output_length), tag_buffer,
551 tag_length);
552 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553
554 output_length += tag_length;
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 TEST_EQUAL(output_length,
557 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
558 input_data->len));
559 TEST_LE_U(output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
561 } else {
562 TEST_EQUAL(output_length,
563 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
564 input_data->len));
565 TEST_LE_U(output_length,
566 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100567 }
568
Paul Elliottd3f82412021-06-16 16:52:21 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 ASSERT_COMPARE(expected_output->x, expected_output->len,
571 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100572
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100574 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 psa_destroy_key(key);
578 psa_aead_abort(&operation);
579 mbedtls_free(output_data);
580 mbedtls_free(part_data);
581 mbedtls_free(final_data);
582 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100585}
586
Neil Armstrong4766f992022-02-28 16:23:59 +0100587/*!
588 * \brief Internal Function for MAC multipart tests.
589 * \param key_type_arg Type of key passed in
590 * \param key_data The encryption / decryption key data
591 * \param alg_arg The type of algorithm used
592 * \param input_data Data to encrypt / decrypt
593 * \param data_part_len_arg If not -1, the length of chunks to feed
594 * the data in to be encrypted / decrypted. If
595 * -1, no chunking
596 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000597 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100598 * \param do_zero_parts If non-zero, interleave zero length chunks
599 * with normal length chunks.
600 * \return int Zero on failure, non-zero on success.
601 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
603 int alg_arg,
604 data_t *input_data,
605 int data_part_len_arg,
606 data_t *expected_output,
607 int is_verify,
608 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100609{
610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
611 psa_key_type_t key_type = key_type_arg;
612 psa_algorithm_t alg = alg_arg;
613 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
614 unsigned char mac[PSA_MAC_MAX_SIZE];
615 size_t part_offset = 0;
616 size_t part_length = 0;
617 size_t data_part_len = 0;
618 size_t mac_len = 0;
619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
621
622 int test_ok = 0;
623 size_t part_count = 0;
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (is_verify) {
628 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
629 } else {
630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
631 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 psa_set_key_algorithm(&attributes, alg);
634 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
637 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (is_verify) {
640 status = psa_mac_verify_setup(&operation, key, alg);
641 } else {
642 status = psa_mac_sign_setup(&operation, key, alg);
643 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100648 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100652 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 part_offset += part_length, part_count++) {
654 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else if ((input_data->len - part_offset) < data_part_len) {
657 part_length = (input_data->len - part_offset);
658 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_length = data_part_len;
660 }
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 PSA_ASSERT(psa_mac_update(&operation,
663 (input_data->x + part_offset),
664 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100667 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
669 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (is_verify) {
673 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
674 expected_output->len));
675 } else {
676 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
677 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 ASSERT_COMPARE(expected_output->x, expected_output->len,
680 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100681 }
682
683 test_ok = 1;
684
685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 psa_destroy_key(key);
687 psa_mac_abort(&operation);
688 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100691}
692
Neil Armstrong75673ab2022-06-15 17:39:01 +0200693#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
695 psa_pake_operation_t *server,
696 psa_pake_operation_t *client,
697 int client_input_first,
698 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200699{
700 unsigned char *buffer0 = NULL, *buffer1 = NULL;
701 size_t buffer_length = (
702 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
703 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
704 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200705 /* The output should be exactly this size according to the spec */
706 const size_t expected_size_key_share =
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
708 /* The output should be exactly this size according to the spec */
709 const size_t expected_size_zk_public =
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
711 /* The output can be smaller: the spec allows stripping leading zeroes */
712 const size_t max_expected_size_zk_proof =
713 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200714 size_t buffer0_off = 0;
715 size_t buffer1_off = 0;
716 size_t s_g1_len, s_g2_len, s_a_len;
717 size_t s_g1_off, s_g2_off, s_a_off;
718 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
719 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
720 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
721 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
722 size_t c_g1_len, c_g2_len, c_a_len;
723 size_t c_g1_off, c_g2_off, c_a_off;
724 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
725 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
726 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
727 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
728 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200729 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 ASSERT_ALLOC(buffer0, buffer_length);
732 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735 case 1:
736 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
738 buffer0 + buffer0_off,
739 512 - buffer0_off, &s_g1_len));
740 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200741 s_g1_off = buffer0_off;
742 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
744 buffer0 + buffer0_off,
745 512 - buffer0_off, &s_x1_pk_len));
746 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200747 s_x1_pk_off = buffer0_off;
748 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
750 buffer0 + buffer0_off,
751 512 - buffer0_off, &s_x1_pr_len));
752 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200753 s_x1_pr_off = buffer0_off;
754 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_g2_len));
758 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200759 s_g2_off = buffer0_off;
760 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
762 buffer0 + buffer0_off,
763 512 - buffer0_off, &s_x2_pk_len));
764 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200765 s_x2_pk_off = buffer0_off;
766 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
768 buffer0 + buffer0_off,
769 512 - buffer0_off, &s_x2_pr_len));
770 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200771 s_x2_pr_off = buffer0_off;
772 buffer0_off += s_x2_pr_len;
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500775 buffer0[s_x1_pr_off + 8] ^= 1;
776 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200777 expected_status = PSA_ERROR_DATA_INVALID;
778 }
779
Neil Armstrong51009d72022-09-05 17:59:54 +0200780 /*
781 * When injecting errors in inputs, the implementation is
782 * free to detect it right away of with a delay.
783 * This permits delaying the error until the end of the input
784 * sequence, if no error appears then, this will be treated
785 * as an error.
786 */
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200789 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
791 buffer0 + s_g1_off, s_g1_len);
792 if (inject_error == 1 && status != PSA_SUCCESS) {
793 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200794 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 } else {
796 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200797 }
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
800 buffer0 + s_x1_pk_off,
801 s_x1_pk_len);
802 if (inject_error == 1 && status != PSA_SUCCESS) {
803 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 } else {
806 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200807 }
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
810 buffer0 + s_x1_pr_off,
811 s_x1_pr_len);
812 if (inject_error == 1 && status != PSA_SUCCESS) {
813 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 } else {
816 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200817 }
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
820 buffer0 + s_g2_off,
821 s_g2_len);
822 if (inject_error == 1 && status != PSA_SUCCESS) {
823 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 } else {
826 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
830 buffer0 + s_x2_pk_off,
831 s_x2_pk_len);
832 if (inject_error == 1 && status != PSA_SUCCESS) {
833 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 } else {
836 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
840 buffer0 + s_x2_pr_off,
841 s_x2_pr_len);
842 if (inject_error == 1 && status != PSA_SUCCESS) {
843 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 } else {
846 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200847 }
848
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200849 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (inject_error == 1) {
851 TEST_ASSERT(
852 !"One of the last psa_pake_input() calls should have returned the expected error.");
853 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200854 }
855
856 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
858 buffer1 + buffer1_off,
859 512 - buffer1_off, &c_g1_len));
860 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 c_g1_off = buffer1_off;
862 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
864 buffer1 + buffer1_off,
865 512 - buffer1_off, &c_x1_pk_len));
866 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200867 c_x1_pk_off = buffer1_off;
868 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
870 buffer1 + buffer1_off,
871 512 - buffer1_off, &c_x1_pr_len));
872 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200873 c_x1_pr_off = buffer1_off;
874 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
876 buffer1 + buffer1_off,
877 512 - buffer1_off, &c_g2_len));
878 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200879 c_g2_off = buffer1_off;
880 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
882 buffer1 + buffer1_off,
883 512 - buffer1_off, &c_x2_pk_len));
884 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200885 c_x2_pk_off = buffer1_off;
886 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
888 buffer1 + buffer1_off,
889 512 - buffer1_off, &c_x2_pr_len));
890 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200891 c_x2_pr_off = buffer1_off;
892 buffer1_off += c_x2_pr_len;
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
897 buffer0 + s_g1_off, s_g1_len);
898 if (inject_error == 1 && status != PSA_SUCCESS) {
899 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200900 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 } else {
902 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
906 buffer0 + s_x1_pk_off,
907 s_x1_pk_len);
908 if (inject_error == 1 && status != PSA_SUCCESS) {
909 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 } else {
912 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200913 }
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
916 buffer0 + s_x1_pr_off,
917 s_x1_pr_len);
918 if (inject_error == 1 && status != PSA_SUCCESS) {
919 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 } else {
922 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200923 }
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
926 buffer0 + s_g2_off,
927 s_g2_len);
928 if (inject_error == 1 && status != PSA_SUCCESS) {
929 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 } else {
932 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200933 }
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
936 buffer0 + s_x2_pk_off,
937 s_x2_pk_len);
938 if (inject_error == 1 && status != PSA_SUCCESS) {
939 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 } else {
942 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200943 }
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
946 buffer0 + s_x2_pr_off,
947 s_x2_pr_len);
948 if (inject_error == 1 && status != PSA_SUCCESS) {
949 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 } else {
952 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200953 }
954
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200955 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (inject_error == 1) {
957 TEST_ASSERT(
958 !"One of the last psa_pake_input() calls should have returned the expected error.");
959 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500963 buffer1[c_x1_pr_off + 12] ^= 1;
964 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200965 expected_status = PSA_ERROR_DATA_INVALID;
966 }
967
968 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
970 buffer1 + c_g1_off, c_g1_len);
971 if (inject_error == 2 && status != PSA_SUCCESS) {
972 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200973 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 } else {
975 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
979 buffer1 + c_x1_pk_off, c_x1_pk_len);
980 if (inject_error == 2 && status != PSA_SUCCESS) {
981 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200982 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 } else {
984 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200985 }
986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
988 buffer1 + c_x1_pr_off, c_x1_pr_len);
989 if (inject_error == 2 && status != PSA_SUCCESS) {
990 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200991 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 } else {
993 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200994 }
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
997 buffer1 + c_g2_off, c_g2_len);
998 if (inject_error == 2 && status != PSA_SUCCESS) {
999 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001000 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 } else {
1002 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1006 buffer1 + c_x2_pk_off, c_x2_pk_len);
1007 if (inject_error == 2 && status != PSA_SUCCESS) {
1008 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 } else {
1011 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001012 }
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1015 buffer1 + c_x2_pr_off, c_x2_pr_len);
1016 if (inject_error == 2 && status != PSA_SUCCESS) {
1017 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001018 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 } else {
1020 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001021 }
1022
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001023 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if (inject_error == 2) {
1025 TEST_ASSERT(
1026 !"One of the last psa_pake_input() calls should have returned the expected error.");
1027 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001028
1029 break;
1030
1031 case 2:
1032 /* Server second round Output */
1033 buffer0_off = 0;
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1036 buffer0 + buffer0_off,
1037 512 - buffer0_off, &s_a_len));
1038 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001039 s_a_off = buffer0_off;
1040 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1042 buffer0 + buffer0_off,
1043 512 - buffer0_off, &s_x2s_pk_len));
1044 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001045 s_x2s_pk_off = buffer0_off;
1046 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1048 buffer0 + buffer0_off,
1049 512 - buffer0_off, &s_x2s_pr_len));
1050 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001051 s_x2s_pr_off = buffer0_off;
1052 buffer0_off += s_x2s_pr_len;
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001055 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001056 expected_status = PSA_ERROR_DATA_INVALID;
1057 }
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001060 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1062 buffer0 + s_a_off, s_a_len);
1063 if (inject_error == 3 && status != PSA_SUCCESS) {
1064 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001065 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 } else {
1067 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1071 buffer0 + s_x2s_pk_off,
1072 s_x2s_pk_len);
1073 if (inject_error == 3 && status != PSA_SUCCESS) {
1074 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 } else {
1077 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001078 }
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1081 buffer0 + s_x2s_pr_off,
1082 s_x2s_pr_len);
1083 if (inject_error == 3 && status != PSA_SUCCESS) {
1084 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 } else {
1087 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001088 }
1089
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001090 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (inject_error == 3) {
1092 TEST_ASSERT(
1093 !"One of the last psa_pake_input() calls should have returned the expected error.");
1094 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001095 }
1096
1097 /* Client second round Output */
1098 buffer1_off = 0;
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1101 buffer1 + buffer1_off,
1102 512 - buffer1_off, &c_a_len));
1103 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001104 c_a_off = buffer1_off;
1105 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1107 buffer1 + buffer1_off,
1108 512 - buffer1_off, &c_x2s_pk_len));
1109 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001110 c_x2s_pk_off = buffer1_off;
1111 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1113 buffer1 + buffer1_off,
1114 512 - buffer1_off, &c_x2s_pr_len));
1115 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001116 c_x2s_pr_off = buffer1_off;
1117 buffer1_off += c_x2s_pr_len;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1122 buffer0 + s_a_off, s_a_len);
1123 if (inject_error == 3 && status != PSA_SUCCESS) {
1124 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001125 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 } else {
1127 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001128 }
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1131 buffer0 + s_x2s_pk_off,
1132 s_x2s_pk_len);
1133 if (inject_error == 3 && status != PSA_SUCCESS) {
1134 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else {
1137 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001138 }
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1141 buffer0 + s_x2s_pr_off,
1142 s_x2s_pr_len);
1143 if (inject_error == 3 && status != PSA_SUCCESS) {
1144 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 } else {
1147 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001148 }
1149
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001150 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (inject_error == 3) {
1152 TEST_ASSERT(
1153 !"One of the last psa_pake_input() calls should have returned the expected error.");
1154 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001155 }
1156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001158 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001159 expected_status = PSA_ERROR_DATA_INVALID;
1160 }
1161
1162 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1164 buffer1 + c_a_off, c_a_len);
1165 if (inject_error == 4 && status != PSA_SUCCESS) {
1166 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001167 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 } else {
1169 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1173 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1174 if (inject_error == 4 && status != PSA_SUCCESS) {
1175 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001176 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 } else {
1178 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001179 }
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1182 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1183 if (inject_error == 4 && status != PSA_SUCCESS) {
1184 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001185 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 } else {
1187 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001188 }
1189
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001190 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (inject_error == 4) {
1192 TEST_ASSERT(
1193 !"One of the last psa_pake_input() calls should have returned the expected error.");
1194 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001195
1196 break;
1197
1198 }
1199
Neil Armstrongf983caf2022-06-15 15:27:48 +02001200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_free(buffer0);
1202 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001203}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001204#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001207 INJECT_ERR_NONE = 0,
1208 INJECT_ERR_UNINITIALIZED_ACCESS,
1209 INJECT_ERR_DUPLICATE_SETUP,
1210 INJECT_ERR_INVALID_USER,
1211 INJECT_ERR_INVALID_PEER,
1212 INJECT_ERR_SET_USER,
1213 INJECT_ERR_SET_PEER,
1214 INJECT_EMPTY_IO_BUFFER,
1215 INJECT_UNKNOWN_STEP,
1216 INJECT_INVALID_FIRST_STEP,
1217 INJECT_WRONG_BUFFER_SIZE,
1218 INJECT_VALID_OPERATION_AFTER_FAILURE,
1219 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1220 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1221} ecjpake_injected_failure_t;
1222
Paul Elliott01885fa2023-02-09 12:07:30 +00001223#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001224
Paul Elliott6f600372023-02-06 18:41:05 +00001225static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1226 psa_status_t expected_status,
1227 size_t *min_completes,
1228 size_t *max_completes)
1229{
1230
1231 /* This is slightly contrived, but we only really know that with a minimum
1232 value of max_ops that a successful operation should take more than one op
1233 to complete, and likewise that with a max_ops of
1234 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1235 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001236
Paul Elliott6f600372023-02-06 18:41:05 +00001237 if (expected_status == PSA_SUCCESS) {
1238 *min_completes = 2;
1239 } else {
1240 *min_completes = 1;
1241 }
1242
1243 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1244 } else {
1245 *min_completes = 1;
1246 *max_completes = 1;
1247 }
1248}
Paul Elliott01885fa2023-02-09 12:07:30 +00001249#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001250
Gilles Peskinee59236f2018-01-27 23:32:46 +01001251/* END_HEADER */
1252
1253/* BEGIN_DEPENDENCIES
1254 * depends_on:MBEDTLS_PSA_CRYPTO_C
1255 * END_DEPENDENCIES
1256 */
1257
1258/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001260{
1261 size_t max_truncated_mac_size =
1262 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1263
1264 /* Check that the length for a truncated MAC always fits in the algorithm
1265 * encoding. The shifted mask is the maximum truncated value. The
1266 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001268}
1269/* END_CASE */
1270
1271/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272void import_with_policy(int type_arg,
1273 int usage_arg, int alg_arg,
1274 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001275{
1276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1277 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001279 psa_key_type_t type = type_arg;
1280 psa_key_usage_t usage = usage_arg;
1281 psa_algorithm_t alg = alg_arg;
1282 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001284 psa_status_t status;
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 psa_set_key_type(&attributes, type);
1289 psa_set_key_usage_flags(&attributes, usage);
1290 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 status = psa_import_key(&attributes,
1293 key_material, sizeof(key_material),
1294 &key);
1295 TEST_EQUAL(status, expected_status);
1296 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1301 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1302 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1303 mbedtls_test_update_key_usage_flags(usage));
1304 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1305 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 PSA_ASSERT(psa_destroy_key(key));
1308 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001309
1310exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001311 /*
1312 * Key attributes may have been returned by psa_get_key_attributes()
1313 * thus reset them as required.
1314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 psa_destroy_key(key);
1318 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001319}
1320/* END_CASE */
1321
1322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323void import_with_data(data_t *data, int type_arg,
1324 int attr_bits_arg,
1325 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326{
1327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1328 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001329 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001330 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001331 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001332 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001333 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 psa_set_key_type(&attributes, type);
1338 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 status = psa_import_key(&attributes, data->x, data->len, &key);
1341 TEST_EQUAL(status, expected_status);
1342 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001343 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1347 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1348 if (attr_bits != 0) {
1349 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1350 }
1351 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 PSA_ASSERT(psa_destroy_key(key));
1354 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
1356exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001357 /*
1358 * Key attributes may have been returned by psa_get_key_attributes()
1359 * thus reset them as required.
1360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 psa_destroy_key(key);
1364 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001369/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370void import_large_key(int type_arg, int byte_size_arg,
1371 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001372{
1373 psa_key_type_t type = type_arg;
1374 size_t byte_size = byte_size_arg;
1375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1376 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001378 psa_status_t status;
1379 uint8_t *buffer = NULL;
1380 size_t buffer_size = byte_size + 1;
1381 size_t n;
1382
Steven Cooreman69967ce2021-01-18 18:01:08 +01001383 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001384 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1386 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001389
1390 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1392 psa_set_key_type(&attributes, type);
1393 status = psa_import_key(&attributes, buffer, byte_size, &key);
1394 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1395 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (status == PSA_SUCCESS) {
1398 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1399 TEST_EQUAL(psa_get_key_type(&attributes), type);
1400 TEST_EQUAL(psa_get_key_bits(&attributes),
1401 PSA_BYTES_TO_BITS(byte_size));
1402 ASSERT_NO_SLOT_NUMBER(&attributes);
1403 memset(buffer, 0, byte_size + 1);
1404 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1405 for (n = 0; n < byte_size; n++) {
1406 TEST_EQUAL(buffer[n], 'K');
1407 }
1408 for (n = byte_size; n < buffer_size; n++) {
1409 TEST_EQUAL(buffer[n], 0);
1410 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001411 }
1412
1413exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001414 /*
1415 * Key attributes may have been returned by psa_get_key_attributes()
1416 * thus reset them as required.
1417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 psa_destroy_key(key);
1421 PSA_DONE();
1422 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001423}
1424/* END_CASE */
1425
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001426/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001427/* Import an RSA key with a valid structure (but not valid numbers
1428 * inside, beyond having sensible size and parity). This is expected to
1429 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001431{
Ronald Cron5425a212020-08-04 14:58:35 +02001432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001433 size_t bits = bits_arg;
1434 psa_status_t expected_status = expected_status_arg;
1435 psa_status_t status;
1436 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001437 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001438 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001440 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001441 unsigned char *p;
1442 int ret;
1443 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 PSA_ASSERT(psa_crypto_init());
1447 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1450 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001451 length = ret;
1452
1453 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 psa_set_key_type(&attributes, type);
1455 status = psa_import_key(&attributes, p, length, &key);
1456 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (status == PSA_SUCCESS) {
1459 PSA_ASSERT(psa_destroy_key(key));
1460 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001461
1462exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_free(buffer);
1464 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001465}
1466/* END_CASE */
1467
1468/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469void import_export(data_t *data,
1470 int type_arg,
1471 int usage_arg, int alg_arg,
1472 int lifetime_arg,
1473 int expected_bits,
1474 int export_size_delta,
1475 int expected_export_status_arg,
1476 /*whether reexport must give the original input exactly*/
1477 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478{
Ronald Cron5425a212020-08-04 14:58:35 +02001479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001481 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001482 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001483 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301484 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001485 unsigned char *exported = NULL;
1486 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001487 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001488 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001491 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492
Moran Pekercb088e72018-07-17 17:36:59 +03001493 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 ASSERT_ALLOC(exported, export_size);
1495 if (!canonical_input) {
1496 ASSERT_ALLOC(reexported, export_size);
1497 }
1498 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 psa_set_key_lifetime(&attributes, lifetime);
1501 psa_set_key_usage_flags(&attributes, usage_arg);
1502 psa_set_key_algorithm(&attributes, alg);
1503 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001504
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001505 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001507
1508 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1510 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1511 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1512 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513
1514 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 status = psa_export_key(key, exported, export_size, &exported_length);
1516 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001517
1518 /* The exported length must be set by psa_export_key() to a value between 0
1519 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1521 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1522 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1525 export_size - exported_length));
1526 if (status != PSA_SUCCESS) {
1527 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001529 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001530
Gilles Peskineea38a922021-02-13 00:05:16 +01001531 /* Run sanity checks on the exported key. For non-canonical inputs,
1532 * this validates the canonical representations. For canonical inputs,
1533 * this doesn't directly validate the implementation, but it still helps
1534 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (!psa_key_lifetime_is_external(lifetime)) {
1536 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301537 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 }
Archana4d7ae1d2021-07-07 02:50:22 +05301539 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (canonical_input) {
1542 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1543 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001544 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1546 &key2));
1547 PSA_ASSERT(psa_export_key(key2,
1548 reexported,
1549 export_size,
1550 &reexported_length));
1551 ASSERT_COMPARE(exported, exported_length,
1552 reexported, reexported_length);
1553 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001554 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 TEST_LE_U(exported_length,
1556 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1557 psa_get_key_bits(&got_attributes)));
1558 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001559
1560destroy:
1561 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 PSA_ASSERT(psa_destroy_key(key));
1563 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564
1565exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001566 /*
1567 * Key attributes may have been returned by psa_get_key_attributes()
1568 * thus reset them as required.
1569 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 psa_reset_key_attributes(&got_attributes);
1571 psa_destroy_key(key);
1572 mbedtls_free(exported);
1573 mbedtls_free(reexported);
1574 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575}
1576/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001577
Moran Pekerf709f4a2018-06-06 17:26:04 +03001578/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579void import_export_public_key(data_t *data,
1580 int type_arg, // key pair or public key
1581 int alg_arg,
1582 int lifetime_arg,
1583 int export_size_delta,
1584 int expected_export_status_arg,
1585 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001586{
Ronald Cron5425a212020-08-04 14:58:35 +02001587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001588 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001589 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001590 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001591 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301592 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001593 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001594 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001595 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 psa_set_key_lifetime(&attributes, lifetime);
1601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1602 psa_set_key_algorithm(&attributes, alg);
1603 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001604
1605 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001607
Gilles Peskine49c25912018-10-29 15:15:31 +01001608 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 ASSERT_ALLOC(exported, export_size);
1610 status = psa_export_public_key(key,
1611 exported, export_size,
1612 &exported_length);
1613 TEST_EQUAL(status, expected_export_status);
1614 if (status == PSA_SUCCESS) {
1615 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001616 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1618 bits = psa_get_key_bits(&attributes);
1619 TEST_LE_U(expected_public_key->len,
1620 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1621 TEST_LE_U(expected_public_key->len,
1622 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1623 TEST_LE_U(expected_public_key->len,
1624 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1625 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1626 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001627 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001628exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001629 /*
1630 * Key attributes may have been returned by psa_get_key_attributes()
1631 * thus reset them as required.
1632 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_free(exported);
1636 psa_destroy_key(key);
1637 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001638}
1639/* END_CASE */
1640
Gilles Peskine20035e32018-02-03 22:44:14 +01001641/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001642void import_and_exercise_key(data_t *data,
1643 int type_arg,
1644 int bits_arg,
1645 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001646{
Ronald Cron5425a212020-08-04 14:58:35 +02001647 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001648 psa_key_type_t type = type_arg;
1649 size_t bits = bits_arg;
1650 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001653 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 psa_set_key_usage_flags(&attributes, usage);
1658 psa_set_key_algorithm(&attributes, alg);
1659 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001660
1661 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001663
1664 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1666 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1667 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001668
1669 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001671 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 PSA_ASSERT(psa_destroy_key(key));
1675 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001676
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001678 /*
1679 * Key attributes may have been returned by psa_get_key_attributes()
1680 * thus reset them as required.
1681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 psa_reset_key_attributes(&attributes);
1685 psa_destroy_key(key);
1686 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001687}
1688/* END_CASE */
1689
1690/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691void effective_key_attributes(int type_arg, int expected_type_arg,
1692 int bits_arg, int expected_bits_arg,
1693 int usage_arg, int expected_usage_arg,
1694 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001695{
Ronald Cron5425a212020-08-04 14:58:35 +02001696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001697 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001698 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001699 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001700 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001701 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001702 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001703 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001704 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 psa_set_key_usage_flags(&attributes, usage);
1710 psa_set_key_algorithm(&attributes, alg);
1711 psa_set_key_type(&attributes, key_type);
1712 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 PSA_ASSERT(psa_generate_key(&attributes, &key));
1715 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1718 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1719 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1720 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1721 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001722
1723exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001724 /*
1725 * Key attributes may have been returned by psa_get_key_attributes()
1726 * thus reset them as required.
1727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 psa_destroy_key(key);
1731 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001732}
1733/* END_CASE */
1734
1735/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736void check_key_policy(int type_arg, int bits_arg,
1737 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001738{
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1740 usage_arg,
1741 mbedtls_test_update_key_usage_flags(usage_arg),
1742 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 goto exit;
1744}
1745/* END_CASE */
1746
1747/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001749{
1750 /* Test each valid way of initializing the object, except for `= {0}`, as
1751 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1752 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001753 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001755 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1756 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1761 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1762 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 TEST_EQUAL(psa_get_key_type(&func), 0);
1765 TEST_EQUAL(psa_get_key_type(&init), 0);
1766 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 TEST_EQUAL(psa_get_key_bits(&func), 0);
1769 TEST_EQUAL(psa_get_key_bits(&init), 0);
1770 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1773 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1774 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1777 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1778 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001779}
1780/* END_CASE */
1781
1782/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783void mac_key_policy(int policy_usage_arg,
1784 int policy_alg_arg,
1785 int key_type_arg,
1786 data_t *key_data,
1787 int exercise_alg_arg,
1788 int expected_status_sign_arg,
1789 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001790{
Ronald Cron5425a212020-08-04 14:58:35 +02001791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001793 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001794 psa_key_type_t key_type = key_type_arg;
1795 psa_algorithm_t policy_alg = policy_alg_arg;
1796 psa_algorithm_t exercise_alg = exercise_alg_arg;
1797 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001798 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001799 psa_status_t expected_status_sign = expected_status_sign_arg;
1800 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001801 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 psa_set_key_usage_flags(&attributes, policy_usage);
1806 psa_set_key_algorithm(&attributes, policy_alg);
1807 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1810 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1813 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1816 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001817
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001818 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001820 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1822 input, 128,
1823 mac, PSA_MAC_MAX_SIZE, &mac_len),
1824 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001825
Neil Armstrong3af9b972022-02-07 12:20:21 +01001826 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 PSA_ASSERT(psa_mac_abort(&operation));
1828 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1829 if (status == PSA_SUCCESS) {
1830 status = psa_mac_update(&operation, input, 128);
1831 if (status == PSA_SUCCESS) {
1832 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1833 &mac_len),
1834 expected_status_sign);
1835 } else {
1836 TEST_EQUAL(status, expected_status_sign);
1837 }
1838 } else {
1839 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001840 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001842
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001843 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 status = psa_mac_verify(key, exercise_alg, input, 128,
1845 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1848 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1849 } else {
1850 TEST_EQUAL(status, expected_status_verify);
1851 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852
Neil Armstrong3af9b972022-02-07 12:20:21 +01001853 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1855 if (status == PSA_SUCCESS) {
1856 status = psa_mac_update(&operation, input, 128);
1857 if (status == PSA_SUCCESS) {
1858 status = psa_mac_verify_finish(&operation, mac, mac_len);
1859 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1860 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1861 } else {
1862 TEST_EQUAL(status, expected_status_verify);
1863 }
1864 } else {
1865 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001866 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 } else {
1868 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001869 }
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 memset(mac, 0, sizeof(mac));
1874 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1875 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876
1877exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 psa_mac_abort(&operation);
1879 psa_destroy_key(key);
1880 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001881}
1882/* END_CASE */
1883
1884/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885void cipher_key_policy(int policy_usage_arg,
1886 int policy_alg,
1887 int key_type,
1888 data_t *key_data,
1889 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890{
Ronald Cron5425a212020-08-04 14:58:35 +02001891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001893 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001894 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001895 size_t output_buffer_size = 0;
1896 size_t input_buffer_size = 0;
1897 size_t output_length = 0;
1898 uint8_t *output = NULL;
1899 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001900 psa_status_t status;
1901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1903 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1904 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 ASSERT_ALLOC(input, input_buffer_size);
1907 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 psa_set_key_usage_flags(&attributes, policy_usage);
1912 psa_set_key_algorithm(&attributes, policy_alg);
1913 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1916 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001918 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 TEST_EQUAL(policy_usage,
1920 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001921
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001922 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1924 output, output_buffer_size,
1925 &output_length);
1926 if (policy_alg == exercise_alg &&
1927 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1928 PSA_ASSERT(status);
1929 } else {
1930 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1931 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001932
1933 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1935 if (policy_alg == exercise_alg &&
1936 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1937 PSA_ASSERT(status);
1938 } else {
1939 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1940 }
1941 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001942
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001943 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1945 input, input_buffer_size,
1946 &output_length);
1947 if (policy_alg == exercise_alg &&
1948 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1949 PSA_ASSERT(status);
1950 } else {
1951 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1952 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001953
1954 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1956 if (policy_alg == exercise_alg &&
1957 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1958 PSA_ASSERT(status);
1959 } else {
1960 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1961 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962
1963exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 psa_cipher_abort(&operation);
1965 mbedtls_free(input);
1966 mbedtls_free(output);
1967 psa_destroy_key(key);
1968 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001969}
1970/* END_CASE */
1971
1972/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973void aead_key_policy(int policy_usage_arg,
1974 int policy_alg,
1975 int key_type,
1976 data_t *key_data,
1977 int nonce_length_arg,
1978 int tag_length_arg,
1979 int exercise_alg,
1980 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981{
Ronald Cron5425a212020-08-04 14:58:35 +02001982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001984 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001985 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001987 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989 size_t nonce_length = nonce_length_arg;
1990 unsigned char tag[16];
1991 size_t tag_length = tag_length_arg;
1992 size_t output_length;
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 TEST_LE_U(nonce_length, sizeof(nonce));
1995 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 psa_set_key_usage_flags(&attributes, policy_usage);
2000 psa_set_key_algorithm(&attributes, policy_alg);
2001 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2004 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002006 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 TEST_EQUAL(policy_usage,
2008 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002009
Neil Armstrong752d8112022-02-07 14:51:11 +01002010 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 status = psa_aead_encrypt(key, exercise_alg,
2012 nonce, nonce_length,
2013 NULL, 0,
2014 NULL, 0,
2015 tag, tag_length,
2016 &output_length);
2017 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2018 TEST_EQUAL(status, expected_status);
2019 } else {
2020 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2021 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022
Neil Armstrong752d8112022-02-07 14:51:11 +01002023 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2025 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2026 TEST_EQUAL(status, expected_status);
2027 } else {
2028 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2029 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002030
2031 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 memset(tag, 0, sizeof(tag));
2033 status = psa_aead_decrypt(key, exercise_alg,
2034 nonce, nonce_length,
2035 NULL, 0,
2036 tag, tag_length,
2037 NULL, 0,
2038 &output_length);
2039 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2040 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2041 } else if (expected_status == PSA_SUCCESS) {
2042 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2043 } else {
2044 TEST_EQUAL(status, expected_status);
2045 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046
Neil Armstrong752d8112022-02-07 14:51:11 +01002047 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 PSA_ASSERT(psa_aead_abort(&operation));
2049 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2050 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2051 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2052 } else {
2053 TEST_EQUAL(status, expected_status);
2054 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002055
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 PSA_ASSERT(psa_aead_abort(&operation));
2058 psa_destroy_key(key);
2059 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002060}
2061/* END_CASE */
2062
2063/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064void asymmetric_encryption_key_policy(int policy_usage_arg,
2065 int policy_alg,
2066 int key_type,
2067 data_t *key_data,
2068 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069{
Ronald Cron5425a212020-08-04 14:58:35 +02002070 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002072 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 psa_status_t status;
2074 size_t key_bits;
2075 size_t buffer_length;
2076 unsigned char *buffer = NULL;
2077 size_t output_length;
2078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 psa_set_key_usage_flags(&attributes, policy_usage);
2082 psa_set_key_algorithm(&attributes, policy_alg);
2083 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2086 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002088 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 TEST_EQUAL(policy_usage,
2090 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2093 key_bits = psa_get_key_bits(&attributes);
2094 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2095 exercise_alg);
2096 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 status = psa_asymmetric_encrypt(key, exercise_alg,
2099 NULL, 0,
2100 NULL, 0,
2101 buffer, buffer_length,
2102 &output_length);
2103 if (policy_alg == exercise_alg &&
2104 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2105 PSA_ASSERT(status);
2106 } else {
2107 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2108 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (buffer_length != 0) {
2111 memset(buffer, 0, buffer_length);
2112 }
2113 status = psa_asymmetric_decrypt(key, exercise_alg,
2114 buffer, buffer_length,
2115 NULL, 0,
2116 buffer, buffer_length,
2117 &output_length);
2118 if (policy_alg == exercise_alg &&
2119 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2120 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2121 } else {
2122 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2123 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
2125exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002126 /*
2127 * Key attributes may have been returned by psa_get_key_attributes()
2128 * thus reset them as required.
2129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 psa_destroy_key(key);
2133 PSA_DONE();
2134 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135}
2136/* END_CASE */
2137
2138/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139void asymmetric_signature_key_policy(int policy_usage_arg,
2140 int policy_alg,
2141 int key_type,
2142 data_t *key_data,
2143 int exercise_alg,
2144 int payload_length_arg,
2145 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002146{
Ronald Cron5425a212020-08-04 14:58:35 +02002147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002149 psa_key_usage_t policy_usage = policy_usage_arg;
2150 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002153 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2154 * compatible with the policy and `payload_length_arg` is supposed to be
2155 * a valid input length to sign. If `payload_length_arg <= 0`,
2156 * `exercise_alg` is supposed to be forbidden by the policy. */
2157 int compatible_alg = payload_length_arg > 0;
2158 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002160 size_t signature_length;
2161
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002162 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002163 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 TEST_EQUAL(expected_usage,
2165 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 psa_set_key_usage_flags(&attributes, policy_usage);
2170 psa_set_key_algorithm(&attributes, policy_alg);
2171 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2174 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 status = psa_sign_hash(key, exercise_alg,
2179 payload, payload_length,
2180 signature, sizeof(signature),
2181 &signature_length);
2182 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2183 PSA_ASSERT(status);
2184 } else {
2185 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2186 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 memset(signature, 0, sizeof(signature));
2189 status = psa_verify_hash(key, exercise_alg,
2190 payload, payload_length,
2191 signature, sizeof(signature));
2192 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2193 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2194 } else {
2195 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2196 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2199 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2200 status = psa_sign_message(key, exercise_alg,
2201 payload, payload_length,
2202 signature, sizeof(signature),
2203 &signature_length);
2204 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2205 PSA_ASSERT(status);
2206 } else {
2207 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2208 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 memset(signature, 0, sizeof(signature));
2211 status = psa_verify_message(key, exercise_alg,
2212 payload, payload_length,
2213 signature, sizeof(signature));
2214 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2215 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2216 } else {
2217 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2218 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219 }
2220
Gilles Peskined5b33222018-06-18 22:20:03 +02002221exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 psa_destroy_key(key);
2223 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002224}
2225/* END_CASE */
2226
Janos Follathba3fab92019-06-11 14:50:16 +01002227/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228void derive_key_policy(int policy_usage,
2229 int policy_alg,
2230 int key_type,
2231 data_t *key_data,
2232 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002233{
Ronald Cron5425a212020-08-04 14:58:35 +02002234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002236 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002237 psa_status_t status;
2238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 psa_set_key_usage_flags(&attributes, policy_usage);
2242 psa_set_key_algorithm(&attributes, policy_alg);
2243 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2246 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2251 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2252 PSA_ASSERT(psa_key_derivation_input_bytes(
2253 &operation,
2254 PSA_KEY_DERIVATION_INPUT_SEED,
2255 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002256 }
Janos Follathba3fab92019-06-11 14:50:16 +01002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 status = psa_key_derivation_input_key(&operation,
2259 PSA_KEY_DERIVATION_INPUT_SECRET,
2260 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (policy_alg == exercise_alg &&
2263 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2264 PSA_ASSERT(status);
2265 } else {
2266 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2267 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268
2269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 psa_key_derivation_abort(&operation);
2271 psa_destroy_key(key);
2272 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002273}
2274/* END_CASE */
2275
2276/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002277void agreement_key_policy(int policy_usage,
2278 int policy_alg,
2279 int key_type_arg,
2280 data_t *key_data,
2281 int exercise_alg,
2282 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002283{
Ronald Cron5425a212020-08-04 14:58:35 +02002284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002286 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002287 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002288 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002289 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 psa_set_key_usage_flags(&attributes, policy_usage);
2294 psa_set_key_algorithm(&attributes, policy_alg);
2295 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2298 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2301 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
2305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 psa_key_derivation_abort(&operation);
2307 psa_destroy_key(key);
2308 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309}
2310/* END_CASE */
2311
2312/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313void key_policy_alg2(int key_type_arg, data_t *key_data,
2314 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002315{
Ronald Cron5425a212020-08-04 14:58:35 +02002316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002317 psa_key_type_t key_type = key_type_arg;
2318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2319 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2320 psa_key_usage_t usage = usage_arg;
2321 psa_algorithm_t alg = alg_arg;
2322 psa_algorithm_t alg2 = alg2_arg;
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 psa_set_key_usage_flags(&attributes, usage);
2327 psa_set_key_algorithm(&attributes, alg);
2328 psa_set_key_enrollment_algorithm(&attributes, alg2);
2329 psa_set_key_type(&attributes, key_type);
2330 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2331 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002332
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002333 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 usage = mbedtls_test_update_key_usage_flags(usage);
2335 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2336 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2337 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2338 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002341 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 }
2343 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002344 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002346
2347exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002348 /*
2349 * Key attributes may have been returned by psa_get_key_attributes()
2350 * thus reset them as required.
2351 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_destroy_key(key);
2355 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002356}
2357/* END_CASE */
2358
2359/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002360void raw_agreement_key_policy(int policy_usage,
2361 int policy_alg,
2362 int key_type_arg,
2363 data_t *key_data,
2364 int exercise_alg,
2365 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366{
Ronald Cron5425a212020-08-04 14:58:35 +02002367 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002369 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002370 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002371 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002372 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 psa_set_key_usage_flags(&attributes, policy_usage);
2377 psa_set_key_algorithm(&attributes, policy_alg);
2378 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2381 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002386
2387exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 psa_key_derivation_abort(&operation);
2389 psa_destroy_key(key);
2390 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002391}
2392/* END_CASE */
2393
2394/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395void copy_success(int source_usage_arg,
2396 int source_alg_arg, int source_alg2_arg,
2397 unsigned int source_lifetime_arg,
2398 int type_arg, data_t *material,
2399 int copy_attributes,
2400 int target_usage_arg,
2401 int target_alg_arg, int target_alg2_arg,
2402 unsigned int target_lifetime_arg,
2403 int expected_usage_arg,
2404 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002405{
Gilles Peskineca25db92019-04-19 11:43:08 +02002406 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2407 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002408 psa_key_usage_t expected_usage = expected_usage_arg;
2409 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002410 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302411 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2412 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002413 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2414 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002415 uint8_t *export_buffer = NULL;
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002418
Gilles Peskineca25db92019-04-19 11:43:08 +02002419 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2421 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2422 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2423 psa_set_key_type(&source_attributes, type_arg);
2424 psa_set_key_lifetime(&source_attributes, source_lifetime);
2425 PSA_ASSERT(psa_import_key(&source_attributes,
2426 material->x, material->len,
2427 &source_key));
2428 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002429
Gilles Peskineca25db92019-04-19 11:43:08 +02002430 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002432 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002433 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (target_usage_arg != -1) {
2437 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2438 }
2439 if (target_alg_arg != -1) {
2440 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2441 }
2442 if (target_alg2_arg != -1) {
2443 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2444 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002445
Archana8a180362021-07-05 02:18:48 +05302446
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 PSA_ASSERT(psa_copy_key(source_key,
2449 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450
2451 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453
2454 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2456 TEST_EQUAL(psa_get_key_type(&source_attributes),
2457 psa_get_key_type(&target_attributes));
2458 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2459 psa_get_key_bits(&target_attributes));
2460 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2461 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2462 TEST_EQUAL(expected_alg2,
2463 psa_get_key_enrollment_algorithm(&target_attributes));
2464 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002465 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 ASSERT_ALLOC(export_buffer, material->len);
2467 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2468 material->len, &length));
2469 ASSERT_COMPARE(material->x, material->len,
2470 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002471 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (!psa_key_lifetime_is_external(target_lifetime)) {
2474 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302475 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 }
2477 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302478 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 }
Archana8a180362021-07-05 02:18:48 +05302480 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002483
2484exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002485 /*
2486 * Source and target key attributes may have been returned by
2487 * psa_get_key_attributes() thus reset them as required.
2488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_reset_key_attributes(&source_attributes);
2490 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 PSA_DONE();
2493 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002494}
2495/* END_CASE */
2496
2497/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002498void copy_fail(int source_usage_arg,
2499 int source_alg_arg, int source_alg2_arg,
2500 int source_lifetime_arg,
2501 int type_arg, data_t *material,
2502 int target_type_arg, int target_bits_arg,
2503 int target_usage_arg,
2504 int target_alg_arg, int target_alg2_arg,
2505 int target_id_arg, int target_lifetime_arg,
2506 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002507{
2508 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2509 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002510 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2511 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002515
2516 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2518 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2519 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2520 psa_set_key_type(&source_attributes, type_arg);
2521 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2522 PSA_ASSERT(psa_import_key(&source_attributes,
2523 material->x, material->len,
2524 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002525
2526 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 psa_set_key_id(&target_attributes, key_id);
2528 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2529 psa_set_key_type(&target_attributes, target_type_arg);
2530 psa_set_key_bits(&target_attributes, target_bits_arg);
2531 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2532 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2533 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002534
2535 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 TEST_EQUAL(psa_copy_key(source_key,
2537 &target_attributes, &target_key),
2538 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002541
Gilles Peskine4a644642019-05-03 17:14:08 +02002542exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 psa_reset_key_attributes(&source_attributes);
2544 psa_reset_key_attributes(&target_attributes);
2545 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002546}
2547/* END_CASE */
2548
2549/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002551{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002552 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002553 /* Test each valid way of initializing the object, except for `= {0}`, as
2554 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2555 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002556 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002558 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2559 psa_hash_operation_t zero;
2560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002562
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002563 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2565 PSA_ERROR_BAD_STATE);
2566 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2567 PSA_ERROR_BAD_STATE);
2568 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2569 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002570
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002571 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 PSA_ASSERT(psa_hash_abort(&func));
2573 PSA_ASSERT(psa_hash_abort(&init));
2574 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002575}
2576/* END_CASE */
2577
2578/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002579void hash_setup(int alg_arg,
2580 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581{
2582 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002583 uint8_t *output = NULL;
2584 size_t output_size = 0;
2585 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002586 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002587 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588 psa_status_t status;
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002591
Neil Armstrongedb20862022-02-07 15:47:44 +01002592 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 output_size = PSA_HASH_LENGTH(alg);
2594 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 status = psa_hash_compute(alg, NULL, 0,
2597 output, output_size, &output_length);
2598 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002599
2600 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 status = psa_hash_setup(&operation, alg);
2602 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002603
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002604 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002606
2607 /* If setup failed, reproduce the failure, so as to
2608 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (status != PSA_SUCCESS) {
2610 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2611 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002612
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002613 /* Now the operation object should be reusable. */
2614#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2616 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002617#endif
2618
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 mbedtls_free(output);
2621 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002622}
2623/* END_CASE */
2624
2625/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626void hash_compute_fail(int alg_arg, data_t *input,
2627 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002628{
2629 psa_algorithm_t alg = alg_arg;
2630 uint8_t *output = NULL;
2631 size_t output_size = output_size_arg;
2632 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002633 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002634 psa_status_t expected_status = expected_status_arg;
2635 psa_status_t status;
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002640
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002641 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 status = psa_hash_compute(alg, input->x, input->len,
2643 output, output_size, &output_length);
2644 TEST_EQUAL(status, expected_status);
2645 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002646
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002647 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 status = psa_hash_setup(&operation, alg);
2649 if (status == PSA_SUCCESS) {
2650 status = psa_hash_update(&operation, input->x, input->len);
2651 if (status == PSA_SUCCESS) {
2652 status = psa_hash_finish(&operation, output, output_size,
2653 &output_length);
2654 if (status == PSA_SUCCESS) {
2655 TEST_LE_U(output_length, output_size);
2656 } else {
2657 TEST_EQUAL(status, expected_status);
2658 }
2659 } else {
2660 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002661 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 } else {
2663 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002664 }
2665
Gilles Peskine0a749c82019-11-28 19:33:58 +01002666exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 PSA_ASSERT(psa_hash_abort(&operation));
2668 mbedtls_free(output);
2669 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002670}
2671/* END_CASE */
2672
2673/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002674void hash_compare_fail(int alg_arg, data_t *input,
2675 data_t *reference_hash,
2676 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002677{
2678 psa_algorithm_t alg = alg_arg;
2679 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002680 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002681 psa_status_t status;
2682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002684
Neil Armstrong55a1be12022-02-07 11:23:20 +01002685 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 status = psa_hash_compare(alg, input->x, input->len,
2687 reference_hash->x, reference_hash->len);
2688 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002689
Neil Armstrong55a1be12022-02-07 11:23:20 +01002690 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 status = psa_hash_setup(&operation, alg);
2692 if (status == PSA_SUCCESS) {
2693 status = psa_hash_update(&operation, input->x, input->len);
2694 if (status == PSA_SUCCESS) {
2695 status = psa_hash_verify(&operation, reference_hash->x,
2696 reference_hash->len);
2697 TEST_EQUAL(status, expected_status);
2698 } else {
2699 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002700 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 } else {
2702 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002703 }
2704
Gilles Peskine88e08462020-01-28 20:43:00 +01002705exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 PSA_ASSERT(psa_hash_abort(&operation));
2707 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002708}
2709/* END_CASE */
2710
2711/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002712void hash_compute_compare(int alg_arg, data_t *input,
2713 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002714{
2715 psa_algorithm_t alg = alg_arg;
2716 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2717 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002718 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002719 size_t i;
2720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002722
Neil Armstrongca30a002022-02-07 11:40:23 +01002723 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2725 output, PSA_HASH_LENGTH(alg),
2726 &output_length));
2727 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2728 ASSERT_COMPARE(output, output_length,
2729 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002730
Neil Armstrongca30a002022-02-07 11:40:23 +01002731 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 PSA_ASSERT(psa_hash_setup(&operation, alg));
2733 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2734 PSA_ASSERT(psa_hash_finish(&operation, output,
2735 PSA_HASH_LENGTH(alg),
2736 &output_length));
2737 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2738 ASSERT_COMPARE(output, output_length,
2739 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002740
2741 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2743 output, sizeof(output),
2744 &output_length));
2745 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2746 ASSERT_COMPARE(output, output_length,
2747 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002748
Neil Armstrongca30a002022-02-07 11:40:23 +01002749 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 PSA_ASSERT(psa_hash_setup(&operation, alg));
2751 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2752 PSA_ASSERT(psa_hash_finish(&operation, output,
2753 sizeof(output), &output_length));
2754 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2755 ASSERT_COMPARE(output, output_length,
2756 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002757
2758 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2760 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002761
Neil Armstrongca30a002022-02-07 11:40:23 +01002762 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 PSA_ASSERT(psa_hash_setup(&operation, alg));
2764 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2765 PSA_ASSERT(psa_hash_verify(&operation, output,
2766 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002767
2768 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2770 output, output_length + 1),
2771 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002772
Neil Armstrongca30a002022-02-07 11:40:23 +01002773 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 PSA_ASSERT(psa_hash_setup(&operation, alg));
2775 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2776 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2777 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002778
2779 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2781 output, output_length - 1),
2782 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002783
Neil Armstrongca30a002022-02-07 11:40:23 +01002784 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 PSA_ASSERT(psa_hash_setup(&operation, alg));
2786 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2787 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2788 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002789
Gilles Peskine0a749c82019-11-28 19:33:58 +01002790 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 for (i = 0; i < output_length; i++) {
2792 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002793 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002794
2795 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2797 output, output_length),
2798 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002799
2800 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 PSA_ASSERT(psa_hash_setup(&operation, alg));
2802 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2803 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2804 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002805
Gilles Peskine0a749c82019-11-28 19:33:58 +01002806 output[i] ^= 1;
2807 }
2808
2809exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 PSA_ASSERT(psa_hash_abort(&operation));
2811 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002812}
2813/* END_CASE */
2814
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002815/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002817{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002818 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002819 unsigned char input[] = "";
2820 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002821 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002822 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2823 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2825 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002826 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002827 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002828 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002831
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002832 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 PSA_ASSERT(psa_hash_setup(&operation, alg));
2834 ASSERT_OPERATION_IS_ACTIVE(operation);
2835 TEST_EQUAL(psa_hash_setup(&operation, alg),
2836 PSA_ERROR_BAD_STATE);
2837 ASSERT_OPERATION_IS_INACTIVE(operation);
2838 PSA_ASSERT(psa_hash_abort(&operation));
2839 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002840
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002841 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2843 PSA_ERROR_BAD_STATE);
2844 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002845
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002846 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002848 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 ASSERT_OPERATION_IS_ACTIVE(operation);
2850 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2851 PSA_ERROR_BAD_STATE);
2852 ASSERT_OPERATION_IS_INACTIVE(operation);
2853 PSA_ASSERT(psa_hash_abort(&operation));
2854 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002855
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002856 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 PSA_ASSERT(psa_hash_setup(&operation, alg));
2858 PSA_ASSERT(psa_hash_finish(&operation,
2859 hash, sizeof(hash), &hash_len));
2860 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2861 PSA_ERROR_BAD_STATE);
2862 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002863
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002864 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 TEST_EQUAL(psa_hash_verify(&operation,
2866 valid_hash, sizeof(valid_hash)),
2867 PSA_ERROR_BAD_STATE);
2868 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002869
2870 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 PSA_ASSERT(psa_hash_setup(&operation, alg));
2872 PSA_ASSERT(psa_hash_finish(&operation,
2873 hash, sizeof(hash), &hash_len));
2874 TEST_EQUAL(psa_hash_verify(&operation,
2875 valid_hash, sizeof(valid_hash)),
2876 PSA_ERROR_BAD_STATE);
2877 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002878
2879 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 PSA_ASSERT(psa_hash_setup(&operation, alg));
2881 ASSERT_OPERATION_IS_ACTIVE(operation);
2882 PSA_ASSERT(psa_hash_verify(&operation,
2883 valid_hash, sizeof(valid_hash)));
2884 ASSERT_OPERATION_IS_INACTIVE(operation);
2885 TEST_EQUAL(psa_hash_verify(&operation,
2886 valid_hash, sizeof(valid_hash)),
2887 PSA_ERROR_BAD_STATE);
2888 ASSERT_OPERATION_IS_INACTIVE(operation);
2889 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002890
2891 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 TEST_EQUAL(psa_hash_finish(&operation,
2893 hash, sizeof(hash), &hash_len),
2894 PSA_ERROR_BAD_STATE);
2895 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002896
2897 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 PSA_ASSERT(psa_hash_setup(&operation, alg));
2899 PSA_ASSERT(psa_hash_finish(&operation,
2900 hash, sizeof(hash), &hash_len));
2901 TEST_EQUAL(psa_hash_finish(&operation,
2902 hash, sizeof(hash), &hash_len),
2903 PSA_ERROR_BAD_STATE);
2904 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905
2906 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 PSA_ASSERT(psa_hash_setup(&operation, alg));
2908 PSA_ASSERT(psa_hash_verify(&operation,
2909 valid_hash, sizeof(valid_hash)));
2910 TEST_EQUAL(psa_hash_finish(&operation,
2911 hash, sizeof(hash), &hash_len),
2912 PSA_ERROR_BAD_STATE);
2913 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002914
2915exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002917}
2918/* END_CASE */
2919
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002920/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002922{
2923 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002924 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2925 * appended to it */
2926 unsigned char hash[] = {
2927 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2928 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2930 };
2931 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002932 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002935
itayzafrir27e69452018-11-01 14:26:34 +02002936 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 PSA_ASSERT(psa_hash_setup(&operation, alg));
2938 ASSERT_OPERATION_IS_ACTIVE(operation);
2939 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2940 PSA_ERROR_INVALID_SIGNATURE);
2941 ASSERT_OPERATION_IS_INACTIVE(operation);
2942 PSA_ASSERT(psa_hash_abort(&operation));
2943 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002944
itayzafrir27e69452018-11-01 14:26:34 +02002945 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 PSA_ASSERT(psa_hash_setup(&operation, alg));
2947 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2948 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002949
itayzafrir27e69452018-11-01 14:26:34 +02002950 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 PSA_ASSERT(psa_hash_setup(&operation, alg));
2952 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2953 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002954
itayzafrirec93d302018-10-18 18:01:10 +03002955exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002957}
2958/* END_CASE */
2959
Ronald Cronee414c72021-03-18 18:50:08 +01002960/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002962{
2963 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002964 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002966 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002967 size_t hash_len;
2968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002970
itayzafrir58028322018-10-25 10:22:01 +03002971 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 PSA_ASSERT(psa_hash_setup(&operation, alg));
2973 TEST_EQUAL(psa_hash_finish(&operation,
2974 hash, expected_size - 1, &hash_len),
2975 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002976
2977exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002979}
2980/* END_CASE */
2981
Ronald Cronee414c72021-03-18 18:50:08 +01002982/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002984{
2985 psa_algorithm_t alg = PSA_ALG_SHA_256;
2986 unsigned char hash[PSA_HASH_MAX_SIZE];
2987 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2988 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2989 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2990 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2991 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2992 size_t hash_len;
2993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 PSA_ASSERT(psa_crypto_init());
2995 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
2998 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
2999 PSA_ASSERT(psa_hash_finish(&op_finished,
3000 hash, sizeof(hash), &hash_len));
3001 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3002 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3005 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3008 PSA_ASSERT(psa_hash_finish(&op_init,
3009 hash, sizeof(hash), &hash_len));
3010 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3011 PSA_ASSERT(psa_hash_finish(&op_finished,
3012 hash, sizeof(hash), &hash_len));
3013 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3014 PSA_ASSERT(psa_hash_finish(&op_aborted,
3015 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003016
3017exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 psa_hash_abort(&op_source);
3019 psa_hash_abort(&op_init);
3020 psa_hash_abort(&op_setup);
3021 psa_hash_abort(&op_finished);
3022 psa_hash_abort(&op_aborted);
3023 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003024}
3025/* END_CASE */
3026
Ronald Cronee414c72021-03-18 18:50:08 +01003027/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003028void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003029{
3030 psa_algorithm_t alg = PSA_ALG_SHA_256;
3031 unsigned char hash[PSA_HASH_MAX_SIZE];
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 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3037 size_t hash_len;
3038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 PSA_ASSERT(psa_crypto_init());
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 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3049 PSA_ASSERT(psa_hash_finish(&op_target,
3050 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3053 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3054 PSA_ERROR_BAD_STATE);
3055 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3056 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003057
3058exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 psa_hash_abort(&op_target);
3060 psa_hash_abort(&op_init);
3061 psa_hash_abort(&op_setup);
3062 psa_hash_abort(&op_finished);
3063 psa_hash_abort(&op_aborted);
3064 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003065}
3066/* END_CASE */
3067
itayzafrir58028322018-10-25 10:22:01 +03003068/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003070{
Jaeden Amero252ef282019-02-15 14:05:35 +00003071 const uint8_t input[1] = { 0 };
3072
Jaeden Amero769ce272019-01-04 11:48:03 +00003073 /* Test each valid way of initializing the object, except for `= {0}`, as
3074 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3075 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003076 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003078 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3079 psa_mac_operation_t zero;
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003082
Jaeden Amero252ef282019-02-15 14:05:35 +00003083 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 TEST_EQUAL(psa_mac_update(&func,
3085 input, sizeof(input)),
3086 PSA_ERROR_BAD_STATE);
3087 TEST_EQUAL(psa_mac_update(&init,
3088 input, sizeof(input)),
3089 PSA_ERROR_BAD_STATE);
3090 TEST_EQUAL(psa_mac_update(&zero,
3091 input, sizeof(input)),
3092 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003093
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003094 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 PSA_ASSERT(psa_mac_abort(&func));
3096 PSA_ASSERT(psa_mac_abort(&init));
3097 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003098}
3099/* END_CASE */
3100
3101/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003102void mac_setup(int key_type_arg,
3103 data_t *key,
3104 int alg_arg,
3105 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003106{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107 psa_key_type_t key_type = key_type_arg;
3108 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003109 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003110 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003111 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3112#if defined(KNOWN_SUPPORTED_MAC_ALG)
3113 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3114#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003117
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3119 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 }
3122 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003123
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003124 /* The operation object should be reusable. */
3125#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3127 smoke_test_key_data,
3128 sizeof(smoke_test_key_data),
3129 KNOWN_SUPPORTED_MAC_ALG,
3130 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 }
3133 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003134#endif
3135
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138}
3139/* END_CASE */
3140
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003141/* 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 +01003142void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003143{
Ronald Cron5425a212020-08-04 14:58:35 +02003144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003145 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3146 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003147 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003148 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3149 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3151 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003153 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3154 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3155 size_t sign_mac_length = 0;
3156 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3157 const uint8_t verify_mac[] = {
3158 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3159 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3161 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003162
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 PSA_ASSERT(psa_crypto_init());
3164 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3165 psa_set_key_algorithm(&attributes, alg);
3166 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3169 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003170
Jaeden Amero252ef282019-02-15 14:05:35 +00003171 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3173 PSA_ERROR_BAD_STATE);
3174 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003175
3176 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3178 &sign_mac_length),
3179 PSA_ERROR_BAD_STATE);
3180 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003181
3182 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 TEST_EQUAL(psa_mac_verify_finish(&operation,
3184 verify_mac, sizeof(verify_mac)),
3185 PSA_ERROR_BAD_STATE);
3186 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003187
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003188 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3190 ASSERT_OPERATION_IS_ACTIVE(operation);
3191 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3192 PSA_ERROR_BAD_STATE);
3193 ASSERT_OPERATION_IS_INACTIVE(operation);
3194 PSA_ASSERT(psa_mac_abort(&operation));
3195 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003196
Jaeden Amero252ef282019-02-15 14:05:35 +00003197 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3199 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3200 PSA_ASSERT(psa_mac_sign_finish(&operation,
3201 sign_mac, sizeof(sign_mac),
3202 &sign_mac_length));
3203 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3204 PSA_ERROR_BAD_STATE);
3205 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003206
3207 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3209 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3210 PSA_ASSERT(psa_mac_verify_finish(&operation,
3211 verify_mac, sizeof(verify_mac)));
3212 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3213 PSA_ERROR_BAD_STATE);
3214 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003215
3216 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3218 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3219 PSA_ASSERT(psa_mac_sign_finish(&operation,
3220 sign_mac, sizeof(sign_mac),
3221 &sign_mac_length));
3222 TEST_EQUAL(psa_mac_sign_finish(&operation,
3223 sign_mac, sizeof(sign_mac),
3224 &sign_mac_length),
3225 PSA_ERROR_BAD_STATE);
3226 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003227
3228 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3230 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3231 PSA_ASSERT(psa_mac_verify_finish(&operation,
3232 verify_mac, sizeof(verify_mac)));
3233 TEST_EQUAL(psa_mac_verify_finish(&operation,
3234 verify_mac, sizeof(verify_mac)),
3235 PSA_ERROR_BAD_STATE);
3236 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003237
3238 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3240 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3241 ASSERT_OPERATION_IS_ACTIVE(operation);
3242 TEST_EQUAL(psa_mac_verify_finish(&operation,
3243 verify_mac, sizeof(verify_mac)),
3244 PSA_ERROR_BAD_STATE);
3245 ASSERT_OPERATION_IS_INACTIVE(operation);
3246 PSA_ASSERT(psa_mac_abort(&operation));
3247 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003248
3249 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3251 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3252 ASSERT_OPERATION_IS_ACTIVE(operation);
3253 TEST_EQUAL(psa_mac_sign_finish(&operation,
3254 sign_mac, sizeof(sign_mac),
3255 &sign_mac_length),
3256 PSA_ERROR_BAD_STATE);
3257 ASSERT_OPERATION_IS_INACTIVE(operation);
3258 PSA_ASSERT(psa_mac_abort(&operation));
3259 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003262
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003265}
3266/* END_CASE */
3267
3268/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269void mac_sign_verify_multi(int key_type_arg,
3270 data_t *key_data,
3271 int alg_arg,
3272 data_t *input,
3273 int is_verify,
3274 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003275{
3276 size_t data_part_len = 0;
3277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003279 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 if (mac_multipart_internal_func(key_type_arg, key_data,
3283 alg_arg,
3284 input, data_part_len,
3285 expected_mac,
3286 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003287 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003289
3290 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003292
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 if (mac_multipart_internal_func(key_type_arg, key_data,
3294 alg_arg,
3295 input, data_part_len,
3296 expected_mac,
3297 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003298 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003300 }
3301
3302 /* Goto is required to silence warnings about unused labels, as we
3303 * don't actually do any test assertions in this function. */
3304 goto exit;
3305}
3306/* END_CASE */
3307
3308/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003309void mac_sign(int key_type_arg,
3310 data_t *key_data,
3311 int alg_arg,
3312 data_t *input,
3313 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003314{
Ronald Cron5425a212020-08-04 14:58:35 +02003315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003316 psa_key_type_t key_type = key_type_arg;
3317 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003318 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003320 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003321 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003323 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003324 const size_t output_sizes_to_test[] = {
3325 0,
3326 1,
3327 expected_mac->len - 1,
3328 expected_mac->len,
3329 expected_mac->len + 1,
3330 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003333 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3339 psa_set_key_algorithm(&attributes, alg);
3340 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3343 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003344
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003346 const size_t output_size = output_sizes_to_test[i];
3347 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 (output_size >= expected_mac->len ? PSA_SUCCESS :
3349 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003350
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 mbedtls_test_set_step(output_size);
3352 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003353
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003354 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 TEST_EQUAL(psa_mac_compute(key, alg,
3356 input->x, input->len,
3357 actual_mac, output_size, &mac_length),
3358 expected_status);
3359 if (expected_status == PSA_SUCCESS) {
3360 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3361 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003362 }
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (output_size > 0) {
3365 memset(actual_mac, 0, output_size);
3366 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003367
3368 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3370 PSA_ASSERT(psa_mac_update(&operation,
3371 input->x, input->len));
3372 TEST_EQUAL(psa_mac_sign_finish(&operation,
3373 actual_mac, output_size,
3374 &mac_length),
3375 expected_status);
3376 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 if (expected_status == PSA_SUCCESS) {
3379 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3380 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003381 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003383 actual_mac = NULL;
3384 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003386exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 psa_mac_abort(&operation);
3388 psa_destroy_key(key);
3389 PSA_DONE();
3390 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003391}
3392/* END_CASE */
3393
3394/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003395void mac_verify(int key_type_arg,
3396 data_t *key_data,
3397 int alg_arg,
3398 data_t *input,
3399 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003400{
Ronald Cron5425a212020-08-04 14:58:35 +02003401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402 psa_key_type_t key_type = key_type_arg;
3403 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003404 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003406 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003407
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3413 psa_set_key_algorithm(&attributes, alg);
3414 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3417 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003418
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003419 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3421 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003422
3423 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3425 PSA_ASSERT(psa_mac_update(&operation,
3426 input->x, input->len));
3427 PSA_ASSERT(psa_mac_verify_finish(&operation,
3428 expected_mac->x,
3429 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003430
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003431 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 TEST_EQUAL(psa_mac_verify(key, alg,
3433 input->x, input->len,
3434 expected_mac->x,
3435 expected_mac->len - 1),
3436 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003437
3438 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3440 PSA_ASSERT(psa_mac_update(&operation,
3441 input->x, input->len));
3442 TEST_EQUAL(psa_mac_verify_finish(&operation,
3443 expected_mac->x,
3444 expected_mac->len - 1),
3445 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003446
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003447 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3449 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3450 TEST_EQUAL(psa_mac_verify(key, alg,
3451 input->x, input->len,
3452 perturbed_mac, expected_mac->len + 1),
3453 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003454
3455 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3457 PSA_ASSERT(psa_mac_update(&operation,
3458 input->x, input->len));
3459 TEST_EQUAL(psa_mac_verify_finish(&operation,
3460 perturbed_mac,
3461 expected_mac->len + 1),
3462 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003463
3464 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 for (size_t i = 0; i < expected_mac->len; i++) {
3466 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003467 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 TEST_EQUAL(psa_mac_verify(key, alg,
3470 input->x, input->len,
3471 perturbed_mac, expected_mac->len),
3472 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003473
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3475 PSA_ASSERT(psa_mac_update(&operation,
3476 input->x, input->len));
3477 TEST_EQUAL(psa_mac_verify_finish(&operation,
3478 perturbed_mac,
3479 expected_mac->len),
3480 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003481 perturbed_mac[i] ^= 1;
3482 }
3483
Gilles Peskine8c9def32018-02-08 10:02:12 +01003484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 psa_mac_abort(&operation);
3486 psa_destroy_key(key);
3487 PSA_DONE();
3488 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003489}
3490/* END_CASE */
3491
3492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003493void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003494{
Jaeden Ameroab439972019-02-15 14:12:05 +00003495 const uint8_t input[1] = { 0 };
3496 unsigned char output[1] = { 0 };
3497 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003498 /* Test each valid way of initializing the object, except for `= {0}`, as
3499 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3500 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003501 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003503 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3504 psa_cipher_operation_t zero;
3505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003507
Jaeden Ameroab439972019-02-15 14:12:05 +00003508 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 TEST_EQUAL(psa_cipher_update(&func,
3510 input, sizeof(input),
3511 output, sizeof(output),
3512 &output_length),
3513 PSA_ERROR_BAD_STATE);
3514 TEST_EQUAL(psa_cipher_update(&init,
3515 input, sizeof(input),
3516 output, sizeof(output),
3517 &output_length),
3518 PSA_ERROR_BAD_STATE);
3519 TEST_EQUAL(psa_cipher_update(&zero,
3520 input, sizeof(input),
3521 output, sizeof(output),
3522 &output_length),
3523 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003524
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003525 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 PSA_ASSERT(psa_cipher_abort(&func));
3527 PSA_ASSERT(psa_cipher_abort(&init));
3528 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003529}
3530/* END_CASE */
3531
3532/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003533void cipher_setup(int key_type_arg,
3534 data_t *key,
3535 int alg_arg,
3536 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003537{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003538 psa_key_type_t key_type = key_type_arg;
3539 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003540 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003541 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003542 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003543#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003544 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3545#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003546
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003548
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3550 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003551 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 }
3553 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003554
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003555 /* The operation object should be reusable. */
3556#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3558 smoke_test_key_data,
3559 sizeof(smoke_test_key_data),
3560 KNOWN_SUPPORTED_CIPHER_ALG,
3561 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003562 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 }
3564 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003565#endif
3566
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 psa_cipher_abort(&operation);
3569 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003570}
3571/* END_CASE */
3572
Ronald Cronee414c72021-03-18 18:50:08 +01003573/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003574void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003575{
Ronald Cron5425a212020-08-04 14:58:35 +02003576 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003577 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3578 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003580 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003581 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003582 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003583 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 0xaa, 0xaa, 0xaa, 0xaa
3585 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003586 const uint8_t text[] = {
3587 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 0xbb, 0xbb, 0xbb, 0xbb
3589 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003590 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003591 size_t length = 0;
3592
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 PSA_ASSERT(psa_crypto_init());
3594 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3595 psa_set_key_algorithm(&attributes, alg);
3596 psa_set_key_type(&attributes, key_type);
3597 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3598 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003599
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003600 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3602 ASSERT_OPERATION_IS_ACTIVE(operation);
3603 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3604 PSA_ERROR_BAD_STATE);
3605 ASSERT_OPERATION_IS_INACTIVE(operation);
3606 PSA_ASSERT(psa_cipher_abort(&operation));
3607 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003608
3609 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003610 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3611 ASSERT_OPERATION_IS_ACTIVE(operation);
3612 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3613 PSA_ERROR_BAD_STATE);
3614 ASSERT_OPERATION_IS_INACTIVE(operation);
3615 PSA_ASSERT(psa_cipher_abort(&operation));
3616 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003617
Jaeden Ameroab439972019-02-15 14:12:05 +00003618 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003619 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3620 buffer, sizeof(buffer),
3621 &length),
3622 PSA_ERROR_BAD_STATE);
3623 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003624
3625 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003626 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3627 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3628 buffer, sizeof(buffer),
3629 &length));
3630 ASSERT_OPERATION_IS_ACTIVE(operation);
3631 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3632 buffer, sizeof(buffer),
3633 &length),
3634 PSA_ERROR_BAD_STATE);
3635 ASSERT_OPERATION_IS_INACTIVE(operation);
3636 PSA_ASSERT(psa_cipher_abort(&operation));
3637 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003638
3639 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3641 PSA_ASSERT(psa_cipher_set_iv(&operation,
3642 iv, sizeof(iv)));
3643 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3644 buffer, sizeof(buffer),
3645 &length),
3646 PSA_ERROR_BAD_STATE);
3647 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003648
3649 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 TEST_EQUAL(psa_cipher_set_iv(&operation,
3651 iv, sizeof(iv)),
3652 PSA_ERROR_BAD_STATE);
3653 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003654
3655 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003656 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3657 PSA_ASSERT(psa_cipher_set_iv(&operation,
3658 iv, sizeof(iv)));
3659 ASSERT_OPERATION_IS_ACTIVE(operation);
3660 TEST_EQUAL(psa_cipher_set_iv(&operation,
3661 iv, sizeof(iv)),
3662 PSA_ERROR_BAD_STATE);
3663 ASSERT_OPERATION_IS_INACTIVE(operation);
3664 PSA_ASSERT(psa_cipher_abort(&operation));
3665 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003666
3667 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3669 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3670 buffer, sizeof(buffer),
3671 &length));
3672 TEST_EQUAL(psa_cipher_set_iv(&operation,
3673 iv, sizeof(iv)),
3674 PSA_ERROR_BAD_STATE);
3675 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003676
3677 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003678 TEST_EQUAL(psa_cipher_update(&operation,
3679 text, sizeof(text),
3680 buffer, sizeof(buffer),
3681 &length),
3682 PSA_ERROR_BAD_STATE);
3683 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003684
3685 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003686 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3687 ASSERT_OPERATION_IS_ACTIVE(operation);
3688 TEST_EQUAL(psa_cipher_update(&operation,
3689 text, sizeof(text),
3690 buffer, sizeof(buffer),
3691 &length),
3692 PSA_ERROR_BAD_STATE);
3693 ASSERT_OPERATION_IS_INACTIVE(operation);
3694 PSA_ASSERT(psa_cipher_abort(&operation));
3695 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003696
3697 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003698 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3699 PSA_ASSERT(psa_cipher_set_iv(&operation,
3700 iv, sizeof(iv)));
3701 PSA_ASSERT(psa_cipher_finish(&operation,
3702 buffer, sizeof(buffer), &length));
3703 TEST_EQUAL(psa_cipher_update(&operation,
3704 text, sizeof(text),
3705 buffer, sizeof(buffer),
3706 &length),
3707 PSA_ERROR_BAD_STATE);
3708 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003709
3710 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 TEST_EQUAL(psa_cipher_finish(&operation,
3712 buffer, sizeof(buffer), &length),
3713 PSA_ERROR_BAD_STATE);
3714 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003715
3716 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003718 /* Not calling update means we are encrypting an empty buffer, which is OK
3719 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003720 ASSERT_OPERATION_IS_ACTIVE(operation);
3721 TEST_EQUAL(psa_cipher_finish(&operation,
3722 buffer, sizeof(buffer), &length),
3723 PSA_ERROR_BAD_STATE);
3724 ASSERT_OPERATION_IS_INACTIVE(operation);
3725 PSA_ASSERT(psa_cipher_abort(&operation));
3726 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003727
3728 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3730 PSA_ASSERT(psa_cipher_set_iv(&operation,
3731 iv, sizeof(iv)));
3732 PSA_ASSERT(psa_cipher_finish(&operation,
3733 buffer, sizeof(buffer), &length));
3734 TEST_EQUAL(psa_cipher_finish(&operation,
3735 buffer, sizeof(buffer), &length),
3736 PSA_ERROR_BAD_STATE);
3737 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003738
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003740
Jaeden Ameroab439972019-02-15 14:12:05 +00003741exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 psa_cipher_abort(&operation);
3743 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003744}
3745/* END_CASE */
3746
3747/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003748void cipher_encrypt_fail(int alg_arg,
3749 int key_type_arg,
3750 data_t *key_data,
3751 data_t *input,
3752 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003753{
Ronald Cron5425a212020-08-04 14:58:35 +02003754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003755 psa_status_t status;
3756 psa_key_type_t key_type = key_type_arg;
3757 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003758 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003760 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3761 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003762 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003763 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003764 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003765 size_t function_output_length;
3766 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3768
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 if (PSA_ERROR_BAD_STATE != expected_status) {
3770 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003771
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3773 psa_set_key_algorithm(&attributes, alg);
3774 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003775
Gilles Peskine449bd832023-01-11 14:50:10 +01003776 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3777 input->len);
3778 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003779
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3781 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003782 }
3783
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003784 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3786 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003787
Gilles Peskine449bd832023-01-11 14:50:10 +01003788 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003789
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003790 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 status = psa_cipher_encrypt_setup(&operation, key, alg);
3792 if (status == PSA_SUCCESS) {
3793 if (alg != PSA_ALG_ECB_NO_PADDING) {
3794 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3795 iv, iv_size,
3796 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003797 }
3798
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 status = psa_cipher_update(&operation, input->x, input->len,
3800 output, output_buffer_size,
3801 &function_output_length);
3802 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003803 output_length += function_output_length;
3804
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 status = psa_cipher_finish(&operation, output + output_length,
3806 output_buffer_size - output_length,
3807 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003808
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 TEST_EQUAL(status, expected_status);
3810 } else {
3811 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 } else {
3814 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003815 }
3816
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 psa_cipher_abort(&operation);
3819 mbedtls_free(output);
3820 psa_destroy_key(key);
3821 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003822}
3823/* END_CASE */
3824
3825/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003826void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3827 data_t *input, int iv_length,
3828 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003829{
3830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3831 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3833 size_t output_buffer_size = 0;
3834 unsigned char *output = NULL;
3835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3837 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003838
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003840
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3842 psa_set_key_algorithm(&attributes, alg);
3843 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3846 &key));
3847 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3848 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3849 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003850
3851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 psa_cipher_abort(&operation);
3853 mbedtls_free(output);
3854 psa_destroy_key(key);
3855 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003856}
3857/* END_CASE */
3858
3859/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003860void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3861 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003862{
3863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3864 psa_key_type_t key_type = key_type_arg;
3865 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003866 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3867 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003868 unsigned char *output = NULL;
3869 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003870 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3872
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003874
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003875 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003876 TEST_LE_U(ciphertext->len,
3877 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3878 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3879 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3880 TEST_LE_U(plaintext->len,
3881 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3882 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3883 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003884
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003885
3886 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 psa_set_key_usage_flags(&attributes,
3888 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3889 psa_set_key_algorithm(&attributes, alg);
3890 psa_set_key_type(&attributes, key_type);
3891 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3892 &key));
3893 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3894 plaintext->len);
3895 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003896
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003897 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3899 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3900 PSA_ERROR_BAD_STATE);
3901 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3902 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3903 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003904
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003905 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3907 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3908 &length),
3909 PSA_ERROR_BAD_STATE);
3910 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3911 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3912 &length),
3913 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003914
Gilles Peskine286c3142022-04-20 17:09:38 +02003915 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003916 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003917 output_length = 0;
3918 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 PSA_ASSERT(psa_cipher_update(&operation,
3920 plaintext->x, plaintext->len,
3921 output, output_buffer_size,
3922 &length));
3923 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003924 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 PSA_ASSERT(psa_cipher_finish(&operation,
3926 mbedtls_buffer_offset(output, output_length),
3927 output_buffer_size - output_length,
3928 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003929 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3931 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003932
Gilles Peskine286c3142022-04-20 17:09:38 +02003933 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003935 output_length = 0;
3936 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 PSA_ASSERT(psa_cipher_update(&operation,
3938 ciphertext->x, ciphertext->len,
3939 output, output_buffer_size,
3940 &length));
3941 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003942 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 PSA_ASSERT(psa_cipher_finish(&operation,
3944 mbedtls_buffer_offset(output, output_length),
3945 output_buffer_size - output_length,
3946 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003947 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 ASSERT_COMPARE(plaintext->x, plaintext->len,
3949 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003950
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003951 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003952 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3954 output, output_buffer_size,
3955 &output_length));
3956 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3957 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003958
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003959 /* One-shot decryption */
3960 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3962 output, output_buffer_size,
3963 &output_length));
3964 ASSERT_COMPARE(plaintext->x, plaintext->len,
3965 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003966
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003967exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 PSA_ASSERT(psa_cipher_abort(&operation));
3969 mbedtls_free(output);
3970 psa_cipher_abort(&operation);
3971 psa_destroy_key(key);
3972 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003973}
3974/* END_CASE */
3975
3976/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003977void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003978{
3979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3980 psa_algorithm_t alg = alg_arg;
3981 psa_key_type_t key_type = key_type_arg;
3982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3983 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3984 psa_status_t status;
3985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01003987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3989 psa_set_key_algorithm(&attributes, alg);
3990 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01003991
3992 /* Usage of either of these two size macros would cause divide by zero
3993 * with incorrect key types previously. Input length should be irrelevant
3994 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
3996 0);
3997 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01003998
3999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4001 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004002
4003 /* Should fail due to invalid alg type (to support invalid key type).
4004 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004006
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004008
4009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 psa_cipher_abort(&operation);
4011 psa_destroy_key(key);
4012 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004013}
4014/* END_CASE */
4015
4016/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004017void cipher_encrypt_validation(int alg_arg,
4018 int key_type_arg,
4019 data_t *key_data,
4020 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004021{
4022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4023 psa_key_type_t key_type = key_type_arg;
4024 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004026 unsigned char *output1 = NULL;
4027 size_t output1_buffer_size = 0;
4028 size_t output1_length = 0;
4029 unsigned char *output2 = NULL;
4030 size_t output2_buffer_size = 0;
4031 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004032 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004033 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004037
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4039 psa_set_key_algorithm(&attributes, alg);
4040 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4043 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4044 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4045 ASSERT_ALLOC(output1, output1_buffer_size);
4046 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4049 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004050
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004051 /* The one-shot cipher encryption uses generated iv so validating
4052 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4054 output1_buffer_size, &output1_length));
4055 TEST_LE_U(output1_length,
4056 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4057 TEST_LE_U(output1_length,
4058 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4061 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 PSA_ASSERT(psa_cipher_update(&operation,
4064 input->x, input->len,
4065 output2, output2_buffer_size,
4066 &function_output_length));
4067 TEST_LE_U(function_output_length,
4068 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4069 TEST_LE_U(function_output_length,
4070 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004071 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 PSA_ASSERT(psa_cipher_finish(&operation,
4074 output2 + output2_length,
4075 output2_buffer_size - output2_length,
4076 &function_output_length));
4077 TEST_LE_U(function_output_length,
4078 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4079 TEST_LE_U(function_output_length,
4080 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004081 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 PSA_ASSERT(psa_cipher_abort(&operation));
4084 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4085 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004086
Gilles Peskine50e586b2018-06-08 14:28:46 +02004087exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 psa_cipher_abort(&operation);
4089 mbedtls_free(output1);
4090 mbedtls_free(output2);
4091 psa_destroy_key(key);
4092 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004093}
4094/* END_CASE */
4095
4096/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004097void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4098 data_t *key_data, data_t *iv,
4099 data_t *input,
4100 int first_part_size_arg,
4101 int output1_length_arg, int output2_length_arg,
4102 data_t *expected_output,
4103 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004104{
Ronald Cron5425a212020-08-04 14:58:35 +02004105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004106 psa_key_type_t key_type = key_type_arg;
4107 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004108 psa_status_t status;
4109 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004110 size_t first_part_size = first_part_size_arg;
4111 size_t output1_length = output1_length_arg;
4112 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004113 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004114 size_t output_buffer_size = 0;
4115 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004116 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004117 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4123 psa_set_key_algorithm(&attributes, alg);
4124 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4127 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004128
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 if (iv->len > 0) {
4132 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004133 }
4134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4136 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4137 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 TEST_LE_U(first_part_size, input->len);
4140 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4141 output, output_buffer_size,
4142 &function_output_length));
4143 TEST_ASSERT(function_output_length == output1_length);
4144 TEST_LE_U(function_output_length,
4145 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4146 TEST_LE_U(function_output_length,
4147 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004148 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 if (first_part_size < input->len) {
4151 PSA_ASSERT(psa_cipher_update(&operation,
4152 input->x + first_part_size,
4153 input->len - first_part_size,
4154 (output_buffer_size == 0 ? NULL :
4155 output + total_output_length),
4156 output_buffer_size - total_output_length,
4157 &function_output_length));
4158 TEST_ASSERT(function_output_length == output2_length);
4159 TEST_LE_U(function_output_length,
4160 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4161 alg,
4162 input->len - first_part_size));
4163 TEST_LE_U(function_output_length,
4164 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004165 total_output_length += function_output_length;
4166 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 status = psa_cipher_finish(&operation,
4169 (output_buffer_size == 0 ? NULL :
4170 output + total_output_length),
4171 output_buffer_size - total_output_length,
4172 &function_output_length);
4173 TEST_LE_U(function_output_length,
4174 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4175 TEST_LE_U(function_output_length,
4176 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004177 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (expected_status == PSA_SUCCESS) {
4181 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 ASSERT_COMPARE(expected_output->x, expected_output->len,
4184 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004185 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004186
4187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 psa_cipher_abort(&operation);
4189 mbedtls_free(output);
4190 psa_destroy_key(key);
4191 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192}
4193/* END_CASE */
4194
4195/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004196void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4197 data_t *key_data, data_t *iv,
4198 data_t *input,
4199 int first_part_size_arg,
4200 int output1_length_arg, int output2_length_arg,
4201 data_t *expected_output,
4202 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004203{
Ronald Cron5425a212020-08-04 14:58:35 +02004204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004205 psa_key_type_t key_type = key_type_arg;
4206 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004207 psa_status_t status;
4208 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004209 size_t first_part_size = first_part_size_arg;
4210 size_t output1_length = output1_length_arg;
4211 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004212 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004213 size_t output_buffer_size = 0;
4214 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004215 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004216 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4222 psa_set_key_algorithm(&attributes, alg);
4223 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4226 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (iv->len > 0) {
4231 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004232 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4235 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4236 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 TEST_LE_U(first_part_size, input->len);
4239 PSA_ASSERT(psa_cipher_update(&operation,
4240 input->x, first_part_size,
4241 output, output_buffer_size,
4242 &function_output_length));
4243 TEST_ASSERT(function_output_length == output1_length);
4244 TEST_LE_U(function_output_length,
4245 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4246 TEST_LE_U(function_output_length,
4247 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004248 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 if (first_part_size < input->len) {
4251 PSA_ASSERT(psa_cipher_update(&operation,
4252 input->x + first_part_size,
4253 input->len - first_part_size,
4254 (output_buffer_size == 0 ? NULL :
4255 output + total_output_length),
4256 output_buffer_size - total_output_length,
4257 &function_output_length));
4258 TEST_ASSERT(function_output_length == output2_length);
4259 TEST_LE_U(function_output_length,
4260 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4261 alg,
4262 input->len - first_part_size));
4263 TEST_LE_U(function_output_length,
4264 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004265 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004266 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 status = psa_cipher_finish(&operation,
4269 (output_buffer_size == 0 ? NULL :
4270 output + total_output_length),
4271 output_buffer_size - total_output_length,
4272 &function_output_length);
4273 TEST_LE_U(function_output_length,
4274 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4275 TEST_LE_U(function_output_length,
4276 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004277 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (expected_status == PSA_SUCCESS) {
4281 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 ASSERT_COMPARE(expected_output->x, expected_output->len,
4284 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004285 }
4286
Gilles Peskine50e586b2018-06-08 14:28:46 +02004287exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 psa_cipher_abort(&operation);
4289 mbedtls_free(output);
4290 psa_destroy_key(key);
4291 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004292}
4293/* END_CASE */
4294
Gilles Peskine50e586b2018-06-08 14:28:46 +02004295/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004296void cipher_decrypt_fail(int alg_arg,
4297 int key_type_arg,
4298 data_t *key_data,
4299 data_t *iv,
4300 data_t *input_arg,
4301 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004302{
4303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4304 psa_status_t status;
4305 psa_key_type_t key_type = key_type_arg;
4306 psa_algorithm_t alg = alg_arg;
4307 psa_status_t expected_status = expected_status_arg;
4308 unsigned char *input = NULL;
4309 size_t input_buffer_size = 0;
4310 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004311 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004312 size_t output_buffer_size = 0;
4313 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004314 size_t function_output_length;
4315 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (PSA_ERROR_BAD_STATE != expected_status) {
4319 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4322 psa_set_key_algorithm(&attributes, alg);
4323 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4326 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004327 }
4328
4329 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4331 if (input_buffer_size > 0) {
4332 ASSERT_ALLOC(input, input_buffer_size);
4333 memcpy(input, iv->x, iv->len);
4334 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004335 }
4336
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4338 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004339
Neil Armstrong66a479f2022-02-07 15:41:19 +01004340 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4342 output_buffer_size, &output_length);
4343 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004344
Neil Armstrong66a479f2022-02-07 15:41:19 +01004345 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 status = psa_cipher_decrypt_setup(&operation, key, alg);
4347 if (status == PSA_SUCCESS) {
4348 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4349 input_arg->len) +
4350 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4351 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (iv->len > 0) {
4354 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 if (status != PSA_SUCCESS) {
4357 TEST_EQUAL(status, expected_status);
4358 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004359 }
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 if (status == PSA_SUCCESS) {
4362 status = psa_cipher_update(&operation,
4363 input_arg->x, input_arg->len,
4364 output_multi, output_buffer_size,
4365 &function_output_length);
4366 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004367 output_length = function_output_length;
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 status = psa_cipher_finish(&operation,
4370 output_multi + output_length,
4371 output_buffer_size - output_length,
4372 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 TEST_EQUAL(status, expected_status);
4375 } else {
4376 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004377 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 } else {
4379 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004380 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 } else {
4382 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004383 }
4384
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 psa_cipher_abort(&operation);
4387 mbedtls_free(input);
4388 mbedtls_free(output);
4389 mbedtls_free(output_multi);
4390 psa_destroy_key(key);
4391 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004392}
4393/* END_CASE */
4394
4395/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004396void cipher_decrypt(int alg_arg,
4397 int key_type_arg,
4398 data_t *key_data,
4399 data_t *iv,
4400 data_t *input_arg,
4401 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004402{
4403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4404 psa_key_type_t key_type = key_type_arg;
4405 psa_algorithm_t alg = alg_arg;
4406 unsigned char *input = NULL;
4407 size_t input_buffer_size = 0;
4408 unsigned char *output = NULL;
4409 size_t output_buffer_size = 0;
4410 size_t output_length = 0;
4411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4416 psa_set_key_algorithm(&attributes, alg);
4417 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004418
4419 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4421 if (input_buffer_size > 0) {
4422 ASSERT_ALLOC(input, input_buffer_size);
4423 memcpy(input, iv->x, iv->len);
4424 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004425 }
4426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4428 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4431 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4434 output_buffer_size, &output_length));
4435 TEST_LE_U(output_length,
4436 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4437 TEST_LE_U(output_length,
4438 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004439
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 ASSERT_COMPARE(expected_output->x, expected_output->len,
4441 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004442exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 mbedtls_free(input);
4444 mbedtls_free(output);
4445 psa_destroy_key(key);
4446 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004447}
4448/* END_CASE */
4449
4450/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004451void cipher_verify_output(int alg_arg,
4452 int key_type_arg,
4453 data_t *key_data,
4454 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004455{
Ronald Cron5425a212020-08-04 14:58:35 +02004456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004457 psa_key_type_t key_type = key_type_arg;
4458 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004459 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004460 size_t output1_size = 0;
4461 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004462 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004463 size_t output2_size = 0;
4464 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4470 psa_set_key_algorithm(&attributes, alg);
4471 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4474 &key));
4475 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4476 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4479 output1, output1_size,
4480 &output1_length));
4481 TEST_LE_U(output1_length,
4482 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4483 TEST_LE_U(output1_length,
4484 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004485
4486 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004488
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4490 output2, output2_size,
4491 &output2_length));
4492 TEST_LE_U(output2_length,
4493 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4494 TEST_LE_U(output2_length,
4495 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004498
4499exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 mbedtls_free(output1);
4501 mbedtls_free(output2);
4502 psa_destroy_key(key);
4503 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004504}
4505/* END_CASE */
4506
4507/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004508void cipher_verify_output_multipart(int alg_arg,
4509 int key_type_arg,
4510 data_t *key_data,
4511 data_t *input,
4512 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004513{
Ronald Cron5425a212020-08-04 14:58:35 +02004514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004515 psa_key_type_t key_type = key_type_arg;
4516 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004517 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004519 size_t iv_size = 16;
4520 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004521 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004522 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004523 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004524 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004525 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004526 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004527 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004528 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4529 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4535 psa_set_key_algorithm(&attributes, alg);
4536 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4539 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4542 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 if (alg != PSA_ALG_ECB_NO_PADDING) {
4545 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4546 iv, iv_size,
4547 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004548 }
4549
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4551 TEST_LE_U(output1_buffer_size,
4552 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4553 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004556
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4558 output1, output1_buffer_size,
4559 &function_output_length));
4560 TEST_LE_U(function_output_length,
4561 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4562 TEST_LE_U(function_output_length,
4563 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004564 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 PSA_ASSERT(psa_cipher_update(&operation1,
4567 input->x + first_part_size,
4568 input->len - first_part_size,
4569 output1, output1_buffer_size,
4570 &function_output_length));
4571 TEST_LE_U(function_output_length,
4572 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4573 alg,
4574 input->len - first_part_size));
4575 TEST_LE_U(function_output_length,
4576 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004577 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 PSA_ASSERT(psa_cipher_finish(&operation1,
4580 output1 + output1_length,
4581 output1_buffer_size - output1_length,
4582 &function_output_length));
4583 TEST_LE_U(function_output_length,
4584 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4585 TEST_LE_U(function_output_length,
4586 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004587 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004590
Gilles Peskine048b7f02018-06-08 14:20:49 +02004591 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 TEST_LE_U(output2_buffer_size,
4593 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4594 TEST_LE_U(output2_buffer_size,
4595 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4596 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 if (iv_length > 0) {
4599 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4600 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004601 }
Moran Pekerded84402018-06-06 16:36:50 +03004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4604 output2, output2_buffer_size,
4605 &function_output_length));
4606 TEST_LE_U(function_output_length,
4607 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4608 TEST_LE_U(function_output_length,
4609 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004610 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 PSA_ASSERT(psa_cipher_update(&operation2,
4613 output1 + first_part_size,
4614 output1_length - first_part_size,
4615 output2, output2_buffer_size,
4616 &function_output_length));
4617 TEST_LE_U(function_output_length,
4618 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4619 alg,
4620 output1_length - first_part_size));
4621 TEST_LE_U(function_output_length,
4622 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004623 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 PSA_ASSERT(psa_cipher_finish(&operation2,
4626 output2 + output2_length,
4627 output2_buffer_size - output2_length,
4628 &function_output_length));
4629 TEST_LE_U(function_output_length,
4630 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4631 TEST_LE_U(function_output_length,
4632 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004633 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004638
4639exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 psa_cipher_abort(&operation1);
4641 psa_cipher_abort(&operation2);
4642 mbedtls_free(output1);
4643 mbedtls_free(output2);
4644 psa_destroy_key(key);
4645 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004646}
4647/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004648
Gilles Peskine20035e32018-02-03 22:44:14 +01004649/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004650void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4651 int alg_arg,
4652 data_t *nonce,
4653 data_t *additional_data,
4654 data_t *input_data,
4655 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004656{
Ronald Cron5425a212020-08-04 14:58:35 +02004657 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004658 psa_key_type_t key_type = key_type_arg;
4659 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004660 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004661 unsigned char *output_data = NULL;
4662 size_t output_size = 0;
4663 size_t output_length = 0;
4664 unsigned char *output_data2 = NULL;
4665 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004666 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004667 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4673 psa_set_key_algorithm(&attributes, alg);
4674 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4677 &key));
4678 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4679 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004680
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4682 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004683 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4684 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4686 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4687 TEST_EQUAL(output_size,
4688 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4689 TEST_LE_U(output_size,
4690 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004691 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 status = psa_aead_encrypt(key, alg,
4695 nonce->x, nonce->len,
4696 additional_data->x,
4697 additional_data->len,
4698 input_data->x, input_data->len,
4699 output_data, output_size,
4700 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004701
4702 /* If the operation is not supported, just skip and not fail in case the
4703 * encryption involves a common limitation of cryptography hardwares and
4704 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 if (status == PSA_ERROR_NOT_SUPPORTED) {
4706 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4707 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004708 }
4709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (PSA_SUCCESS == expected_result) {
4713 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004714
Gilles Peskine003a4a92019-05-14 16:09:40 +02004715 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4716 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 TEST_EQUAL(input_data->len,
4718 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 TEST_LE_U(input_data->len,
4721 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 TEST_EQUAL(psa_aead_decrypt(key, alg,
4724 nonce->x, nonce->len,
4725 additional_data->x,
4726 additional_data->len,
4727 output_data, output_length,
4728 output_data2, output_length,
4729 &output_length2),
4730 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 ASSERT_COMPARE(input_data->x, input_data->len,
4733 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004734 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004735
Gilles Peskinea1cac842018-06-11 19:33:02 +02004736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 psa_destroy_key(key);
4738 mbedtls_free(output_data);
4739 mbedtls_free(output_data2);
4740 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004741}
4742/* END_CASE */
4743
4744/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004745void aead_encrypt(int key_type_arg, data_t *key_data,
4746 int alg_arg,
4747 data_t *nonce,
4748 data_t *additional_data,
4749 data_t *input_data,
4750 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004751{
Ronald Cron5425a212020-08-04 14:58:35 +02004752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004753 psa_key_type_t key_type = key_type_arg;
4754 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004755 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004756 unsigned char *output_data = NULL;
4757 size_t output_size = 0;
4758 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004760 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4765 psa_set_key_algorithm(&attributes, alg);
4766 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4769 &key));
4770 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4771 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4774 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004775 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4776 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 TEST_EQUAL(output_size,
4778 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4779 TEST_LE_U(output_size,
4780 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4781 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 status = psa_aead_encrypt(key, alg,
4784 nonce->x, nonce->len,
4785 additional_data->x, additional_data->len,
4786 input_data->x, input_data->len,
4787 output_data, output_size,
4788 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789
Ronald Cron28a45ed2021-02-09 20:35:42 +01004790 /* If the operation is not supported, just skip and not fail in case the
4791 * encryption involves a common limitation of cryptography hardwares and
4792 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 if (status == PSA_ERROR_NOT_SUPPORTED) {
4794 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4795 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004796 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 PSA_ASSERT(status);
4799 ASSERT_COMPARE(expected_result->x, expected_result->len,
4800 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004801
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 psa_destroy_key(key);
4804 mbedtls_free(output_data);
4805 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806}
4807/* END_CASE */
4808
4809/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004810void aead_decrypt(int key_type_arg, data_t *key_data,
4811 int alg_arg,
4812 data_t *nonce,
4813 data_t *additional_data,
4814 data_t *input_data,
4815 data_t *expected_data,
4816 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817{
Ronald Cron5425a212020-08-04 14:58:35 +02004818 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004819 psa_key_type_t key_type = key_type_arg;
4820 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004821 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004822 unsigned char *output_data = NULL;
4823 size_t output_size = 0;
4824 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004826 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004827 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4832 psa_set_key_algorithm(&attributes, alg);
4833 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4836 &key));
4837 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4838 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4841 alg);
4842 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4843 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004844 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4845 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 TEST_EQUAL(output_size,
4847 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4848 TEST_LE_U(output_size,
4849 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004850 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 status = psa_aead_decrypt(key, alg,
4854 nonce->x, nonce->len,
4855 additional_data->x,
4856 additional_data->len,
4857 input_data->x, input_data->len,
4858 output_data, output_size,
4859 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004860
Ronald Cron28a45ed2021-02-09 20:35:42 +01004861 /* If the operation is not supported, just skip and not fail in case the
4862 * decryption involves a common limitation of cryptography hardwares and
4863 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (status == PSA_ERROR_NOT_SUPPORTED) {
4865 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4866 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004867 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (expected_result == PSA_SUCCESS) {
4872 ASSERT_COMPARE(expected_data->x, expected_data->len,
4873 output_data, output_length);
4874 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004875
Gilles Peskinea1cac842018-06-11 19:33:02 +02004876exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 psa_destroy_key(key);
4878 mbedtls_free(output_data);
4879 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880}
4881/* END_CASE */
4882
4883/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004884void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4885 int alg_arg,
4886 data_t *nonce,
4887 data_t *additional_data,
4888 data_t *input_data,
4889 int do_set_lengths,
4890 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004891{
Paul Elliottd3f82412021-06-16 16:52:21 +01004892 size_t ad_part_len = 0;
4893 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004894 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004895
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4897 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 if (do_set_lengths) {
4900 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004901 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004903 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004905 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004906
4907 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (!aead_multipart_internal_func(key_type_arg, key_data,
4909 alg_arg, nonce,
4910 additional_data,
4911 ad_part_len,
4912 input_data, -1,
4913 set_lengths_method,
4914 expected_output,
4915 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004916 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004917 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 /* length(0) part, length(ad_part_len) part, length(0) part... */
4920 mbedtls_test_set_step(1000 + ad_part_len);
4921
4922 if (!aead_multipart_internal_func(key_type_arg, key_data,
4923 alg_arg, nonce,
4924 additional_data,
4925 ad_part_len,
4926 input_data, -1,
4927 set_lengths_method,
4928 expected_output,
4929 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004930 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 }
4932 }
4933
4934 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4935 /* Split data into length(data_part_len) parts. */
4936 mbedtls_test_set_step(2000 + data_part_len);
4937
4938 if (do_set_lengths) {
4939 if (data_part_len & 0x01) {
4940 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4941 } else {
4942 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4943 }
4944 }
4945
4946 if (!aead_multipart_internal_func(key_type_arg, key_data,
4947 alg_arg, nonce,
4948 additional_data, -1,
4949 input_data, data_part_len,
4950 set_lengths_method,
4951 expected_output,
4952 1, 0)) {
4953 break;
4954 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004955
4956 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 if (!aead_multipart_internal_func(key_type_arg, key_data,
4960 alg_arg, nonce,
4961 additional_data, -1,
4962 input_data, data_part_len,
4963 set_lengths_method,
4964 expected_output,
4965 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004966 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004968 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004969
Paul Elliott8fc45162021-06-23 16:06:01 +01004970 /* Goto is required to silence warnings about unused labels, as we
4971 * don't actually do any test assertions in this function. */
4972 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004973}
4974/* END_CASE */
4975
4976/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004977void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4978 int alg_arg,
4979 data_t *nonce,
4980 data_t *additional_data,
4981 data_t *input_data,
4982 int do_set_lengths,
4983 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004984{
Paul Elliottd3f82412021-06-16 16:52:21 +01004985 size_t ad_part_len = 0;
4986 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004987 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004990 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 if (do_set_lengths) {
4994 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004995 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004997 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004999 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 if (!aead_multipart_internal_func(key_type_arg, key_data,
5002 alg_arg, nonce,
5003 additional_data,
5004 ad_part_len,
5005 input_data, -1,
5006 set_lengths_method,
5007 expected_output,
5008 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005011
5012 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 if (!aead_multipart_internal_func(key_type_arg, key_data,
5016 alg_arg, nonce,
5017 additional_data,
5018 ad_part_len,
5019 input_data, -1,
5020 set_lengths_method,
5021 expected_output,
5022 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005025 }
5026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005028 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005030
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 if (do_set_lengths) {
5032 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005033 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005035 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005037 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005038
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 if (!aead_multipart_internal_func(key_type_arg, key_data,
5040 alg_arg, nonce,
5041 additional_data, -1,
5042 input_data, data_part_len,
5043 set_lengths_method,
5044 expected_output,
5045 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005046 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005048
5049 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (!aead_multipart_internal_func(key_type_arg, key_data,
5053 alg_arg, nonce,
5054 additional_data, -1,
5055 input_data, data_part_len,
5056 set_lengths_method,
5057 expected_output,
5058 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005059 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005061 }
5062
Paul Elliott8fc45162021-06-23 16:06:01 +01005063 /* Goto is required to silence warnings about unused labels, as we
5064 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005065 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005066}
5067/* END_CASE */
5068
5069/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005070void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5071 int alg_arg,
5072 int nonce_length,
5073 int expected_nonce_length_arg,
5074 data_t *additional_data,
5075 data_t *input_data,
5076 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005077{
5078
5079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5080 psa_key_type_t key_type = key_type_arg;
5081 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005082 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005083 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5085 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005086 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005087 size_t actual_nonce_length = 0;
5088 size_t expected_nonce_length = expected_nonce_length_arg;
5089 unsigned char *output = NULL;
5090 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005091 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005092 size_t ciphertext_size = 0;
5093 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005094 size_t tag_length = 0;
5095 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5100 psa_set_key_algorithm(&attributes, alg);
5101 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5104 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005119
5120 /* If the operation is not supported, just skip and not fail in case the
5121 * encryption involves a common limitation of cryptography hardwares and
5122 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (status == PSA_ERROR_NOT_SUPPORTED) {
5124 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5125 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005126 }
5127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5131 nonce_length,
5132 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 if (expected_status == PSA_SUCCESS) {
5139 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5140 alg));
5141 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005146 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5148 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5151 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5154 output, output_size,
5155 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5158 &ciphertext_length, tag_buffer,
5159 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005160 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005161
5162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 psa_destroy_key(key);
5164 mbedtls_free(output);
5165 mbedtls_free(ciphertext);
5166 psa_aead_abort(&operation);
5167 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005168}
5169/* END_CASE */
5170
5171/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005172void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5173 int alg_arg,
5174 int nonce_length_arg,
5175 int set_lengths_method_arg,
5176 data_t *additional_data,
5177 data_t *input_data,
5178 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005179{
5180
5181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5182 psa_key_type_t key_type = key_type_arg;
5183 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005184 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005185 uint8_t *nonce_buffer = NULL;
5186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5187 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5188 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005189 unsigned char *output = NULL;
5190 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005191 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005192 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005193 size_t ciphertext_size = 0;
5194 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005195 size_t tag_length = 0;
5196 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005197 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005198 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005199
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5203 psa_set_key_algorithm(&attributes, alg);
5204 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5207 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005208
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005210
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005212
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005216
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005218
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005222
5223 /* If the operation is not supported, just skip and not fail in case the
5224 * encryption involves a common limitation of cryptography hardwares and
5225 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (status == PSA_ERROR_NOT_SUPPORTED) {
5227 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5228 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005229 }
5230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005232
Paul Elliott4023ffd2021-09-10 16:21:22 +01005233 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 if (nonce_length_arg == -1) {
5235 /* Arbitrary size buffer, to test zero length valid buffer. */
5236 ASSERT_ALLOC(nonce_buffer, 4);
5237 nonce_length = 0;
5238 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005239 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 nonce_length = (size_t) nonce_length_arg;
5241 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (nonce_buffer) {
5244 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005245 nonce_buffer[index] = 'a' + index;
5246 }
Paul Elliott66696b52021-08-16 18:42:41 +01005247 }
Paul Elliott863864a2021-07-23 17:28:31 +01005248 }
5249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5251 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5252 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005253 }
5254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 if (expected_status == PSA_SUCCESS) {
5260 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5261 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5262 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005263 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005265 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 }
Paul Elliott863864a2021-07-23 17:28:31 +01005267
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005268 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5270 additional_data->len),
5271 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5274 output, output_size,
5275 &ciphertext_length),
5276 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5279 &ciphertext_length, tag_buffer,
5280 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5281 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005282 }
5283
5284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 psa_destroy_key(key);
5286 mbedtls_free(output);
5287 mbedtls_free(ciphertext);
5288 mbedtls_free(nonce_buffer);
5289 psa_aead_abort(&operation);
5290 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005291}
5292/* END_CASE */
5293
5294/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005295void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005296 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005297 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005298 data_t *nonce,
5299 data_t *additional_data,
5300 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005302{
5303
5304 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5305 psa_key_type_t key_type = key_type_arg;
5306 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005307 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5309 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5310 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005311 unsigned char *output = NULL;
5312 unsigned char *ciphertext = NULL;
5313 size_t output_size = output_size_arg;
5314 size_t ciphertext_size = 0;
5315 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005316 size_t tag_length = 0;
5317 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005320
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5322 psa_set_key_algorithm(&attributes, alg);
5323 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5326 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005329
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005331
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005337
5338 /* If the operation is not supported, just skip and not fail in case the
5339 * encryption involves a common limitation of cryptography hardwares and
5340 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (status == PSA_ERROR_NOT_SUPPORTED) {
5342 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5343 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005344 }
5345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5349 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5354 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 status = psa_aead_update(&operation, input_data->x, input_data->len,
5357 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005362 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5364 &ciphertext_length, tag_buffer,
5365 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005366 }
5367
5368exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 psa_destroy_key(key);
5370 mbedtls_free(output);
5371 mbedtls_free(ciphertext);
5372 psa_aead_abort(&operation);
5373 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005374}
5375/* END_CASE */
5376
Paul Elliott91b021e2021-07-23 18:52:31 +01005377/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005378void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5379 int alg_arg,
5380 int finish_ciphertext_size_arg,
5381 int tag_size_arg,
5382 data_t *nonce,
5383 data_t *additional_data,
5384 data_t *input_data,
5385 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005386{
5387
5388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5389 psa_key_type_t key_type = key_type_arg;
5390 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005391 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5393 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5394 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005395 unsigned char *ciphertext = NULL;
5396 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005397 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005398 size_t ciphertext_size = 0;
5399 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5401 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005402 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5407 psa_set_key_algorithm(&attributes, alg);
5408 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5411 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005424
5425 /* If the operation is not supported, just skip and not fail in case the
5426 * encryption involves a common limitation of cryptography hardwares and
5427 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 if (status == PSA_ERROR_NOT_SUPPORTED) {
5429 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5430 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005431 }
5432
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5438 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5441 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5444 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005445
5446 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 status = psa_aead_finish(&operation, finish_ciphertext,
5448 finish_ciphertext_size,
5449 &ciphertext_length, tag_buffer,
5450 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
5454exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 psa_destroy_key(key);
5456 mbedtls_free(ciphertext);
5457 mbedtls_free(finish_ciphertext);
5458 mbedtls_free(tag_buffer);
5459 psa_aead_abort(&operation);
5460 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005461}
5462/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005463
5464/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005465void aead_multipart_verify(int key_type_arg, data_t *key_data,
5466 int alg_arg,
5467 data_t *nonce,
5468 data_t *additional_data,
5469 data_t *input_data,
5470 data_t *tag,
5471 int tag_usage_arg,
5472 int expected_setup_status_arg,
5473 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005474{
5475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5476 psa_key_type_t key_type = key_type_arg;
5477 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005478 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5480 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5481 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005482 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005483 unsigned char *plaintext = NULL;
5484 unsigned char *finish_plaintext = NULL;
5485 size_t plaintext_size = 0;
5486 size_t plaintext_length = 0;
5487 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005488 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005489 unsigned char *tag_buffer = NULL;
5490 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005491
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005493
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5495 psa_set_key_algorithm(&attributes, alg);
5496 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005497
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5499 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5504 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005505
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005513
5514 /* If the operation is not supported, just skip and not fail in case the
5515 * encryption involves a common limitation of cryptography hardwares and
5516 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 if (status == PSA_ERROR_NOT_SUPPORTED) {
5518 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5519 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005520 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005524 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 }
Paul Elliott9961a662021-09-17 19:19:02 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 status = psa_aead_set_lengths(&operation, additional_data->len,
5532 input_data->len);
5533 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5536 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5539 input_data->len,
5540 plaintext, plaintext_size,
5541 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005544 tag_buffer = tag->x;
5545 tag_size = tag->len;
5546 }
5547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 status = psa_aead_verify(&operation, finish_plaintext,
5549 verify_plaintext_size,
5550 &plaintext_length,
5551 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005554
5555exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 psa_destroy_key(key);
5557 mbedtls_free(plaintext);
5558 mbedtls_free(finish_plaintext);
5559 psa_aead_abort(&operation);
5560 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005561}
5562/* END_CASE */
5563
Paul Elliott9961a662021-09-17 19:19:02 +01005564/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005565void aead_multipart_setup(int key_type_arg, data_t *key_data,
5566 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005567{
5568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5569 psa_key_type_t key_type = key_type_arg;
5570 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005571 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5573 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5574 psa_status_t expected_status = expected_status_arg;
5575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 psa_set_key_usage_flags(&attributes,
5579 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5580 psa_set_key_algorithm(&attributes, alg);
5581 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5584 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005587
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005595
5596exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 psa_destroy_key(key);
5598 psa_aead_abort(&operation);
5599 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005600}
5601/* END_CASE */
5602
5603/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005604void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5605 int alg_arg,
5606 data_t *nonce,
5607 data_t *additional_data,
5608 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005609{
5610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5611 psa_key_type_t key_type = key_type_arg;
5612 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005613 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005614 unsigned char *output_data = NULL;
5615 unsigned char *final_data = NULL;
5616 size_t output_size = 0;
5617 size_t finish_output_size = 0;
5618 size_t output_length = 0;
5619 size_t key_bits = 0;
5620 size_t tag_length = 0;
5621 size_t tag_size = 0;
5622 size_t nonce_length = 0;
5623 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5624 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5625 size_t output_part_length = 0;
5626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005629
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 psa_set_key_usage_flags(&attributes,
5631 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5632 psa_set_key_algorithm(&attributes, alg);
5633 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5636 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005637
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5639 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005644
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005652
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005654
5655 /* Test all operations error without calling setup first. */
5656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5658 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5663 PSA_AEAD_NONCE_MAX_SIZE,
5664 &nonce_length),
5665 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005668
Paul Elliott481be342021-07-16 17:38:47 +01005669 /* ------------------------------------------------------- */
5670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5672 input_data->len),
5673 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Paul Elliott481be342021-07-16 17:38:47 +01005677 /* ------------------------------------------------------- */
5678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5680 additional_data->len),
5681 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005684
Paul Elliott481be342021-07-16 17:38:47 +01005685 /* ------------------------------------------------------- */
5686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5688 input_data->len, output_data,
5689 output_size, &output_length),
5690 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
Paul Elliott481be342021-07-16 17:38:47 +01005694 /* ------------------------------------------------------- */
5695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5697 finish_output_size,
5698 &output_part_length,
5699 tag_buffer, tag_length,
5700 &tag_size),
5701 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005704
Paul Elliott481be342021-07-16 17:38:47 +01005705 /* ------------------------------------------------------- */
5706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5708 finish_output_size,
5709 &output_part_length,
5710 tag_buffer,
5711 tag_length),
5712 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
5716 /* Test for double setups. */
5717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5721 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724
Paul Elliott481be342021-07-16 17:38:47 +01005725 /* ------------------------------------------------------- */
5726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5730 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Paul Elliott374a2be2021-07-16 17:53:40 +01005734 /* ------------------------------------------------------- */
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5739 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005742
5743 /* ------------------------------------------------------- */
5744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5748 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005751
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752 /* Test for not setting a nonce. */
5753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5757 additional_data->len),
5758 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005761
Paul Elliott7f628422021-09-01 12:08:29 +01005762 /* ------------------------------------------------------- */
5763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005765
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5767 input_data->len, output_data,
5768 output_size, &output_length),
5769 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005772
Paul Elliottbdc2c682021-09-21 18:37:10 +01005773 /* ------------------------------------------------------- */
5774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5778 finish_output_size,
5779 &output_part_length,
5780 tag_buffer, tag_length,
5781 &tag_size),
5782 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005785
5786 /* ------------------------------------------------------- */
5787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5791 finish_output_size,
5792 &output_part_length,
5793 tag_buffer,
5794 tag_length),
5795 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005798
Paul Elliottc23a9a02021-06-21 18:32:46 +01005799 /* Test for double setting nonce. */
5800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5806 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005809
Paul Elliott374a2be2021-07-16 17:53:40 +01005810 /* Test for double generating nonce. */
5811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005813
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5815 PSA_AEAD_NONCE_MAX_SIZE,
5816 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5819 PSA_AEAD_NONCE_MAX_SIZE,
5820 &nonce_length),
5821 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005822
5823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005825
5826 /* Test for generate nonce then set and vice versa */
5827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5831 PSA_AEAD_NONCE_MAX_SIZE,
5832 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5835 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005838
Andrzej Kurekad837522021-12-15 15:28:49 +01005839 /* Test for generating nonce after calling set lengths */
5840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5844 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5847 PSA_AEAD_NONCE_MAX_SIZE,
5848 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005851
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005852 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 if (operation.alg == PSA_ALG_CCM) {
5857 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5858 input_data->len),
5859 PSA_ERROR_INVALID_ARGUMENT);
5860 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5861 PSA_AEAD_NONCE_MAX_SIZE,
5862 &nonce_length),
5863 PSA_ERROR_BAD_STATE);
5864 } else {
5865 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5866 input_data->len));
5867 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5868 PSA_AEAD_NONCE_MAX_SIZE,
5869 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005870 }
5871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005873
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005874 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005875#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5879 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5880 input_data->len),
5881 PSA_ERROR_INVALID_ARGUMENT);
5882 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5883 PSA_AEAD_NONCE_MAX_SIZE,
5884 &nonce_length),
5885 PSA_ERROR_BAD_STATE);
5886 } else {
5887 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5888 input_data->len));
5889 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5890 PSA_AEAD_NONCE_MAX_SIZE,
5891 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005892 }
5893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005895#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005896
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005897 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005900
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5902 PSA_AEAD_NONCE_MAX_SIZE,
5903 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 if (operation.alg == PSA_ALG_CCM) {
5906 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5907 input_data->len),
5908 PSA_ERROR_INVALID_ARGUMENT);
5909 } else {
5910 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5911 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005912 }
5913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005915
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005916 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005917 /* Test for setting nonce after calling set lengths */
5918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005920
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5922 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005927
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005928 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (operation.alg == PSA_ALG_CCM) {
5933 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5934 input_data->len),
5935 PSA_ERROR_INVALID_ARGUMENT);
5936 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5937 PSA_ERROR_BAD_STATE);
5938 } else {
5939 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5940 input_data->len));
5941 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005942 }
5943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005945
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005946 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005947#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005949
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5951 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5952 input_data->len),
5953 PSA_ERROR_INVALID_ARGUMENT);
5954 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5955 PSA_ERROR_BAD_STATE);
5956 } else {
5957 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5958 input_data->len));
5959 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005960 }
5961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005963#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005965 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 if (operation.alg == PSA_ALG_CCM) {
5972 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5973 input_data->len),
5974 PSA_ERROR_INVALID_ARGUMENT);
5975 } else {
5976 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5977 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005978 }
5979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005981
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005982 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00005983#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (operation.alg == PSA_ALG_GCM) {
5987 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5988 SIZE_MAX),
5989 PSA_ERROR_INVALID_ARGUMENT);
5990 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5991 PSA_ERROR_BAD_STATE);
5992 } else if (operation.alg != PSA_ALG_CCM) {
5993 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5994 SIZE_MAX));
5995 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005996 }
5997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00005999#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006000
Tom Cosgrove1797b052022-12-04 17:19:59 +00006001 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006002#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006004
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006006
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 if (operation.alg == PSA_ALG_GCM) {
6008 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6009 SIZE_MAX),
6010 PSA_ERROR_INVALID_ARGUMENT);
6011 } else if (operation.alg != PSA_ALG_CCM) {
6012 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6013 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006014 }
6015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006017#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006018
6019 /* ------------------------------------------------------- */
6020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6026 PSA_AEAD_NONCE_MAX_SIZE,
6027 &nonce_length),
6028 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006031
Paul Elliott7220cae2021-06-22 17:25:57 +01006032 /* Test for generating nonce in decrypt setup. */
6033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6037 PSA_AEAD_NONCE_MAX_SIZE,
6038 &nonce_length),
6039 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006042
Paul Elliottc23a9a02021-06-21 18:32:46 +01006043 /* Test for setting lengths twice. */
6044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6050 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6053 input_data->len),
6054 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006057
Andrzej Kurekad837522021-12-15 15:28:49 +01006058 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6067 additional_data->len),
6068 PSA_ERROR_BAD_STATE);
6069 } else {
6070 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6071 additional_data->len));
6072
6073 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6074 input_data->len),
6075 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006078
6079 /* ------------------------------------------------------- */
6080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (operation.alg == PSA_ALG_CCM) {
6086 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6087 input_data->len, output_data,
6088 output_size, &output_length),
6089 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 } else {
6092 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6093 input_data->len, output_data,
6094 output_size, &output_length));
6095
6096 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6097 input_data->len),
6098 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006101
6102 /* ------------------------------------------------------- */
6103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (operation.alg == PSA_ALG_CCM) {
6109 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6110 finish_output_size,
6111 &output_part_length,
6112 tag_buffer, tag_length,
6113 &tag_size));
6114 } else {
6115 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6116 finish_output_size,
6117 &output_part_length,
6118 tag_buffer, tag_length,
6119 &tag_size));
6120
6121 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6122 input_data->len),
6123 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006124 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006126
6127 /* Test for setting lengths after generating nonce + already starting data. */
6128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6132 PSA_AEAD_NONCE_MAX_SIZE,
6133 &nonce_length));
6134 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6137 additional_data->len),
6138 PSA_ERROR_BAD_STATE);
6139 } else {
6140 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6141 additional_data->len));
6142
6143 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6144 input_data->len),
6145 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006146 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006148
6149 /* ------------------------------------------------------- */
6150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6154 PSA_AEAD_NONCE_MAX_SIZE,
6155 &nonce_length));
6156 if (operation.alg == PSA_ALG_CCM) {
6157 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6158 input_data->len, output_data,
6159 output_size, &output_length),
6160 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 } else {
6163 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6164 input_data->len, output_data,
6165 output_size, &output_length));
6166
6167 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6168 input_data->len),
6169 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006170 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006172
6173 /* ------------------------------------------------------- */
6174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6178 PSA_AEAD_NONCE_MAX_SIZE,
6179 &nonce_length));
6180 if (operation.alg == PSA_ALG_CCM) {
6181 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6182 finish_output_size,
6183 &output_part_length,
6184 tag_buffer, tag_length,
6185 &tag_size));
6186 } else {
6187 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6188 finish_output_size,
6189 &output_part_length,
6190 tag_buffer, tag_length,
6191 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6194 input_data->len),
6195 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006196 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006198
Paul Elliott243080c2021-07-21 19:01:17 +01006199 /* Test for not sending any additional data or data after setting non zero
6200 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6207 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6210 finish_output_size,
6211 &output_part_length,
6212 tag_buffer, tag_length,
6213 &tag_size),
6214 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006217
Paul Elliott243080c2021-07-21 19:01:17 +01006218 /* Test for not sending any additional data or data after setting non-zero
6219 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006222
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6226 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6229 finish_output_size,
6230 &output_part_length,
6231 tag_buffer,
6232 tag_length),
6233 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Paul Elliott243080c2021-07-21 19:01:17 +01006237 /* Test for not sending any additional data after setting a non-zero length
6238 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6245 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6248 input_data->len, output_data,
6249 output_size, &output_length),
6250 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006251
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006253
Paul Elliottf94bd992021-09-19 18:15:59 +01006254 /* Test for not sending any data after setting a non-zero length for it.*/
6255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6261 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6264 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6267 finish_output_size,
6268 &output_part_length,
6269 tag_buffer, tag_length,
6270 &tag_size),
6271 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006274
Paul Elliottb0450fe2021-09-01 15:06:26 +01006275 /* Test for sending too much additional data after setting lengths. */
6276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006282
6283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6285 additional_data->len),
6286 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006289
Paul Elliotta2a09b02021-09-22 14:56:40 +01006290 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6297 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6300 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6303 1),
6304 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006307
Paul Elliottb0450fe2021-09-01 15:06:26 +01006308 /* Test for sending too much data after setting lengths. */
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6317 input_data->len, output_data,
6318 output_size, &output_length),
6319 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006322
Paul Elliotta2a09b02021-09-22 14:56:40 +01006323 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6330 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6333 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6336 input_data->len, output_data,
6337 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6340 1, output_data,
6341 output_size, &output_length),
6342 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006345
Paul Elliottc23a9a02021-06-21 18:32:46 +01006346 /* Test sending additional data after data. */
6347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 if (operation.alg != PSA_ALG_CCM) {
6353 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6354 input_data->len, output_data,
6355 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6358 additional_data->len),
6359 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006360 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006362
Paul Elliott534d0b42021-06-22 19:15:20 +01006363 /* Test calling finish on decryption. */
6364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6370 finish_output_size,
6371 &output_part_length,
6372 tag_buffer, tag_length,
6373 &tag_size),
6374 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006377
6378 /* Test calling verify on encryption. */
6379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6385 finish_output_size,
6386 &output_part_length,
6387 tag_buffer,
6388 tag_length),
6389 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006392
6393
Paul Elliottc23a9a02021-06-21 18:32:46 +01006394exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 psa_destroy_key(key);
6396 psa_aead_abort(&operation);
6397 mbedtls_free(output_data);
6398 mbedtls_free(final_data);
6399 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006400}
6401/* END_CASE */
6402
6403/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006404void signature_size(int type_arg,
6405 int bits,
6406 int alg_arg,
6407 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006408{
6409 psa_key_type_t type = type_arg;
6410 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006414
Gilles Peskinee59236f2018-01-27 23:32:46 +01006415exit:
6416 ;
6417}
6418/* END_CASE */
6419
6420/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006421void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6422 int alg_arg, data_t *input_data,
6423 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006424{
Ronald Cron5425a212020-08-04 14:58:35 +02006425 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006426 psa_key_type_t key_type = key_type_arg;
6427 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006428 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006429 unsigned char *signature = NULL;
6430 size_t signature_size;
6431 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6437 psa_set_key_algorithm(&attributes, alg);
6438 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6441 &key));
6442 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6443 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006444
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006445 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006446 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6448 key_bits, alg);
6449 TEST_ASSERT(signature_size != 0);
6450 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6451 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006452
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006453 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 PSA_ASSERT(psa_sign_hash(key, alg,
6455 input_data->x, input_data->len,
6456 signature, signature_size,
6457 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006458 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 ASSERT_COMPARE(output_data->x, output_data->len,
6460 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006461
6462exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006463 /*
6464 * Key attributes may have been returned by psa_get_key_attributes()
6465 * thus reset them as required.
6466 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 psa_destroy_key(key);
6470 mbedtls_free(signature);
6471 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006472}
6473/* END_CASE */
6474
Paul Elliott712d5122022-12-07 14:03:10 +00006475/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006476/**
6477 * sign_hash_interruptible() test intentions:
6478 *
6479 * Note: This test can currently only handle ECDSA.
6480 *
6481 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
6482 * only).
6483 *
6484 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6485 * expected for different max_ops values.
6486 *
6487 * 3. Test that the number of ops done prior to start and after abort is zero
6488 * and that each successful stage completes some ops (this is not mandated by
6489 * the PSA specification, but is currently the case).
6490 *
6491 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6492 * complete() calls does not alter the number of ops returned.
6493 */
Paul Elliott712d5122022-12-07 14:03:10 +00006494void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6495 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006496 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006497{
6498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6499 psa_key_type_t key_type = key_type_arg;
6500 psa_algorithm_t alg = alg_arg;
6501 size_t key_bits;
6502 unsigned char *signature = NULL;
6503 size_t signature_size;
6504 size_t signature_length = 0xdeadbeef;
6505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6506 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006507 uint32_t num_ops = 0;
6508 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006509 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006510 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006511 size_t min_completes = 0;
6512 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006513
Paul Elliott712d5122022-12-07 14:03:10 +00006514 psa_sign_hash_interruptible_operation_t operation =
6515 psa_sign_hash_interruptible_operation_init();
6516
6517 PSA_ASSERT(psa_crypto_init());
6518
6519 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6520 psa_set_key_algorithm(&attributes, alg);
6521 psa_set_key_type(&attributes, key_type);
6522
6523 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6524 &key));
6525 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6526 key_bits = psa_get_key_bits(&attributes);
6527
6528 /* Allocate a buffer which has the size advertised by the
6529 * library. */
6530 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6531 key_bits, alg);
6532 TEST_ASSERT(signature_size != 0);
6533 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6534 ASSERT_ALLOC(signature, signature_size);
6535
Paul Elliott0c683352022-12-16 19:16:56 +00006536 psa_interruptible_set_max_ops(max_ops);
6537
Paul Elliott6f600372023-02-06 18:41:05 +00006538 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6539 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006540
Paul Elliott712d5122022-12-07 14:03:10 +00006541 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6542 TEST_ASSERT(num_ops_prior == 0);
6543
6544 /* Start performing the signature. */
6545 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6546 input_data->x, input_data->len));
6547
6548 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6549 TEST_ASSERT(num_ops_prior == 0);
6550
6551 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006552 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006553 status = psa_sign_hash_complete(&operation, signature, signature_size,
6554 &signature_length);
6555
Paul Elliott0c683352022-12-16 19:16:56 +00006556 num_completes++;
6557
Paul Elliott712d5122022-12-07 14:03:10 +00006558 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6559 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006560 /* We are asserting here that every complete makes progress
6561 * (completes some ops), which is true of the internal
6562 * implementation and probably any implementation, however this is
6563 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006564 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006565
Paul Elliott712d5122022-12-07 14:03:10 +00006566 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006567
6568 /* Ensure calling get_num_ops() twice still returns the same
6569 * number of ops as previously reported. */
6570 num_ops = psa_sign_hash_get_num_ops(&operation);
6571
6572 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006573 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006574 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006575
6576 TEST_ASSERT(status == PSA_SUCCESS);
6577
Paul Elliott0c683352022-12-16 19:16:56 +00006578 TEST_LE_U(min_completes, num_completes);
6579 TEST_LE_U(num_completes, max_completes);
6580
Paul Elliott712d5122022-12-07 14:03:10 +00006581 /* Verify that the signature is what is expected. */
6582 ASSERT_COMPARE(output_data->x, output_data->len,
6583 signature, signature_length);
6584
6585 PSA_ASSERT(psa_sign_hash_abort(&operation));
6586
Paul Elliott59ad9452022-12-18 15:09:02 +00006587 num_ops = psa_sign_hash_get_num_ops(&operation);
6588 TEST_ASSERT(num_ops == 0);
6589
Paul Elliott712d5122022-12-07 14:03:10 +00006590exit:
6591
6592 /*
6593 * Key attributes may have been returned by psa_get_key_attributes()
6594 * thus reset them as required.
6595 */
6596 psa_reset_key_attributes(&attributes);
6597
6598 psa_destroy_key(key);
6599 mbedtls_free(signature);
6600 PSA_DONE();
6601}
6602/* END_CASE */
6603
Gilles Peskine20035e32018-02-03 22:44:14 +01006604/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006605void sign_hash_fail(int key_type_arg, data_t *key_data,
6606 int alg_arg, data_t *input_data,
6607 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006608{
Ronald Cron5425a212020-08-04 14:58:35 +02006609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006610 psa_key_type_t key_type = key_type_arg;
6611 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006612 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006613 psa_status_t actual_status;
6614 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006615 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006616 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006618
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6624 psa_set_key_algorithm(&attributes, alg);
6625 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6628 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006629
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 actual_status = psa_sign_hash(key, alg,
6631 input_data->x, input_data->len,
6632 signature, signature_size,
6633 &signature_length);
6634 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006635 /* The value of *signature_length is unspecified on error, but
6636 * whatever it is, it should be less than signature_size, so that
6637 * if the caller tries to read *signature_length bytes without
6638 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006640
6641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 psa_reset_key_attributes(&attributes);
6643 psa_destroy_key(key);
6644 mbedtls_free(signature);
6645 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006646}
6647/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006648
Paul Elliott91007972022-12-16 12:21:24 +00006649/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006650/**
6651 * sign_hash_fail_interruptible() test intentions:
6652 *
6653 * Note: This test can currently only handle ECDSA.
6654 *
6655 * 1. Test that various failure cases for interruptible sign hash fail with the
6656 * correct error codes, and at the correct point (at start or during
6657 * complete).
6658 *
6659 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6660 * expected for different max_ops values.
6661 *
6662 * 3. Test that the number of ops done prior to start and after abort is zero
6663 * and that each successful stage completes some ops (this is not mandated by
6664 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006665 *
6666 * 4. Check that calling complete() when start() fails and complete()
6667 * after completion results in a BAD_STATE error.
6668 *
6669 * 5. Check that calling start() again after start fails results in a BAD_STATE
6670 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006671 */
Paul Elliott91007972022-12-16 12:21:24 +00006672void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6673 int alg_arg, data_t *input_data,
6674 int signature_size_arg,
6675 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006676 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006677 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006678{
6679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6680 psa_key_type_t key_type = key_type_arg;
6681 psa_algorithm_t alg = alg_arg;
6682 size_t signature_size = signature_size_arg;
6683 psa_status_t actual_status;
6684 psa_status_t expected_start_status = expected_start_status_arg;
6685 psa_status_t expected_complete_status = expected_complete_status_arg;
6686 unsigned char *signature = NULL;
6687 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006688 uint32_t num_ops = 0;
6689 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006690 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006691 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006692 size_t min_completes = 0;
6693 size_t max_completes = 0;
6694
Paul Elliott91007972022-12-16 12:21:24 +00006695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6696 psa_sign_hash_interruptible_operation_t operation =
6697 psa_sign_hash_interruptible_operation_init();
6698
6699 ASSERT_ALLOC(signature, signature_size);
6700
6701 PSA_ASSERT(psa_crypto_init());
6702
6703 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6704 psa_set_key_algorithm(&attributes, alg);
6705 psa_set_key_type(&attributes, key_type);
6706
6707 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6708 &key));
6709
Paul Elliott0c683352022-12-16 19:16:56 +00006710 psa_interruptible_set_max_ops(max_ops);
6711
Paul Elliott6f600372023-02-06 18:41:05 +00006712 interruptible_signverify_get_minmax_completes(max_ops,
6713 expected_complete_status,
6714 &min_completes,
6715 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006716
Paul Elliott91007972022-12-16 12:21:24 +00006717 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6718 TEST_ASSERT(num_ops_prior == 0);
6719
6720 /* Start performing the signature. */
6721 actual_status = psa_sign_hash_start(&operation, key, alg,
6722 input_data->x, input_data->len);
6723
6724 TEST_EQUAL(actual_status, expected_start_status);
6725
Paul Elliottc9774412023-02-06 15:14:07 +00006726 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006727 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006728 * start failed. */
6729 actual_status = psa_sign_hash_complete(&operation, signature,
6730 signature_size,
6731 &signature_length);
6732
6733 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6734
6735 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006736 actual_status = psa_sign_hash_start(&operation, key, alg,
6737 input_data->x, input_data->len);
6738
6739 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6740 }
6741
Paul Elliott91007972022-12-16 12:21:24 +00006742 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6743 TEST_ASSERT(num_ops_prior == 0);
6744
Paul Elliott91007972022-12-16 12:21:24 +00006745 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006746 do {
Paul Elliott91007972022-12-16 12:21:24 +00006747 actual_status = psa_sign_hash_complete(&operation, signature,
6748 signature_size,
6749 &signature_length);
6750
Paul Elliott0c683352022-12-16 19:16:56 +00006751 num_completes++;
6752
Paul Elliott334d7262023-01-20 17:29:41 +00006753 if (actual_status == PSA_SUCCESS ||
6754 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006755 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006756 /* We are asserting here that every complete makes progress
6757 * (completes some ops), which is true of the internal
6758 * implementation and probably any implementation, however this is
6759 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006760 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006761
Paul Elliott91007972022-12-16 12:21:24 +00006762 num_ops_prior = num_ops;
6763 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006764 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006765
Paul Elliottc9774412023-02-06 15:14:07 +00006766 TEST_EQUAL(actual_status, expected_complete_status);
6767
Paul Elliottefebad02023-02-15 16:56:45 +00006768 /* Check that another complete returns BAD_STATE. */
6769 actual_status = psa_sign_hash_complete(&operation, signature,
6770 signature_size,
6771 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006772
Paul Elliottefebad02023-02-15 16:56:45 +00006773 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006774
Paul Elliott91007972022-12-16 12:21:24 +00006775 PSA_ASSERT(psa_sign_hash_abort(&operation));
6776
Paul Elliott59ad9452022-12-18 15:09:02 +00006777 num_ops = psa_sign_hash_get_num_ops(&operation);
6778 TEST_ASSERT(num_ops == 0);
6779
Paul Elliott91007972022-12-16 12:21:24 +00006780 /* The value of *signature_length is unspecified on error, but
6781 * whatever it is, it should be less than signature_size, so that
6782 * if the caller tries to read *signature_length bytes without
6783 * checking the error code then they don't overflow a buffer. */
6784 TEST_LE_U(signature_length, signature_size);
6785
Paul Elliott0c683352022-12-16 19:16:56 +00006786 TEST_LE_U(min_completes, num_completes);
6787 TEST_LE_U(num_completes, max_completes);
6788
Paul Elliott91007972022-12-16 12:21:24 +00006789exit:
6790 psa_reset_key_attributes(&attributes);
6791 psa_destroy_key(key);
6792 mbedtls_free(signature);
6793 PSA_DONE();
6794}
6795/* END_CASE */
6796
mohammad16038cc1cee2018-03-28 01:21:33 +03006797/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006798void sign_verify_hash(int key_type_arg, data_t *key_data,
6799 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006800{
Ronald Cron5425a212020-08-04 14:58:35 +02006801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006802 psa_key_type_t key_type = key_type_arg;
6803 psa_algorithm_t alg = alg_arg;
6804 size_t key_bits;
6805 unsigned char *signature = NULL;
6806 size_t signature_size;
6807 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006809
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6813 psa_set_key_algorithm(&attributes, alg);
6814 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006815
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6817 &key));
6818 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6819 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006820
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006821 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006822 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6824 key_bits, alg);
6825 TEST_ASSERT(signature_size != 0);
6826 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6827 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006828
6829 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 PSA_ASSERT(psa_sign_hash(key, alg,
6831 input_data->x, input_data->len,
6832 signature, signature_size,
6833 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006834 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 TEST_LE_U(signature_length, signature_size);
6836 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006837
6838 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 PSA_ASSERT(psa_verify_hash(key, alg,
6840 input_data->x, input_data->len,
6841 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006842
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006844 /* Flip a bit in the input and verify that the signature is now
6845 * detected as invalid. Flip a bit at the beginning, not at the end,
6846 * because ECDSA may ignore the last few bits of the input. */
6847 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 TEST_EQUAL(psa_verify_hash(key, alg,
6849 input_data->x, input_data->len,
6850 signature, signature_length),
6851 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006852 }
6853
6854exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006855 /*
6856 * Key attributes may have been returned by psa_get_key_attributes()
6857 * thus reset them as required.
6858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006860
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 psa_destroy_key(key);
6862 mbedtls_free(signature);
6863 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006864}
6865/* END_CASE */
6866
Paul Elliott712d5122022-12-07 14:03:10 +00006867/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006868/**
6869 * sign_verify_hash_interruptible() test intentions:
6870 *
6871 * Note: This test can currently only handle ECDSA.
6872 *
6873 * 1. Test that we can sign an input hash with the given key and then afterwards
6874 * verify that signature. This is currently the only way to test non
6875 * deterministic ECDSA, but this test can also handle deterministic.
6876 *
6877 * 2. Test that after corrupting the hash, the verification detects an invalid
6878 * signature.
6879 *
6880 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6881 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006882 *
6883 * 4. Test that the number of ops done prior to starting signing and after abort
6884 * is zero and that each successful signing stage completes some ops (this is
6885 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006886 */
Paul Elliott712d5122022-12-07 14:03:10 +00006887void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006888 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006889 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006890{
6891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6892 psa_key_type_t key_type = key_type_arg;
6893 psa_algorithm_t alg = alg_arg;
6894 size_t key_bits;
6895 unsigned char *signature = NULL;
6896 size_t signature_size;
6897 size_t signature_length = 0xdeadbeef;
6898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6899 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006900 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006901 uint32_t num_ops = 0;
6902 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006903 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006904 size_t min_completes = 0;
6905 size_t max_completes = 0;
6906
Paul Elliott712d5122022-12-07 14:03:10 +00006907 psa_sign_hash_interruptible_operation_t sign_operation =
6908 psa_sign_hash_interruptible_operation_init();
6909 psa_verify_hash_interruptible_operation_t verify_operation =
6910 psa_verify_hash_interruptible_operation_init();
6911
6912 PSA_ASSERT(psa_crypto_init());
6913
Paul Elliott0c683352022-12-16 19:16:56 +00006914 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6915 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006916 psa_set_key_algorithm(&attributes, alg);
6917 psa_set_key_type(&attributes, key_type);
6918
6919 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6920 &key));
6921 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6922 key_bits = psa_get_key_bits(&attributes);
6923
6924 /* Allocate a buffer which has the size advertised by the
6925 * library. */
6926 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6927 key_bits, alg);
6928 TEST_ASSERT(signature_size != 0);
6929 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6930 ASSERT_ALLOC(signature, signature_size);
6931
Paul Elliott0c683352022-12-16 19:16:56 +00006932 psa_interruptible_set_max_ops(max_ops);
6933
Paul Elliott6f600372023-02-06 18:41:05 +00006934 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6935 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006936
Paul Elliott7c173082023-02-26 18:44:45 +00006937 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6938 TEST_ASSERT(num_ops_prior == 0);
6939
Paul Elliott712d5122022-12-07 14:03:10 +00006940 /* Start performing the signature. */
6941 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6942 input_data->x, input_data->len));
6943
Paul Elliott7c173082023-02-26 18:44:45 +00006944 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6945 TEST_ASSERT(num_ops_prior == 0);
6946
Paul Elliott712d5122022-12-07 14:03:10 +00006947 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006948 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006949
Paul Elliott0c683352022-12-16 19:16:56 +00006950 status = psa_sign_hash_complete(&sign_operation, signature,
6951 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006952 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006953
6954 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006955
6956 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6957 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6958 /* We are asserting here that every complete makes progress
6959 * (completes some ops), which is true of the internal
6960 * implementation and probably any implementation, however this is
6961 * not mandated by the PSA specification. */
6962 TEST_ASSERT(num_ops > num_ops_prior);
6963
6964 num_ops_prior = num_ops;
6965 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006966 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006967
6968 TEST_ASSERT(status == PSA_SUCCESS);
6969
Paul Elliott0c683352022-12-16 19:16:56 +00006970 TEST_LE_U(min_completes, num_completes);
6971 TEST_LE_U(num_completes, max_completes);
6972
Paul Elliott712d5122022-12-07 14:03:10 +00006973 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6974
Paul Elliott7c173082023-02-26 18:44:45 +00006975 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6976 TEST_ASSERT(num_ops == 0);
6977
Paul Elliott712d5122022-12-07 14:03:10 +00006978 /* Check that the signature length looks sensible. */
6979 TEST_LE_U(signature_length, signature_size);
6980 TEST_ASSERT(signature_length > 0);
6981
Paul Elliott0c683352022-12-16 19:16:56 +00006982 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006983
6984 /* Start verification. */
6985 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6986 input_data->x, input_data->len,
6987 signature, signature_length));
6988
6989 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006990 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006991 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00006992
6993 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006994 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006995
6996 TEST_ASSERT(status == PSA_SUCCESS);
6997
Paul Elliott0c683352022-12-16 19:16:56 +00006998 TEST_LE_U(min_completes, num_completes);
6999 TEST_LE_U(num_completes, max_completes);
7000
Paul Elliott712d5122022-12-07 14:03:10 +00007001 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7002
7003 verify_operation = psa_verify_hash_interruptible_operation_init();
7004
7005 if (input_data->len != 0) {
7006 /* Flip a bit in the input and verify that the signature is now
7007 * detected as invalid. Flip a bit at the beginning, not at the end,
7008 * because ECDSA may ignore the last few bits of the input. */
7009 input_data->x[0] ^= 1;
7010
Paul Elliott712d5122022-12-07 14:03:10 +00007011 /* Start verification. */
7012 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7013 input_data->x, input_data->len,
7014 signature, signature_length));
7015
7016 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007017 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007018 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007019 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007020
7021 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7022 }
7023
7024 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7025
7026exit:
7027 /*
7028 * Key attributes may have been returned by psa_get_key_attributes()
7029 * thus reset them as required.
7030 */
7031 psa_reset_key_attributes(&attributes);
7032
7033 psa_destroy_key(key);
7034 mbedtls_free(signature);
7035 PSA_DONE();
7036}
7037/* END_CASE */
7038
Gilles Peskine9911b022018-06-29 17:30:48 +02007039/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007040void verify_hash(int key_type_arg, data_t *key_data,
7041 int alg_arg, data_t *hash_data,
7042 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007043{
Ronald Cron5425a212020-08-04 14:58:35 +02007044 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007045 psa_key_type_t key_type = key_type_arg;
7046 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007048
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007050
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7054 psa_set_key_algorithm(&attributes, alg);
7055 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7058 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 PSA_ASSERT(psa_verify_hash(key, alg,
7061 hash_data->x, hash_data->len,
7062 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007063
itayzafrir5c753392018-05-08 11:18:38 +03007064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 psa_reset_key_attributes(&attributes);
7066 psa_destroy_key(key);
7067 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007068}
7069/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007070
Paul Elliott712d5122022-12-07 14:03:10 +00007071/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007072/**
7073 * verify_hash_interruptible() test intentions:
7074 *
7075 * Note: This test can currently only handle ECDSA.
7076 *
7077 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
7078 * only).
7079 *
7080 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7081 * expected for different max_ops values.
7082 *
7083 * 3. Test that the number of ops done prior to start and after abort is zero
7084 * and that each successful stage completes some ops (this is not mandated by
7085 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007086 *
7087 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7088 * complete() calls does not alter the number of ops returned.
7089 *
7090 * 5. Test that after corrupting the hash, the verification detects an invalid
7091 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007092 */
Paul Elliott712d5122022-12-07 14:03:10 +00007093void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7094 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007095 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007096{
7097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7098 psa_key_type_t key_type = key_type_arg;
7099 psa_algorithm_t alg = alg_arg;
7100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7101 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007102 uint32_t num_ops = 0;
7103 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007104 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007105 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007106 size_t min_completes = 0;
7107 size_t max_completes = 0;
7108
Paul Elliott712d5122022-12-07 14:03:10 +00007109 psa_verify_hash_interruptible_operation_t operation =
7110 psa_verify_hash_interruptible_operation_init();
7111
7112 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7113
7114 PSA_ASSERT(psa_crypto_init());
7115
7116 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7117 psa_set_key_algorithm(&attributes, alg);
7118 psa_set_key_type(&attributes, key_type);
7119
7120 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7121 &key));
7122
Paul Elliott0c683352022-12-16 19:16:56 +00007123 psa_interruptible_set_max_ops(max_ops);
7124
Paul Elliott6f600372023-02-06 18:41:05 +00007125 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7126 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007127
Paul Elliott712d5122022-12-07 14:03:10 +00007128 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7129
7130 TEST_ASSERT(num_ops_prior == 0);
7131
7132 /* Start verification. */
7133 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7134 hash_data->x, hash_data->len,
7135 signature_data->x, signature_data->len)
7136 );
7137
7138 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7139
7140 TEST_ASSERT(num_ops_prior == 0);
7141
7142 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007143 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007144 status = psa_verify_hash_complete(&operation);
7145
Paul Elliott0c683352022-12-16 19:16:56 +00007146 num_completes++;
7147
Paul Elliott712d5122022-12-07 14:03:10 +00007148 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7149 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007150 /* We are asserting here that every complete makes progress
7151 * (completes some ops), which is true of the internal
7152 * implementation and probably any implementation, however this is
7153 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007154 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007155
Paul Elliott712d5122022-12-07 14:03:10 +00007156 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007157
7158 /* Ensure calling get_num_ops() twice still returns the same
7159 * number of ops as previously reported. */
7160 num_ops = psa_verify_hash_get_num_ops(&operation);
7161
7162 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007163 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007164 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007165
7166 TEST_ASSERT(status == PSA_SUCCESS);
7167
Paul Elliott0c683352022-12-16 19:16:56 +00007168 TEST_LE_U(min_completes, num_completes);
7169 TEST_LE_U(num_completes, max_completes);
7170
Paul Elliott712d5122022-12-07 14:03:10 +00007171 PSA_ASSERT(psa_verify_hash_abort(&operation));
7172
Paul Elliott59ad9452022-12-18 15:09:02 +00007173 num_ops = psa_verify_hash_get_num_ops(&operation);
7174 TEST_ASSERT(num_ops == 0);
7175
Paul Elliott8359c142023-02-24 18:40:10 +00007176 if (hash_data->len != 0) {
7177 /* Flip a bit in the hash and verify that the signature is now detected
7178 * as invalid. Flip a bit at the beginning, not at the end, because
7179 * ECDSA may ignore the last few bits of the input. */
7180 hash_data->x[0] ^= 1;
7181
7182 /* Start verification. */
7183 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7184 hash_data->x, hash_data->len,
7185 signature_data->x, signature_data->len));
7186
7187 /* Continue performing the signature until complete. */
7188 do {
7189 status = psa_verify_hash_complete(&operation);
7190 } while (status == PSA_OPERATION_INCOMPLETE);
7191
7192 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7193 }
7194
Paul Elliott712d5122022-12-07 14:03:10 +00007195exit:
7196 psa_reset_key_attributes(&attributes);
7197 psa_destroy_key(key);
7198 PSA_DONE();
7199}
7200/* END_CASE */
7201
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007202/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007203void verify_hash_fail(int key_type_arg, data_t *key_data,
7204 int alg_arg, data_t *hash_data,
7205 data_t *signature_data,
7206 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007207{
Ronald Cron5425a212020-08-04 14:58:35 +02007208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007209 psa_key_type_t key_type = key_type_arg;
7210 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007211 psa_status_t actual_status;
7212 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007214
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007216
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7218 psa_set_key_algorithm(&attributes, alg);
7219 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7222 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 actual_status = psa_verify_hash(key, alg,
7225 hash_data->x, hash_data->len,
7226 signature_data->x, signature_data->len);
7227 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007228
7229exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 psa_reset_key_attributes(&attributes);
7231 psa_destroy_key(key);
7232 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007233}
7234/* END_CASE */
7235
Paul Elliott91007972022-12-16 12:21:24 +00007236/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007237/**
7238 * verify_hash_fail_interruptible() test intentions:
7239 *
7240 * Note: This test can currently only handle ECDSA.
7241 *
7242 * 1. Test that various failure cases for interruptible verify hash fail with
7243 * the correct error codes, and at the correct point (at start or during
7244 * complete).
7245 *
7246 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7247 * expected for different max_ops values.
7248 *
7249 * 3. Test that the number of ops done prior to start and after abort is zero
7250 * and that each successful stage completes some ops (this is not mandated by
7251 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007252 *
7253 * 4. Check that calling complete() when start() fails and complete()
7254 * after completion results in a BAD_STATE error.
7255 *
7256 * 5. Check that calling start() again after start fails results in a BAD_STATE
7257 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007258 */
Paul Elliott91007972022-12-16 12:21:24 +00007259void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7260 int alg_arg, data_t *hash_data,
7261 data_t *signature_data,
7262 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007263 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007264 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007265{
7266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7267 psa_key_type_t key_type = key_type_arg;
7268 psa_algorithm_t alg = alg_arg;
7269 psa_status_t actual_status;
7270 psa_status_t expected_start_status = expected_start_status_arg;
7271 psa_status_t expected_complete_status = expected_complete_status_arg;
7272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007273 uint32_t num_ops = 0;
7274 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007275 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007276 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007277 size_t min_completes = 0;
7278 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007279 psa_verify_hash_interruptible_operation_t operation =
7280 psa_verify_hash_interruptible_operation_init();
7281
7282 PSA_ASSERT(psa_crypto_init());
7283
7284 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7285 psa_set_key_algorithm(&attributes, alg);
7286 psa_set_key_type(&attributes, key_type);
7287
7288 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7289 &key));
7290
Paul Elliott0c683352022-12-16 19:16:56 +00007291 psa_interruptible_set_max_ops(max_ops);
7292
Paul Elliott6f600372023-02-06 18:41:05 +00007293 interruptible_signverify_get_minmax_completes(max_ops,
7294 expected_complete_status,
7295 &min_completes,
7296 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007297
Paul Elliott91007972022-12-16 12:21:24 +00007298 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7299 TEST_ASSERT(num_ops_prior == 0);
7300
7301 /* Start verification. */
7302 actual_status = psa_verify_hash_start(&operation, key, alg,
7303 hash_data->x, hash_data->len,
7304 signature_data->x,
7305 signature_data->len);
7306
7307 TEST_EQUAL(actual_status, expected_start_status);
7308
Paul Elliottc9774412023-02-06 15:14:07 +00007309 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007310 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007311 * start failed. */
7312 actual_status = psa_verify_hash_complete(&operation);
7313
7314 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7315
7316 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007317 actual_status = psa_verify_hash_start(&operation, key, alg,
7318 hash_data->x, hash_data->len,
7319 signature_data->x,
7320 signature_data->len);
7321
7322 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7323 }
7324
Paul Elliott91007972022-12-16 12:21:24 +00007325 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7326 TEST_ASSERT(num_ops_prior == 0);
7327
Paul Elliott91007972022-12-16 12:21:24 +00007328 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007329 do {
Paul Elliott91007972022-12-16 12:21:24 +00007330 actual_status = psa_verify_hash_complete(&operation);
7331
Paul Elliott0c683352022-12-16 19:16:56 +00007332 num_completes++;
7333
Paul Elliott334d7262023-01-20 17:29:41 +00007334 if (actual_status == PSA_SUCCESS ||
7335 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007336 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007337 /* We are asserting here that every complete makes progress
7338 * (completes some ops), which is true of the internal
7339 * implementation and probably any implementation, however this is
7340 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007341 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007342
Paul Elliott91007972022-12-16 12:21:24 +00007343 num_ops_prior = num_ops;
7344 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007345 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007346
Paul Elliottc9774412023-02-06 15:14:07 +00007347 TEST_EQUAL(actual_status, expected_complete_status);
7348
Paul Elliottefebad02023-02-15 16:56:45 +00007349 /* Check that another complete returns BAD_STATE. */
7350 actual_status = psa_verify_hash_complete(&operation);
7351 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007352
Paul Elliott0c683352022-12-16 19:16:56 +00007353 TEST_LE_U(min_completes, num_completes);
7354 TEST_LE_U(num_completes, max_completes);
7355
Paul Elliott91007972022-12-16 12:21:24 +00007356 PSA_ASSERT(psa_verify_hash_abort(&operation));
7357
Paul Elliott59ad9452022-12-18 15:09:02 +00007358 num_ops = psa_verify_hash_get_num_ops(&operation);
7359 TEST_ASSERT(num_ops == 0);
7360
Paul Elliott91007972022-12-16 12:21:24 +00007361exit:
7362 psa_reset_key_attributes(&attributes);
7363 psa_destroy_key(key);
7364 PSA_DONE();
7365}
7366/* END_CASE */
7367
Paul Elliott20a36062022-12-18 13:21:25 +00007368/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007369/**
7370 * interruptible_signverify_hash_state_test() test intentions:
7371 *
7372 * Note: This test can currently only handle ECDSA.
7373 *
7374 * 1. Test that calling the various interruptible sign and verify hash functions
7375 * in incorrect orders returns BAD_STATE errors.
7376 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007377void interruptible_signverify_hash_state_test(int key_type_arg,
7378 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007379{
7380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7381 psa_key_type_t key_type = key_type_arg;
7382 psa_algorithm_t alg = alg_arg;
7383 size_t key_bits;
7384 unsigned char *signature = NULL;
7385 size_t signature_size;
7386 size_t signature_length = 0xdeadbeef;
7387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7388 psa_sign_hash_interruptible_operation_t sign_operation =
7389 psa_sign_hash_interruptible_operation_init();
7390 psa_verify_hash_interruptible_operation_t verify_operation =
7391 psa_verify_hash_interruptible_operation_init();
7392
7393 PSA_ASSERT(psa_crypto_init());
7394
7395 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7396 PSA_KEY_USAGE_VERIFY_HASH);
7397 psa_set_key_algorithm(&attributes, alg);
7398 psa_set_key_type(&attributes, key_type);
7399
7400 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7401 &key));
7402 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7403 key_bits = psa_get_key_bits(&attributes);
7404
7405 /* Allocate a buffer which has the size advertised by the
7406 * library. */
7407 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7408 key_bits, alg);
7409 TEST_ASSERT(signature_size != 0);
7410 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7411 ASSERT_ALLOC(signature, signature_size);
7412
7413 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7414
7415 /* --- Attempt completes prior to starts --- */
7416 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7417 signature_size,
7418 &signature_length),
7419 PSA_ERROR_BAD_STATE);
7420
7421 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7422
7423 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7424 PSA_ERROR_BAD_STATE);
7425
7426 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7427
7428 /* --- Aborts in all other places. --- */
7429 psa_sign_hash_abort(&sign_operation);
7430
7431 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7432 input_data->x, input_data->len));
7433
7434 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7435
7436 psa_interruptible_set_max_ops(1);
7437
7438 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7439 input_data->x, input_data->len));
7440
7441 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7442 signature_size,
7443 &signature_length),
7444 PSA_OPERATION_INCOMPLETE);
7445
7446 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7447
7448 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7449
7450 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7451 input_data->x, input_data->len));
7452
7453 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7454 signature_size,
7455 &signature_length));
7456
7457 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7458
7459 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7460
7461 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7462 input_data->x, input_data->len,
7463 signature, signature_length));
7464
7465 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7466
7467 psa_interruptible_set_max_ops(1);
7468
7469 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7470 input_data->x, input_data->len,
7471 signature, signature_length));
7472
7473 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7474 PSA_OPERATION_INCOMPLETE);
7475
7476 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7477
7478 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7479
7480 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7481 input_data->x, input_data->len,
7482 signature, signature_length));
7483
7484 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7485
7486 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7487
7488 /* --- Attempt double starts. --- */
7489
7490 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7491 input_data->x, input_data->len));
7492
7493 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7494 input_data->x, input_data->len),
7495 PSA_ERROR_BAD_STATE);
7496
7497 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7498
7499 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7500 input_data->x, input_data->len,
7501 signature, signature_length));
7502
7503 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7504 input_data->x, input_data->len,
7505 signature, signature_length),
7506 PSA_ERROR_BAD_STATE);
7507
7508 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7509
Paul Elliott76d671a2023-02-07 17:45:18 +00007510exit:
7511 /*
7512 * Key attributes may have been returned by psa_get_key_attributes()
7513 * thus reset them as required.
7514 */
7515 psa_reset_key_attributes(&attributes);
7516
7517 psa_destroy_key(key);
7518 mbedtls_free(signature);
7519 PSA_DONE();
7520}
7521/* END_CASE */
7522
7523/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007524/**
Paul Elliottc2033502023-02-26 17:09:14 +00007525 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007526 *
7527 * Note: This test can currently only handle ECDSA.
7528 *
7529 * 1. Test various edge cases in the interruptible sign and verify hash
7530 * interfaces.
7531 */
Paul Elliottc2033502023-02-26 17:09:14 +00007532void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007533 data_t *key_data, int alg_arg, data_t *input_data)
7534{
7535 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7536 psa_key_type_t key_type = key_type_arg;
7537 psa_algorithm_t alg = alg_arg;
7538 size_t key_bits;
7539 unsigned char *signature = NULL;
7540 size_t signature_size;
7541 size_t signature_length = 0xdeadbeef;
7542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7543 uint8_t *input_buffer = NULL;
7544 psa_sign_hash_interruptible_operation_t sign_operation =
7545 psa_sign_hash_interruptible_operation_init();
7546 psa_verify_hash_interruptible_operation_t verify_operation =
7547 psa_verify_hash_interruptible_operation_init();
7548
7549 PSA_ASSERT(psa_crypto_init());
7550
7551 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7552 PSA_KEY_USAGE_VERIFY_HASH);
7553 psa_set_key_algorithm(&attributes, alg);
7554 psa_set_key_type(&attributes, key_type);
7555
7556 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7557 &key));
7558 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7559 key_bits = psa_get_key_bits(&attributes);
7560
7561 /* Allocate a buffer which has the size advertised by the
7562 * library. */
7563 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7564 key_bits, alg);
7565 TEST_ASSERT(signature_size != 0);
7566 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7567 ASSERT_ALLOC(signature, signature_size);
7568
Paul Elliott20a36062022-12-18 13:21:25 +00007569 /* --- Change function inputs mid run, to cause an error (sign only,
7570 * verify passes all inputs to start. --- */
7571
7572 psa_interruptible_set_max_ops(1);
7573
7574 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7575 input_data->x, input_data->len));
7576
7577 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7578 signature_size,
7579 &signature_length),
7580 PSA_OPERATION_INCOMPLETE);
7581
7582 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7583 0,
7584 &signature_length),
7585 PSA_ERROR_BUFFER_TOO_SMALL);
7586
Paul Elliottc9774412023-02-06 15:14:07 +00007587 /* And test that this invalidates the operation. */
7588 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7589 0,
7590 &signature_length),
7591 PSA_ERROR_BAD_STATE);
7592
Paul Elliott20a36062022-12-18 13:21:25 +00007593 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7594
Paul Elliottf9c91a72023-02-05 18:06:38 +00007595 /* Trash the hash buffer in between start and complete, to ensure
7596 * no reliance on external buffers. */
7597 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7598
7599 input_buffer = mbedtls_calloc(1, input_data->len);
7600 TEST_ASSERT(input_buffer != NULL);
7601
7602 memcpy(input_buffer, input_data->x, input_data->len);
7603
7604 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7605 input_buffer, input_data->len));
7606
7607 memset(input_buffer, '!', input_data->len);
7608 mbedtls_free(input_buffer);
7609 input_buffer = NULL;
7610
7611 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7612 signature_size,
7613 &signature_length));
7614
7615 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7616
7617 input_buffer = mbedtls_calloc(1, input_data->len);
7618 TEST_ASSERT(input_buffer != NULL);
7619
7620 memcpy(input_buffer, input_data->x, input_data->len);
7621
7622 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7623 input_buffer, input_data->len,
7624 signature, signature_length));
7625
7626 memset(input_buffer, '!', input_data->len);
7627 mbedtls_free(input_buffer);
7628 input_buffer = NULL;
7629
7630 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7631
7632 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7633
Paul Elliott20a36062022-12-18 13:21:25 +00007634exit:
7635 /*
7636 * Key attributes may have been returned by psa_get_key_attributes()
7637 * thus reset them as required.
7638 */
7639 psa_reset_key_attributes(&attributes);
7640
7641 psa_destroy_key(key);
7642 mbedtls_free(signature);
7643 PSA_DONE();
7644}
7645/* END_CASE */
7646
Paul Elliotta4cb9092023-02-07 18:01:55 +00007647/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007648/**
Paul Elliott57702242023-02-26 20:36:10 +00007649 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007650 *
7651 * Note: This test can currently only handle ECDSA.
7652 *
7653 * 1. Test that setting max ops is reflected in both interruptible sign and
7654 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007655 * 2. Test that changing the value of max_ops to unlimited during an operation
7656 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007657 *
7658 * 3. Test that calling get_num_ops() between complete calls gives the same
7659 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007660 */
Paul Elliott57702242023-02-26 20:36:10 +00007661void interruptible_signverify_hash_ops_tests(int key_type_arg,
7662 data_t *key_data, int alg_arg,
7663 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007664{
7665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7666 psa_key_type_t key_type = key_type_arg;
7667 psa_algorithm_t alg = alg_arg;
7668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007669 size_t key_bits;
7670 unsigned char *signature = NULL;
7671 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007672 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007673 uint32_t num_ops = 0;
7674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7675
Paul Elliotta4cb9092023-02-07 18:01:55 +00007676 psa_sign_hash_interruptible_operation_t sign_operation =
7677 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007678 psa_verify_hash_interruptible_operation_t verify_operation =
7679 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007680
7681 PSA_ASSERT(psa_crypto_init());
7682
7683 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7684 PSA_KEY_USAGE_VERIFY_HASH);
7685 psa_set_key_algorithm(&attributes, alg);
7686 psa_set_key_type(&attributes, key_type);
7687
Paul Elliottf1743e22023-02-15 18:44:16 +00007688 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7689 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7690 key_bits = psa_get_key_bits(&attributes);
7691
7692 /* Allocate a buffer which has the size advertised by the
7693 * library. */
7694 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7695
7696 TEST_ASSERT(signature_size != 0);
7697 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7698 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007699
7700 /* Check that default max ops gets set if we don't set it. */
7701 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7702 input_data->x, input_data->len));
7703
7704 TEST_EQUAL(psa_interruptible_get_max_ops(),
7705 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7706
7707 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7708
Paul Elliottf1743e22023-02-15 18:44:16 +00007709 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7710 input_data->x, input_data->len,
7711 signature, signature_size));
7712
7713 TEST_EQUAL(psa_interruptible_get_max_ops(),
7714 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7715
7716 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7717
Paul Elliotta4cb9092023-02-07 18:01:55 +00007718 /* Check that max ops gets set properly. */
7719
7720 psa_interruptible_set_max_ops(0xbeef);
7721
Paul Elliottf1743e22023-02-15 18:44:16 +00007722 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007723
Paul Elliott9e8819f2023-02-26 19:01:35 +00007724 /* --- Ensure changing the max ops mid operation works (operation should
7725 * complete successfully after setting max ops to unlimited --- */
7726 psa_interruptible_set_max_ops(1);
7727
7728 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7729 input_data->x, input_data->len));
7730
7731 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7732 signature_size,
7733 &signature_length),
7734 PSA_OPERATION_INCOMPLETE);
7735
7736 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7737
7738 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7739 signature_size,
7740 &signature_length));
7741
7742 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7743
7744 psa_interruptible_set_max_ops(1);
7745
7746 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7747 input_data->x, input_data->len,
7748 signature, signature_length));
7749
7750 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7751 PSA_OPERATION_INCOMPLETE);
7752
7753 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7754
7755 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7756
7757 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7758
Paul Elliottc1e04002023-02-26 20:27:23 +00007759 /* --- Test that not calling get_num_ops inbetween complete calls does not
7760 * result in lost ops. ---*/
7761
7762 psa_interruptible_set_max_ops(1);
7763
7764 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7765 input_data->x, input_data->len));
7766
7767 /* Continue performing the signature until complete. */
7768 do {
7769 status = psa_sign_hash_complete(&sign_operation, signature,
7770 signature_size,
7771 &signature_length);
7772
7773 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7774
7775 } while (status == PSA_OPERATION_INCOMPLETE);
7776
7777 PSA_ASSERT(status);
7778
7779 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7780
7781 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7782 input_data->x, input_data->len));
7783
7784 /* Continue performing the signature until complete. */
7785 do {
7786 status = psa_sign_hash_complete(&sign_operation, signature,
7787 signature_size,
7788 &signature_length);
7789 } while (status == PSA_OPERATION_INCOMPLETE);
7790
7791 PSA_ASSERT(status);
7792
7793 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7794
7795 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7796
7797 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7798 input_data->x, input_data->len,
7799 signature, signature_length));
7800
7801 /* Continue performing the verification until complete. */
7802 do {
7803 status = psa_verify_hash_complete(&verify_operation);
7804
7805 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7806
7807 } while (status == PSA_OPERATION_INCOMPLETE);
7808
7809 PSA_ASSERT(status);
7810
7811 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7812
7813 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7814 input_data->x, input_data->len,
7815 signature, signature_length));
7816
7817 /* Continue performing the verification until complete. */
7818 do {
7819 status = psa_verify_hash_complete(&verify_operation);
7820
7821 } while (status == PSA_OPERATION_INCOMPLETE);
7822
7823 PSA_ASSERT(status);
7824
7825 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7826
7827 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7828
Paul Elliotta4cb9092023-02-07 18:01:55 +00007829exit:
7830 /*
7831 * Key attributes may have been returned by psa_get_key_attributes()
7832 * thus reset them as required.
7833 */
7834 psa_reset_key_attributes(&attributes);
7835
7836 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007837 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007838 PSA_DONE();
7839}
7840/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007841
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007842/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007843void sign_message_deterministic(int key_type_arg,
7844 data_t *key_data,
7845 int alg_arg,
7846 data_t *input_data,
7847 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007848{
7849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7850 psa_key_type_t key_type = key_type_arg;
7851 psa_algorithm_t alg = alg_arg;
7852 size_t key_bits;
7853 unsigned char *signature = NULL;
7854 size_t signature_size;
7855 size_t signature_length = 0xdeadbeef;
7856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7857
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007859
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7861 psa_set_key_algorithm(&attributes, alg);
7862 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007863
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7865 &key));
7866 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7867 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007868
Gilles Peskine449bd832023-01-11 14:50:10 +01007869 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7870 TEST_ASSERT(signature_size != 0);
7871 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7872 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007873
Gilles Peskine449bd832023-01-11 14:50:10 +01007874 PSA_ASSERT(psa_sign_message(key, alg,
7875 input_data->x, input_data->len,
7876 signature, signature_size,
7877 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007878
Gilles Peskine449bd832023-01-11 14:50:10 +01007879 ASSERT_COMPARE(output_data->x, output_data->len,
7880 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007881
7882exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007883 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007884
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 psa_destroy_key(key);
7886 mbedtls_free(signature);
7887 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007888
7889}
7890/* END_CASE */
7891
7892/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007893void sign_message_fail(int key_type_arg,
7894 data_t *key_data,
7895 int alg_arg,
7896 data_t *input_data,
7897 int signature_size_arg,
7898 int expected_status_arg)
7899{
7900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7901 psa_key_type_t key_type = key_type_arg;
7902 psa_algorithm_t alg = alg_arg;
7903 size_t signature_size = signature_size_arg;
7904 psa_status_t actual_status;
7905 psa_status_t expected_status = expected_status_arg;
7906 unsigned char *signature = NULL;
7907 size_t signature_length = 0xdeadbeef;
7908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7909
7910 ASSERT_ALLOC(signature, signature_size);
7911
7912 PSA_ASSERT(psa_crypto_init());
7913
7914 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7915 psa_set_key_algorithm(&attributes, alg);
7916 psa_set_key_type(&attributes, key_type);
7917
7918 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7919 &key));
7920
7921 actual_status = psa_sign_message(key, alg,
7922 input_data->x, input_data->len,
7923 signature, signature_size,
7924 &signature_length);
7925 TEST_EQUAL(actual_status, expected_status);
7926 /* The value of *signature_length is unspecified on error, but
7927 * whatever it is, it should be less than signature_size, so that
7928 * if the caller tries to read *signature_length bytes without
7929 * checking the error code then they don't overflow a buffer. */
7930 TEST_LE_U(signature_length, signature_size);
7931
7932exit:
7933 psa_reset_key_attributes(&attributes);
7934 psa_destroy_key(key);
7935 mbedtls_free(signature);
7936 PSA_DONE();
7937}
7938/* END_CASE */
7939
7940/* BEGIN_CASE */
7941void sign_verify_message(int key_type_arg,
7942 data_t *key_data,
7943 int alg_arg,
7944 data_t *input_data)
7945{
7946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7947 psa_key_type_t key_type = key_type_arg;
7948 psa_algorithm_t alg = alg_arg;
7949 size_t key_bits;
7950 unsigned char *signature = NULL;
7951 size_t signature_size;
7952 size_t signature_length = 0xdeadbeef;
7953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7954
7955 PSA_ASSERT(psa_crypto_init());
7956
7957 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7958 PSA_KEY_USAGE_VERIFY_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 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7965 key_bits = psa_get_key_bits(&attributes);
7966
7967 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7968 TEST_ASSERT(signature_size != 0);
7969 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7970 ASSERT_ALLOC(signature, signature_size);
7971
7972 PSA_ASSERT(psa_sign_message(key, alg,
7973 input_data->x, input_data->len,
7974 signature, signature_size,
7975 &signature_length));
7976 TEST_LE_U(signature_length, signature_size);
7977 TEST_ASSERT(signature_length > 0);
7978
7979 PSA_ASSERT(psa_verify_message(key, alg,
7980 input_data->x, input_data->len,
7981 signature, signature_length));
7982
7983 if (input_data->len != 0) {
7984 /* Flip a bit in the input and verify that the signature is now
7985 * detected as invalid. Flip a bit at the beginning, not at the end,
7986 * because ECDSA may ignore the last few bits of the input. */
7987 input_data->x[0] ^= 1;
7988 TEST_EQUAL(psa_verify_message(key, alg,
7989 input_data->x, input_data->len,
7990 signature, signature_length),
7991 PSA_ERROR_INVALID_SIGNATURE);
7992 }
7993
7994exit:
7995 psa_reset_key_attributes(&attributes);
7996
7997 psa_destroy_key(key);
7998 mbedtls_free(signature);
7999 PSA_DONE();
8000}
8001/* END_CASE */
8002
8003/* BEGIN_CASE */
8004void verify_message(int key_type_arg,
8005 data_t *key_data,
8006 int alg_arg,
8007 data_t *input_data,
8008 data_t *signature_data)
8009{
8010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8011 psa_key_type_t key_type = key_type_arg;
8012 psa_algorithm_t alg = alg_arg;
8013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8014
8015 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8016
8017 PSA_ASSERT(psa_crypto_init());
8018
8019 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8020 psa_set_key_algorithm(&attributes, alg);
8021 psa_set_key_type(&attributes, key_type);
8022
8023 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8024 &key));
8025
8026 PSA_ASSERT(psa_verify_message(key, alg,
8027 input_data->x, input_data->len,
8028 signature_data->x, signature_data->len));
8029
8030exit:
8031 psa_reset_key_attributes(&attributes);
8032 psa_destroy_key(key);
8033 PSA_DONE();
8034}
8035/* END_CASE */
8036
8037/* BEGIN_CASE */
8038void verify_message_fail(int key_type_arg,
8039 data_t *key_data,
8040 int alg_arg,
8041 data_t *hash_data,
8042 data_t *signature_data,
8043 int expected_status_arg)
8044{
8045 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8046 psa_key_type_t key_type = key_type_arg;
8047 psa_algorithm_t alg = alg_arg;
8048 psa_status_t actual_status;
8049 psa_status_t expected_status = expected_status_arg;
8050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8051
8052 PSA_ASSERT(psa_crypto_init());
8053
8054 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8055 psa_set_key_algorithm(&attributes, alg);
8056 psa_set_key_type(&attributes, key_type);
8057
8058 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8059 &key));
8060
8061 actual_status = psa_verify_message(key, alg,
8062 hash_data->x, hash_data->len,
8063 signature_data->x,
8064 signature_data->len);
8065 TEST_EQUAL(actual_status, expected_status);
8066
8067exit:
8068 psa_reset_key_attributes(&attributes);
8069 psa_destroy_key(key);
8070 PSA_DONE();
8071}
8072/* END_CASE */
8073
8074/* BEGIN_CASE */
8075void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008076 data_t *key_data,
8077 int alg_arg,
8078 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008079 data_t *label,
8080 int expected_output_length_arg,
8081 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008082{
Ronald Cron5425a212020-08-04 14:58:35 +02008083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008084 psa_key_type_t key_type = key_type_arg;
8085 psa_algorithm_t alg = alg_arg;
8086 size_t expected_output_length = expected_output_length_arg;
8087 size_t key_bits;
8088 unsigned char *output = NULL;
8089 size_t output_size;
8090 size_t output_length = ~0;
8091 psa_status_t actual_status;
8092 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008094
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008096
Gilles Peskine656896e2018-06-29 19:12:28 +02008097 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8099 psa_set_key_algorithm(&attributes, alg);
8100 psa_set_key_type(&attributes, key_type);
8101 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8102 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008103
8104 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8106 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008107
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8109 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8110 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008111
8112 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 actual_status = psa_asymmetric_encrypt(key, alg,
8114 input_data->x, input_data->len,
8115 label->x, label->len,
8116 output, output_size,
8117 &output_length);
8118 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008119 if (actual_status == PSA_SUCCESS) {
8120 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008121 } else {
8122 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008123 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008124
Gilles Peskine68428122018-06-30 18:42:41 +02008125 /* If the label is empty, the test framework puts a non-null pointer
8126 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008128 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 if (output_size != 0) {
8130 memset(output, 0, output_size);
8131 }
8132 actual_status = psa_asymmetric_encrypt(key, alg,
8133 input_data->x, input_data->len,
8134 NULL, label->len,
8135 output, output_size,
8136 &output_length);
8137 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008138 if (actual_status == PSA_SUCCESS) {
8139 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008140 } else {
8141 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008142 }
Gilles Peskine68428122018-06-30 18:42:41 +02008143 }
8144
Gilles Peskine656896e2018-06-29 19:12:28 +02008145exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008146 /*
8147 * Key attributes may have been returned by psa_get_key_attributes()
8148 * thus reset them as required.
8149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008150 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008151
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 psa_destroy_key(key);
8153 mbedtls_free(output);
8154 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008155}
8156/* END_CASE */
8157
8158/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008159void asymmetric_encrypt_decrypt(int key_type_arg,
8160 data_t *key_data,
8161 int alg_arg,
8162 data_t *input_data,
8163 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008164{
Ronald Cron5425a212020-08-04 14:58:35 +02008165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008166 psa_key_type_t key_type = key_type_arg;
8167 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008168 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008169 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008170 size_t output_size;
8171 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008172 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008173 size_t output2_size;
8174 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008176
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008178
Gilles Peskine449bd832023-01-11 14:50:10 +01008179 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8180 psa_set_key_algorithm(&attributes, alg);
8181 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008182
Gilles Peskine449bd832023-01-11 14:50:10 +01008183 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8184 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008185
8186 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008187 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8188 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008189
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8191 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8192 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008193
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008194 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 TEST_LE_U(output2_size,
8196 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8197 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8198 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008199
Gilles Peskineeebd7382018-06-08 18:11:54 +02008200 /* We test encryption by checking that encrypt-then-decrypt gives back
8201 * the original plaintext because of the non-optional random
8202 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8204 input_data->x, input_data->len,
8205 label->x, label->len,
8206 output, output_size,
8207 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008208 /* We don't know what ciphertext length to expect, but check that
8209 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008210 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008211
Gilles Peskine449bd832023-01-11 14:50:10 +01008212 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8213 output, output_length,
8214 label->x, label->len,
8215 output2, output2_size,
8216 &output2_length));
8217 ASSERT_COMPARE(input_data->x, input_data->len,
8218 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008219
8220exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008221 /*
8222 * Key attributes may have been returned by psa_get_key_attributes()
8223 * thus reset them as required.
8224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 psa_destroy_key(key);
8228 mbedtls_free(output);
8229 mbedtls_free(output2);
8230 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008231}
8232/* END_CASE */
8233
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008234/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008235void asymmetric_decrypt(int key_type_arg,
8236 data_t *key_data,
8237 int alg_arg,
8238 data_t *input_data,
8239 data_t *label,
8240 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008241{
Ronald Cron5425a212020-08-04 14:58:35 +02008242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008243 psa_key_type_t key_type = key_type_arg;
8244 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008245 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008246 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008247 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008248 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008250
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008252
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8254 psa_set_key_algorithm(&attributes, alg);
8255 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008256
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8258 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008259
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8261 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008262
8263 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8265 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8266 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008267
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8269 input_data->x, input_data->len,
8270 label->x, label->len,
8271 output,
8272 output_size,
8273 &output_length));
8274 ASSERT_COMPARE(expected_data->x, expected_data->len,
8275 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008276
Gilles Peskine68428122018-06-30 18:42:41 +02008277 /* If the label is empty, the test framework puts a non-null pointer
8278 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008280 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 if (output_size != 0) {
8282 memset(output, 0, output_size);
8283 }
8284 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8285 input_data->x, input_data->len,
8286 NULL, label->len,
8287 output,
8288 output_size,
8289 &output_length));
8290 ASSERT_COMPARE(expected_data->x, expected_data->len,
8291 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008292 }
8293
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 psa_reset_key_attributes(&attributes);
8296 psa_destroy_key(key);
8297 mbedtls_free(output);
8298 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008299}
8300/* END_CASE */
8301
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008302/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008303void asymmetric_decrypt_fail(int key_type_arg,
8304 data_t *key_data,
8305 int alg_arg,
8306 data_t *input_data,
8307 data_t *label,
8308 int output_size_arg,
8309 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008310{
Ronald Cron5425a212020-08-04 14:58:35 +02008311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008312 psa_key_type_t key_type = key_type_arg;
8313 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008314 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008315 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008316 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008317 psa_status_t actual_status;
8318 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008320
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008322
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008324
Gilles Peskine449bd832023-01-11 14:50:10 +01008325 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8326 psa_set_key_algorithm(&attributes, alg);
8327 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008328
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8330 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008331
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 actual_status = psa_asymmetric_decrypt(key, alg,
8333 input_data->x, input_data->len,
8334 label->x, label->len,
8335 output, output_size,
8336 &output_length);
8337 TEST_EQUAL(actual_status, expected_status);
8338 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008339
Gilles Peskine68428122018-06-30 18:42:41 +02008340 /* If the label is empty, the test framework puts a non-null pointer
8341 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008343 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 if (output_size != 0) {
8345 memset(output, 0, output_size);
8346 }
8347 actual_status = psa_asymmetric_decrypt(key, alg,
8348 input_data->x, input_data->len,
8349 NULL, label->len,
8350 output, output_size,
8351 &output_length);
8352 TEST_EQUAL(actual_status, expected_status);
8353 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008354 }
8355
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 psa_reset_key_attributes(&attributes);
8358 psa_destroy_key(key);
8359 mbedtls_free(output);
8360 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008361}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008362/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008363
8364/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008365void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008366{
8367 /* Test each valid way of initializing the object, except for `= {0}`, as
8368 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8369 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008370 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008371 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008373 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8374 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008375
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008378 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8380 PSA_ERROR_BAD_STATE);
8381 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8382 PSA_ERROR_BAD_STATE);
8383 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8384 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008386 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 PSA_ASSERT(psa_key_derivation_abort(&func));
8388 PSA_ASSERT(psa_key_derivation_abort(&init));
8389 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008390}
8391/* END_CASE */
8392
Janos Follath16de4a42019-06-13 16:32:24 +01008393/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008394void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008395{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008396 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008397 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008398 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008399
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008401
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8403 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008404
8405exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008406 psa_key_derivation_abort(&operation);
8407 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008408}
8409/* END_CASE */
8410
Janos Follathaf3c2a02019-06-12 12:34:34 +01008411/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008412void derive_set_capacity(int alg_arg, int capacity_arg,
8413 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008414{
8415 psa_algorithm_t alg = alg_arg;
8416 size_t capacity = capacity_arg;
8417 psa_status_t expected_status = expected_status_arg;
8418 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8419
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008421
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008423
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8425 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008426
8427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 psa_key_derivation_abort(&operation);
8429 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008430}
8431/* END_CASE */
8432
8433/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008434void derive_input(int alg_arg,
8435 int step_arg1, int key_type_arg1, data_t *input1,
8436 int expected_status_arg1,
8437 int step_arg2, int key_type_arg2, data_t *input2,
8438 int expected_status_arg2,
8439 int step_arg3, int key_type_arg3, data_t *input3,
8440 int expected_status_arg3,
8441 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008442{
8443 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8445 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8446 psa_status_t expected_statuses[] = { expected_status_arg1,
8447 expected_status_arg2,
8448 expected_status_arg3 };
8449 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008450 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8451 MBEDTLS_SVC_KEY_ID_INIT,
8452 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008453 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8455 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008456 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008457 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008458 psa_status_t expected_output_status = expected_output_status_arg;
8459 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008460
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008462
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8464 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008465
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008467
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8469 mbedtls_test_set_step(i);
8470 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008471 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8473 psa_set_key_type(&attributes, key_types[i]);
8474 PSA_ASSERT(psa_import_key(&attributes,
8475 inputs[i]->x, inputs[i]->len,
8476 &keys[i]));
8477 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8478 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008479 // When taking a private key as secret input, use key agreement
8480 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8482 &operation, keys[i]),
8483 expected_statuses[i]);
8484 } else {
8485 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8486 keys[i]),
8487 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008488 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 } else {
8490 TEST_EQUAL(psa_key_derivation_input_bytes(
8491 &operation, steps[i],
8492 inputs[i]->x, inputs[i]->len),
8493 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008494 }
8495 }
8496
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 if (output_key_type != PSA_KEY_TYPE_NONE) {
8498 psa_reset_key_attributes(&attributes);
8499 psa_set_key_type(&attributes, output_key_type);
8500 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008501 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 psa_key_derivation_output_key(&attributes, &operation,
8503 &output_key);
8504 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008505 uint8_t buffer[1];
8506 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 psa_key_derivation_output_bytes(&operation,
8508 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008509 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008510 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008511
Janos Follathaf3c2a02019-06-12 12:34:34 +01008512exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 psa_key_derivation_abort(&operation);
8514 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8515 psa_destroy_key(keys[i]);
8516 }
8517 psa_destroy_key(output_key);
8518 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008519}
8520/* END_CASE */
8521
Janos Follathd958bb72019-07-03 15:02:16 +01008522/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008523void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008524{
Janos Follathd958bb72019-07-03 15:02:16 +01008525 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008527 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008528 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008529 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008530 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008531 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008533 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008535 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8536 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008541
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8543 psa_set_key_algorithm(&attributes, alg);
8544 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008545
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 PSA_ASSERT(psa_import_key(&attributes,
8547 key_data, sizeof(key_data),
8548 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008549
8550 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008551 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8552 input1, input1_length,
8553 input2, input2_length,
8554 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008557
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008558 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008559 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8560 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008561
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008563
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8565 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008566
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 psa_key_derivation_abort(&operation);
8569 psa_destroy_key(key);
8570 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008571}
8572/* END_CASE */
8573
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008574/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008575void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008576{
8577 uint8_t output_buffer[16];
8578 size_t buffer_size = 16;
8579 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008580 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008581
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8583 output_buffer, buffer_size)
8584 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008585
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8587 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008588
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008590
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8592 output_buffer, buffer_size)
8593 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008594
Gilles Peskine449bd832023-01-11 14:50:10 +01008595 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8596 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008597
8598exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008600}
8601/* END_CASE */
8602
8603/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008604void derive_output(int alg_arg,
8605 int step1_arg, data_t *input1, int expected_status_arg1,
8606 int step2_arg, data_t *input2, int expected_status_arg2,
8607 int step3_arg, data_t *input3, int expected_status_arg3,
8608 int step4_arg, data_t *input4, int expected_status_arg4,
8609 data_t *key_agreement_peer_key,
8610 int requested_capacity_arg,
8611 data_t *expected_output1,
8612 data_t *expected_output2,
8613 int other_key_input_type,
8614 int key_input_type,
8615 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008616{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008617 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8619 data_t *inputs[] = { input1, input2, input3, input4 };
8620 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8621 MBEDTLS_SVC_KEY_ID_INIT,
8622 MBEDTLS_SVC_KEY_ID_INIT,
8623 MBEDTLS_SVC_KEY_ID_INIT };
8624 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8625 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008626 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008627 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008628 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008630 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008632 size_t output_buffer_size = 0;
8633 uint8_t *output_buffer = NULL;
8634 size_t expected_capacity;
8635 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008636 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8637 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8638 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8639 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008640 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008641 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008642 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008643
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8645 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008646 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 }
8648 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008649 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008651 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 ASSERT_ALLOC(output_buffer, output_buffer_size);
8653 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008654
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008655 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8657 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8658 requested_capacity));
8659 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8660 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008661 case 0:
8662 break;
8663 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008665 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 TEST_EQUAL(psa_key_derivation_input_bytes(
8667 &operation, steps[i],
8668 inputs[i]->x, inputs[i]->len),
8669 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008670
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008672 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008674 break;
8675 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8677 psa_set_key_algorithm(&attributes1, alg);
8678 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008679
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 PSA_ASSERT(psa_import_key(&attributes1,
8681 inputs[i]->x, inputs[i]->len,
8682 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008683
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8685 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8686 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8687 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008688 }
8689
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8691 steps[i],
8692 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008693 break;
8694 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008696 break;
8697 }
8698 break;
8699 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008701 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8703 steps[i],
8704 inputs[i]->x,
8705 inputs[i]->len),
8706 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008707 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008708 case 1: // input key, type DERIVE
8709 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8711 psa_set_key_algorithm(&attributes2, alg);
8712 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008713
8714 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 if (other_key_input_type == 11) {
8716 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8717 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008718
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 PSA_ASSERT(psa_import_key(&attributes2,
8720 inputs[i]->x, inputs[i]->len,
8721 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008722
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8724 steps[i],
8725 keys[i]),
8726 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008727 break;
8728 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8730 psa_set_key_algorithm(&attributes3, alg);
8731 psa_set_key_type(&attributes3,
8732 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 PSA_ASSERT(psa_import_key(&attributes3,
8735 inputs[i]->x, inputs[i]->len,
8736 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008737
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 TEST_EQUAL(psa_key_derivation_key_agreement(
8739 &operation,
8740 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8741 keys[i], key_agreement_peer_key->x,
8742 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008743 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008744 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008746 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008747 }
8748
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008752 break;
8753 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008754 TEST_EQUAL(psa_key_derivation_input_bytes(
8755 &operation, steps[i],
8756 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008757
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008759 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008761 break;
8762 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008763 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8766 &current_capacity));
8767 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008768 expected_capacity = requested_capacity;
8769
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008771 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8772
8773 /* For output key derivation secret must be provided using
8774 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008776 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008778
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8780 psa_set_key_algorithm(&attributes4, alg);
8781 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8782 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008783
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8785 &derived_key), expected_status);
8786 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008787 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008789 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 status = psa_key_derivation_output_bytes(&operation,
8791 output_buffer, output_sizes[i]);
8792 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008793 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 TEST_ASSERT(status == PSA_SUCCESS ||
8795 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008796 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 } else if (expected_capacity == 0 ||
8798 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008799 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008801 expected_capacity = 0;
8802 continue;
8803 }
8804 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 PSA_ASSERT(status);
8806 if (output_sizes[i] != 0) {
8807 ASSERT_COMPARE(output_buffer, output_sizes[i],
8808 expected_outputs[i], output_sizes[i]);
8809 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008810 /* Check the operation status. */
8811 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8813 &current_capacity));
8814 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008815 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008816 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008818
8819exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 mbedtls_free(output_buffer);
8821 psa_key_derivation_abort(&operation);
8822 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8823 psa_destroy_key(keys[i]);
8824 }
8825 psa_destroy_key(derived_key);
8826 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008827}
8828/* END_CASE */
8829
8830/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008831void derive_full(int alg_arg,
8832 data_t *key_data,
8833 data_t *input1,
8834 data_t *input2,
8835 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008836{
Ronald Cron5425a212020-08-04 14:58:35 +02008837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008838 psa_algorithm_t alg = alg_arg;
8839 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008840 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008841 unsigned char output_buffer[16];
8842 size_t expected_capacity = requested_capacity;
8843 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008845
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008847
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8849 psa_set_key_algorithm(&attributes, alg);
8850 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008851
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8853 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8856 input1->x, input1->len,
8857 input2->x, input2->len,
8858 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008859 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8863 &current_capacity));
8864 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008865
8866 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 while (current_capacity > 0) {
8868 size_t read_size = sizeof(output_buffer);
8869 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008870 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 }
8872 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8873 output_buffer,
8874 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008875 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8877 &current_capacity));
8878 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008879 }
8880
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008881 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8883 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008886
8887exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 psa_key_derivation_abort(&operation);
8889 psa_destroy_key(key);
8890 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008891}
8892/* END_CASE */
8893
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008894/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008895void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8896 int derivation_step,
8897 int capacity, int expected_capacity_status_arg,
8898 data_t *expected_output,
8899 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008900{
8901 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8902 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008903 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008904 uint8_t *output_buffer = NULL;
8905 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008906 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8907 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8908 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008909
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 ASSERT_ALLOC(output_buffer, expected_output->len);
8911 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008912
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8914 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8915 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008916
Gilles Peskine449bd832023-01-11 14:50:10 +01008917 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8918 step, input->x, input->len),
8919 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008920
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008922 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008924
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8926 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008927
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 TEST_EQUAL(status, expected_output_status);
8929 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8930 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8931 expected_output->len);
8932 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008933
8934exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 mbedtls_free(output_buffer);
8936 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008937 PSA_DONE();
8938}
8939/* END_CASE */
8940
Janos Follathe60c9052019-07-03 13:51:30 +01008941/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008942void derive_key_exercise(int alg_arg,
8943 data_t *key_data,
8944 data_t *input1,
8945 data_t *input2,
8946 int derived_type_arg,
8947 int derived_bits_arg,
8948 int derived_usage_arg,
8949 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008950{
Ronald Cron5425a212020-08-04 14:58:35 +02008951 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8952 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008953 psa_algorithm_t alg = alg_arg;
8954 psa_key_type_t derived_type = derived_type_arg;
8955 size_t derived_bits = derived_bits_arg;
8956 psa_key_usage_t derived_usage = derived_usage_arg;
8957 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008959 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008961 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8966 psa_set_key_algorithm(&attributes, alg);
8967 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8968 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8969 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008970
8971 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8973 input1->x, input1->len,
8974 input2->x, input2->len,
8975 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008976 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 }
Janos Follathe60c9052019-07-03 13:51:30 +01008978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 psa_set_key_usage_flags(&attributes, derived_usage);
8980 psa_set_key_algorithm(&attributes, derived_alg);
8981 psa_set_key_type(&attributes, derived_type);
8982 psa_set_key_bits(&attributes, derived_bits);
8983 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8984 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008985
8986 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8988 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8989 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008990
8991 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008995
8996exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008997 /*
8998 * Key attributes may have been returned by psa_get_key_attributes()
8999 * thus reset them as required.
9000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009002
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 psa_key_derivation_abort(&operation);
9004 psa_destroy_key(base_key);
9005 psa_destroy_key(derived_key);
9006 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009007}
9008/* END_CASE */
9009
Janos Follath42fd8882019-07-03 14:17:09 +01009010/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009011void derive_key_export(int alg_arg,
9012 data_t *key_data,
9013 data_t *input1,
9014 data_t *input2,
9015 int bytes1_arg,
9016 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009017{
Ronald Cron5425a212020-08-04 14:58:35 +02009018 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9019 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009020 psa_algorithm_t alg = alg_arg;
9021 size_t bytes1 = bytes1_arg;
9022 size_t bytes2 = bytes2_arg;
9023 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009024 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009025 uint8_t *output_buffer = NULL;
9026 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009027 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9028 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009029 size_t length;
9030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 ASSERT_ALLOC(output_buffer, capacity);
9032 ASSERT_ALLOC(export_buffer, capacity);
9033 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9036 psa_set_key_algorithm(&base_attributes, alg);
9037 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9038 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9039 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009040
9041 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9043 input1->x, input1->len,
9044 input2->x, input2->len,
9045 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009046 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 }
Janos Follath42fd8882019-07-03 14:17:09 +01009048
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9050 output_buffer,
9051 capacity));
9052 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009053
9054 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9056 input1->x, input1->len,
9057 input2->x, input2->len,
9058 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009059 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 }
Janos Follath42fd8882019-07-03 14:17:09 +01009061
Gilles Peskine449bd832023-01-11 14:50:10 +01009062 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9063 psa_set_key_algorithm(&derived_attributes, 0);
9064 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9065 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9066 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9067 &derived_key));
9068 PSA_ASSERT(psa_export_key(derived_key,
9069 export_buffer, bytes1,
9070 &length));
9071 TEST_EQUAL(length, bytes1);
9072 PSA_ASSERT(psa_destroy_key(derived_key));
9073 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9074 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9075 &derived_key));
9076 PSA_ASSERT(psa_export_key(derived_key,
9077 export_buffer + bytes1, bytes2,
9078 &length));
9079 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009080
9081 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9083 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009084
9085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 mbedtls_free(output_buffer);
9087 mbedtls_free(export_buffer);
9088 psa_key_derivation_abort(&operation);
9089 psa_destroy_key(base_key);
9090 psa_destroy_key(derived_key);
9091 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009092}
9093/* END_CASE */
9094
9095/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009096void derive_key_type(int alg_arg,
9097 data_t *key_data,
9098 data_t *input1,
9099 data_t *input2,
9100 int key_type_arg, int bits_arg,
9101 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009102{
9103 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9104 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9105 const psa_algorithm_t alg = alg_arg;
9106 const psa_key_type_t key_type = key_type_arg;
9107 const size_t bits = bits_arg;
9108 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9109 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009111 uint8_t *export_buffer = NULL;
9112 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9113 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9114 size_t export_length;
9115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 ASSERT_ALLOC(export_buffer, export_buffer_size);
9117 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009118
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9120 psa_set_key_algorithm(&base_attributes, alg);
9121 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9122 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9123 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009124
Gilles Peskine449bd832023-01-11 14:50:10 +01009125 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009126 &operation, base_key, alg,
9127 input1->x, input1->len,
9128 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009131 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009132
Gilles Peskine449bd832023-01-11 14:50:10 +01009133 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9134 psa_set_key_algorithm(&derived_attributes, 0);
9135 psa_set_key_type(&derived_attributes, key_type);
9136 psa_set_key_bits(&derived_attributes, bits);
9137 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9138 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009139
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 PSA_ASSERT(psa_export_key(derived_key,
9141 export_buffer, export_buffer_size,
9142 &export_length));
9143 ASSERT_COMPARE(export_buffer, export_length,
9144 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009145
9146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 mbedtls_free(export_buffer);
9148 psa_key_derivation_abort(&operation);
9149 psa_destroy_key(base_key);
9150 psa_destroy_key(derived_key);
9151 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009152}
9153/* END_CASE */
9154
9155/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009156void derive_key(int alg_arg,
9157 data_t *key_data, data_t *input1, data_t *input2,
9158 int type_arg, int bits_arg,
9159 int expected_status_arg,
9160 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009161{
Ronald Cron5425a212020-08-04 14:58:35 +02009162 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9163 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009164 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009165 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009166 size_t bits = bits_arg;
9167 psa_status_t expected_status = expected_status_arg;
9168 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9169 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9170 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9175 psa_set_key_algorithm(&base_attributes, alg);
9176 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9177 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9178 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009179
Gilles Peskine449bd832023-01-11 14:50:10 +01009180 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9181 input1->x, input1->len,
9182 input2->x, input2->len,
9183 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009186
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9188 psa_set_key_algorithm(&derived_attributes, 0);
9189 psa_set_key_type(&derived_attributes, type);
9190 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009191
9192 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 psa_key_derivation_output_key(&derived_attributes,
9194 &operation,
9195 &derived_key);
9196 if (is_large_output > 0) {
9197 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9198 }
9199 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009200
9201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 psa_key_derivation_abort(&operation);
9203 psa_destroy_key(base_key);
9204 psa_destroy_key(derived_key);
9205 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009206}
9207/* END_CASE */
9208
9209/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009210void key_agreement_setup(int alg_arg,
9211 int our_key_type_arg, int our_key_alg_arg,
9212 data_t *our_key_data, data_t *peer_key_data,
9213 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009214{
Ronald Cron5425a212020-08-04 14:58:35 +02009215 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009216 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009217 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009218 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009219 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009221 psa_status_t expected_status = expected_status_arg;
9222 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009223
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009225
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9227 psa_set_key_algorithm(&attributes, our_key_alg);
9228 psa_set_key_type(&attributes, our_key_type);
9229 PSA_ASSERT(psa_import_key(&attributes,
9230 our_key_data->x, our_key_data->len,
9231 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009232
Gilles Peskine77f40d82019-04-11 21:27:06 +02009233 /* The tests currently include inputs that should fail at either step.
9234 * Test cases that fail at the setup step should be changed to call
9235 * key_derivation_setup instead, and this function should be renamed
9236 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 status = psa_key_derivation_setup(&operation, alg);
9238 if (status == PSA_SUCCESS) {
9239 TEST_EQUAL(psa_key_derivation_key_agreement(
9240 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9241 our_key,
9242 peer_key_data->x, peer_key_data->len),
9243 expected_status);
9244 } else {
9245 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009246 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009247
9248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 psa_key_derivation_abort(&operation);
9250 psa_destroy_key(our_key);
9251 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009252}
9253/* END_CASE */
9254
9255/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009256void raw_key_agreement(int alg_arg,
9257 int our_key_type_arg, data_t *our_key_data,
9258 data_t *peer_key_data,
9259 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009260{
Ronald Cron5425a212020-08-04 14:58:35 +02009261 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009262 psa_algorithm_t alg = alg_arg;
9263 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009265 unsigned char *output = NULL;
9266 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009267 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009268
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009270
Gilles Peskine449bd832023-01-11 14:50:10 +01009271 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9272 psa_set_key_algorithm(&attributes, alg);
9273 psa_set_key_type(&attributes, our_key_type);
9274 PSA_ASSERT(psa_import_key(&attributes,
9275 our_key_data->x, our_key_data->len,
9276 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009277
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9279 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009280
Gilles Peskine992bee82022-04-13 23:25:52 +02009281 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 TEST_LE_U(expected_output->len,
9283 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9284 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9285 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009286
9287 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 ASSERT_ALLOC(output, expected_output->len);
9289 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9290 peer_key_data->x, peer_key_data->len,
9291 output, expected_output->len,
9292 &output_length));
9293 ASSERT_COMPARE(output, output_length,
9294 expected_output->x, expected_output->len);
9295 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009296 output = NULL;
9297 output_length = ~0;
9298
9299 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 ASSERT_ALLOC(output, expected_output->len + 1);
9301 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9302 peer_key_data->x, peer_key_data->len,
9303 output, expected_output->len + 1,
9304 &output_length));
9305 ASSERT_COMPARE(output, output_length,
9306 expected_output->x, expected_output->len);
9307 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009308 output = NULL;
9309 output_length = ~0;
9310
9311 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 ASSERT_ALLOC(output, expected_output->len - 1);
9313 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9314 peer_key_data->x, peer_key_data->len,
9315 output, expected_output->len - 1,
9316 &output_length),
9317 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009318 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 TEST_LE_U(output_length, expected_output->len - 1);
9320 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009321 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009322
9323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 mbedtls_free(output);
9325 psa_destroy_key(our_key);
9326 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009327}
9328/* END_CASE */
9329
9330/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009331void key_agreement_capacity(int alg_arg,
9332 int our_key_type_arg, data_t *our_key_data,
9333 data_t *peer_key_data,
9334 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009335{
Ronald Cron5425a212020-08-04 14:58:35 +02009336 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009337 psa_algorithm_t alg = alg_arg;
9338 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009339 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009341 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009342 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009345
Gilles Peskine449bd832023-01-11 14:50:10 +01009346 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9347 psa_set_key_algorithm(&attributes, alg);
9348 psa_set_key_type(&attributes, our_key_type);
9349 PSA_ASSERT(psa_import_key(&attributes,
9350 our_key_data->x, our_key_data->len,
9351 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009352
Gilles Peskine449bd832023-01-11 14:50:10 +01009353 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9354 PSA_ASSERT(psa_key_derivation_key_agreement(
9355 &operation,
9356 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9357 peer_key_data->x, peer_key_data->len));
9358 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009359 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9361 PSA_KEY_DERIVATION_INPUT_INFO,
9362 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009363 }
Gilles Peskine59685592018-09-18 12:11:34 +02009364
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009365 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009366 PSA_ASSERT(psa_key_derivation_get_capacity(
9367 &operation, &actual_capacity));
9368 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009369
Gilles Peskinebf491972018-10-25 22:36:12 +02009370 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 while (actual_capacity > sizeof(output)) {
9372 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9373 output, sizeof(output)));
9374 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009375 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9377 output, actual_capacity));
9378 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9379 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009380
Gilles Peskine59685592018-09-18 12:11:34 +02009381exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 psa_key_derivation_abort(&operation);
9383 psa_destroy_key(our_key);
9384 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009385}
9386/* END_CASE */
9387
9388/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009389void key_agreement_output(int alg_arg,
9390 int our_key_type_arg, data_t *our_key_data,
9391 data_t *peer_key_data,
9392 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009393{
Ronald Cron5425a212020-08-04 14:58:35 +02009394 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009395 psa_algorithm_t alg = alg_arg;
9396 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009397 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009399 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009400
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9402 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009403
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009405
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9407 psa_set_key_algorithm(&attributes, alg);
9408 psa_set_key_type(&attributes, our_key_type);
9409 PSA_ASSERT(psa_import_key(&attributes,
9410 our_key_data->x, our_key_data->len,
9411 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009412
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9414 PSA_ASSERT(psa_key_derivation_key_agreement(
9415 &operation,
9416 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9417 peer_key_data->x, peer_key_data->len));
9418 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009419 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9421 PSA_KEY_DERIVATION_INPUT_INFO,
9422 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009423 }
Gilles Peskine59685592018-09-18 12:11:34 +02009424
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9426 actual_output,
9427 expected_output1->len));
9428 ASSERT_COMPARE(actual_output, expected_output1->len,
9429 expected_output1->x, expected_output1->len);
9430 if (expected_output2->len != 0) {
9431 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9432 actual_output,
9433 expected_output2->len));
9434 ASSERT_COMPARE(actual_output, expected_output2->len,
9435 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009436 }
Gilles Peskine59685592018-09-18 12:11:34 +02009437
9438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 psa_key_derivation_abort(&operation);
9440 psa_destroy_key(our_key);
9441 PSA_DONE();
9442 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009443}
9444/* END_CASE */
9445
9446/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009447void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009448{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009449 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009450 unsigned char *output = NULL;
9451 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009452 size_t i;
9453 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009454
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009456
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 ASSERT_ALLOC(output, bytes);
9458 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009459
Gilles Peskine449bd832023-01-11 14:50:10 +01009460 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009461
Gilles Peskinea50d7392018-06-21 10:22:13 +02009462 /* Run several times, to ensure that every output byte will be
9463 * nonzero at least once with overwhelming probability
9464 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009465 for (run = 0; run < 10; run++) {
9466 if (bytes != 0) {
9467 memset(output, 0, bytes);
9468 }
9469 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 for (i = 0; i < bytes; i++) {
9472 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009473 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009475 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009476 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009477
9478 /* Check that every byte was changed to nonzero at least once. This
9479 * validates that psa_generate_random is overwriting every byte of
9480 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009481 for (i = 0; i < bytes; i++) {
9482 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009483 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009484
9485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 PSA_DONE();
9487 mbedtls_free(output);
9488 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009489}
9490/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009491
9492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009493void generate_key(int type_arg,
9494 int bits_arg,
9495 int usage_arg,
9496 int alg_arg,
9497 int expected_status_arg,
9498 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009499{
Ronald Cron5425a212020-08-04 14:58:35 +02009500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009501 psa_key_type_t type = type_arg;
9502 psa_key_usage_t usage = usage_arg;
9503 size_t bits = bits_arg;
9504 psa_algorithm_t alg = alg_arg;
9505 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009507 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009508
Gilles Peskine449bd832023-01-11 14:50:10 +01009509 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009510
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 psa_set_key_usage_flags(&attributes, usage);
9512 psa_set_key_algorithm(&attributes, alg);
9513 psa_set_key_type(&attributes, type);
9514 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009515
9516 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009518
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 if (is_large_key > 0) {
9520 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9521 }
9522 TEST_EQUAL(status, expected_status);
9523 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009524 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009526
9527 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9529 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9530 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009531
Gilles Peskine818ca122018-06-20 18:16:48 +02009532 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009534 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009535 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009536
9537exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009538 /*
9539 * Key attributes may have been returned by psa_get_key_attributes()
9540 * thus reset them as required.
9541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009542 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009543
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 psa_destroy_key(key);
9545 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009546}
9547/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009548
Ronald Cronee414c72021-03-18 18:50:08 +01009549/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskine449bd832023-01-11 14:50:10 +01009550void generate_key_rsa(int bits_arg,
9551 data_t *e_arg,
9552 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009553{
Ronald Cron5425a212020-08-04 14:58:35 +02009554 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009555 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009556 size_t bits = bits_arg;
9557 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9558 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9559 psa_status_t expected_status = expected_status_arg;
9560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9561 uint8_t *exported = NULL;
9562 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009564 size_t exported_length = SIZE_MAX;
9565 uint8_t *e_read_buffer = NULL;
9566 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009568 size_t e_read_length = SIZE_MAX;
9569
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 if (e_arg->len == 0 ||
9571 (e_arg->len == 3 &&
9572 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009573 is_default_public_exponent = 1;
9574 e_read_size = 0;
9575 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009576 ASSERT_ALLOC(e_read_buffer, e_read_size);
9577 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009578
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009580
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 psa_set_key_usage_flags(&attributes, usage);
9582 psa_set_key_algorithm(&attributes, alg);
9583 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9584 e_arg->x, e_arg->len));
9585 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009586
9587 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009588 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9589 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009592
9593 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009594 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9595 TEST_EQUAL(psa_get_key_type(&attributes), type);
9596 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9597 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9598 e_read_buffer, e_read_size,
9599 &e_read_length));
9600 if (is_default_public_exponent) {
9601 TEST_EQUAL(e_read_length, 0);
9602 } else {
9603 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9604 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009605
9606 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009608 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009610
9611 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 PSA_ASSERT(psa_export_public_key(key,
9613 exported, exported_size,
9614 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009615 {
9616 uint8_t *p = exported;
9617 uint8_t *end = exported + exported_length;
9618 size_t len;
9619 /* RSAPublicKey ::= SEQUENCE {
9620 * modulus INTEGER, -- n
9621 * publicExponent INTEGER } -- e
9622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9624 MBEDTLS_ASN1_SEQUENCE |
9625 MBEDTLS_ASN1_CONSTRUCTED));
9626 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9627 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9628 MBEDTLS_ASN1_INTEGER));
9629 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009630 ++p;
9631 --len;
9632 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009633 if (e_arg->len == 0) {
9634 TEST_EQUAL(len, 3);
9635 TEST_EQUAL(p[0], 1);
9636 TEST_EQUAL(p[1], 0);
9637 TEST_EQUAL(p[2], 1);
9638 } else {
9639 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009640 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009641 }
9642
9643exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009644 /*
9645 * Key attributes may have been returned by psa_get_key_attributes() or
9646 * set by psa_set_key_domain_parameters() thus reset them as required.
9647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009649
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 psa_destroy_key(key);
9651 PSA_DONE();
9652 mbedtls_free(e_read_buffer);
9653 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009654}
9655/* END_CASE */
9656
Darryl Greend49a4992018-06-18 17:27:26 +01009657/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009658void persistent_key_load_key_from_storage(data_t *data,
9659 int type_arg, int bits_arg,
9660 int usage_flags_arg, int alg_arg,
9661 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009662{
Gilles Peskine449bd832023-01-11 14:50:10 +01009663 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9666 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009667 psa_key_type_t type = type_arg;
9668 size_t bits = bits_arg;
9669 psa_key_usage_t usage_flags = usage_flags_arg;
9670 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009671 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009672 unsigned char *first_export = NULL;
9673 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009674 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009675 size_t first_exported_length;
9676 size_t second_exported_length;
9677
Gilles Peskine449bd832023-01-11 14:50:10 +01009678 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9679 ASSERT_ALLOC(first_export, export_size);
9680 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009681 }
Darryl Greend49a4992018-06-18 17:27:26 +01009682
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 psa_set_key_id(&attributes, key_id);
9686 psa_set_key_usage_flags(&attributes, usage_flags);
9687 psa_set_key_algorithm(&attributes, alg);
9688 psa_set_key_type(&attributes, type);
9689 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009690
Gilles Peskine449bd832023-01-11 14:50:10 +01009691 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009692 case IMPORT_KEY:
9693 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9695 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009696 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009697
Darryl Green0c6575a2018-11-07 16:05:30 +00009698 case GENERATE_KEY:
9699 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009701 break;
9702
9703 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009704#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009705 {
9706 /* Create base key */
9707 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9708 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9709 psa_set_key_usage_flags(&base_attributes,
9710 PSA_KEY_USAGE_DERIVE);
9711 psa_set_key_algorithm(&base_attributes, derive_alg);
9712 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9713 PSA_ASSERT(psa_import_key(&base_attributes,
9714 data->x, data->len,
9715 &base_key));
9716 /* Derive a key. */
9717 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9718 PSA_ASSERT(psa_key_derivation_input_key(
9719 &operation,
9720 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9721 PSA_ASSERT(psa_key_derivation_input_bytes(
9722 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9723 NULL, 0));
9724 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9725 &operation,
9726 &key));
9727 PSA_ASSERT(psa_key_derivation_abort(&operation));
9728 PSA_ASSERT(psa_destroy_key(base_key));
9729 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9730 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009731#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009732 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009733#endif
9734 break;
9735
9736 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009738 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009739 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009740 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009741
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009742 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009743 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9744 PSA_ASSERT(psa_export_key(key,
9745 first_export, export_size,
9746 &first_exported_length));
9747 if (generation_method == IMPORT_KEY) {
9748 ASSERT_COMPARE(data->x, data->len,
9749 first_export, first_exported_length);
9750 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009751 }
Darryl Greend49a4992018-06-18 17:27:26 +01009752
9753 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009754 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009755 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009756 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009757
Darryl Greend49a4992018-06-18 17:27:26 +01009758 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009759 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9760 TEST_ASSERT(mbedtls_svc_key_id_equal(
9761 psa_get_key_id(&attributes), key_id));
9762 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9763 PSA_KEY_LIFETIME_PERSISTENT);
9764 TEST_EQUAL(psa_get_key_type(&attributes), type);
9765 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9766 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9767 mbedtls_test_update_key_usage_flags(usage_flags));
9768 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009769
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009770 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009771 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9772 PSA_ASSERT(psa_export_key(key,
9773 second_export, export_size,
9774 &second_exported_length));
9775 ASSERT_COMPARE(first_export, first_exported_length,
9776 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009777 }
9778
9779 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009782 }
Darryl Greend49a4992018-06-18 17:27:26 +01009783
9784exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009785 /*
9786 * Key attributes may have been returned by psa_get_key_attributes()
9787 * thus reset them as required.
9788 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009789 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009790
Gilles Peskine449bd832023-01-11 14:50:10 +01009791 mbedtls_free(first_export);
9792 mbedtls_free(second_export);
9793 psa_key_derivation_abort(&operation);
9794 psa_destroy_key(base_key);
9795 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009796 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009797}
9798/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009799
Neil Armstronga557cb82022-06-10 08:58:32 +02009800/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009801void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9802 int primitive_arg, int hash_arg, int role_arg,
9803 int test_input, data_t *pw_data,
9804 int inj_err_type_arg,
9805 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009806{
9807 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9808 psa_pake_operation_t operation = psa_pake_operation_init();
9809 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009810 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009811 psa_key_type_t key_type_pw = key_type_pw_arg;
9812 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009813 psa_algorithm_t hash_alg = hash_arg;
9814 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009815 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009817 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9818 psa_status_t expected_error = expected_error_arg;
9819 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009820 unsigned char *output_buffer = NULL;
9821 size_t output_len = 0;
9822
Gilles Peskine449bd832023-01-11 14:50:10 +01009823 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009824
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009825 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009826 PSA_PAKE_STEP_KEY_SHARE);
9827 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009828
Gilles Peskine449bd832023-01-11 14:50:10 +01009829 if (pw_data->len > 0) {
9830 psa_set_key_usage_flags(&attributes, key_usage_pw);
9831 psa_set_key_algorithm(&attributes, alg);
9832 psa_set_key_type(&attributes, key_type_pw);
9833 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9834 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009835 }
9836
Gilles Peskine449bd832023-01-11 14:50:10 +01009837 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9838 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9839 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009840
Gilles Peskine449bd832023-01-11 14:50:10 +01009841 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009842
Gilles Peskine449bd832023-01-11 14:50:10 +01009843 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9844 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9845 expected_error);
9846 PSA_ASSERT(psa_pake_abort(&operation));
9847 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9848 expected_error);
9849 PSA_ASSERT(psa_pake_abort(&operation));
9850 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9851 expected_error);
9852 PSA_ASSERT(psa_pake_abort(&operation));
9853 TEST_EQUAL(psa_pake_set_role(&operation, role),
9854 expected_error);
9855 PSA_ASSERT(psa_pake_abort(&operation));
9856 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9857 NULL, 0, NULL),
9858 expected_error);
9859 PSA_ASSERT(psa_pake_abort(&operation));
9860 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9861 expected_error);
9862 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009863 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009864 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009865
Gilles Peskine449bd832023-01-11 14:50:10 +01009866 status = psa_pake_setup(&operation, &cipher_suite);
9867 if (status != PSA_SUCCESS) {
9868 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009869 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009870 }
9871
Gilles Peskine449bd832023-01-11 14:50:10 +01009872 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9873 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9874 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009875 goto exit;
9876 }
9877
Gilles Peskine449bd832023-01-11 14:50:10 +01009878 status = psa_pake_set_role(&operation, role);
9879 if (status != PSA_SUCCESS) {
9880 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009881 goto exit;
9882 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009883
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 if (pw_data->len > 0) {
9885 status = psa_pake_set_password_key(&operation, key);
9886 if (status != PSA_SUCCESS) {
9887 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009888 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009889 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009890 }
9891
Gilles Peskine449bd832023-01-11 14:50:10 +01009892 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9893 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9894 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009895 goto exit;
9896 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009897
Gilles Peskine449bd832023-01-11 14:50:10 +01009898 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9899 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9900 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009901 goto exit;
9902 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009903
Gilles Peskine449bd832023-01-11 14:50:10 +01009904 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009905 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009906 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9907 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009908 goto exit;
9909 }
9910
Gilles Peskine449bd832023-01-11 14:50:10 +01009911 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009912 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009913 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9914 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009915 goto exit;
9916 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009917
Gilles Peskine449bd832023-01-11 14:50:10 +01009918 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9919 PSA_PAKE_STEP_KEY_SHARE);
9920 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9921 PSA_PAKE_STEP_ZK_PUBLIC);
9922 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9923 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009924
Gilles Peskine449bd832023-01-11 14:50:10 +01009925 if (test_input) {
9926 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9927 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9928 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009929 goto exit;
9930 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009931
Gilles Peskine449bd832023-01-11 14:50:10 +01009932 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9933 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9934 output_buffer, size_zk_proof),
9935 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009936 goto exit;
9937 }
9938
Gilles Peskine449bd832023-01-11 14:50:10 +01009939 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9940 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9941 output_buffer, size_zk_proof),
9942 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009943 goto exit;
9944 }
9945
Gilles Peskine449bd832023-01-11 14:50:10 +01009946 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9947 output_buffer, size_key_share);
9948 if (status != PSA_SUCCESS) {
9949 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009950 goto exit;
9951 }
9952
Gilles Peskine449bd832023-01-11 14:50:10 +01009953 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9954 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9955 output_buffer, size_zk_public + 1),
9956 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009957 goto exit;
9958 }
9959
Gilles Peskine449bd832023-01-11 14:50:10 +01009960 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009961 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009962 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9963 output_buffer, size_zk_public + 1);
9964 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9965 output_buffer, size_zk_public),
9966 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009967 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009968 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009969 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009970 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9971 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9972 NULL, 0, NULL),
9973 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009974 goto exit;
9975 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009976
Gilles Peskine449bd832023-01-11 14:50:10 +01009977 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9978 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9979 output_buffer, buf_size, &output_len),
9980 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009981 goto exit;
9982 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009983
Gilles Peskine449bd832023-01-11 14:50:10 +01009984 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9985 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9986 output_buffer, buf_size, &output_len),
9987 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009988 goto exit;
9989 }
9990
Gilles Peskine449bd832023-01-11 14:50:10 +01009991 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9992 output_buffer, buf_size, &output_len);
9993 if (status != PSA_SUCCESS) {
9994 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009995 goto exit;
9996 }
9997
Gilles Peskine449bd832023-01-11 14:50:10 +01009998 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009999
Gilles Peskine449bd832023-01-11 14:50:10 +010010000 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10001 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10002 output_buffer, size_zk_public - 1, &output_len),
10003 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010004 goto exit;
10005 }
10006
Gilles Peskine449bd832023-01-11 14:50:10 +010010007 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010008 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010009 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10010 output_buffer, size_zk_public - 1, &output_len);
10011 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10012 output_buffer, buf_size, &output_len),
10013 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010014 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010015 }
10016 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010017
10018exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010019 PSA_ASSERT(psa_destroy_key(key));
10020 PSA_ASSERT(psa_pake_abort(&operation));
10021 mbedtls_free(output_buffer);
10022 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010023}
10024/* END_CASE */
10025
Neil Armstronga557cb82022-06-10 08:58:32 +020010026/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010027void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10028 int client_input_first, int inject_error,
10029 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010030{
10031 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10032 psa_pake_operation_t server = psa_pake_operation_init();
10033 psa_pake_operation_t client = psa_pake_operation_init();
10034 psa_algorithm_t alg = alg_arg;
10035 psa_algorithm_t hash_alg = hash_arg;
10036 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10038
Gilles Peskine449bd832023-01-11 14:50:10 +010010039 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010040
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10042 psa_set_key_algorithm(&attributes, alg);
10043 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10044 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10045 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010046
Gilles Peskine449bd832023-01-11 14:50:10 +010010047 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10048 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10049 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010050
10051
Gilles Peskine449bd832023-01-11 14:50:10 +010010052 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10053 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010054
Gilles Peskine449bd832023-01-11 14:50:10 +010010055 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10056 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010057
Gilles Peskine449bd832023-01-11 14:50:10 +010010058 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10059 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010060
Gilles Peskine449bd832023-01-11 14:50:10 +010010061 ecjpake_do_round(alg, primitive_arg, &server, &client,
10062 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010063
Gilles Peskine449bd832023-01-11 14:50:10 +010010064 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010066 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010067
Gilles Peskine449bd832023-01-11 14:50:10 +010010068 ecjpake_do_round(alg, primitive_arg, &server, &client,
10069 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010070
10071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010072 psa_destroy_key(key);
10073 psa_pake_abort(&server);
10074 psa_pake_abort(&client);
10075 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010076}
10077/* END_CASE */
10078
10079/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010080void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10081 int derive_alg_arg, data_t *pw_data,
10082 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010083{
10084 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10085 psa_pake_operation_t server = psa_pake_operation_init();
10086 psa_pake_operation_t client = psa_pake_operation_init();
10087 psa_algorithm_t alg = alg_arg;
10088 psa_algorithm_t hash_alg = hash_arg;
10089 psa_algorithm_t derive_alg = derive_alg_arg;
10090 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10091 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10092 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010094 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010095 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010096 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010097
Gilles Peskine449bd832023-01-11 14:50:10 +010010098 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010099
Gilles Peskine449bd832023-01-11 14:50:10 +010010100 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10101 psa_set_key_algorithm(&attributes, alg);
10102 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10103 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10104 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010105
Gilles Peskine449bd832023-01-11 14:50:10 +010010106 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10107 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10108 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010109
Neil Armstrong1e855602022-06-15 11:32:11 +020010110 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010111 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10112 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010113
Gilles Peskine449bd832023-01-11 14:50:10 +010010114 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10115 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10116 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10117 PSA_KEY_DERIVATION_INPUT_SEED,
10118 (const uint8_t *) "", 0));
10119 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10120 PSA_KEY_DERIVATION_INPUT_SEED,
10121 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010122 }
10123
Gilles Peskine449bd832023-01-11 14:50:10 +010010124 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10125 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010126
Gilles Peskine449bd832023-01-11 14:50:10 +010010127 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10128 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010129
Gilles Peskine449bd832023-01-11 14:50:10 +010010130 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10131 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010132
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10134 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10135 PSA_ERROR_BAD_STATE);
10136 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10137 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010138 goto exit;
10139 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010140
Neil Armstrongf983caf2022-06-15 15:27:48 +020010141 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010142 ecjpake_do_round(alg, primitive_arg, &server, &client,
10143 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010144
Gilles Peskine449bd832023-01-11 14:50:10 +010010145 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10146 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10147 PSA_ERROR_BAD_STATE);
10148 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10149 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010150 goto exit;
10151 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010152
Neil Armstrongf983caf2022-06-15 15:27:48 +020010153 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010154 ecjpake_do_round(alg, primitive_arg, &server, &client,
10155 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010156
Gilles Peskine449bd832023-01-11 14:50:10 +010010157 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10158 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010159
10160exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 psa_key_derivation_abort(&server_derive);
10162 psa_key_derivation_abort(&client_derive);
10163 psa_destroy_key(key);
10164 psa_pake_abort(&server);
10165 psa_pake_abort(&client);
10166 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010167}
10168/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010169
10170/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010171void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010172{
10173 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10174 const size_t bits = 256;
10175 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010176 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010177 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010178 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010179
10180 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10181 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010182 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10183 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10184 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10185 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010186 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010187 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10188 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010189
10190 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10192 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10193 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10194 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10195 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10196 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010197
10198 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010199 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10200 PSA_PAKE_OUTPUT_MAX_SIZE);
10201 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10202 PSA_PAKE_OUTPUT_MAX_SIZE);
10203 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10204 PSA_PAKE_OUTPUT_MAX_SIZE);
10205 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10206 PSA_PAKE_INPUT_MAX_SIZE);
10207 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10208 PSA_PAKE_INPUT_MAX_SIZE);
10209 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10210 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010211}
10212/* END_CASE */