blob: 8f0ea253c6201c4f7c50ce37915017031939a3b4 [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 */
6476void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6477 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006478 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006479{
6480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6481 psa_key_type_t key_type = key_type_arg;
6482 psa_algorithm_t alg = alg_arg;
6483 size_t key_bits;
6484 unsigned char *signature = NULL;
6485 size_t signature_size;
6486 size_t signature_length = 0xdeadbeef;
6487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6488 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006489 uint32_t num_ops = 0;
6490 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006491 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006492 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006493 size_t min_completes = 0;
6494 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006495
Paul Elliott712d5122022-12-07 14:03:10 +00006496 psa_sign_hash_interruptible_operation_t operation =
6497 psa_sign_hash_interruptible_operation_init();
6498
6499 PSA_ASSERT(psa_crypto_init());
6500
6501 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6502 psa_set_key_algorithm(&attributes, alg);
6503 psa_set_key_type(&attributes, key_type);
6504
6505 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6506 &key));
6507 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6508 key_bits = psa_get_key_bits(&attributes);
6509
6510 /* Allocate a buffer which has the size advertised by the
6511 * library. */
6512 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6513 key_bits, alg);
6514 TEST_ASSERT(signature_size != 0);
6515 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6516 ASSERT_ALLOC(signature, signature_size);
6517
Paul Elliott0c683352022-12-16 19:16:56 +00006518 psa_interruptible_set_max_ops(max_ops);
6519
Paul Elliott6f600372023-02-06 18:41:05 +00006520 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6521 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006522
Paul Elliott712d5122022-12-07 14:03:10 +00006523 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6524 TEST_ASSERT(num_ops_prior == 0);
6525
6526 /* Start performing the signature. */
6527 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6528 input_data->x, input_data->len));
6529
6530 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6531 TEST_ASSERT(num_ops_prior == 0);
6532
6533 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006534 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006535 status = psa_sign_hash_complete(&operation, signature, signature_size,
6536 &signature_length);
6537
Paul Elliott0c683352022-12-16 19:16:56 +00006538 num_completes++;
6539
Paul Elliott712d5122022-12-07 14:03:10 +00006540 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6541 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006542 /* We are asserting here that every complete makes progress
6543 * (completes some ops), which is true of the internal
6544 * implementation and probably any implementation, however this is
6545 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006546 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006547
Paul Elliott712d5122022-12-07 14:03:10 +00006548 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006549
6550 /* Ensure calling get_num_ops() twice still returns the same
6551 * number of ops as previously reported. */
6552 num_ops = psa_sign_hash_get_num_ops(&operation);
6553
6554 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006555 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006556 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006557
6558 TEST_ASSERT(status == PSA_SUCCESS);
6559
Paul Elliott0c683352022-12-16 19:16:56 +00006560 TEST_LE_U(min_completes, num_completes);
6561 TEST_LE_U(num_completes, max_completes);
6562
Paul Elliott712d5122022-12-07 14:03:10 +00006563 /* Verify that the signature is what is expected. */
6564 ASSERT_COMPARE(output_data->x, output_data->len,
6565 signature, signature_length);
6566
6567 PSA_ASSERT(psa_sign_hash_abort(&operation));
6568
Paul Elliott59ad9452022-12-18 15:09:02 +00006569 num_ops = psa_sign_hash_get_num_ops(&operation);
6570 TEST_ASSERT(num_ops == 0);
6571
Paul Elliott712d5122022-12-07 14:03:10 +00006572exit:
6573
6574 /*
6575 * Key attributes may have been returned by psa_get_key_attributes()
6576 * thus reset them as required.
6577 */
6578 psa_reset_key_attributes(&attributes);
6579
6580 psa_destroy_key(key);
6581 mbedtls_free(signature);
6582 PSA_DONE();
6583}
6584/* END_CASE */
6585
Gilles Peskine20035e32018-02-03 22:44:14 +01006586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006587void sign_hash_fail(int key_type_arg, data_t *key_data,
6588 int alg_arg, data_t *input_data,
6589 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006590{
Ronald Cron5425a212020-08-04 14:58:35 +02006591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006592 psa_key_type_t key_type = key_type_arg;
6593 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006594 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006595 psa_status_t actual_status;
6596 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006597 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006598 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006600
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6606 psa_set_key_algorithm(&attributes, alg);
6607 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006608
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6610 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 actual_status = psa_sign_hash(key, alg,
6613 input_data->x, input_data->len,
6614 signature, signature_size,
6615 &signature_length);
6616 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006617 /* The value of *signature_length is unspecified on error, but
6618 * whatever it is, it should be less than signature_size, so that
6619 * if the caller tries to read *signature_length bytes without
6620 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006622
6623exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 psa_reset_key_attributes(&attributes);
6625 psa_destroy_key(key);
6626 mbedtls_free(signature);
6627 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006628}
6629/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006630
Paul Elliott91007972022-12-16 12:21:24 +00006631/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6632void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6633 int alg_arg, data_t *input_data,
6634 int signature_size_arg,
6635 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006636 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006637 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006638{
6639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6640 psa_key_type_t key_type = key_type_arg;
6641 psa_algorithm_t alg = alg_arg;
6642 size_t signature_size = signature_size_arg;
6643 psa_status_t actual_status;
6644 psa_status_t expected_start_status = expected_start_status_arg;
6645 psa_status_t expected_complete_status = expected_complete_status_arg;
6646 unsigned char *signature = NULL;
6647 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006648 uint32_t num_ops = 0;
6649 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006650 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006651 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006652 size_t min_completes = 0;
6653 size_t max_completes = 0;
6654
Paul Elliott91007972022-12-16 12:21:24 +00006655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6656 psa_sign_hash_interruptible_operation_t operation =
6657 psa_sign_hash_interruptible_operation_init();
6658
6659 ASSERT_ALLOC(signature, signature_size);
6660
6661 PSA_ASSERT(psa_crypto_init());
6662
6663 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6664 psa_set_key_algorithm(&attributes, alg);
6665 psa_set_key_type(&attributes, key_type);
6666
6667 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6668 &key));
6669
Paul Elliott0c683352022-12-16 19:16:56 +00006670 psa_interruptible_set_max_ops(max_ops);
6671
Paul Elliott6f600372023-02-06 18:41:05 +00006672 interruptible_signverify_get_minmax_completes(max_ops,
6673 expected_complete_status,
6674 &min_completes,
6675 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006676
Paul Elliott91007972022-12-16 12:21:24 +00006677 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6678 TEST_ASSERT(num_ops_prior == 0);
6679
6680 /* Start performing the signature. */
6681 actual_status = psa_sign_hash_start(&operation, key, alg,
6682 input_data->x, input_data->len);
6683
6684 TEST_EQUAL(actual_status, expected_start_status);
6685
Paul Elliottc9774412023-02-06 15:14:07 +00006686 if (expected_start_status != PSA_SUCCESS) {
6687 actual_status = psa_sign_hash_start(&operation, key, alg,
6688 input_data->x, input_data->len);
6689
6690 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6691 }
6692
Paul Elliott91007972022-12-16 12:21:24 +00006693 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6694 TEST_ASSERT(num_ops_prior == 0);
6695
Paul Elliott91007972022-12-16 12:21:24 +00006696 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006697 do {
Paul Elliott91007972022-12-16 12:21:24 +00006698 actual_status = psa_sign_hash_complete(&operation, signature,
6699 signature_size,
6700 &signature_length);
6701
Paul Elliott0c683352022-12-16 19:16:56 +00006702 num_completes++;
6703
Paul Elliott334d7262023-01-20 17:29:41 +00006704 if (actual_status == PSA_SUCCESS ||
6705 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006706 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006707 /* We are asserting here that every complete makes progress
6708 * (completes some ops), which is true of the internal
6709 * implementation and probably any implementation, however this is
6710 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006711 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006712
Paul Elliott91007972022-12-16 12:21:24 +00006713 num_ops_prior = num_ops;
6714 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006715 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006716
Paul Elliottc9774412023-02-06 15:14:07 +00006717 TEST_EQUAL(actual_status, expected_complete_status);
6718
Paul Elliottefebad02023-02-15 16:56:45 +00006719 /* Check that another complete returns BAD_STATE. */
6720 actual_status = psa_sign_hash_complete(&operation, signature,
6721 signature_size,
6722 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006723
Paul Elliottefebad02023-02-15 16:56:45 +00006724 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006725
Paul Elliott91007972022-12-16 12:21:24 +00006726 PSA_ASSERT(psa_sign_hash_abort(&operation));
6727
Paul Elliott59ad9452022-12-18 15:09:02 +00006728 num_ops = psa_sign_hash_get_num_ops(&operation);
6729 TEST_ASSERT(num_ops == 0);
6730
Paul Elliott91007972022-12-16 12:21:24 +00006731 /* The value of *signature_length is unspecified on error, but
6732 * whatever it is, it should be less than signature_size, so that
6733 * if the caller tries to read *signature_length bytes without
6734 * checking the error code then they don't overflow a buffer. */
6735 TEST_LE_U(signature_length, signature_size);
6736
Paul Elliott0c683352022-12-16 19:16:56 +00006737 TEST_LE_U(min_completes, num_completes);
6738 TEST_LE_U(num_completes, max_completes);
6739
Paul Elliott91007972022-12-16 12:21:24 +00006740exit:
6741 psa_reset_key_attributes(&attributes);
6742 psa_destroy_key(key);
6743 mbedtls_free(signature);
6744 PSA_DONE();
6745}
6746/* END_CASE */
6747
mohammad16038cc1cee2018-03-28 01:21:33 +03006748/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006749void sign_verify_hash(int key_type_arg, data_t *key_data,
6750 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006751{
Ronald Cron5425a212020-08-04 14:58:35 +02006752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006753 psa_key_type_t key_type = key_type_arg;
6754 psa_algorithm_t alg = alg_arg;
6755 size_t key_bits;
6756 unsigned char *signature = NULL;
6757 size_t signature_size;
6758 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6764 psa_set_key_algorithm(&attributes, alg);
6765 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006766
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6768 &key));
6769 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6770 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006771
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006772 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006773 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6775 key_bits, alg);
6776 TEST_ASSERT(signature_size != 0);
6777 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6778 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006779
6780 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 PSA_ASSERT(psa_sign_hash(key, alg,
6782 input_data->x, input_data->len,
6783 signature, signature_size,
6784 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006785 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 TEST_LE_U(signature_length, signature_size);
6787 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006788
6789 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 PSA_ASSERT(psa_verify_hash(key, alg,
6791 input_data->x, input_data->len,
6792 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006793
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006795 /* Flip a bit in the input and verify that the signature is now
6796 * detected as invalid. Flip a bit at the beginning, not at the end,
6797 * because ECDSA may ignore the last few bits of the input. */
6798 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 TEST_EQUAL(psa_verify_hash(key, alg,
6800 input_data->x, input_data->len,
6801 signature, signature_length),
6802 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006803 }
6804
6805exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006806 /*
6807 * Key attributes may have been returned by psa_get_key_attributes()
6808 * thus reset them as required.
6809 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 psa_destroy_key(key);
6813 mbedtls_free(signature);
6814 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006815}
6816/* END_CASE */
6817
Paul Elliott712d5122022-12-07 14:03:10 +00006818/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6819void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006820 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006821 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006822{
6823 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6824 psa_key_type_t key_type = key_type_arg;
6825 psa_algorithm_t alg = alg_arg;
6826 size_t key_bits;
6827 unsigned char *signature = NULL;
6828 size_t signature_size;
6829 size_t signature_length = 0xdeadbeef;
6830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6831 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006832 uint32_t max_ops = max_ops_arg;
Paul Elliott0c683352022-12-16 19:16:56 +00006833 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006834 size_t min_completes = 0;
6835 size_t max_completes = 0;
6836
Paul Elliott712d5122022-12-07 14:03:10 +00006837 psa_sign_hash_interruptible_operation_t sign_operation =
6838 psa_sign_hash_interruptible_operation_init();
6839 psa_verify_hash_interruptible_operation_t verify_operation =
6840 psa_verify_hash_interruptible_operation_init();
6841
6842 PSA_ASSERT(psa_crypto_init());
6843
Paul Elliott0c683352022-12-16 19:16:56 +00006844 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6845 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006846 psa_set_key_algorithm(&attributes, alg);
6847 psa_set_key_type(&attributes, key_type);
6848
6849 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6850 &key));
6851 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6852 key_bits = psa_get_key_bits(&attributes);
6853
6854 /* Allocate a buffer which has the size advertised by the
6855 * library. */
6856 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6857 key_bits, alg);
6858 TEST_ASSERT(signature_size != 0);
6859 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6860 ASSERT_ALLOC(signature, signature_size);
6861
Paul Elliott0c683352022-12-16 19:16:56 +00006862 psa_interruptible_set_max_ops(max_ops);
6863
Paul Elliott6f600372023-02-06 18:41:05 +00006864 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6865 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006866
Paul Elliott712d5122022-12-07 14:03:10 +00006867 /* Start performing the signature. */
6868 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6869 input_data->x, input_data->len));
6870
6871 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006872 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006873
Paul Elliott0c683352022-12-16 19:16:56 +00006874 status = psa_sign_hash_complete(&sign_operation, signature,
6875 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006876 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006877
6878 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006879 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006880
6881 TEST_ASSERT(status == PSA_SUCCESS);
6882
Paul Elliott0c683352022-12-16 19:16:56 +00006883 TEST_LE_U(min_completes, num_completes);
6884 TEST_LE_U(num_completes, max_completes);
6885
Paul Elliott712d5122022-12-07 14:03:10 +00006886 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6887
6888 /* Check that the signature length looks sensible. */
6889 TEST_LE_U(signature_length, signature_size);
6890 TEST_ASSERT(signature_length > 0);
6891
Paul Elliott0c683352022-12-16 19:16:56 +00006892 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006893
6894 /* Start verification. */
6895 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6896 input_data->x, input_data->len,
6897 signature, signature_length));
6898
6899 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006900 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006901 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00006902
6903 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006904 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006905
6906 TEST_ASSERT(status == PSA_SUCCESS);
6907
Paul Elliott0c683352022-12-16 19:16:56 +00006908 TEST_LE_U(min_completes, num_completes);
6909 TEST_LE_U(num_completes, max_completes);
6910
Paul Elliott712d5122022-12-07 14:03:10 +00006911 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6912
6913 verify_operation = psa_verify_hash_interruptible_operation_init();
6914
6915 if (input_data->len != 0) {
6916 /* Flip a bit in the input and verify that the signature is now
6917 * detected as invalid. Flip a bit at the beginning, not at the end,
6918 * because ECDSA may ignore the last few bits of the input. */
6919 input_data->x[0] ^= 1;
6920
Paul Elliott712d5122022-12-07 14:03:10 +00006921 /* Start verification. */
6922 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6923 input_data->x, input_data->len,
6924 signature, signature_length));
6925
6926 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006927 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006928 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00006929 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006930
6931 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
6932 }
6933
6934 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6935
6936exit:
6937 /*
6938 * Key attributes may have been returned by psa_get_key_attributes()
6939 * thus reset them as required.
6940 */
6941 psa_reset_key_attributes(&attributes);
6942
6943 psa_destroy_key(key);
6944 mbedtls_free(signature);
6945 PSA_DONE();
6946}
6947/* END_CASE */
6948
Gilles Peskine9911b022018-06-29 17:30:48 +02006949/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006950void verify_hash(int key_type_arg, data_t *key_data,
6951 int alg_arg, data_t *hash_data,
6952 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006953{
Ronald Cron5425a212020-08-04 14:58:35 +02006954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006955 psa_key_type_t key_type = key_type_arg;
6956 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006958
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006962
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6964 psa_set_key_algorithm(&attributes, alg);
6965 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006966
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6968 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 PSA_ASSERT(psa_verify_hash(key, alg,
6971 hash_data->x, hash_data->len,
6972 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006973
itayzafrir5c753392018-05-08 11:18:38 +03006974exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 psa_reset_key_attributes(&attributes);
6976 psa_destroy_key(key);
6977 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006978}
6979/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006980
Paul Elliott712d5122022-12-07 14:03:10 +00006981/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6982void verify_hash_interruptible(int key_type_arg, data_t *key_data,
6983 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006984 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006985{
6986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6987 psa_key_type_t key_type = key_type_arg;
6988 psa_algorithm_t alg = alg_arg;
6989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6990 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006991 uint32_t num_ops = 0;
6992 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006993 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006994 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006995 size_t min_completes = 0;
6996 size_t max_completes = 0;
6997
Paul Elliott712d5122022-12-07 14:03:10 +00006998 psa_verify_hash_interruptible_operation_t operation =
6999 psa_verify_hash_interruptible_operation_init();
7000
7001 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7002
7003 PSA_ASSERT(psa_crypto_init());
7004
7005 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7006 psa_set_key_algorithm(&attributes, alg);
7007 psa_set_key_type(&attributes, key_type);
7008
7009 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7010 &key));
7011
Paul Elliott0c683352022-12-16 19:16:56 +00007012 psa_interruptible_set_max_ops(max_ops);
7013
Paul Elliott6f600372023-02-06 18:41:05 +00007014 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7015 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007016
Paul Elliott712d5122022-12-07 14:03:10 +00007017 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7018
7019 TEST_ASSERT(num_ops_prior == 0);
7020
7021 /* Start verification. */
7022 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7023 hash_data->x, hash_data->len,
7024 signature_data->x, signature_data->len)
7025 );
7026
7027 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7028
7029 TEST_ASSERT(num_ops_prior == 0);
7030
7031 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007032 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007033 status = psa_verify_hash_complete(&operation);
7034
Paul Elliott0c683352022-12-16 19:16:56 +00007035 num_completes++;
7036
Paul Elliott712d5122022-12-07 14:03:10 +00007037 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7038 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007039 /* We are asserting here that every complete makes progress
7040 * (completes some ops), which is true of the internal
7041 * implementation and probably any implementation, however this is
7042 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007043 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007044
Paul Elliott712d5122022-12-07 14:03:10 +00007045 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007046
7047 /* Ensure calling get_num_ops() twice still returns the same
7048 * number of ops as previously reported. */
7049 num_ops = psa_verify_hash_get_num_ops(&operation);
7050
7051 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007052 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007053 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007054
7055 TEST_ASSERT(status == PSA_SUCCESS);
7056
Paul Elliott0c683352022-12-16 19:16:56 +00007057 TEST_LE_U(min_completes, num_completes);
7058 TEST_LE_U(num_completes, max_completes);
7059
Paul Elliott712d5122022-12-07 14:03:10 +00007060 PSA_ASSERT(psa_verify_hash_abort(&operation));
7061
Paul Elliott59ad9452022-12-18 15:09:02 +00007062 num_ops = psa_verify_hash_get_num_ops(&operation);
7063 TEST_ASSERT(num_ops == 0);
7064
Paul Elliott712d5122022-12-07 14:03:10 +00007065exit:
7066 psa_reset_key_attributes(&attributes);
7067 psa_destroy_key(key);
7068 PSA_DONE();
7069}
7070/* END_CASE */
7071
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007072/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007073void verify_hash_fail(int key_type_arg, data_t *key_data,
7074 int alg_arg, data_t *hash_data,
7075 data_t *signature_data,
7076 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007077{
Ronald Cron5425a212020-08-04 14:58:35 +02007078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007079 psa_key_type_t key_type = key_type_arg;
7080 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007081 psa_status_t actual_status;
7082 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007086
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7088 psa_set_key_algorithm(&attributes, alg);
7089 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7092 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007093
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 actual_status = psa_verify_hash(key, alg,
7095 hash_data->x, hash_data->len,
7096 signature_data->x, signature_data->len);
7097 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007098
7099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 psa_reset_key_attributes(&attributes);
7101 psa_destroy_key(key);
7102 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007103}
7104/* END_CASE */
7105
Paul Elliott91007972022-12-16 12:21:24 +00007106/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7107void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7108 int alg_arg, data_t *hash_data,
7109 data_t *signature_data,
7110 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007111 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007112 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007113{
7114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7115 psa_key_type_t key_type = key_type_arg;
7116 psa_algorithm_t alg = alg_arg;
7117 psa_status_t actual_status;
7118 psa_status_t expected_start_status = expected_start_status_arg;
7119 psa_status_t expected_complete_status = expected_complete_status_arg;
7120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007121 uint32_t num_ops = 0;
7122 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007123 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007124 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007125 size_t min_completes = 0;
7126 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007127 psa_verify_hash_interruptible_operation_t operation =
7128 psa_verify_hash_interruptible_operation_init();
7129
7130 PSA_ASSERT(psa_crypto_init());
7131
7132 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7133 psa_set_key_algorithm(&attributes, alg);
7134 psa_set_key_type(&attributes, key_type);
7135
7136 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7137 &key));
7138
Paul Elliott0c683352022-12-16 19:16:56 +00007139 psa_interruptible_set_max_ops(max_ops);
7140
Paul Elliott6f600372023-02-06 18:41:05 +00007141 interruptible_signverify_get_minmax_completes(max_ops,
7142 expected_complete_status,
7143 &min_completes,
7144 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007145
Paul Elliott91007972022-12-16 12:21:24 +00007146 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7147 TEST_ASSERT(num_ops_prior == 0);
7148
7149 /* Start verification. */
7150 actual_status = psa_verify_hash_start(&operation, key, alg,
7151 hash_data->x, hash_data->len,
7152 signature_data->x,
7153 signature_data->len);
7154
7155 TEST_EQUAL(actual_status, expected_start_status);
7156
Paul Elliottc9774412023-02-06 15:14:07 +00007157 if (expected_start_status != PSA_SUCCESS) {
7158 actual_status = psa_verify_hash_start(&operation, key, alg,
7159 hash_data->x, hash_data->len,
7160 signature_data->x,
7161 signature_data->len);
7162
7163 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7164 }
7165
Paul Elliott91007972022-12-16 12:21:24 +00007166 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7167 TEST_ASSERT(num_ops_prior == 0);
7168
Paul Elliott91007972022-12-16 12:21:24 +00007169 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007170 do {
Paul Elliott91007972022-12-16 12:21:24 +00007171 actual_status = psa_verify_hash_complete(&operation);
7172
Paul Elliott0c683352022-12-16 19:16:56 +00007173 num_completes++;
7174
Paul Elliott334d7262023-01-20 17:29:41 +00007175 if (actual_status == PSA_SUCCESS ||
7176 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007177 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007178 /* We are asserting here that every complete makes progress
7179 * (completes some ops), which is true of the internal
7180 * implementation and probably any implementation, however this is
7181 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007182 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007183
Paul Elliott91007972022-12-16 12:21:24 +00007184 num_ops_prior = num_ops;
7185 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007186 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007187
Paul Elliottc9774412023-02-06 15:14:07 +00007188 TEST_EQUAL(actual_status, expected_complete_status);
7189
Paul Elliottefebad02023-02-15 16:56:45 +00007190 /* Check that another complete returns BAD_STATE. */
7191 actual_status = psa_verify_hash_complete(&operation);
7192 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007193
Paul Elliott0c683352022-12-16 19:16:56 +00007194 TEST_LE_U(min_completes, num_completes);
7195 TEST_LE_U(num_completes, max_completes);
7196
Paul Elliott91007972022-12-16 12:21:24 +00007197 PSA_ASSERT(psa_verify_hash_abort(&operation));
7198
Paul Elliott59ad9452022-12-18 15:09:02 +00007199 num_ops = psa_verify_hash_get_num_ops(&operation);
7200 TEST_ASSERT(num_ops == 0);
7201
Paul Elliott91007972022-12-16 12:21:24 +00007202exit:
7203 psa_reset_key_attributes(&attributes);
7204 psa_destroy_key(key);
7205 PSA_DONE();
7206}
7207/* END_CASE */
7208
Paul Elliott20a36062022-12-18 13:21:25 +00007209/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007210void interruptible_signverify_hash_state_test(int key_type_arg,
7211 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007212{
7213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7214 psa_key_type_t key_type = key_type_arg;
7215 psa_algorithm_t alg = alg_arg;
7216 size_t key_bits;
7217 unsigned char *signature = NULL;
7218 size_t signature_size;
7219 size_t signature_length = 0xdeadbeef;
7220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7221 psa_sign_hash_interruptible_operation_t sign_operation =
7222 psa_sign_hash_interruptible_operation_init();
7223 psa_verify_hash_interruptible_operation_t verify_operation =
7224 psa_verify_hash_interruptible_operation_init();
7225
7226 PSA_ASSERT(psa_crypto_init());
7227
7228 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7229 PSA_KEY_USAGE_VERIFY_HASH);
7230 psa_set_key_algorithm(&attributes, alg);
7231 psa_set_key_type(&attributes, key_type);
7232
7233 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7234 &key));
7235 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7236 key_bits = psa_get_key_bits(&attributes);
7237
7238 /* Allocate a buffer which has the size advertised by the
7239 * library. */
7240 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7241 key_bits, alg);
7242 TEST_ASSERT(signature_size != 0);
7243 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7244 ASSERT_ALLOC(signature, signature_size);
7245
7246 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7247
7248 /* --- Attempt completes prior to starts --- */
7249 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7250 signature_size,
7251 &signature_length),
7252 PSA_ERROR_BAD_STATE);
7253
7254 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7255
7256 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7257 PSA_ERROR_BAD_STATE);
7258
7259 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7260
7261 /* --- Aborts in all other places. --- */
7262 psa_sign_hash_abort(&sign_operation);
7263
7264 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7265 input_data->x, input_data->len));
7266
7267 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7268
7269 psa_interruptible_set_max_ops(1);
7270
7271 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7272 input_data->x, input_data->len));
7273
7274 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7275 signature_size,
7276 &signature_length),
7277 PSA_OPERATION_INCOMPLETE);
7278
7279 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7280
7281 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7282
7283 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7284 input_data->x, input_data->len));
7285
7286 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7287 signature_size,
7288 &signature_length));
7289
7290 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7291
7292 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7293
7294 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7295 input_data->x, input_data->len,
7296 signature, signature_length));
7297
7298 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7299
7300 psa_interruptible_set_max_ops(1);
7301
7302 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7303 input_data->x, input_data->len,
7304 signature, signature_length));
7305
7306 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7307 PSA_OPERATION_INCOMPLETE);
7308
7309 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7310
7311 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7312
7313 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7314 input_data->x, input_data->len,
7315 signature, signature_length));
7316
7317 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7318
7319 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7320
7321 /* --- Attempt double starts. --- */
7322
7323 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7324 input_data->x, input_data->len));
7325
7326 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7327 input_data->x, input_data->len),
7328 PSA_ERROR_BAD_STATE);
7329
7330 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7331
7332 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7333 input_data->x, input_data->len,
7334 signature, signature_length));
7335
7336 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7337 input_data->x, input_data->len,
7338 signature, signature_length),
7339 PSA_ERROR_BAD_STATE);
7340
7341 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7342
Paul Elliott76d671a2023-02-07 17:45:18 +00007343exit:
7344 /*
7345 * Key attributes may have been returned by psa_get_key_attributes()
7346 * thus reset them as required.
7347 */
7348 psa_reset_key_attributes(&attributes);
7349
7350 psa_destroy_key(key);
7351 mbedtls_free(signature);
7352 PSA_DONE();
7353}
7354/* END_CASE */
7355
7356/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7357void interruptible_signverify_hash_negative_tests(int key_type_arg,
7358 data_t *key_data, int alg_arg, data_t *input_data)
7359{
7360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7361 psa_key_type_t key_type = key_type_arg;
7362 psa_algorithm_t alg = alg_arg;
7363 size_t key_bits;
7364 unsigned char *signature = NULL;
7365 size_t signature_size;
7366 size_t signature_length = 0xdeadbeef;
7367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7368 uint8_t *input_buffer = NULL;
7369 psa_sign_hash_interruptible_operation_t sign_operation =
7370 psa_sign_hash_interruptible_operation_init();
7371 psa_verify_hash_interruptible_operation_t verify_operation =
7372 psa_verify_hash_interruptible_operation_init();
7373
7374 PSA_ASSERT(psa_crypto_init());
7375
7376 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7377 PSA_KEY_USAGE_VERIFY_HASH);
7378 psa_set_key_algorithm(&attributes, alg);
7379 psa_set_key_type(&attributes, key_type);
7380
7381 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7382 &key));
7383 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7384 key_bits = psa_get_key_bits(&attributes);
7385
7386 /* Allocate a buffer which has the size advertised by the
7387 * library. */
7388 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7389 key_bits, alg);
7390 TEST_ASSERT(signature_size != 0);
7391 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7392 ASSERT_ALLOC(signature, signature_size);
7393
Paul Elliott20a36062022-12-18 13:21:25 +00007394 /* --- Ensure changing the max ops mid operation works (operation should
7395 * complete successfully after setting max ops to unlimited --- */
7396 psa_interruptible_set_max_ops(1);
7397
7398 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7399 input_data->x, input_data->len));
7400
7401 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7402 signature_size,
7403 &signature_length),
7404 PSA_OPERATION_INCOMPLETE);
7405
7406 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7407
7408 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7409 signature_size,
7410 &signature_length));
7411
7412 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7413
7414 psa_interruptible_set_max_ops(1);
7415
7416 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7417 input_data->x, input_data->len,
7418 signature, signature_length));
7419
7420 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7421 PSA_OPERATION_INCOMPLETE);
7422
7423 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7424
7425 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7426
7427 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7428
7429 /* --- Change function inputs mid run, to cause an error (sign only,
7430 * verify passes all inputs to start. --- */
7431
7432 psa_interruptible_set_max_ops(1);
7433
7434 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7435 input_data->x, input_data->len));
7436
7437 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7438 signature_size,
7439 &signature_length),
7440 PSA_OPERATION_INCOMPLETE);
7441
7442 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7443 0,
7444 &signature_length),
7445 PSA_ERROR_BUFFER_TOO_SMALL);
7446
Paul Elliottc9774412023-02-06 15:14:07 +00007447 /* And test that this invalidates the operation. */
7448 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7449 0,
7450 &signature_length),
7451 PSA_ERROR_BAD_STATE);
7452
Paul Elliott20a36062022-12-18 13:21:25 +00007453 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7454
Paul Elliottf9c91a72023-02-05 18:06:38 +00007455 /* Trash the hash buffer in between start and complete, to ensure
7456 * no reliance on external buffers. */
7457 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7458
7459 input_buffer = mbedtls_calloc(1, input_data->len);
7460 TEST_ASSERT(input_buffer != NULL);
7461
7462 memcpy(input_buffer, input_data->x, input_data->len);
7463
7464 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7465 input_buffer, input_data->len));
7466
7467 memset(input_buffer, '!', input_data->len);
7468 mbedtls_free(input_buffer);
7469 input_buffer = NULL;
7470
7471 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7472 signature_size,
7473 &signature_length));
7474
7475 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7476
7477 input_buffer = mbedtls_calloc(1, input_data->len);
7478 TEST_ASSERT(input_buffer != NULL);
7479
7480 memcpy(input_buffer, input_data->x, input_data->len);
7481
7482 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7483 input_buffer, input_data->len,
7484 signature, signature_length));
7485
7486 memset(input_buffer, '!', input_data->len);
7487 mbedtls_free(input_buffer);
7488 input_buffer = NULL;
7489
7490 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7491
7492 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7493
Paul Elliott20a36062022-12-18 13:21:25 +00007494exit:
7495 /*
7496 * Key attributes may have been returned by psa_get_key_attributes()
7497 * thus reset them as required.
7498 */
7499 psa_reset_key_attributes(&attributes);
7500
7501 psa_destroy_key(key);
7502 mbedtls_free(signature);
7503 PSA_DONE();
7504}
7505/* END_CASE */
7506
Paul Elliotta4cb9092023-02-07 18:01:55 +00007507/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7508void interruptible_signverify_hash_maxops_tests(int key_type_arg,
7509 data_t *key_data, int alg_arg, data_t *input_data)
7510{
7511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7512 psa_key_type_t key_type = key_type_arg;
7513 psa_algorithm_t alg = alg_arg;
7514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007515 size_t key_bits;
7516 unsigned char *signature = NULL;
7517 size_t signature_size;
Paul Elliotta4cb9092023-02-07 18:01:55 +00007518 psa_sign_hash_interruptible_operation_t sign_operation =
7519 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007520 psa_verify_hash_interruptible_operation_t verify_operation =
7521 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007522
7523 PSA_ASSERT(psa_crypto_init());
7524
7525 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7526 PSA_KEY_USAGE_VERIFY_HASH);
7527 psa_set_key_algorithm(&attributes, alg);
7528 psa_set_key_type(&attributes, key_type);
7529
Paul Elliottf1743e22023-02-15 18:44:16 +00007530 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7531 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7532 key_bits = psa_get_key_bits(&attributes);
7533
7534 /* Allocate a buffer which has the size advertised by the
7535 * library. */
7536 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7537
7538 TEST_ASSERT(signature_size != 0);
7539 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7540 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007541
7542 /* Check that default max ops gets set if we don't set it. */
7543 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7544 input_data->x, input_data->len));
7545
7546 TEST_EQUAL(psa_interruptible_get_max_ops(),
7547 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7548
7549 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7550
Paul Elliottf1743e22023-02-15 18:44:16 +00007551 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7552 input_data->x, input_data->len,
7553 signature, signature_size));
7554
7555 TEST_EQUAL(psa_interruptible_get_max_ops(),
7556 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7557
7558 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7559
Paul Elliotta4cb9092023-02-07 18:01:55 +00007560 /* Check that max ops gets set properly. */
7561
7562 psa_interruptible_set_max_ops(0xbeef);
7563
Paul Elliottf1743e22023-02-15 18:44:16 +00007564 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007565
7566exit:
7567 /*
7568 * Key attributes may have been returned by psa_get_key_attributes()
7569 * thus reset them as required.
7570 */
7571 psa_reset_key_attributes(&attributes);
7572
7573 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007574 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007575 PSA_DONE();
7576}
7577/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007578
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007579/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007580void sign_message_deterministic(int key_type_arg,
7581 data_t *key_data,
7582 int alg_arg,
7583 data_t *input_data,
7584 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007585{
7586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7587 psa_key_type_t key_type = key_type_arg;
7588 psa_algorithm_t alg = alg_arg;
7589 size_t key_bits;
7590 unsigned char *signature = NULL;
7591 size_t signature_size;
7592 size_t signature_length = 0xdeadbeef;
7593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007596
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7598 psa_set_key_algorithm(&attributes, alg);
7599 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007600
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7602 &key));
7603 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7604 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007605
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7607 TEST_ASSERT(signature_size != 0);
7608 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7609 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007610
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 PSA_ASSERT(psa_sign_message(key, alg,
7612 input_data->x, input_data->len,
7613 signature, signature_size,
7614 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007615
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 ASSERT_COMPARE(output_data->x, output_data->len,
7617 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007618
7619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007621
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 psa_destroy_key(key);
7623 mbedtls_free(signature);
7624 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007625
7626}
7627/* END_CASE */
7628
7629/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007630void sign_message_fail(int key_type_arg,
7631 data_t *key_data,
7632 int alg_arg,
7633 data_t *input_data,
7634 int signature_size_arg,
7635 int expected_status_arg)
7636{
7637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7638 psa_key_type_t key_type = key_type_arg;
7639 psa_algorithm_t alg = alg_arg;
7640 size_t signature_size = signature_size_arg;
7641 psa_status_t actual_status;
7642 psa_status_t expected_status = expected_status_arg;
7643 unsigned char *signature = NULL;
7644 size_t signature_length = 0xdeadbeef;
7645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7646
7647 ASSERT_ALLOC(signature, signature_size);
7648
7649 PSA_ASSERT(psa_crypto_init());
7650
7651 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7652 psa_set_key_algorithm(&attributes, alg);
7653 psa_set_key_type(&attributes, key_type);
7654
7655 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7656 &key));
7657
7658 actual_status = psa_sign_message(key, alg,
7659 input_data->x, input_data->len,
7660 signature, signature_size,
7661 &signature_length);
7662 TEST_EQUAL(actual_status, expected_status);
7663 /* The value of *signature_length is unspecified on error, but
7664 * whatever it is, it should be less than signature_size, so that
7665 * if the caller tries to read *signature_length bytes without
7666 * checking the error code then they don't overflow a buffer. */
7667 TEST_LE_U(signature_length, signature_size);
7668
7669exit:
7670 psa_reset_key_attributes(&attributes);
7671 psa_destroy_key(key);
7672 mbedtls_free(signature);
7673 PSA_DONE();
7674}
7675/* END_CASE */
7676
7677/* BEGIN_CASE */
7678void sign_verify_message(int key_type_arg,
7679 data_t *key_data,
7680 int alg_arg,
7681 data_t *input_data)
7682{
7683 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7684 psa_key_type_t key_type = key_type_arg;
7685 psa_algorithm_t alg = alg_arg;
7686 size_t key_bits;
7687 unsigned char *signature = NULL;
7688 size_t signature_size;
7689 size_t signature_length = 0xdeadbeef;
7690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7691
7692 PSA_ASSERT(psa_crypto_init());
7693
7694 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7695 PSA_KEY_USAGE_VERIFY_MESSAGE);
7696 psa_set_key_algorithm(&attributes, alg);
7697 psa_set_key_type(&attributes, key_type);
7698
7699 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7700 &key));
7701 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7702 key_bits = psa_get_key_bits(&attributes);
7703
7704 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7705 TEST_ASSERT(signature_size != 0);
7706 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7707 ASSERT_ALLOC(signature, signature_size);
7708
7709 PSA_ASSERT(psa_sign_message(key, alg,
7710 input_data->x, input_data->len,
7711 signature, signature_size,
7712 &signature_length));
7713 TEST_LE_U(signature_length, signature_size);
7714 TEST_ASSERT(signature_length > 0);
7715
7716 PSA_ASSERT(psa_verify_message(key, alg,
7717 input_data->x, input_data->len,
7718 signature, signature_length));
7719
7720 if (input_data->len != 0) {
7721 /* Flip a bit in the input and verify that the signature is now
7722 * detected as invalid. Flip a bit at the beginning, not at the end,
7723 * because ECDSA may ignore the last few bits of the input. */
7724 input_data->x[0] ^= 1;
7725 TEST_EQUAL(psa_verify_message(key, alg,
7726 input_data->x, input_data->len,
7727 signature, signature_length),
7728 PSA_ERROR_INVALID_SIGNATURE);
7729 }
7730
7731exit:
7732 psa_reset_key_attributes(&attributes);
7733
7734 psa_destroy_key(key);
7735 mbedtls_free(signature);
7736 PSA_DONE();
7737}
7738/* END_CASE */
7739
7740/* BEGIN_CASE */
7741void verify_message(int key_type_arg,
7742 data_t *key_data,
7743 int alg_arg,
7744 data_t *input_data,
7745 data_t *signature_data)
7746{
7747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7748 psa_key_type_t key_type = key_type_arg;
7749 psa_algorithm_t alg = alg_arg;
7750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7751
7752 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7753
7754 PSA_ASSERT(psa_crypto_init());
7755
7756 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7757 psa_set_key_algorithm(&attributes, alg);
7758 psa_set_key_type(&attributes, key_type);
7759
7760 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7761 &key));
7762
7763 PSA_ASSERT(psa_verify_message(key, alg,
7764 input_data->x, input_data->len,
7765 signature_data->x, signature_data->len));
7766
7767exit:
7768 psa_reset_key_attributes(&attributes);
7769 psa_destroy_key(key);
7770 PSA_DONE();
7771}
7772/* END_CASE */
7773
7774/* BEGIN_CASE */
7775void verify_message_fail(int key_type_arg,
7776 data_t *key_data,
7777 int alg_arg,
7778 data_t *hash_data,
7779 data_t *signature_data,
7780 int expected_status_arg)
7781{
7782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7783 psa_key_type_t key_type = key_type_arg;
7784 psa_algorithm_t alg = alg_arg;
7785 psa_status_t actual_status;
7786 psa_status_t expected_status = expected_status_arg;
7787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7788
7789 PSA_ASSERT(psa_crypto_init());
7790
7791 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7792 psa_set_key_algorithm(&attributes, alg);
7793 psa_set_key_type(&attributes, key_type);
7794
7795 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7796 &key));
7797
7798 actual_status = psa_verify_message(key, alg,
7799 hash_data->x, hash_data->len,
7800 signature_data->x,
7801 signature_data->len);
7802 TEST_EQUAL(actual_status, expected_status);
7803
7804exit:
7805 psa_reset_key_attributes(&attributes);
7806 psa_destroy_key(key);
7807 PSA_DONE();
7808}
7809/* END_CASE */
7810
7811/* BEGIN_CASE */
7812void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02007813 data_t *key_data,
7814 int alg_arg,
7815 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 data_t *label,
7817 int expected_output_length_arg,
7818 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02007819{
Ronald Cron5425a212020-08-04 14:58:35 +02007820 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007821 psa_key_type_t key_type = key_type_arg;
7822 psa_algorithm_t alg = alg_arg;
7823 size_t expected_output_length = expected_output_length_arg;
7824 size_t key_bits;
7825 unsigned char *output = NULL;
7826 size_t output_size;
7827 size_t output_length = ~0;
7828 psa_status_t actual_status;
7829 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007831
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007833
Gilles Peskine656896e2018-06-29 19:12:28 +02007834 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
7836 psa_set_key_algorithm(&attributes, alg);
7837 psa_set_key_type(&attributes, key_type);
7838 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7839 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02007840
7841 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7843 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007844
Gilles Peskine449bd832023-01-11 14:50:10 +01007845 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7846 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7847 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02007848
7849 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01007850 actual_status = psa_asymmetric_encrypt(key, alg,
7851 input_data->x, input_data->len,
7852 label->x, label->len,
7853 output, output_size,
7854 &output_length);
7855 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01007856 if (actual_status == PSA_SUCCESS) {
7857 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01007858 } else {
7859 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01007860 }
Gilles Peskine656896e2018-06-29 19:12:28 +02007861
Gilles Peskine68428122018-06-30 18:42:41 +02007862 /* If the label is empty, the test framework puts a non-null pointer
7863 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007865 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 if (output_size != 0) {
7867 memset(output, 0, output_size);
7868 }
7869 actual_status = psa_asymmetric_encrypt(key, alg,
7870 input_data->x, input_data->len,
7871 NULL, label->len,
7872 output, output_size,
7873 &output_length);
7874 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01007875 if (actual_status == PSA_SUCCESS) {
7876 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01007877 } else {
7878 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01007879 }
Gilles Peskine68428122018-06-30 18:42:41 +02007880 }
7881
Gilles Peskine656896e2018-06-29 19:12:28 +02007882exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007883 /*
7884 * Key attributes may have been returned by psa_get_key_attributes()
7885 * thus reset them as required.
7886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007888
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 psa_destroy_key(key);
7890 mbedtls_free(output);
7891 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02007892}
7893/* END_CASE */
7894
7895/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007896void asymmetric_encrypt_decrypt(int key_type_arg,
7897 data_t *key_data,
7898 int alg_arg,
7899 data_t *input_data,
7900 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007901{
Ronald Cron5425a212020-08-04 14:58:35 +02007902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007903 psa_key_type_t key_type = key_type_arg;
7904 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007905 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007906 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007907 size_t output_size;
7908 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007909 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007910 size_t output2_size;
7911 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007915
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
7917 psa_set_key_algorithm(&attributes, alg);
7918 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007919
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7921 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007922
7923 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7925 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007926
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7928 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7929 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007930
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007931 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 TEST_LE_U(output2_size,
7933 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
7934 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7935 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007936
Gilles Peskineeebd7382018-06-08 18:11:54 +02007937 /* We test encryption by checking that encrypt-then-decrypt gives back
7938 * the original plaintext because of the non-optional random
7939 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007940 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
7941 input_data->x, input_data->len,
7942 label->x, label->len,
7943 output, output_size,
7944 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007945 /* We don't know what ciphertext length to expect, but check that
7946 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007948
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7950 output, output_length,
7951 label->x, label->len,
7952 output2, output2_size,
7953 &output2_length));
7954 ASSERT_COMPARE(input_data->x, input_data->len,
7955 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007956
7957exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007958 /*
7959 * Key attributes may have been returned by psa_get_key_attributes()
7960 * thus reset them as required.
7961 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007963
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 psa_destroy_key(key);
7965 mbedtls_free(output);
7966 mbedtls_free(output2);
7967 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007968}
7969/* END_CASE */
7970
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007971/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007972void asymmetric_decrypt(int key_type_arg,
7973 data_t *key_data,
7974 int alg_arg,
7975 data_t *input_data,
7976 data_t *label,
7977 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007978{
Ronald Cron5425a212020-08-04 14:58:35 +02007979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007980 psa_key_type_t key_type = key_type_arg;
7981 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007982 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007983 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007984 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007985 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007987
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007989
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7991 psa_set_key_algorithm(&attributes, alg);
7992 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007993
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7995 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007996
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7998 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007999
8000 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008001 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8002 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8003 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008004
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8006 input_data->x, input_data->len,
8007 label->x, label->len,
8008 output,
8009 output_size,
8010 &output_length));
8011 ASSERT_COMPARE(expected_data->x, expected_data->len,
8012 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008013
Gilles Peskine68428122018-06-30 18:42:41 +02008014 /* If the label is empty, the test framework puts a non-null pointer
8015 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008016 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008017 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 if (output_size != 0) {
8019 memset(output, 0, output_size);
8020 }
8021 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8022 input_data->x, input_data->len,
8023 NULL, label->len,
8024 output,
8025 output_size,
8026 &output_length));
8027 ASSERT_COMPARE(expected_data->x, expected_data->len,
8028 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008029 }
8030
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008032 psa_reset_key_attributes(&attributes);
8033 psa_destroy_key(key);
8034 mbedtls_free(output);
8035 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008036}
8037/* END_CASE */
8038
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008039/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008040void asymmetric_decrypt_fail(int key_type_arg,
8041 data_t *key_data,
8042 int alg_arg,
8043 data_t *input_data,
8044 data_t *label,
8045 int output_size_arg,
8046 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008047{
Ronald Cron5425a212020-08-04 14:58:35 +02008048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008049 psa_key_type_t key_type = key_type_arg;
8050 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008051 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008052 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008053 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008054 psa_status_t actual_status;
8055 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008057
Gilles Peskine449bd832023-01-11 14:50:10 +01008058 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008059
Gilles Peskine449bd832023-01-11 14:50:10 +01008060 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008061
Gilles Peskine449bd832023-01-11 14:50:10 +01008062 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8063 psa_set_key_algorithm(&attributes, alg);
8064 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008065
Gilles Peskine449bd832023-01-11 14:50:10 +01008066 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8067 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008068
Gilles Peskine449bd832023-01-11 14:50:10 +01008069 actual_status = psa_asymmetric_decrypt(key, alg,
8070 input_data->x, input_data->len,
8071 label->x, label->len,
8072 output, output_size,
8073 &output_length);
8074 TEST_EQUAL(actual_status, expected_status);
8075 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008076
Gilles Peskine68428122018-06-30 18:42:41 +02008077 /* If the label is empty, the test framework puts a non-null pointer
8078 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008079 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008080 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008081 if (output_size != 0) {
8082 memset(output, 0, output_size);
8083 }
8084 actual_status = psa_asymmetric_decrypt(key, alg,
8085 input_data->x, input_data->len,
8086 NULL, label->len,
8087 output, output_size,
8088 &output_length);
8089 TEST_EQUAL(actual_status, expected_status);
8090 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008091 }
8092
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 psa_reset_key_attributes(&attributes);
8095 psa_destroy_key(key);
8096 mbedtls_free(output);
8097 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008098}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008099/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008100
8101/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008102void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008103{
8104 /* Test each valid way of initializing the object, except for `= {0}`, as
8105 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8106 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008107 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008108 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008110 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8111 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008112
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008115 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008116 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8117 PSA_ERROR_BAD_STATE);
8118 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8119 PSA_ERROR_BAD_STATE);
8120 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8121 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008122
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008123 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 PSA_ASSERT(psa_key_derivation_abort(&func));
8125 PSA_ASSERT(psa_key_derivation_abort(&init));
8126 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008127}
8128/* END_CASE */
8129
Janos Follath16de4a42019-06-13 16:32:24 +01008130/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008131void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008132{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008133 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008134 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008135 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008136
Gilles Peskine449bd832023-01-11 14:50:10 +01008137 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008138
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8140 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008141
8142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 psa_key_derivation_abort(&operation);
8144 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008145}
8146/* END_CASE */
8147
Janos Follathaf3c2a02019-06-12 12:34:34 +01008148/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008149void derive_set_capacity(int alg_arg, int capacity_arg,
8150 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008151{
8152 psa_algorithm_t alg = alg_arg;
8153 size_t capacity = capacity_arg;
8154 psa_status_t expected_status = expected_status_arg;
8155 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8156
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008158
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008160
Gilles Peskine449bd832023-01-11 14:50:10 +01008161 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8162 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008163
8164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 psa_key_derivation_abort(&operation);
8166 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008167}
8168/* END_CASE */
8169
8170/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008171void derive_input(int alg_arg,
8172 int step_arg1, int key_type_arg1, data_t *input1,
8173 int expected_status_arg1,
8174 int step_arg2, int key_type_arg2, data_t *input2,
8175 int expected_status_arg2,
8176 int step_arg3, int key_type_arg3, data_t *input3,
8177 int expected_status_arg3,
8178 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008179{
8180 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8182 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8183 psa_status_t expected_statuses[] = { expected_status_arg1,
8184 expected_status_arg2,
8185 expected_status_arg3 };
8186 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008187 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8188 MBEDTLS_SVC_KEY_ID_INIT,
8189 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008190 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8192 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008193 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008194 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008195 psa_status_t expected_output_status = expected_output_status_arg;
8196 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008197
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008199
Gilles Peskine449bd832023-01-11 14:50:10 +01008200 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8201 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008202
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008204
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8206 mbedtls_test_set_step(i);
8207 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008208 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8210 psa_set_key_type(&attributes, key_types[i]);
8211 PSA_ASSERT(psa_import_key(&attributes,
8212 inputs[i]->x, inputs[i]->len,
8213 &keys[i]));
8214 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8215 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008216 // When taking a private key as secret input, use key agreement
8217 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8219 &operation, keys[i]),
8220 expected_statuses[i]);
8221 } else {
8222 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8223 keys[i]),
8224 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008225 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 } else {
8227 TEST_EQUAL(psa_key_derivation_input_bytes(
8228 &operation, steps[i],
8229 inputs[i]->x, inputs[i]->len),
8230 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008231 }
8232 }
8233
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 if (output_key_type != PSA_KEY_TYPE_NONE) {
8235 psa_reset_key_attributes(&attributes);
8236 psa_set_key_type(&attributes, output_key_type);
8237 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008238 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 psa_key_derivation_output_key(&attributes, &operation,
8240 &output_key);
8241 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008242 uint8_t buffer[1];
8243 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 psa_key_derivation_output_bytes(&operation,
8245 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008246 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008248
Janos Follathaf3c2a02019-06-12 12:34:34 +01008249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 psa_key_derivation_abort(&operation);
8251 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8252 psa_destroy_key(keys[i]);
8253 }
8254 psa_destroy_key(output_key);
8255 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008256}
8257/* END_CASE */
8258
Janos Follathd958bb72019-07-03 15:02:16 +01008259/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008260void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008261{
Janos Follathd958bb72019-07-03 15:02:16 +01008262 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008264 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008265 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008266 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008268 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008270 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008272 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8273 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008276
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008278
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8280 psa_set_key_algorithm(&attributes, alg);
8281 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008282
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 PSA_ASSERT(psa_import_key(&attributes,
8284 key_data, sizeof(key_data),
8285 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008286
8287 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8289 input1, input1_length,
8290 input2, input2_length,
8291 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008292 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008294
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008295 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8297 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008298
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008300
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8302 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008303
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 psa_key_derivation_abort(&operation);
8306 psa_destroy_key(key);
8307 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008308}
8309/* END_CASE */
8310
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008311/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008312void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008313{
8314 uint8_t output_buffer[16];
8315 size_t buffer_size = 16;
8316 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008317 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008318
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8320 output_buffer, buffer_size)
8321 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008322
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8324 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008325
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008327
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8329 output_buffer, buffer_size)
8330 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008331
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8333 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008334
8335exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008337}
8338/* END_CASE */
8339
8340/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008341void derive_output(int alg_arg,
8342 int step1_arg, data_t *input1, int expected_status_arg1,
8343 int step2_arg, data_t *input2, int expected_status_arg2,
8344 int step3_arg, data_t *input3, int expected_status_arg3,
8345 int step4_arg, data_t *input4, int expected_status_arg4,
8346 data_t *key_agreement_peer_key,
8347 int requested_capacity_arg,
8348 data_t *expected_output1,
8349 data_t *expected_output2,
8350 int other_key_input_type,
8351 int key_input_type,
8352 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008353{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008354 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8356 data_t *inputs[] = { input1, input2, input3, input4 };
8357 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8358 MBEDTLS_SVC_KEY_ID_INIT,
8359 MBEDTLS_SVC_KEY_ID_INIT,
8360 MBEDTLS_SVC_KEY_ID_INIT };
8361 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8362 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008363 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008364 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008365 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008367 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008369 size_t output_buffer_size = 0;
8370 uint8_t *output_buffer = NULL;
8371 size_t expected_capacity;
8372 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008373 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8374 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8375 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8376 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008377 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008378 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008379 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008380
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8382 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008383 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 }
8385 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008386 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008388 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 ASSERT_ALLOC(output_buffer, output_buffer_size);
8390 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008391
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008392 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8394 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8395 requested_capacity));
8396 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8397 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008398 case 0:
8399 break;
8400 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008402 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 TEST_EQUAL(psa_key_derivation_input_bytes(
8404 &operation, steps[i],
8405 inputs[i]->x, inputs[i]->len),
8406 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008409 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008411 break;
8412 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008413 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8414 psa_set_key_algorithm(&attributes1, alg);
8415 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008416
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 PSA_ASSERT(psa_import_key(&attributes1,
8418 inputs[i]->x, inputs[i]->len,
8419 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008420
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8422 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8423 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8424 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008425 }
8426
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8428 steps[i],
8429 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008430 break;
8431 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008433 break;
8434 }
8435 break;
8436 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008438 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8440 steps[i],
8441 inputs[i]->x,
8442 inputs[i]->len),
8443 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008444 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008445 case 1: // input key, type DERIVE
8446 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008447 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8448 psa_set_key_algorithm(&attributes2, alg);
8449 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008450
8451 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 if (other_key_input_type == 11) {
8453 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8454 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 PSA_ASSERT(psa_import_key(&attributes2,
8457 inputs[i]->x, inputs[i]->len,
8458 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8461 steps[i],
8462 keys[i]),
8463 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008464 break;
8465 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8467 psa_set_key_algorithm(&attributes3, alg);
8468 psa_set_key_type(&attributes3,
8469 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008470
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 PSA_ASSERT(psa_import_key(&attributes3,
8472 inputs[i]->x, inputs[i]->len,
8473 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008474
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 TEST_EQUAL(psa_key_derivation_key_agreement(
8476 &operation,
8477 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8478 keys[i], key_agreement_peer_key->x,
8479 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008480 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008481 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008483 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008484 }
8485
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008487 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008489 break;
8490 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 TEST_EQUAL(psa_key_derivation_input_bytes(
8492 &operation, steps[i],
8493 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008498 break;
8499 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008500 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008501
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8503 &current_capacity));
8504 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008505 expected_capacity = requested_capacity;
8506
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008508 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8509
8510 /* For output key derivation secret must be provided using
8511 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008513 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008515
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8517 psa_set_key_algorithm(&attributes4, alg);
8518 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8519 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008520
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8522 &derived_key), expected_status);
8523 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008524 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008526 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 status = psa_key_derivation_output_bytes(&operation,
8528 output_buffer, output_sizes[i]);
8529 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008530 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 TEST_ASSERT(status == PSA_SUCCESS ||
8532 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008533 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 } else if (expected_capacity == 0 ||
8535 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008536 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008538 expected_capacity = 0;
8539 continue;
8540 }
8541 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 PSA_ASSERT(status);
8543 if (output_sizes[i] != 0) {
8544 ASSERT_COMPARE(output_buffer, output_sizes[i],
8545 expected_outputs[i], output_sizes[i]);
8546 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008547 /* Check the operation status. */
8548 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8550 &current_capacity));
8551 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008552 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008553 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008555
8556exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 mbedtls_free(output_buffer);
8558 psa_key_derivation_abort(&operation);
8559 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8560 psa_destroy_key(keys[i]);
8561 }
8562 psa_destroy_key(derived_key);
8563 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008564}
8565/* END_CASE */
8566
8567/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008568void derive_full(int alg_arg,
8569 data_t *key_data,
8570 data_t *input1,
8571 data_t *input2,
8572 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008573{
Ronald Cron5425a212020-08-04 14:58:35 +02008574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008575 psa_algorithm_t alg = alg_arg;
8576 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008577 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008578 unsigned char output_buffer[16];
8579 size_t expected_capacity = requested_capacity;
8580 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008582
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008584
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8586 psa_set_key_algorithm(&attributes, alg);
8587 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008588
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8590 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008591
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8593 input1->x, input1->len,
8594 input2->x, input2->len,
8595 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008598
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8600 &current_capacity));
8601 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008602
8603 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 while (current_capacity > 0) {
8605 size_t read_size = sizeof(output_buffer);
8606 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008607 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 }
8609 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8610 output_buffer,
8611 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008612 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8614 &current_capacity));
8615 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008616 }
8617
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008618 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8620 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008621
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008623
8624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 psa_key_derivation_abort(&operation);
8626 psa_destroy_key(key);
8627 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008628}
8629/* END_CASE */
8630
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008631/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008632void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8633 int derivation_step,
8634 int capacity, int expected_capacity_status_arg,
8635 data_t *expected_output,
8636 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008637{
8638 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8639 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008640 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008641 uint8_t *output_buffer = NULL;
8642 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008643 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8644 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8645 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008646
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 ASSERT_ALLOC(output_buffer, expected_output->len);
8648 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008649
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8651 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8652 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008653
Gilles Peskine449bd832023-01-11 14:50:10 +01008654 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8655 step, input->x, input->len),
8656 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008657
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008660 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008661
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8663 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008664
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 TEST_EQUAL(status, expected_output_status);
8666 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8667 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8668 expected_output->len);
8669 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008670
8671exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 mbedtls_free(output_buffer);
8673 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008674 PSA_DONE();
8675}
8676/* END_CASE */
8677
Janos Follathe60c9052019-07-03 13:51:30 +01008678/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008679void derive_key_exercise(int alg_arg,
8680 data_t *key_data,
8681 data_t *input1,
8682 data_t *input2,
8683 int derived_type_arg,
8684 int derived_bits_arg,
8685 int derived_usage_arg,
8686 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008687{
Ronald Cron5425a212020-08-04 14:58:35 +02008688 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8689 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008690 psa_algorithm_t alg = alg_arg;
8691 psa_key_type_t derived_type = derived_type_arg;
8692 size_t derived_bits = derived_bits_arg;
8693 psa_key_usage_t derived_usage = derived_usage_arg;
8694 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008696 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008697 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008698 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008699
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008701
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8703 psa_set_key_algorithm(&attributes, alg);
8704 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8705 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8706 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008707
8708 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8710 input1->x, input1->len,
8711 input2->x, input2->len,
8712 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 }
Janos Follathe60c9052019-07-03 13:51:30 +01008715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 psa_set_key_usage_flags(&attributes, derived_usage);
8717 psa_set_key_algorithm(&attributes, derived_alg);
8718 psa_set_key_type(&attributes, derived_type);
8719 psa_set_key_bits(&attributes, derived_bits);
8720 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8721 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008722
8723 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8725 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8726 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008727
8728 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008730 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008732
8733exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008734 /*
8735 * Key attributes may have been returned by psa_get_key_attributes()
8736 * thus reset them as required.
8737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008739
Gilles Peskine449bd832023-01-11 14:50:10 +01008740 psa_key_derivation_abort(&operation);
8741 psa_destroy_key(base_key);
8742 psa_destroy_key(derived_key);
8743 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008744}
8745/* END_CASE */
8746
Janos Follath42fd8882019-07-03 14:17:09 +01008747/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008748void derive_key_export(int alg_arg,
8749 data_t *key_data,
8750 data_t *input1,
8751 data_t *input2,
8752 int bytes1_arg,
8753 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008754{
Ronald Cron5425a212020-08-04 14:58:35 +02008755 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8756 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008757 psa_algorithm_t alg = alg_arg;
8758 size_t bytes1 = bytes1_arg;
8759 size_t bytes2 = bytes2_arg;
8760 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008761 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008762 uint8_t *output_buffer = NULL;
8763 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008764 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8765 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008766 size_t length;
8767
Gilles Peskine449bd832023-01-11 14:50:10 +01008768 ASSERT_ALLOC(output_buffer, capacity);
8769 ASSERT_ALLOC(export_buffer, capacity);
8770 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8773 psa_set_key_algorithm(&base_attributes, alg);
8774 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8775 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8776 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008777
8778 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8780 input1->x, input1->len,
8781 input2->x, input2->len,
8782 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008783 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 }
Janos Follath42fd8882019-07-03 14:17:09 +01008785
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8787 output_buffer,
8788 capacity));
8789 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008790
8791 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8793 input1->x, input1->len,
8794 input2->x, input2->len,
8795 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008796 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 }
Janos Follath42fd8882019-07-03 14:17:09 +01008798
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8800 psa_set_key_algorithm(&derived_attributes, 0);
8801 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
8802 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
8803 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8804 &derived_key));
8805 PSA_ASSERT(psa_export_key(derived_key,
8806 export_buffer, bytes1,
8807 &length));
8808 TEST_EQUAL(length, bytes1);
8809 PSA_ASSERT(psa_destroy_key(derived_key));
8810 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
8811 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8812 &derived_key));
8813 PSA_ASSERT(psa_export_key(derived_key,
8814 export_buffer + bytes1, bytes2,
8815 &length));
8816 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008817
8818 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
8820 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008821
8822exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 mbedtls_free(output_buffer);
8824 mbedtls_free(export_buffer);
8825 psa_key_derivation_abort(&operation);
8826 psa_destroy_key(base_key);
8827 psa_destroy_key(derived_key);
8828 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008829}
8830/* END_CASE */
8831
8832/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008833void derive_key_type(int alg_arg,
8834 data_t *key_data,
8835 data_t *input1,
8836 data_t *input2,
8837 int key_type_arg, int bits_arg,
8838 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008839{
8840 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8841 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8842 const psa_algorithm_t alg = alg_arg;
8843 const psa_key_type_t key_type = key_type_arg;
8844 const size_t bits = bits_arg;
8845 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8846 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008848 uint8_t *export_buffer = NULL;
8849 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8850 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8851 size_t export_length;
8852
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 ASSERT_ALLOC(export_buffer, export_buffer_size);
8854 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008855
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8857 psa_set_key_algorithm(&base_attributes, alg);
8858 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8859 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8860 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008863 &operation, base_key, alg,
8864 input1->x, input1->len,
8865 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008867 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008869
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8871 psa_set_key_algorithm(&derived_attributes, 0);
8872 psa_set_key_type(&derived_attributes, key_type);
8873 psa_set_key_bits(&derived_attributes, bits);
8874 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8875 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008876
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 PSA_ASSERT(psa_export_key(derived_key,
8878 export_buffer, export_buffer_size,
8879 &export_length));
8880 ASSERT_COMPARE(export_buffer, export_length,
8881 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008882
8883exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 mbedtls_free(export_buffer);
8885 psa_key_derivation_abort(&operation);
8886 psa_destroy_key(base_key);
8887 psa_destroy_key(derived_key);
8888 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008889}
8890/* END_CASE */
8891
8892/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008893void derive_key(int alg_arg,
8894 data_t *key_data, data_t *input1, data_t *input2,
8895 int type_arg, int bits_arg,
8896 int expected_status_arg,
8897 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02008898{
Ronald Cron5425a212020-08-04 14:58:35 +02008899 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8900 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008901 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008902 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008903 size_t bits = bits_arg;
8904 psa_status_t expected_status = expected_status_arg;
8905 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8906 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8907 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8908
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02008910
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8912 psa_set_key_algorithm(&base_attributes, alg);
8913 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8914 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8915 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02008916
Gilles Peskine449bd832023-01-11 14:50:10 +01008917 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8918 input1->x, input1->len,
8919 input2->x, input2->len,
8920 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02008921 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 }
Gilles Peskinec744d992019-07-30 17:26:54 +02008923
Gilles Peskine449bd832023-01-11 14:50:10 +01008924 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8925 psa_set_key_algorithm(&derived_attributes, 0);
8926 psa_set_key_type(&derived_attributes, type);
8927 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008928
8929 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 psa_key_derivation_output_key(&derived_attributes,
8931 &operation,
8932 &derived_key);
8933 if (is_large_output > 0) {
8934 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8935 }
8936 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02008937
8938exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 psa_key_derivation_abort(&operation);
8940 psa_destroy_key(base_key);
8941 psa_destroy_key(derived_key);
8942 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02008943}
8944/* END_CASE */
8945
8946/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008947void key_agreement_setup(int alg_arg,
8948 int our_key_type_arg, int our_key_alg_arg,
8949 data_t *our_key_data, data_t *peer_key_data,
8950 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02008951{
Ronald Cron5425a212020-08-04 14:58:35 +02008952 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008953 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008954 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008955 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008956 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008958 psa_status_t expected_status = expected_status_arg;
8959 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8964 psa_set_key_algorithm(&attributes, our_key_alg);
8965 psa_set_key_type(&attributes, our_key_type);
8966 PSA_ASSERT(psa_import_key(&attributes,
8967 our_key_data->x, our_key_data->len,
8968 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008969
Gilles Peskine77f40d82019-04-11 21:27:06 +02008970 /* The tests currently include inputs that should fail at either step.
8971 * Test cases that fail at the setup step should be changed to call
8972 * key_derivation_setup instead, and this function should be renamed
8973 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 status = psa_key_derivation_setup(&operation, alg);
8975 if (status == PSA_SUCCESS) {
8976 TEST_EQUAL(psa_key_derivation_key_agreement(
8977 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8978 our_key,
8979 peer_key_data->x, peer_key_data->len),
8980 expected_status);
8981 } else {
8982 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008983 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008984
8985exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 psa_key_derivation_abort(&operation);
8987 psa_destroy_key(our_key);
8988 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008989}
8990/* END_CASE */
8991
8992/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008993void raw_key_agreement(int alg_arg,
8994 int our_key_type_arg, data_t *our_key_data,
8995 data_t *peer_key_data,
8996 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008997{
Ronald Cron5425a212020-08-04 14:58:35 +02008998 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008999 psa_algorithm_t alg = alg_arg;
9000 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009002 unsigned char *output = NULL;
9003 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009004 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009005
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9009 psa_set_key_algorithm(&attributes, alg);
9010 psa_set_key_type(&attributes, our_key_type);
9011 PSA_ASSERT(psa_import_key(&attributes,
9012 our_key_data->x, our_key_data->len,
9013 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009014
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9016 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009017
Gilles Peskine992bee82022-04-13 23:25:52 +02009018 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 TEST_LE_U(expected_output->len,
9020 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9021 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9022 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009023
9024 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 ASSERT_ALLOC(output, expected_output->len);
9026 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9027 peer_key_data->x, peer_key_data->len,
9028 output, expected_output->len,
9029 &output_length));
9030 ASSERT_COMPARE(output, output_length,
9031 expected_output->x, expected_output->len);
9032 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009033 output = NULL;
9034 output_length = ~0;
9035
9036 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 ASSERT_ALLOC(output, expected_output->len + 1);
9038 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9039 peer_key_data->x, peer_key_data->len,
9040 output, expected_output->len + 1,
9041 &output_length));
9042 ASSERT_COMPARE(output, output_length,
9043 expected_output->x, expected_output->len);
9044 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009045 output = NULL;
9046 output_length = ~0;
9047
9048 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 ASSERT_ALLOC(output, expected_output->len - 1);
9050 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9051 peer_key_data->x, peer_key_data->len,
9052 output, expected_output->len - 1,
9053 &output_length),
9054 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009055 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 TEST_LE_U(output_length, expected_output->len - 1);
9057 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009058 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009059
9060exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 mbedtls_free(output);
9062 psa_destroy_key(our_key);
9063 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009064}
9065/* END_CASE */
9066
9067/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009068void key_agreement_capacity(int alg_arg,
9069 int our_key_type_arg, data_t *our_key_data,
9070 data_t *peer_key_data,
9071 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009072{
Ronald Cron5425a212020-08-04 14:58:35 +02009073 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009074 psa_algorithm_t alg = alg_arg;
9075 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009076 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009077 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009078 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009079 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009080
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9084 psa_set_key_algorithm(&attributes, alg);
9085 psa_set_key_type(&attributes, our_key_type);
9086 PSA_ASSERT(psa_import_key(&attributes,
9087 our_key_data->x, our_key_data->len,
9088 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009089
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9091 PSA_ASSERT(psa_key_derivation_key_agreement(
9092 &operation,
9093 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9094 peer_key_data->x, peer_key_data->len));
9095 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009096 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9098 PSA_KEY_DERIVATION_INPUT_INFO,
9099 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009100 }
Gilles Peskine59685592018-09-18 12:11:34 +02009101
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009102 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 PSA_ASSERT(psa_key_derivation_get_capacity(
9104 &operation, &actual_capacity));
9105 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009106
Gilles Peskinebf491972018-10-25 22:36:12 +02009107 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 while (actual_capacity > sizeof(output)) {
9109 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9110 output, sizeof(output)));
9111 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009112 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9114 output, actual_capacity));
9115 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9116 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009117
Gilles Peskine59685592018-09-18 12:11:34 +02009118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 psa_key_derivation_abort(&operation);
9120 psa_destroy_key(our_key);
9121 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009122}
9123/* END_CASE */
9124
9125/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009126void key_agreement_output(int alg_arg,
9127 int our_key_type_arg, data_t *our_key_data,
9128 data_t *peer_key_data,
9129 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009130{
Ronald Cron5425a212020-08-04 14:58:35 +02009131 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009132 psa_algorithm_t alg = alg_arg;
9133 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009134 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009136 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009137
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9139 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009142
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9144 psa_set_key_algorithm(&attributes, alg);
9145 psa_set_key_type(&attributes, our_key_type);
9146 PSA_ASSERT(psa_import_key(&attributes,
9147 our_key_data->x, our_key_data->len,
9148 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009149
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9151 PSA_ASSERT(psa_key_derivation_key_agreement(
9152 &operation,
9153 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9154 peer_key_data->x, peer_key_data->len));
9155 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009156 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9158 PSA_KEY_DERIVATION_INPUT_INFO,
9159 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009160 }
Gilles Peskine59685592018-09-18 12:11:34 +02009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9163 actual_output,
9164 expected_output1->len));
9165 ASSERT_COMPARE(actual_output, expected_output1->len,
9166 expected_output1->x, expected_output1->len);
9167 if (expected_output2->len != 0) {
9168 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9169 actual_output,
9170 expected_output2->len));
9171 ASSERT_COMPARE(actual_output, expected_output2->len,
9172 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009173 }
Gilles Peskine59685592018-09-18 12:11:34 +02009174
9175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 psa_key_derivation_abort(&operation);
9177 psa_destroy_key(our_key);
9178 PSA_DONE();
9179 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009180}
9181/* END_CASE */
9182
9183/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009184void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009185{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009186 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009187 unsigned char *output = NULL;
9188 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009189 size_t i;
9190 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009191
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009193
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 ASSERT_ALLOC(output, bytes);
9195 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009196
Gilles Peskine449bd832023-01-11 14:50:10 +01009197 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009198
Gilles Peskinea50d7392018-06-21 10:22:13 +02009199 /* Run several times, to ensure that every output byte will be
9200 * nonzero at least once with overwhelming probability
9201 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 for (run = 0; run < 10; run++) {
9203 if (bytes != 0) {
9204 memset(output, 0, bytes);
9205 }
9206 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009207
Gilles Peskine449bd832023-01-11 14:50:10 +01009208 for (i = 0; i < bytes; i++) {
9209 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009210 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009212 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009213 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009214
9215 /* Check that every byte was changed to nonzero at least once. This
9216 * validates that psa_generate_random is overwriting every byte of
9217 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 for (i = 0; i < bytes; i++) {
9219 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009220 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009221
9222exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 PSA_DONE();
9224 mbedtls_free(output);
9225 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009226}
9227/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009228
9229/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009230void generate_key(int type_arg,
9231 int bits_arg,
9232 int usage_arg,
9233 int alg_arg,
9234 int expected_status_arg,
9235 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009236{
Ronald Cron5425a212020-08-04 14:58:35 +02009237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009238 psa_key_type_t type = type_arg;
9239 psa_key_usage_t usage = usage_arg;
9240 size_t bits = bits_arg;
9241 psa_algorithm_t alg = alg_arg;
9242 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009244 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009245
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009247
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 psa_set_key_usage_flags(&attributes, usage);
9249 psa_set_key_algorithm(&attributes, alg);
9250 psa_set_key_type(&attributes, type);
9251 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009252
9253 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009255
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 if (is_large_key > 0) {
9257 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9258 }
9259 TEST_EQUAL(status, expected_status);
9260 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009261 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009263
9264 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9266 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9267 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009268
Gilles Peskine818ca122018-06-20 18:16:48 +02009269 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009272 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009273
9274exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009275 /*
9276 * Key attributes may have been returned by psa_get_key_attributes()
9277 * thus reset them as required.
9278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009280
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 psa_destroy_key(key);
9282 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009283}
9284/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009285
Ronald Cronee414c72021-03-18 18:50:08 +01009286/* 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 +01009287void generate_key_rsa(int bits_arg,
9288 data_t *e_arg,
9289 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009290{
Ronald Cron5425a212020-08-04 14:58:35 +02009291 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009292 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009293 size_t bits = bits_arg;
9294 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9295 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9296 psa_status_t expected_status = expected_status_arg;
9297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9298 uint8_t *exported = NULL;
9299 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009301 size_t exported_length = SIZE_MAX;
9302 uint8_t *e_read_buffer = NULL;
9303 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009305 size_t e_read_length = SIZE_MAX;
9306
Gilles Peskine449bd832023-01-11 14:50:10 +01009307 if (e_arg->len == 0 ||
9308 (e_arg->len == 3 &&
9309 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009310 is_default_public_exponent = 1;
9311 e_read_size = 0;
9312 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009313 ASSERT_ALLOC(e_read_buffer, e_read_size);
9314 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009315
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009317
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 psa_set_key_usage_flags(&attributes, usage);
9319 psa_set_key_algorithm(&attributes, alg);
9320 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9321 e_arg->x, e_arg->len));
9322 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009323
9324 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9326 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009327 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009328 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009329
9330 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9332 TEST_EQUAL(psa_get_key_type(&attributes), type);
9333 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9334 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9335 e_read_buffer, e_read_size,
9336 &e_read_length));
9337 if (is_default_public_exponent) {
9338 TEST_EQUAL(e_read_length, 0);
9339 } else {
9340 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9341 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009342
9343 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009345 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009346 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009347
9348 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009349 PSA_ASSERT(psa_export_public_key(key,
9350 exported, exported_size,
9351 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009352 {
9353 uint8_t *p = exported;
9354 uint8_t *end = exported + exported_length;
9355 size_t len;
9356 /* RSAPublicKey ::= SEQUENCE {
9357 * modulus INTEGER, -- n
9358 * publicExponent INTEGER } -- e
9359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9361 MBEDTLS_ASN1_SEQUENCE |
9362 MBEDTLS_ASN1_CONSTRUCTED));
9363 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9364 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9365 MBEDTLS_ASN1_INTEGER));
9366 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009367 ++p;
9368 --len;
9369 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 if (e_arg->len == 0) {
9371 TEST_EQUAL(len, 3);
9372 TEST_EQUAL(p[0], 1);
9373 TEST_EQUAL(p[1], 0);
9374 TEST_EQUAL(p[2], 1);
9375 } else {
9376 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009377 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009378 }
9379
9380exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009381 /*
9382 * Key attributes may have been returned by psa_get_key_attributes() or
9383 * set by psa_set_key_domain_parameters() thus reset them as required.
9384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009386
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 psa_destroy_key(key);
9388 PSA_DONE();
9389 mbedtls_free(e_read_buffer);
9390 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009391}
9392/* END_CASE */
9393
Darryl Greend49a4992018-06-18 17:27:26 +01009394/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009395void persistent_key_load_key_from_storage(data_t *data,
9396 int type_arg, int bits_arg,
9397 int usage_flags_arg, int alg_arg,
9398 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009399{
Gilles Peskine449bd832023-01-11 14:50:10 +01009400 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9403 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009404 psa_key_type_t type = type_arg;
9405 size_t bits = bits_arg;
9406 psa_key_usage_t usage_flags = usage_flags_arg;
9407 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009408 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009409 unsigned char *first_export = NULL;
9410 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009412 size_t first_exported_length;
9413 size_t second_exported_length;
9414
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9416 ASSERT_ALLOC(first_export, export_size);
9417 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009418 }
Darryl Greend49a4992018-06-18 17:27:26 +01009419
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009421
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 psa_set_key_id(&attributes, key_id);
9423 psa_set_key_usage_flags(&attributes, usage_flags);
9424 psa_set_key_algorithm(&attributes, alg);
9425 psa_set_key_type(&attributes, type);
9426 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009429 case IMPORT_KEY:
9430 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9432 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009433 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009434
Darryl Green0c6575a2018-11-07 16:05:30 +00009435 case GENERATE_KEY:
9436 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009437 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009438 break;
9439
9440 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009441#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009442 {
9443 /* Create base key */
9444 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9445 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9446 psa_set_key_usage_flags(&base_attributes,
9447 PSA_KEY_USAGE_DERIVE);
9448 psa_set_key_algorithm(&base_attributes, derive_alg);
9449 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9450 PSA_ASSERT(psa_import_key(&base_attributes,
9451 data->x, data->len,
9452 &base_key));
9453 /* Derive a key. */
9454 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9455 PSA_ASSERT(psa_key_derivation_input_key(
9456 &operation,
9457 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9458 PSA_ASSERT(psa_key_derivation_input_bytes(
9459 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9460 NULL, 0));
9461 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9462 &operation,
9463 &key));
9464 PSA_ASSERT(psa_key_derivation_abort(&operation));
9465 PSA_ASSERT(psa_destroy_key(base_key));
9466 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9467 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009468#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009470#endif
9471 break;
9472
9473 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009475 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009476 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009478
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009479 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009480 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9481 PSA_ASSERT(psa_export_key(key,
9482 first_export, export_size,
9483 &first_exported_length));
9484 if (generation_method == IMPORT_KEY) {
9485 ASSERT_COMPARE(data->x, data->len,
9486 first_export, first_exported_length);
9487 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009488 }
Darryl Greend49a4992018-06-18 17:27:26 +01009489
9490 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009492 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009494
Darryl Greend49a4992018-06-18 17:27:26 +01009495 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9497 TEST_ASSERT(mbedtls_svc_key_id_equal(
9498 psa_get_key_id(&attributes), key_id));
9499 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9500 PSA_KEY_LIFETIME_PERSISTENT);
9501 TEST_EQUAL(psa_get_key_type(&attributes), type);
9502 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9503 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9504 mbedtls_test_update_key_usage_flags(usage_flags));
9505 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009506
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009507 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9509 PSA_ASSERT(psa_export_key(key,
9510 second_export, export_size,
9511 &second_exported_length));
9512 ASSERT_COMPARE(first_export, first_exported_length,
9513 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009514 }
9515
9516 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009518 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 }
Darryl Greend49a4992018-06-18 17:27:26 +01009520
9521exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009522 /*
9523 * Key attributes may have been returned by psa_get_key_attributes()
9524 * thus reset them as required.
9525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009527
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 mbedtls_free(first_export);
9529 mbedtls_free(second_export);
9530 psa_key_derivation_abort(&operation);
9531 psa_destroy_key(base_key);
9532 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009533 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009534}
9535/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009536
Neil Armstronga557cb82022-06-10 08:58:32 +02009537/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009538void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9539 int primitive_arg, int hash_arg, int role_arg,
9540 int test_input, data_t *pw_data,
9541 int inj_err_type_arg,
9542 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009543{
9544 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9545 psa_pake_operation_t operation = psa_pake_operation_init();
9546 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009547 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009548 psa_key_type_t key_type_pw = key_type_pw_arg;
9549 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009550 psa_algorithm_t hash_alg = hash_arg;
9551 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9553 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009554 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9555 psa_status_t expected_error = expected_error_arg;
9556 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009557 unsigned char *output_buffer = NULL;
9558 size_t output_len = 0;
9559
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009561
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009562 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 PSA_PAKE_STEP_KEY_SHARE);
9564 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 if (pw_data->len > 0) {
9567 psa_set_key_usage_flags(&attributes, key_usage_pw);
9568 psa_set_key_algorithm(&attributes, alg);
9569 psa_set_key_type(&attributes, key_type_pw);
9570 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9571 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009572 }
9573
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9575 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9576 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009577
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009579
Gilles Peskine449bd832023-01-11 14:50:10 +01009580 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9581 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9582 expected_error);
9583 PSA_ASSERT(psa_pake_abort(&operation));
9584 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9585 expected_error);
9586 PSA_ASSERT(psa_pake_abort(&operation));
9587 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9588 expected_error);
9589 PSA_ASSERT(psa_pake_abort(&operation));
9590 TEST_EQUAL(psa_pake_set_role(&operation, role),
9591 expected_error);
9592 PSA_ASSERT(psa_pake_abort(&operation));
9593 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9594 NULL, 0, NULL),
9595 expected_error);
9596 PSA_ASSERT(psa_pake_abort(&operation));
9597 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9598 expected_error);
9599 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009600 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009601 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 status = psa_pake_setup(&operation, &cipher_suite);
9604 if (status != PSA_SUCCESS) {
9605 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009606 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009607 }
9608
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9610 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9611 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009612 goto exit;
9613 }
9614
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 status = psa_pake_set_role(&operation, role);
9616 if (status != PSA_SUCCESS) {
9617 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009618 goto exit;
9619 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009620
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 if (pw_data->len > 0) {
9622 status = psa_pake_set_password_key(&operation, key);
9623 if (status != PSA_SUCCESS) {
9624 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009625 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009626 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009627 }
9628
Gilles Peskine449bd832023-01-11 14:50:10 +01009629 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9630 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9631 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009632 goto exit;
9633 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009634
Gilles Peskine449bd832023-01-11 14:50:10 +01009635 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9636 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9637 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009638 goto exit;
9639 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009640
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009642 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9644 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009645 goto exit;
9646 }
9647
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009649 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9651 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009652 goto exit;
9653 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009654
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9656 PSA_PAKE_STEP_KEY_SHARE);
9657 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9658 PSA_PAKE_STEP_ZK_PUBLIC);
9659 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9660 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009661
Gilles Peskine449bd832023-01-11 14:50:10 +01009662 if (test_input) {
9663 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9664 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9665 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009666 goto exit;
9667 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009668
Gilles Peskine449bd832023-01-11 14:50:10 +01009669 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9670 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9671 output_buffer, size_zk_proof),
9672 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009673 goto exit;
9674 }
9675
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9677 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9678 output_buffer, size_zk_proof),
9679 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009680 goto exit;
9681 }
9682
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9684 output_buffer, size_key_share);
9685 if (status != PSA_SUCCESS) {
9686 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009687 goto exit;
9688 }
9689
Gilles Peskine449bd832023-01-11 14:50:10 +01009690 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9691 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9692 output_buffer, size_zk_public + 1),
9693 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009694 goto exit;
9695 }
9696
Gilles Peskine449bd832023-01-11 14:50:10 +01009697 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009698 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009699 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9700 output_buffer, size_zk_public + 1);
9701 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9702 output_buffer, size_zk_public),
9703 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009704 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009705 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009706 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009707 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9708 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9709 NULL, 0, NULL),
9710 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009711 goto exit;
9712 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009713
Gilles Peskine449bd832023-01-11 14:50:10 +01009714 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9715 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9716 output_buffer, buf_size, &output_len),
9717 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009718 goto exit;
9719 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009720
Gilles Peskine449bd832023-01-11 14:50:10 +01009721 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9722 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9723 output_buffer, buf_size, &output_len),
9724 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009725 goto exit;
9726 }
9727
Gilles Peskine449bd832023-01-11 14:50:10 +01009728 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9729 output_buffer, buf_size, &output_len);
9730 if (status != PSA_SUCCESS) {
9731 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009732 goto exit;
9733 }
9734
Gilles Peskine449bd832023-01-11 14:50:10 +01009735 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009736
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9738 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9739 output_buffer, size_zk_public - 1, &output_len),
9740 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01009741 goto exit;
9742 }
9743
Gilles Peskine449bd832023-01-11 14:50:10 +01009744 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009745 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009746 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9747 output_buffer, size_zk_public - 1, &output_len);
9748 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9749 output_buffer, buf_size, &output_len),
9750 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009751 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009752 }
9753 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009754
9755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009756 PSA_ASSERT(psa_destroy_key(key));
9757 PSA_ASSERT(psa_pake_abort(&operation));
9758 mbedtls_free(output_buffer);
9759 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009760}
9761/* END_CASE */
9762
Neil Armstronga557cb82022-06-10 08:58:32 +02009763/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009764void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
9765 int client_input_first, int inject_error,
9766 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009767{
9768 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9769 psa_pake_operation_t server = psa_pake_operation_init();
9770 psa_pake_operation_t client = psa_pake_operation_init();
9771 psa_algorithm_t alg = alg_arg;
9772 psa_algorithm_t hash_alg = hash_arg;
9773 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9775
Gilles Peskine449bd832023-01-11 14:50:10 +01009776 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009777
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9779 psa_set_key_algorithm(&attributes, alg);
9780 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9781 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9782 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009783
Gilles Peskine449bd832023-01-11 14:50:10 +01009784 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9785 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9786 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009787
9788
Gilles Peskine449bd832023-01-11 14:50:10 +01009789 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9790 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009791
Gilles Peskine449bd832023-01-11 14:50:10 +01009792 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9793 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009794
Gilles Peskine449bd832023-01-11 14:50:10 +01009795 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9796 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009797
Gilles Peskine449bd832023-01-11 14:50:10 +01009798 ecjpake_do_round(alg, primitive_arg, &server, &client,
9799 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009800
Gilles Peskine449bd832023-01-11 14:50:10 +01009801 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009802 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009803 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009804
Gilles Peskine449bd832023-01-11 14:50:10 +01009805 ecjpake_do_round(alg, primitive_arg, &server, &client,
9806 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009807
9808exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009809 psa_destroy_key(key);
9810 psa_pake_abort(&server);
9811 psa_pake_abort(&client);
9812 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009813}
9814/* END_CASE */
9815
9816/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009817void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
9818 int derive_alg_arg, data_t *pw_data,
9819 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009820{
9821 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9822 psa_pake_operation_t server = psa_pake_operation_init();
9823 psa_pake_operation_t client = psa_pake_operation_init();
9824 psa_algorithm_t alg = alg_arg;
9825 psa_algorithm_t hash_alg = hash_arg;
9826 psa_algorithm_t derive_alg = derive_alg_arg;
9827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9829 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009830 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009831 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009832 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009833 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009834
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009836
Gilles Peskine449bd832023-01-11 14:50:10 +01009837 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9838 psa_set_key_algorithm(&attributes, alg);
9839 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9840 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9841 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009842
Gilles Peskine449bd832023-01-11 14:50:10 +01009843 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9844 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9845 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009846
Neil Armstrong1e855602022-06-15 11:32:11 +02009847 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009848 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
9849 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02009850
Gilles Peskine449bd832023-01-11 14:50:10 +01009851 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
9852 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
9853 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
9854 PSA_KEY_DERIVATION_INPUT_SEED,
9855 (const uint8_t *) "", 0));
9856 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
9857 PSA_KEY_DERIVATION_INPUT_SEED,
9858 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02009859 }
9860
Gilles Peskine449bd832023-01-11 14:50:10 +01009861 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9862 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009863
Gilles Peskine449bd832023-01-11 14:50:10 +01009864 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9865 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009866
Gilles Peskine449bd832023-01-11 14:50:10 +01009867 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9868 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009869
Gilles Peskine449bd832023-01-11 14:50:10 +01009870 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
9871 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9872 PSA_ERROR_BAD_STATE);
9873 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9874 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009875 goto exit;
9876 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009877
Neil Armstrongf983caf2022-06-15 15:27:48 +02009878 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009879 ecjpake_do_round(alg, primitive_arg, &server, &client,
9880 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009881
Gilles Peskine449bd832023-01-11 14:50:10 +01009882 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
9883 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9884 PSA_ERROR_BAD_STATE);
9885 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9886 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009887 goto exit;
9888 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009889
Neil Armstrongf983caf2022-06-15 15:27:48 +02009890 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009891 ecjpake_do_round(alg, primitive_arg, &server, &client,
9892 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009893
Gilles Peskine449bd832023-01-11 14:50:10 +01009894 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
9895 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009896
9897exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009898 psa_key_derivation_abort(&server_derive);
9899 psa_key_derivation_abort(&client_derive);
9900 psa_destroy_key(key);
9901 psa_pake_abort(&server);
9902 psa_pake_abort(&client);
9903 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009904}
9905/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009906
9907/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009908void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009909{
9910 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9911 const size_t bits = 256;
9912 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01009913 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009914 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01009915 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009916
9917 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9918 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009919 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9920 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
9921 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9922 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009923 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01009924 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9925 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009926
9927 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01009928 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9929 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
9930 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9931 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
9932 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9933 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009934
9935 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01009936 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9937 PSA_PAKE_OUTPUT_MAX_SIZE);
9938 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9939 PSA_PAKE_OUTPUT_MAX_SIZE);
9940 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9941 PSA_PAKE_OUTPUT_MAX_SIZE);
9942 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9943 PSA_PAKE_INPUT_MAX_SIZE);
9944 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9945 PSA_PAKE_INPUT_MAX_SIZE);
9946 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9947 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009948}
9949/* END_CASE */