blob: 66f932b7e18bb37845e79b406d025fe5d344de5c [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) {
1236 /* Failure test cases will fail on the first op. */
1237 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 Elliott712d5122022-12-07 14:03:10 +00006542 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006543
Paul Elliott712d5122022-12-07 14:03:10 +00006544 num_ops_prior = num_ops;
6545 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006546 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006547
6548 TEST_ASSERT(status == PSA_SUCCESS);
6549
Paul Elliott0c683352022-12-16 19:16:56 +00006550 TEST_LE_U(min_completes, num_completes);
6551 TEST_LE_U(num_completes, max_completes);
6552
Paul Elliott712d5122022-12-07 14:03:10 +00006553 /* Verify that the signature is what is expected. */
6554 ASSERT_COMPARE(output_data->x, output_data->len,
6555 signature, signature_length);
6556
6557 PSA_ASSERT(psa_sign_hash_abort(&operation));
6558
Paul Elliott59ad9452022-12-18 15:09:02 +00006559 num_ops = psa_sign_hash_get_num_ops(&operation);
6560 TEST_ASSERT(num_ops == 0);
6561
Paul Elliott712d5122022-12-07 14:03:10 +00006562exit:
6563
6564 /*
6565 * Key attributes may have been returned by psa_get_key_attributes()
6566 * thus reset them as required.
6567 */
6568 psa_reset_key_attributes(&attributes);
6569
6570 psa_destroy_key(key);
6571 mbedtls_free(signature);
6572 PSA_DONE();
6573}
6574/* END_CASE */
6575
Gilles Peskine20035e32018-02-03 22:44:14 +01006576/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006577void sign_hash_fail(int key_type_arg, data_t *key_data,
6578 int alg_arg, data_t *input_data,
6579 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006580{
Ronald Cron5425a212020-08-04 14:58:35 +02006581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006582 psa_key_type_t key_type = key_type_arg;
6583 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006584 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006585 psa_status_t actual_status;
6586 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006587 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006588 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006589 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6596 psa_set_key_algorithm(&attributes, alg);
6597 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6600 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006601
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 actual_status = psa_sign_hash(key, alg,
6603 input_data->x, input_data->len,
6604 signature, signature_size,
6605 &signature_length);
6606 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006607 /* The value of *signature_length is unspecified on error, but
6608 * whatever it is, it should be less than signature_size, so that
6609 * if the caller tries to read *signature_length bytes without
6610 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006612
6613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 psa_reset_key_attributes(&attributes);
6615 psa_destroy_key(key);
6616 mbedtls_free(signature);
6617 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006618}
6619/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006620
Paul Elliott91007972022-12-16 12:21:24 +00006621/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6622void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6623 int alg_arg, data_t *input_data,
6624 int signature_size_arg,
6625 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006626 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006627 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006628{
6629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6630 psa_key_type_t key_type = key_type_arg;
6631 psa_algorithm_t alg = alg_arg;
6632 size_t signature_size = signature_size_arg;
6633 psa_status_t actual_status;
6634 psa_status_t expected_start_status = expected_start_status_arg;
6635 psa_status_t expected_complete_status = expected_complete_status_arg;
6636 unsigned char *signature = NULL;
6637 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006638 uint32_t num_ops = 0;
6639 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006640 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006641 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006642 size_t min_completes = 0;
6643 size_t max_completes = 0;
6644
Paul Elliott91007972022-12-16 12:21:24 +00006645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6646 psa_sign_hash_interruptible_operation_t operation =
6647 psa_sign_hash_interruptible_operation_init();
6648
6649 ASSERT_ALLOC(signature, signature_size);
6650
6651 PSA_ASSERT(psa_crypto_init());
6652
6653 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6654 psa_set_key_algorithm(&attributes, alg);
6655 psa_set_key_type(&attributes, key_type);
6656
6657 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6658 &key));
6659
Paul Elliott0c683352022-12-16 19:16:56 +00006660 psa_interruptible_set_max_ops(max_ops);
6661
Paul Elliott6f600372023-02-06 18:41:05 +00006662 interruptible_signverify_get_minmax_completes(max_ops,
6663 expected_complete_status,
6664 &min_completes,
6665 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006666
Paul Elliott91007972022-12-16 12:21:24 +00006667 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6668 TEST_ASSERT(num_ops_prior == 0);
6669
6670 /* Start performing the signature. */
6671 actual_status = psa_sign_hash_start(&operation, key, alg,
6672 input_data->x, input_data->len);
6673
6674 TEST_EQUAL(actual_status, expected_start_status);
6675
Paul Elliottc9774412023-02-06 15:14:07 +00006676 if (expected_start_status != PSA_SUCCESS) {
6677 actual_status = psa_sign_hash_start(&operation, key, alg,
6678 input_data->x, input_data->len);
6679
6680 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6681 }
6682
Paul Elliott91007972022-12-16 12:21:24 +00006683 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6684 TEST_ASSERT(num_ops_prior == 0);
6685
Paul Elliott91007972022-12-16 12:21:24 +00006686 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006687 do {
Paul Elliott91007972022-12-16 12:21:24 +00006688 actual_status = psa_sign_hash_complete(&operation, signature,
6689 signature_size,
6690 &signature_length);
6691
Paul Elliott0c683352022-12-16 19:16:56 +00006692 num_completes++;
6693
Paul Elliott334d7262023-01-20 17:29:41 +00006694 if (actual_status == PSA_SUCCESS ||
6695 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006696 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00006697 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006698
Paul Elliott91007972022-12-16 12:21:24 +00006699 num_ops_prior = num_ops;
6700 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006701 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006702
Paul Elliottc9774412023-02-06 15:14:07 +00006703 TEST_EQUAL(actual_status, expected_complete_status);
6704
6705 if (expected_complete_status != PSA_SUCCESS) {
6706 actual_status = psa_sign_hash_complete(&operation, signature,
6707 signature_size,
6708 &signature_length);
6709
Paul Elliott334d7262023-01-20 17:29:41 +00006710 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006711 }
6712
Paul Elliott91007972022-12-16 12:21:24 +00006713 PSA_ASSERT(psa_sign_hash_abort(&operation));
6714
Paul Elliott59ad9452022-12-18 15:09:02 +00006715 num_ops = psa_sign_hash_get_num_ops(&operation);
6716 TEST_ASSERT(num_ops == 0);
6717
Paul Elliott91007972022-12-16 12:21:24 +00006718 /* The value of *signature_length is unspecified on error, but
6719 * whatever it is, it should be less than signature_size, so that
6720 * if the caller tries to read *signature_length bytes without
6721 * checking the error code then they don't overflow a buffer. */
6722 TEST_LE_U(signature_length, signature_size);
6723
Paul Elliott0c683352022-12-16 19:16:56 +00006724 TEST_LE_U(min_completes, num_completes);
6725 TEST_LE_U(num_completes, max_completes);
6726
Paul Elliott91007972022-12-16 12:21:24 +00006727exit:
6728 psa_reset_key_attributes(&attributes);
6729 psa_destroy_key(key);
6730 mbedtls_free(signature);
6731 PSA_DONE();
6732}
6733/* END_CASE */
6734
mohammad16038cc1cee2018-03-28 01:21:33 +03006735/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006736void sign_verify_hash(int key_type_arg, data_t *key_data,
6737 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006738{
Ronald Cron5425a212020-08-04 14:58:35 +02006739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006740 psa_key_type_t key_type = key_type_arg;
6741 psa_algorithm_t alg = alg_arg;
6742 size_t key_bits;
6743 unsigned char *signature = NULL;
6744 size_t signature_size;
6745 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006749
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6751 psa_set_key_algorithm(&attributes, alg);
6752 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6755 &key));
6756 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6757 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006758
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006759 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006760 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6762 key_bits, alg);
6763 TEST_ASSERT(signature_size != 0);
6764 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6765 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006766
6767 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 PSA_ASSERT(psa_sign_hash(key, alg,
6769 input_data->x, input_data->len,
6770 signature, signature_size,
6771 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006772 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 TEST_LE_U(signature_length, signature_size);
6774 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006775
6776 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 PSA_ASSERT(psa_verify_hash(key, alg,
6778 input_data->x, input_data->len,
6779 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006782 /* Flip a bit in the input and verify that the signature is now
6783 * detected as invalid. Flip a bit at the beginning, not at the end,
6784 * because ECDSA may ignore the last few bits of the input. */
6785 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 TEST_EQUAL(psa_verify_hash(key, alg,
6787 input_data->x, input_data->len,
6788 signature, signature_length),
6789 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006790 }
6791
6792exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006793 /*
6794 * Key attributes may have been returned by psa_get_key_attributes()
6795 * thus reset them as required.
6796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006798
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 psa_destroy_key(key);
6800 mbedtls_free(signature);
6801 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006802}
6803/* END_CASE */
6804
Paul Elliott712d5122022-12-07 14:03:10 +00006805/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6806void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006807 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006808 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006809{
6810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6811 psa_key_type_t key_type = key_type_arg;
6812 psa_algorithm_t alg = alg_arg;
6813 size_t key_bits;
6814 unsigned char *signature = NULL;
6815 size_t signature_size;
6816 size_t signature_length = 0xdeadbeef;
6817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6818 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006819 uint32_t max_ops = max_ops_arg;
Paul Elliott0c683352022-12-16 19:16:56 +00006820 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006821 size_t min_completes = 0;
6822 size_t max_completes = 0;
6823
Paul Elliott712d5122022-12-07 14:03:10 +00006824 psa_sign_hash_interruptible_operation_t sign_operation =
6825 psa_sign_hash_interruptible_operation_init();
6826 psa_verify_hash_interruptible_operation_t verify_operation =
6827 psa_verify_hash_interruptible_operation_init();
6828
6829 PSA_ASSERT(psa_crypto_init());
6830
Paul Elliott0c683352022-12-16 19:16:56 +00006831 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6832 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006833 psa_set_key_algorithm(&attributes, alg);
6834 psa_set_key_type(&attributes, key_type);
6835
6836 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6837 &key));
6838 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6839 key_bits = psa_get_key_bits(&attributes);
6840
6841 /* Allocate a buffer which has the size advertised by the
6842 * library. */
6843 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6844 key_bits, alg);
6845 TEST_ASSERT(signature_size != 0);
6846 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6847 ASSERT_ALLOC(signature, signature_size);
6848
Paul Elliott0c683352022-12-16 19:16:56 +00006849 psa_interruptible_set_max_ops(max_ops);
6850
Paul Elliott6f600372023-02-06 18:41:05 +00006851 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6852 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006853
Paul Elliott712d5122022-12-07 14:03:10 +00006854 /* Start performing the signature. */
6855 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6856 input_data->x, input_data->len));
6857
6858 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006859 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006860
Paul Elliott0c683352022-12-16 19:16:56 +00006861 status = psa_sign_hash_complete(&sign_operation, signature,
6862 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006863 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006864
6865 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006866 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006867
6868 TEST_ASSERT(status == PSA_SUCCESS);
6869
Paul Elliott0c683352022-12-16 19:16:56 +00006870 TEST_LE_U(min_completes, num_completes);
6871 TEST_LE_U(num_completes, max_completes);
6872
Paul Elliott712d5122022-12-07 14:03:10 +00006873 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6874
6875 /* Check that the signature length looks sensible. */
6876 TEST_LE_U(signature_length, signature_size);
6877 TEST_ASSERT(signature_length > 0);
6878
Paul Elliott0c683352022-12-16 19:16:56 +00006879 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006880
6881 /* Start verification. */
6882 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6883 input_data->x, input_data->len,
6884 signature, signature_length));
6885
6886 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006887 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006888 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00006889
6890 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006891 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006892
6893 TEST_ASSERT(status == PSA_SUCCESS);
6894
Paul Elliott0c683352022-12-16 19:16:56 +00006895 TEST_LE_U(min_completes, num_completes);
6896 TEST_LE_U(num_completes, max_completes);
6897
Paul Elliott712d5122022-12-07 14:03:10 +00006898 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6899
6900 verify_operation = psa_verify_hash_interruptible_operation_init();
6901
6902 if (input_data->len != 0) {
6903 /* Flip a bit in the input and verify that the signature is now
6904 * detected as invalid. Flip a bit at the beginning, not at the end,
6905 * because ECDSA may ignore the last few bits of the input. */
6906 input_data->x[0] ^= 1;
6907
Paul Elliott712d5122022-12-07 14:03:10 +00006908 /* Start verification. */
6909 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6910 input_data->x, input_data->len,
6911 signature, signature_length));
6912
6913 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006914 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006915 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00006916 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006917
6918 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
6919 }
6920
6921 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6922
6923exit:
6924 /*
6925 * Key attributes may have been returned by psa_get_key_attributes()
6926 * thus reset them as required.
6927 */
6928 psa_reset_key_attributes(&attributes);
6929
6930 psa_destroy_key(key);
6931 mbedtls_free(signature);
6932 PSA_DONE();
6933}
6934/* END_CASE */
6935
Gilles Peskine9911b022018-06-29 17:30:48 +02006936/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006937void verify_hash(int key_type_arg, data_t *key_data,
6938 int alg_arg, data_t *hash_data,
6939 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006940{
Ronald Cron5425a212020-08-04 14:58:35 +02006941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006942 psa_key_type_t key_type = key_type_arg;
6943 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006949
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6951 psa_set_key_algorithm(&attributes, alg);
6952 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6955 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 PSA_ASSERT(psa_verify_hash(key, alg,
6958 hash_data->x, hash_data->len,
6959 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006960
itayzafrir5c753392018-05-08 11:18:38 +03006961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 psa_reset_key_attributes(&attributes);
6963 psa_destroy_key(key);
6964 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006965}
6966/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006967
Paul Elliott712d5122022-12-07 14:03:10 +00006968/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6969void verify_hash_interruptible(int key_type_arg, data_t *key_data,
6970 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006971 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006972{
6973 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6974 psa_key_type_t key_type = key_type_arg;
6975 psa_algorithm_t alg = alg_arg;
6976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6977 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006978 uint32_t num_ops = 0;
6979 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006980 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006981 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006982 size_t min_completes = 0;
6983 size_t max_completes = 0;
6984
Paul Elliott712d5122022-12-07 14:03:10 +00006985 psa_verify_hash_interruptible_operation_t operation =
6986 psa_verify_hash_interruptible_operation_init();
6987
6988 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
6989
6990 PSA_ASSERT(psa_crypto_init());
6991
6992 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6993 psa_set_key_algorithm(&attributes, alg);
6994 psa_set_key_type(&attributes, key_type);
6995
6996 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6997 &key));
6998
Paul Elliott0c683352022-12-16 19:16:56 +00006999 psa_interruptible_set_max_ops(max_ops);
7000
Paul Elliott6f600372023-02-06 18:41:05 +00007001 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7002 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007003
Paul Elliott712d5122022-12-07 14:03:10 +00007004 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7005
7006 TEST_ASSERT(num_ops_prior == 0);
7007
7008 /* Start verification. */
7009 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7010 hash_data->x, hash_data->len,
7011 signature_data->x, signature_data->len)
7012 );
7013
7014 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7015
7016 TEST_ASSERT(num_ops_prior == 0);
7017
7018 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007019 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007020 status = psa_verify_hash_complete(&operation);
7021
Paul Elliott0c683352022-12-16 19:16:56 +00007022 num_completes++;
7023
Paul Elliott712d5122022-12-07 14:03:10 +00007024 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7025 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00007026 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007027
Paul Elliott712d5122022-12-07 14:03:10 +00007028 num_ops_prior = num_ops;
7029 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007030 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007031
7032 TEST_ASSERT(status == PSA_SUCCESS);
7033
Paul Elliott0c683352022-12-16 19:16:56 +00007034 TEST_LE_U(min_completes, num_completes);
7035 TEST_LE_U(num_completes, max_completes);
7036
Paul Elliott712d5122022-12-07 14:03:10 +00007037 PSA_ASSERT(psa_verify_hash_abort(&operation));
7038
Paul Elliott59ad9452022-12-18 15:09:02 +00007039 num_ops = psa_verify_hash_get_num_ops(&operation);
7040 TEST_ASSERT(num_ops == 0);
7041
Paul Elliott712d5122022-12-07 14:03:10 +00007042exit:
7043 psa_reset_key_attributes(&attributes);
7044 psa_destroy_key(key);
7045 PSA_DONE();
7046}
7047/* END_CASE */
7048
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007049/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007050void verify_hash_fail(int key_type_arg, data_t *key_data,
7051 int alg_arg, data_t *hash_data,
7052 data_t *signature_data,
7053 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007054{
Ronald Cron5425a212020-08-04 14:58:35 +02007055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007056 psa_key_type_t key_type = key_type_arg;
7057 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007058 psa_status_t actual_status;
7059 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7065 psa_set_key_algorithm(&attributes, alg);
7066 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007067
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7069 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 actual_status = psa_verify_hash(key, alg,
7072 hash_data->x, hash_data->len,
7073 signature_data->x, signature_data->len);
7074 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007075
7076exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 psa_reset_key_attributes(&attributes);
7078 psa_destroy_key(key);
7079 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007080}
7081/* END_CASE */
7082
Paul Elliott91007972022-12-16 12:21:24 +00007083/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7084void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7085 int alg_arg, data_t *hash_data,
7086 data_t *signature_data,
7087 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007088 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007089 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007090{
7091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7092 psa_key_type_t key_type = key_type_arg;
7093 psa_algorithm_t alg = alg_arg;
7094 psa_status_t actual_status;
7095 psa_status_t expected_start_status = expected_start_status_arg;
7096 psa_status_t expected_complete_status = expected_complete_status_arg;
7097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007098 uint32_t num_ops = 0;
7099 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007100 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007101 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007102 size_t min_completes = 0;
7103 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007104 psa_verify_hash_interruptible_operation_t operation =
7105 psa_verify_hash_interruptible_operation_init();
7106
7107 PSA_ASSERT(psa_crypto_init());
7108
7109 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7110 psa_set_key_algorithm(&attributes, alg);
7111 psa_set_key_type(&attributes, key_type);
7112
7113 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7114 &key));
7115
Paul Elliott0c683352022-12-16 19:16:56 +00007116 psa_interruptible_set_max_ops(max_ops);
7117
Paul Elliott6f600372023-02-06 18:41:05 +00007118 interruptible_signverify_get_minmax_completes(max_ops,
7119 expected_complete_status,
7120 &min_completes,
7121 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007122
Paul Elliott91007972022-12-16 12:21:24 +00007123 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7124 TEST_ASSERT(num_ops_prior == 0);
7125
7126 /* Start verification. */
7127 actual_status = psa_verify_hash_start(&operation, key, alg,
7128 hash_data->x, hash_data->len,
7129 signature_data->x,
7130 signature_data->len);
7131
7132 TEST_EQUAL(actual_status, expected_start_status);
7133
Paul Elliottc9774412023-02-06 15:14:07 +00007134 if (expected_start_status != PSA_SUCCESS) {
7135 actual_status = psa_verify_hash_start(&operation, key, alg,
7136 hash_data->x, hash_data->len,
7137 signature_data->x,
7138 signature_data->len);
7139
7140 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7141 }
7142
Paul Elliott91007972022-12-16 12:21:24 +00007143 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7144 TEST_ASSERT(num_ops_prior == 0);
7145
Paul Elliott91007972022-12-16 12:21:24 +00007146 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007147 do {
Paul Elliott91007972022-12-16 12:21:24 +00007148 actual_status = psa_verify_hash_complete(&operation);
7149
Paul Elliott0c683352022-12-16 19:16:56 +00007150 num_completes++;
7151
Paul Elliott334d7262023-01-20 17:29:41 +00007152 if (actual_status == PSA_SUCCESS ||
7153 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007154 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00007155 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007156
Paul Elliott91007972022-12-16 12:21:24 +00007157 num_ops_prior = num_ops;
7158 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007159 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007160
Paul Elliottc9774412023-02-06 15:14:07 +00007161 TEST_EQUAL(actual_status, expected_complete_status);
7162
7163 if (expected_complete_status != PSA_SUCCESS) {
7164 actual_status = psa_verify_hash_complete(&operation);
7165
Paul Elliott334d7262023-01-20 17:29:41 +00007166 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007167 }
7168
Paul Elliott0c683352022-12-16 19:16:56 +00007169 TEST_LE_U(min_completes, num_completes);
7170 TEST_LE_U(num_completes, max_completes);
7171
Paul Elliott91007972022-12-16 12:21:24 +00007172 PSA_ASSERT(psa_verify_hash_abort(&operation));
7173
Paul Elliott59ad9452022-12-18 15:09:02 +00007174 num_ops = psa_verify_hash_get_num_ops(&operation);
7175 TEST_ASSERT(num_ops == 0);
7176
Paul Elliott91007972022-12-16 12:21:24 +00007177exit:
7178 psa_reset_key_attributes(&attributes);
7179 psa_destroy_key(key);
7180 PSA_DONE();
7181}
7182/* END_CASE */
7183
Paul Elliott20a36062022-12-18 13:21:25 +00007184/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007185void interruptible_signverify_hash_state_test(int key_type_arg,
7186 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007187{
7188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7189 psa_key_type_t key_type = key_type_arg;
7190 psa_algorithm_t alg = alg_arg;
7191 size_t key_bits;
7192 unsigned char *signature = NULL;
7193 size_t signature_size;
7194 size_t signature_length = 0xdeadbeef;
7195 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7196 psa_sign_hash_interruptible_operation_t sign_operation =
7197 psa_sign_hash_interruptible_operation_init();
7198 psa_verify_hash_interruptible_operation_t verify_operation =
7199 psa_verify_hash_interruptible_operation_init();
7200
7201 PSA_ASSERT(psa_crypto_init());
7202
7203 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7204 PSA_KEY_USAGE_VERIFY_HASH);
7205 psa_set_key_algorithm(&attributes, alg);
7206 psa_set_key_type(&attributes, key_type);
7207
7208 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7209 &key));
7210 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7211 key_bits = psa_get_key_bits(&attributes);
7212
7213 /* Allocate a buffer which has the size advertised by the
7214 * library. */
7215 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7216 key_bits, alg);
7217 TEST_ASSERT(signature_size != 0);
7218 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7219 ASSERT_ALLOC(signature, signature_size);
7220
7221 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7222
7223 /* --- Attempt completes prior to starts --- */
7224 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7225 signature_size,
7226 &signature_length),
7227 PSA_ERROR_BAD_STATE);
7228
7229 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7230
7231 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7232 PSA_ERROR_BAD_STATE);
7233
7234 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7235
7236 /* --- Aborts in all other places. --- */
7237 psa_sign_hash_abort(&sign_operation);
7238
7239 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7240 input_data->x, input_data->len));
7241
7242 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7243
7244 psa_interruptible_set_max_ops(1);
7245
7246 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7247 input_data->x, input_data->len));
7248
7249 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7250 signature_size,
7251 &signature_length),
7252 PSA_OPERATION_INCOMPLETE);
7253
7254 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7255
7256 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7257
7258 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7259 input_data->x, input_data->len));
7260
7261 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7262 signature_size,
7263 &signature_length));
7264
7265 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7266
7267 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7268
7269 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7270 input_data->x, input_data->len,
7271 signature, signature_length));
7272
7273 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7274
7275 psa_interruptible_set_max_ops(1);
7276
7277 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7278 input_data->x, input_data->len,
7279 signature, signature_length));
7280
7281 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7282 PSA_OPERATION_INCOMPLETE);
7283
7284 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7285
7286 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7287
7288 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7289 input_data->x, input_data->len,
7290 signature, signature_length));
7291
7292 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7293
7294 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7295
7296 /* --- Attempt double starts. --- */
7297
7298 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7299 input_data->x, input_data->len));
7300
7301 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7302 input_data->x, input_data->len),
7303 PSA_ERROR_BAD_STATE);
7304
7305 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7306
7307 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7308 input_data->x, input_data->len,
7309 signature, signature_length));
7310
7311 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7312 input_data->x, input_data->len,
7313 signature, signature_length),
7314 PSA_ERROR_BAD_STATE);
7315
7316 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7317
Paul Elliott76d671a2023-02-07 17:45:18 +00007318exit:
7319 /*
7320 * Key attributes may have been returned by psa_get_key_attributes()
7321 * thus reset them as required.
7322 */
7323 psa_reset_key_attributes(&attributes);
7324
7325 psa_destroy_key(key);
7326 mbedtls_free(signature);
7327 PSA_DONE();
7328}
7329/* END_CASE */
7330
7331/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7332void interruptible_signverify_hash_negative_tests(int key_type_arg,
7333 data_t *key_data, int alg_arg, data_t *input_data)
7334{
7335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7336 psa_key_type_t key_type = key_type_arg;
7337 psa_algorithm_t alg = alg_arg;
7338 size_t key_bits;
7339 unsigned char *signature = NULL;
7340 size_t signature_size;
7341 size_t signature_length = 0xdeadbeef;
7342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7343 uint8_t *input_buffer = NULL;
7344 psa_sign_hash_interruptible_operation_t sign_operation =
7345 psa_sign_hash_interruptible_operation_init();
7346 psa_verify_hash_interruptible_operation_t verify_operation =
7347 psa_verify_hash_interruptible_operation_init();
7348
7349 PSA_ASSERT(psa_crypto_init());
7350
7351 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7352 PSA_KEY_USAGE_VERIFY_HASH);
7353 psa_set_key_algorithm(&attributes, alg);
7354 psa_set_key_type(&attributes, key_type);
7355
7356 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7357 &key));
7358 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7359 key_bits = psa_get_key_bits(&attributes);
7360
7361 /* Allocate a buffer which has the size advertised by the
7362 * library. */
7363 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7364 key_bits, alg);
7365 TEST_ASSERT(signature_size != 0);
7366 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7367 ASSERT_ALLOC(signature, signature_size);
7368
Paul Elliott20a36062022-12-18 13:21:25 +00007369 /* --- Ensure changing the max ops mid operation works (operation should
7370 * complete successfully after setting max ops to unlimited --- */
7371 psa_interruptible_set_max_ops(1);
7372
7373 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7374 input_data->x, input_data->len));
7375
7376 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7377 signature_size,
7378 &signature_length),
7379 PSA_OPERATION_INCOMPLETE);
7380
7381 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7382
7383 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7384 signature_size,
7385 &signature_length));
7386
7387 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7388
7389 psa_interruptible_set_max_ops(1);
7390
7391 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7392 input_data->x, input_data->len,
7393 signature, signature_length));
7394
7395 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7396 PSA_OPERATION_INCOMPLETE);
7397
7398 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7399
7400 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7401
7402 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7403
7404 /* --- Change function inputs mid run, to cause an error (sign only,
7405 * verify passes all inputs to start. --- */
7406
7407 psa_interruptible_set_max_ops(1);
7408
7409 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7410 input_data->x, input_data->len));
7411
7412 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7413 signature_size,
7414 &signature_length),
7415 PSA_OPERATION_INCOMPLETE);
7416
7417 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7418 0,
7419 &signature_length),
7420 PSA_ERROR_BUFFER_TOO_SMALL);
7421
Paul Elliottc9774412023-02-06 15:14:07 +00007422 /* And test that this invalidates the operation. */
7423 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7424 0,
7425 &signature_length),
7426 PSA_ERROR_BAD_STATE);
7427
Paul Elliott20a36062022-12-18 13:21:25 +00007428 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7429
Paul Elliottf9c91a72023-02-05 18:06:38 +00007430 /* Trash the hash buffer in between start and complete, to ensure
7431 * no reliance on external buffers. */
7432 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7433
7434 input_buffer = mbedtls_calloc(1, input_data->len);
7435 TEST_ASSERT(input_buffer != NULL);
7436
7437 memcpy(input_buffer, input_data->x, input_data->len);
7438
7439 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7440 input_buffer, input_data->len));
7441
7442 memset(input_buffer, '!', input_data->len);
7443 mbedtls_free(input_buffer);
7444 input_buffer = NULL;
7445
7446 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7447 signature_size,
7448 &signature_length));
7449
7450 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7451
7452 input_buffer = mbedtls_calloc(1, input_data->len);
7453 TEST_ASSERT(input_buffer != NULL);
7454
7455 memcpy(input_buffer, input_data->x, input_data->len);
7456
7457 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7458 input_buffer, input_data->len,
7459 signature, signature_length));
7460
7461 memset(input_buffer, '!', input_data->len);
7462 mbedtls_free(input_buffer);
7463 input_buffer = NULL;
7464
7465 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7466
7467 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7468
Paul Elliott20a36062022-12-18 13:21:25 +00007469exit:
7470 /*
7471 * Key attributes may have been returned by psa_get_key_attributes()
7472 * thus reset them as required.
7473 */
7474 psa_reset_key_attributes(&attributes);
7475
7476 psa_destroy_key(key);
7477 mbedtls_free(signature);
7478 PSA_DONE();
7479}
7480/* END_CASE */
7481
Paul Elliotta4cb9092023-02-07 18:01:55 +00007482/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7483void interruptible_signverify_hash_maxops_tests(int key_type_arg,
7484 data_t *key_data, int alg_arg, data_t *input_data)
7485{
7486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7487 psa_key_type_t key_type = key_type_arg;
7488 psa_algorithm_t alg = alg_arg;
7489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7490 psa_sign_hash_interruptible_operation_t sign_operation =
7491 psa_sign_hash_interruptible_operation_init();
7492
7493 PSA_ASSERT(psa_crypto_init());
7494
7495 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7496 PSA_KEY_USAGE_VERIFY_HASH);
7497 psa_set_key_algorithm(&attributes, alg);
7498 psa_set_key_type(&attributes, key_type);
7499
7500 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7501 &key));
7502
7503 /* Check that default max ops gets set if we don't set it. */
7504 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7505 input_data->x, input_data->len));
7506
7507 TEST_EQUAL(psa_interruptible_get_max_ops(),
7508 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7509
7510 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7511
7512 /* Check that max ops gets set properly. */
7513
7514 psa_interruptible_set_max_ops(0xbeef);
7515
7516 TEST_EQUAL(psa_interruptible_get_max_ops(),
7517 0xbeef);
7518
7519exit:
7520 /*
7521 * Key attributes may have been returned by psa_get_key_attributes()
7522 * thus reset them as required.
7523 */
7524 psa_reset_key_attributes(&attributes);
7525
7526 psa_destroy_key(key);
7527 PSA_DONE();
7528}
7529/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007530
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007531/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007532void sign_message_deterministic(int key_type_arg,
7533 data_t *key_data,
7534 int alg_arg,
7535 data_t *input_data,
7536 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007537{
7538 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7539 psa_key_type_t key_type = key_type_arg;
7540 psa_algorithm_t alg = alg_arg;
7541 size_t key_bits;
7542 unsigned char *signature = NULL;
7543 size_t signature_size;
7544 size_t signature_length = 0xdeadbeef;
7545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7546
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7550 psa_set_key_algorithm(&attributes, alg);
7551 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007552
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7554 &key));
7555 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7556 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7559 TEST_ASSERT(signature_size != 0);
7560 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7561 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007562
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 PSA_ASSERT(psa_sign_message(key, alg,
7564 input_data->x, input_data->len,
7565 signature, signature_size,
7566 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007567
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 ASSERT_COMPARE(output_data->x, output_data->len,
7569 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007570
7571exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007573
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 psa_destroy_key(key);
7575 mbedtls_free(signature);
7576 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007577
7578}
7579/* END_CASE */
7580
7581/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007582void sign_message_fail(int key_type_arg,
7583 data_t *key_data,
7584 int alg_arg,
7585 data_t *input_data,
7586 int signature_size_arg,
7587 int expected_status_arg)
7588{
7589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7590 psa_key_type_t key_type = key_type_arg;
7591 psa_algorithm_t alg = alg_arg;
7592 size_t signature_size = signature_size_arg;
7593 psa_status_t actual_status;
7594 psa_status_t expected_status = expected_status_arg;
7595 unsigned char *signature = NULL;
7596 size_t signature_length = 0xdeadbeef;
7597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7598
7599 ASSERT_ALLOC(signature, signature_size);
7600
7601 PSA_ASSERT(psa_crypto_init());
7602
7603 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7604 psa_set_key_algorithm(&attributes, alg);
7605 psa_set_key_type(&attributes, key_type);
7606
7607 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7608 &key));
7609
7610 actual_status = psa_sign_message(key, alg,
7611 input_data->x, input_data->len,
7612 signature, signature_size,
7613 &signature_length);
7614 TEST_EQUAL(actual_status, expected_status);
7615 /* The value of *signature_length is unspecified on error, but
7616 * whatever it is, it should be less than signature_size, so that
7617 * if the caller tries to read *signature_length bytes without
7618 * checking the error code then they don't overflow a buffer. */
7619 TEST_LE_U(signature_length, signature_size);
7620
7621exit:
7622 psa_reset_key_attributes(&attributes);
7623 psa_destroy_key(key);
7624 mbedtls_free(signature);
7625 PSA_DONE();
7626}
7627/* END_CASE */
7628
7629/* BEGIN_CASE */
7630void sign_verify_message(int key_type_arg,
7631 data_t *key_data,
7632 int alg_arg,
7633 data_t *input_data)
7634{
7635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7636 psa_key_type_t key_type = key_type_arg;
7637 psa_algorithm_t alg = alg_arg;
7638 size_t key_bits;
7639 unsigned char *signature = NULL;
7640 size_t signature_size;
7641 size_t signature_length = 0xdeadbeef;
7642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7643
7644 PSA_ASSERT(psa_crypto_init());
7645
7646 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7647 PSA_KEY_USAGE_VERIFY_MESSAGE);
7648 psa_set_key_algorithm(&attributes, alg);
7649 psa_set_key_type(&attributes, key_type);
7650
7651 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7652 &key));
7653 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7654 key_bits = psa_get_key_bits(&attributes);
7655
7656 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7657 TEST_ASSERT(signature_size != 0);
7658 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7659 ASSERT_ALLOC(signature, signature_size);
7660
7661 PSA_ASSERT(psa_sign_message(key, alg,
7662 input_data->x, input_data->len,
7663 signature, signature_size,
7664 &signature_length));
7665 TEST_LE_U(signature_length, signature_size);
7666 TEST_ASSERT(signature_length > 0);
7667
7668 PSA_ASSERT(psa_verify_message(key, alg,
7669 input_data->x, input_data->len,
7670 signature, signature_length));
7671
7672 if (input_data->len != 0) {
7673 /* Flip a bit in the input and verify that the signature is now
7674 * detected as invalid. Flip a bit at the beginning, not at the end,
7675 * because ECDSA may ignore the last few bits of the input. */
7676 input_data->x[0] ^= 1;
7677 TEST_EQUAL(psa_verify_message(key, alg,
7678 input_data->x, input_data->len,
7679 signature, signature_length),
7680 PSA_ERROR_INVALID_SIGNATURE);
7681 }
7682
7683exit:
7684 psa_reset_key_attributes(&attributes);
7685
7686 psa_destroy_key(key);
7687 mbedtls_free(signature);
7688 PSA_DONE();
7689}
7690/* END_CASE */
7691
7692/* BEGIN_CASE */
7693void verify_message(int key_type_arg,
7694 data_t *key_data,
7695 int alg_arg,
7696 data_t *input_data,
7697 data_t *signature_data)
7698{
7699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7700 psa_key_type_t key_type = key_type_arg;
7701 psa_algorithm_t alg = alg_arg;
7702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7703
7704 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7705
7706 PSA_ASSERT(psa_crypto_init());
7707
7708 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7709 psa_set_key_algorithm(&attributes, alg);
7710 psa_set_key_type(&attributes, key_type);
7711
7712 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7713 &key));
7714
7715 PSA_ASSERT(psa_verify_message(key, alg,
7716 input_data->x, input_data->len,
7717 signature_data->x, signature_data->len));
7718
7719exit:
7720 psa_reset_key_attributes(&attributes);
7721 psa_destroy_key(key);
7722 PSA_DONE();
7723}
7724/* END_CASE */
7725
7726/* BEGIN_CASE */
7727void verify_message_fail(int key_type_arg,
7728 data_t *key_data,
7729 int alg_arg,
7730 data_t *hash_data,
7731 data_t *signature_data,
7732 int expected_status_arg)
7733{
7734 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7735 psa_key_type_t key_type = key_type_arg;
7736 psa_algorithm_t alg = alg_arg;
7737 psa_status_t actual_status;
7738 psa_status_t expected_status = expected_status_arg;
7739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7740
7741 PSA_ASSERT(psa_crypto_init());
7742
7743 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7744 psa_set_key_algorithm(&attributes, alg);
7745 psa_set_key_type(&attributes, key_type);
7746
7747 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7748 &key));
7749
7750 actual_status = psa_verify_message(key, alg,
7751 hash_data->x, hash_data->len,
7752 signature_data->x,
7753 signature_data->len);
7754 TEST_EQUAL(actual_status, expected_status);
7755
7756exit:
7757 psa_reset_key_attributes(&attributes);
7758 psa_destroy_key(key);
7759 PSA_DONE();
7760}
7761/* END_CASE */
7762
7763/* BEGIN_CASE */
7764void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02007765 data_t *key_data,
7766 int alg_arg,
7767 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 data_t *label,
7769 int expected_output_length_arg,
7770 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02007771{
Ronald Cron5425a212020-08-04 14:58:35 +02007772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007773 psa_key_type_t key_type = key_type_arg;
7774 psa_algorithm_t alg = alg_arg;
7775 size_t expected_output_length = expected_output_length_arg;
7776 size_t key_bits;
7777 unsigned char *output = NULL;
7778 size_t output_size;
7779 size_t output_length = ~0;
7780 psa_status_t actual_status;
7781 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007785
Gilles Peskine656896e2018-06-29 19:12:28 +02007786 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
7788 psa_set_key_algorithm(&attributes, alg);
7789 psa_set_key_type(&attributes, key_type);
7790 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7791 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02007792
7793 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7795 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007796
Gilles Peskine449bd832023-01-11 14:50:10 +01007797 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7798 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7799 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02007800
7801 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 actual_status = psa_asymmetric_encrypt(key, alg,
7803 input_data->x, input_data->len,
7804 label->x, label->len,
7805 output, output_size,
7806 &output_length);
7807 TEST_EQUAL(actual_status, expected_status);
7808 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine656896e2018-06-29 19:12:28 +02007809
Gilles Peskine68428122018-06-30 18:42:41 +02007810 /* If the label is empty, the test framework puts a non-null pointer
7811 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007813 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 if (output_size != 0) {
7815 memset(output, 0, output_size);
7816 }
7817 actual_status = psa_asymmetric_encrypt(key, alg,
7818 input_data->x, input_data->len,
7819 NULL, label->len,
7820 output, output_size,
7821 &output_length);
7822 TEST_EQUAL(actual_status, expected_status);
7823 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007824 }
7825
Gilles Peskine656896e2018-06-29 19:12:28 +02007826exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007827 /*
7828 * Key attributes may have been returned by psa_get_key_attributes()
7829 * thus reset them as required.
7830 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007831 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007832
Gilles Peskine449bd832023-01-11 14:50:10 +01007833 psa_destroy_key(key);
7834 mbedtls_free(output);
7835 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02007836}
7837/* END_CASE */
7838
7839/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007840void asymmetric_encrypt_decrypt(int key_type_arg,
7841 data_t *key_data,
7842 int alg_arg,
7843 data_t *input_data,
7844 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007845{
Ronald Cron5425a212020-08-04 14:58:35 +02007846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007847 psa_key_type_t key_type = key_type_arg;
7848 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007849 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007850 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007851 size_t output_size;
7852 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007853 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007854 size_t output2_size;
7855 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007857
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007859
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
7861 psa_set_key_algorithm(&attributes, alg);
7862 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007863
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7865 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007866
7867 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7869 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007870
Gilles Peskine449bd832023-01-11 14:50:10 +01007871 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7872 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7873 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007874
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007875 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007876 TEST_LE_U(output2_size,
7877 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
7878 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7879 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007880
Gilles Peskineeebd7382018-06-08 18:11:54 +02007881 /* We test encryption by checking that encrypt-then-decrypt gives back
7882 * the original plaintext because of the non-optional random
7883 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
7885 input_data->x, input_data->len,
7886 label->x, label->len,
7887 output, output_size,
7888 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007889 /* We don't know what ciphertext length to expect, but check that
7890 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7894 output, output_length,
7895 label->x, label->len,
7896 output2, output2_size,
7897 &output2_length));
7898 ASSERT_COMPARE(input_data->x, input_data->len,
7899 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007900
7901exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007902 /*
7903 * Key attributes may have been returned by psa_get_key_attributes()
7904 * thus reset them as required.
7905 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007906 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 psa_destroy_key(key);
7909 mbedtls_free(output);
7910 mbedtls_free(output2);
7911 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007912}
7913/* END_CASE */
7914
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007915/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007916void asymmetric_decrypt(int key_type_arg,
7917 data_t *key_data,
7918 int alg_arg,
7919 data_t *input_data,
7920 data_t *label,
7921 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007922{
Ronald Cron5425a212020-08-04 14:58:35 +02007923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007924 psa_key_type_t key_type = key_type_arg;
7925 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007926 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007927 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007928 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007929 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007931
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007933
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7935 psa_set_key_algorithm(&attributes, alg);
7936 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007937
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7939 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007940
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7942 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007943
7944 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7946 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7947 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007948
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7950 input_data->x, input_data->len,
7951 label->x, label->len,
7952 output,
7953 output_size,
7954 &output_length));
7955 ASSERT_COMPARE(expected_data->x, expected_data->len,
7956 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007957
Gilles Peskine68428122018-06-30 18:42:41 +02007958 /* If the label is empty, the test framework puts a non-null pointer
7959 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007961 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 if (output_size != 0) {
7963 memset(output, 0, output_size);
7964 }
7965 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7966 input_data->x, input_data->len,
7967 NULL, label->len,
7968 output,
7969 output_size,
7970 &output_length));
7971 ASSERT_COMPARE(expected_data->x, expected_data->len,
7972 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007973 }
7974
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007975exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 psa_reset_key_attributes(&attributes);
7977 psa_destroy_key(key);
7978 mbedtls_free(output);
7979 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007980}
7981/* END_CASE */
7982
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007983/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007984void asymmetric_decrypt_fail(int key_type_arg,
7985 data_t *key_data,
7986 int alg_arg,
7987 data_t *input_data,
7988 data_t *label,
7989 int output_size_arg,
7990 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007991{
Ronald Cron5425a212020-08-04 14:58:35 +02007992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007993 psa_key_type_t key_type = key_type_arg;
7994 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007995 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007996 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007997 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007998 psa_status_t actual_status;
7999 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008000 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008001
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008005
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8007 psa_set_key_algorithm(&attributes, alg);
8008 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008009
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8011 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008012
Gilles Peskine449bd832023-01-11 14:50:10 +01008013 actual_status = psa_asymmetric_decrypt(key, alg,
8014 input_data->x, input_data->len,
8015 label->x, label->len,
8016 output, output_size,
8017 &output_length);
8018 TEST_EQUAL(actual_status, expected_status);
8019 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008020
Gilles Peskine68428122018-06-30 18:42:41 +02008021 /* If the label is empty, the test framework puts a non-null pointer
8022 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008024 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 if (output_size != 0) {
8026 memset(output, 0, output_size);
8027 }
8028 actual_status = psa_asymmetric_decrypt(key, alg,
8029 input_data->x, input_data->len,
8030 NULL, label->len,
8031 output, output_size,
8032 &output_length);
8033 TEST_EQUAL(actual_status, expected_status);
8034 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008035 }
8036
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008037exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 psa_reset_key_attributes(&attributes);
8039 psa_destroy_key(key);
8040 mbedtls_free(output);
8041 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008042}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008043/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008044
8045/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008046void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008047{
8048 /* Test each valid way of initializing the object, except for `= {0}`, as
8049 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8050 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008051 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008052 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008053 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008054 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8055 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008056
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008058
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008059 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008060 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8061 PSA_ERROR_BAD_STATE);
8062 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8063 PSA_ERROR_BAD_STATE);
8064 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8065 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008066
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008067 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 PSA_ASSERT(psa_key_derivation_abort(&func));
8069 PSA_ASSERT(psa_key_derivation_abort(&init));
8070 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008071}
8072/* END_CASE */
8073
Janos Follath16de4a42019-06-13 16:32:24 +01008074/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008075void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008076{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008077 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008078 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008079 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008080
Gilles Peskine449bd832023-01-11 14:50:10 +01008081 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008082
Gilles Peskine449bd832023-01-11 14:50:10 +01008083 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8084 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008085
8086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008087 psa_key_derivation_abort(&operation);
8088 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008089}
8090/* END_CASE */
8091
Janos Follathaf3c2a02019-06-12 12:34:34 +01008092/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008093void derive_set_capacity(int alg_arg, int capacity_arg,
8094 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008095{
8096 psa_algorithm_t alg = alg_arg;
8097 size_t capacity = capacity_arg;
8098 psa_status_t expected_status = expected_status_arg;
8099 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8100
Gilles Peskine449bd832023-01-11 14:50:10 +01008101 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008102
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008104
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8106 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008107
8108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 psa_key_derivation_abort(&operation);
8110 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008111}
8112/* END_CASE */
8113
8114/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008115void derive_input(int alg_arg,
8116 int step_arg1, int key_type_arg1, data_t *input1,
8117 int expected_status_arg1,
8118 int step_arg2, int key_type_arg2, data_t *input2,
8119 int expected_status_arg2,
8120 int step_arg3, int key_type_arg3, data_t *input3,
8121 int expected_status_arg3,
8122 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008123{
8124 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008125 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8126 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8127 psa_status_t expected_statuses[] = { expected_status_arg1,
8128 expected_status_arg2,
8129 expected_status_arg3 };
8130 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008131 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8132 MBEDTLS_SVC_KEY_ID_INIT,
8133 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008134 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8136 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008137 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008138 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008139 psa_status_t expected_output_status = expected_output_status_arg;
8140 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008141
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008143
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8145 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008146
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008148
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8150 mbedtls_test_set_step(i);
8151 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008152 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8154 psa_set_key_type(&attributes, key_types[i]);
8155 PSA_ASSERT(psa_import_key(&attributes,
8156 inputs[i]->x, inputs[i]->len,
8157 &keys[i]));
8158 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8159 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008160 // When taking a private key as secret input, use key agreement
8161 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8163 &operation, keys[i]),
8164 expected_statuses[i]);
8165 } else {
8166 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8167 keys[i]),
8168 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008170 } else {
8171 TEST_EQUAL(psa_key_derivation_input_bytes(
8172 &operation, steps[i],
8173 inputs[i]->x, inputs[i]->len),
8174 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008175 }
8176 }
8177
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 if (output_key_type != PSA_KEY_TYPE_NONE) {
8179 psa_reset_key_attributes(&attributes);
8180 psa_set_key_type(&attributes, output_key_type);
8181 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008182 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008183 psa_key_derivation_output_key(&attributes, &operation,
8184 &output_key);
8185 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008186 uint8_t buffer[1];
8187 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008188 psa_key_derivation_output_bytes(&operation,
8189 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008190 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008191 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008192
Janos Follathaf3c2a02019-06-12 12:34:34 +01008193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 psa_key_derivation_abort(&operation);
8195 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8196 psa_destroy_key(keys[i]);
8197 }
8198 psa_destroy_key(output_key);
8199 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008200}
8201/* END_CASE */
8202
Janos Follathd958bb72019-07-03 15:02:16 +01008203/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008204void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008205{
Janos Follathd958bb72019-07-03 15:02:16 +01008206 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008208 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008209 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008210 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008212 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008214 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008216 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8217 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008220
Gilles Peskine449bd832023-01-11 14:50:10 +01008221 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8224 psa_set_key_algorithm(&attributes, alg);
8225 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 PSA_ASSERT(psa_import_key(&attributes,
8228 key_data, sizeof(key_data),
8229 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008230
8231 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8233 input1, input1_length,
8234 input2, input2_length,
8235 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008238
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008239 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8241 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008242
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008244
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8246 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008247
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 psa_key_derivation_abort(&operation);
8250 psa_destroy_key(key);
8251 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008252}
8253/* END_CASE */
8254
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008255/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008256void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008257{
8258 uint8_t output_buffer[16];
8259 size_t buffer_size = 16;
8260 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008261 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008262
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8264 output_buffer, buffer_size)
8265 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008266
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8268 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008269
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008271
Gilles Peskine449bd832023-01-11 14:50:10 +01008272 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8273 output_buffer, buffer_size)
8274 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008275
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8277 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008278
8279exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008281}
8282/* END_CASE */
8283
8284/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008285void derive_output(int alg_arg,
8286 int step1_arg, data_t *input1, int expected_status_arg1,
8287 int step2_arg, data_t *input2, int expected_status_arg2,
8288 int step3_arg, data_t *input3, int expected_status_arg3,
8289 int step4_arg, data_t *input4, int expected_status_arg4,
8290 data_t *key_agreement_peer_key,
8291 int requested_capacity_arg,
8292 data_t *expected_output1,
8293 data_t *expected_output2,
8294 int other_key_input_type,
8295 int key_input_type,
8296 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008297{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008298 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8300 data_t *inputs[] = { input1, input2, input3, input4 };
8301 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8302 MBEDTLS_SVC_KEY_ID_INIT,
8303 MBEDTLS_SVC_KEY_ID_INIT,
8304 MBEDTLS_SVC_KEY_ID_INIT };
8305 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8306 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008307 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008308 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008309 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008311 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008313 size_t output_buffer_size = 0;
8314 uint8_t *output_buffer = NULL;
8315 size_t expected_capacity;
8316 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008317 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8318 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8319 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8320 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008321 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008322 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008323 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008324
Gilles Peskine449bd832023-01-11 14:50:10 +01008325 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8326 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008327 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 }
8329 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008330 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008332 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 ASSERT_ALLOC(output_buffer, output_buffer_size);
8334 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008335
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008336 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8338 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8339 requested_capacity));
8340 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8341 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008342 case 0:
8343 break;
8344 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008346 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 TEST_EQUAL(psa_key_derivation_input_bytes(
8348 &operation, steps[i],
8349 inputs[i]->x, inputs[i]->len),
8350 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008351
Gilles Peskine449bd832023-01-11 14:50:10 +01008352 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008353 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008355 break;
8356 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8358 psa_set_key_algorithm(&attributes1, alg);
8359 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008360
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 PSA_ASSERT(psa_import_key(&attributes1,
8362 inputs[i]->x, inputs[i]->len,
8363 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8366 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8367 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8368 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008369 }
8370
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8372 steps[i],
8373 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008374 break;
8375 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008377 break;
8378 }
8379 break;
8380 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008382 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8384 steps[i],
8385 inputs[i]->x,
8386 inputs[i]->len),
8387 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008388 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008389 case 1: // input key, type DERIVE
8390 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8392 psa_set_key_algorithm(&attributes2, alg);
8393 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008394
8395 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 if (other_key_input_type == 11) {
8397 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8398 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008399
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 PSA_ASSERT(psa_import_key(&attributes2,
8401 inputs[i]->x, inputs[i]->len,
8402 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008403
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8405 steps[i],
8406 keys[i]),
8407 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008408 break;
8409 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8411 psa_set_key_algorithm(&attributes3, alg);
8412 psa_set_key_type(&attributes3,
8413 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008414
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 PSA_ASSERT(psa_import_key(&attributes3,
8416 inputs[i]->x, inputs[i]->len,
8417 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008418
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 TEST_EQUAL(psa_key_derivation_key_agreement(
8420 &operation,
8421 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8422 keys[i], key_agreement_peer_key->x,
8423 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008424 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008425 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008427 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008428 }
8429
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008431 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008433 break;
8434 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 TEST_EQUAL(psa_key_derivation_input_bytes(
8436 &operation, steps[i],
8437 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008438
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008442 break;
8443 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008444 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008445
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8447 &current_capacity));
8448 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008449 expected_capacity = requested_capacity;
8450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008452 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8453
8454 /* For output key derivation secret must be provided using
8455 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008457 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008458 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8461 psa_set_key_algorithm(&attributes4, alg);
8462 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8463 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008464
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8466 &derived_key), expected_status);
8467 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008468 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008470 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 status = psa_key_derivation_output_bytes(&operation,
8472 output_buffer, output_sizes[i]);
8473 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008474 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 TEST_ASSERT(status == PSA_SUCCESS ||
8476 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008477 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 } else if (expected_capacity == 0 ||
8479 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008480 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008482 expected_capacity = 0;
8483 continue;
8484 }
8485 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 PSA_ASSERT(status);
8487 if (output_sizes[i] != 0) {
8488 ASSERT_COMPARE(output_buffer, output_sizes[i],
8489 expected_outputs[i], output_sizes[i]);
8490 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008491 /* Check the operation status. */
8492 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8494 &current_capacity));
8495 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008496 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008497 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008498 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008499
8500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008501 mbedtls_free(output_buffer);
8502 psa_key_derivation_abort(&operation);
8503 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8504 psa_destroy_key(keys[i]);
8505 }
8506 psa_destroy_key(derived_key);
8507 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008508}
8509/* END_CASE */
8510
8511/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008512void derive_full(int alg_arg,
8513 data_t *key_data,
8514 data_t *input1,
8515 data_t *input2,
8516 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008517{
Ronald Cron5425a212020-08-04 14:58:35 +02008518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008519 psa_algorithm_t alg = alg_arg;
8520 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008521 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008522 unsigned char output_buffer[16];
8523 size_t expected_capacity = requested_capacity;
8524 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008526
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008528
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8530 psa_set_key_algorithm(&attributes, alg);
8531 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008532
Gilles Peskine449bd832023-01-11 14:50:10 +01008533 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8534 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008535
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8537 input1->x, input1->len,
8538 input2->x, input2->len,
8539 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008540 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008542
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8544 &current_capacity));
8545 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008546
8547 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 while (current_capacity > 0) {
8549 size_t read_size = sizeof(output_buffer);
8550 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008551 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 }
8553 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8554 output_buffer,
8555 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008556 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8558 &current_capacity));
8559 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008560 }
8561
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008562 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8564 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008565
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008567
8568exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 psa_key_derivation_abort(&operation);
8570 psa_destroy_key(key);
8571 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008572}
8573/* END_CASE */
8574
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008575/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008576void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8577 int derivation_step,
8578 int capacity, int expected_capacity_status_arg,
8579 data_t *expected_output,
8580 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008581{
8582 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8583 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008584 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008585 uint8_t *output_buffer = NULL;
8586 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008587 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8588 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8589 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008590
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 ASSERT_ALLOC(output_buffer, expected_output->len);
8592 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008593
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8595 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8596 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8599 step, input->x, input->len),
8600 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008601
Gilles Peskine449bd832023-01-11 14:50:10 +01008602 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008605
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8607 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008608
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 TEST_EQUAL(status, expected_output_status);
8610 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8611 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8612 expected_output->len);
8613 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008614
8615exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 mbedtls_free(output_buffer);
8617 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008618 PSA_DONE();
8619}
8620/* END_CASE */
8621
Janos Follathe60c9052019-07-03 13:51:30 +01008622/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008623void derive_key_exercise(int alg_arg,
8624 data_t *key_data,
8625 data_t *input1,
8626 data_t *input2,
8627 int derived_type_arg,
8628 int derived_bits_arg,
8629 int derived_usage_arg,
8630 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008631{
Ronald Cron5425a212020-08-04 14:58:35 +02008632 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8633 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008634 psa_algorithm_t alg = alg_arg;
8635 psa_key_type_t derived_type = derived_type_arg;
8636 size_t derived_bits = derived_bits_arg;
8637 psa_key_usage_t derived_usage = derived_usage_arg;
8638 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008640 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008642 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008643
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008645
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8647 psa_set_key_algorithm(&attributes, alg);
8648 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8649 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8650 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008651
8652 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8654 input1->x, input1->len,
8655 input2->x, input2->len,
8656 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 }
Janos Follathe60c9052019-07-03 13:51:30 +01008659
Gilles Peskine449bd832023-01-11 14:50:10 +01008660 psa_set_key_usage_flags(&attributes, derived_usage);
8661 psa_set_key_algorithm(&attributes, derived_alg);
8662 psa_set_key_type(&attributes, derived_type);
8663 psa_set_key_bits(&attributes, derived_bits);
8664 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8665 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008666
8667 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8669 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8670 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008671
8672 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008674 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008676
8677exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008678 /*
8679 * Key attributes may have been returned by psa_get_key_attributes()
8680 * thus reset them as required.
8681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008683
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 psa_key_derivation_abort(&operation);
8685 psa_destroy_key(base_key);
8686 psa_destroy_key(derived_key);
8687 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008688}
8689/* END_CASE */
8690
Janos Follath42fd8882019-07-03 14:17:09 +01008691/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008692void derive_key_export(int alg_arg,
8693 data_t *key_data,
8694 data_t *input1,
8695 data_t *input2,
8696 int bytes1_arg,
8697 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008698{
Ronald Cron5425a212020-08-04 14:58:35 +02008699 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8700 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008701 psa_algorithm_t alg = alg_arg;
8702 size_t bytes1 = bytes1_arg;
8703 size_t bytes2 = bytes2_arg;
8704 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008705 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008706 uint8_t *output_buffer = NULL;
8707 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008708 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8709 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008710 size_t length;
8711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 ASSERT_ALLOC(output_buffer, capacity);
8713 ASSERT_ALLOC(export_buffer, capacity);
8714 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8717 psa_set_key_algorithm(&base_attributes, alg);
8718 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8719 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8720 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008721
8722 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8724 input1->x, input1->len,
8725 input2->x, input2->len,
8726 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008727 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 }
Janos Follath42fd8882019-07-03 14:17:09 +01008729
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8731 output_buffer,
8732 capacity));
8733 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008734
8735 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8737 input1->x, input1->len,
8738 input2->x, input2->len,
8739 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008740 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 }
Janos Follath42fd8882019-07-03 14:17:09 +01008742
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8744 psa_set_key_algorithm(&derived_attributes, 0);
8745 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
8746 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
8747 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8748 &derived_key));
8749 PSA_ASSERT(psa_export_key(derived_key,
8750 export_buffer, bytes1,
8751 &length));
8752 TEST_EQUAL(length, bytes1);
8753 PSA_ASSERT(psa_destroy_key(derived_key));
8754 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
8755 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8756 &derived_key));
8757 PSA_ASSERT(psa_export_key(derived_key,
8758 export_buffer + bytes1, bytes2,
8759 &length));
8760 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008761
8762 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
8764 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008765
8766exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 mbedtls_free(output_buffer);
8768 mbedtls_free(export_buffer);
8769 psa_key_derivation_abort(&operation);
8770 psa_destroy_key(base_key);
8771 psa_destroy_key(derived_key);
8772 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008773}
8774/* END_CASE */
8775
8776/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008777void derive_key_type(int alg_arg,
8778 data_t *key_data,
8779 data_t *input1,
8780 data_t *input2,
8781 int key_type_arg, int bits_arg,
8782 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008783{
8784 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8785 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8786 const psa_algorithm_t alg = alg_arg;
8787 const psa_key_type_t key_type = key_type_arg;
8788 const size_t bits = bits_arg;
8789 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8790 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008792 uint8_t *export_buffer = NULL;
8793 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8794 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8795 size_t export_length;
8796
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 ASSERT_ALLOC(export_buffer, export_buffer_size);
8798 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008799
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8801 psa_set_key_algorithm(&base_attributes, alg);
8802 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8803 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8804 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008805
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008807 &operation, base_key, alg,
8808 input1->x, input1->len,
8809 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008811 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008813
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8815 psa_set_key_algorithm(&derived_attributes, 0);
8816 psa_set_key_type(&derived_attributes, key_type);
8817 psa_set_key_bits(&derived_attributes, bits);
8818 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8819 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 PSA_ASSERT(psa_export_key(derived_key,
8822 export_buffer, export_buffer_size,
8823 &export_length));
8824 ASSERT_COMPARE(export_buffer, export_length,
8825 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008826
8827exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 mbedtls_free(export_buffer);
8829 psa_key_derivation_abort(&operation);
8830 psa_destroy_key(base_key);
8831 psa_destroy_key(derived_key);
8832 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008833}
8834/* END_CASE */
8835
8836/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008837void derive_key(int alg_arg,
8838 data_t *key_data, data_t *input1, data_t *input2,
8839 int type_arg, int bits_arg,
8840 int expected_status_arg,
8841 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02008842{
Ronald Cron5425a212020-08-04 14:58:35 +02008843 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8844 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008845 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008846 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008847 size_t bits = bits_arg;
8848 psa_status_t expected_status = expected_status_arg;
8849 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8850 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8851 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8852
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02008854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8856 psa_set_key_algorithm(&base_attributes, alg);
8857 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8858 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8859 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02008860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8862 input1->x, input1->len,
8863 input2->x, input2->len,
8864 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02008865 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 }
Gilles Peskinec744d992019-07-30 17:26:54 +02008867
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8869 psa_set_key_algorithm(&derived_attributes, 0);
8870 psa_set_key_type(&derived_attributes, type);
8871 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008872
8873 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 psa_key_derivation_output_key(&derived_attributes,
8875 &operation,
8876 &derived_key);
8877 if (is_large_output > 0) {
8878 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8879 }
8880 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02008881
8882exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 psa_key_derivation_abort(&operation);
8884 psa_destroy_key(base_key);
8885 psa_destroy_key(derived_key);
8886 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02008887}
8888/* END_CASE */
8889
8890/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008891void key_agreement_setup(int alg_arg,
8892 int our_key_type_arg, int our_key_alg_arg,
8893 data_t *our_key_data, data_t *peer_key_data,
8894 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02008895{
Ronald Cron5425a212020-08-04 14:58:35 +02008896 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008897 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008898 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008899 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008900 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008902 psa_status_t expected_status = expected_status_arg;
8903 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02008906
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8908 psa_set_key_algorithm(&attributes, our_key_alg);
8909 psa_set_key_type(&attributes, our_key_type);
8910 PSA_ASSERT(psa_import_key(&attributes,
8911 our_key_data->x, our_key_data->len,
8912 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008913
Gilles Peskine77f40d82019-04-11 21:27:06 +02008914 /* The tests currently include inputs that should fail at either step.
8915 * Test cases that fail at the setup step should be changed to call
8916 * key_derivation_setup instead, and this function should be renamed
8917 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 status = psa_key_derivation_setup(&operation, alg);
8919 if (status == PSA_SUCCESS) {
8920 TEST_EQUAL(psa_key_derivation_key_agreement(
8921 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8922 our_key,
8923 peer_key_data->x, peer_key_data->len),
8924 expected_status);
8925 } else {
8926 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008927 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008928
8929exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 psa_key_derivation_abort(&operation);
8931 psa_destroy_key(our_key);
8932 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008933}
8934/* END_CASE */
8935
8936/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008937void raw_key_agreement(int alg_arg,
8938 int our_key_type_arg, data_t *our_key_data,
8939 data_t *peer_key_data,
8940 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008941{
Ronald Cron5425a212020-08-04 14:58:35 +02008942 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008943 psa_algorithm_t alg = alg_arg;
8944 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008946 unsigned char *output = NULL;
8947 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008948 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02008951
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8953 psa_set_key_algorithm(&attributes, alg);
8954 psa_set_key_type(&attributes, our_key_type);
8955 PSA_ASSERT(psa_import_key(&attributes,
8956 our_key_data->x, our_key_data->len,
8957 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02008958
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
8960 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008961
Gilles Peskine992bee82022-04-13 23:25:52 +02008962 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 TEST_LE_U(expected_output->len,
8964 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
8965 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
8966 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02008967
8968 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 ASSERT_ALLOC(output, expected_output->len);
8970 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8971 peer_key_data->x, peer_key_data->len,
8972 output, expected_output->len,
8973 &output_length));
8974 ASSERT_COMPARE(output, output_length,
8975 expected_output->x, expected_output->len);
8976 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008977 output = NULL;
8978 output_length = ~0;
8979
8980 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 ASSERT_ALLOC(output, expected_output->len + 1);
8982 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8983 peer_key_data->x, peer_key_data->len,
8984 output, expected_output->len + 1,
8985 &output_length));
8986 ASSERT_COMPARE(output, output_length,
8987 expected_output->x, expected_output->len);
8988 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008989 output = NULL;
8990 output_length = ~0;
8991
8992 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 ASSERT_ALLOC(output, expected_output->len - 1);
8994 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
8995 peer_key_data->x, peer_key_data->len,
8996 output, expected_output->len - 1,
8997 &output_length),
8998 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02008999 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 TEST_LE_U(output_length, expected_output->len - 1);
9001 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009002 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009003
9004exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 mbedtls_free(output);
9006 psa_destroy_key(our_key);
9007 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009008}
9009/* END_CASE */
9010
9011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009012void key_agreement_capacity(int alg_arg,
9013 int our_key_type_arg, data_t *our_key_data,
9014 data_t *peer_key_data,
9015 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009016{
Ronald Cron5425a212020-08-04 14:58:35 +02009017 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009018 psa_algorithm_t alg = alg_arg;
9019 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009020 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009022 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009023 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009024
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009026
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9028 psa_set_key_algorithm(&attributes, alg);
9029 psa_set_key_type(&attributes, our_key_type);
9030 PSA_ASSERT(psa_import_key(&attributes,
9031 our_key_data->x, our_key_data->len,
9032 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9035 PSA_ASSERT(psa_key_derivation_key_agreement(
9036 &operation,
9037 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9038 peer_key_data->x, peer_key_data->len));
9039 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009040 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9042 PSA_KEY_DERIVATION_INPUT_INFO,
9043 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009044 }
Gilles Peskine59685592018-09-18 12:11:34 +02009045
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009046 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 PSA_ASSERT(psa_key_derivation_get_capacity(
9048 &operation, &actual_capacity));
9049 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009050
Gilles Peskinebf491972018-10-25 22:36:12 +02009051 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 while (actual_capacity > sizeof(output)) {
9053 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9054 output, sizeof(output)));
9055 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009056 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9058 output, actual_capacity));
9059 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9060 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009061
Gilles Peskine59685592018-09-18 12:11:34 +02009062exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 psa_key_derivation_abort(&operation);
9064 psa_destroy_key(our_key);
9065 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009066}
9067/* END_CASE */
9068
9069/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009070void key_agreement_output(int alg_arg,
9071 int our_key_type_arg, data_t *our_key_data,
9072 data_t *peer_key_data,
9073 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009074{
Ronald Cron5425a212020-08-04 14:58:35 +02009075 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009076 psa_algorithm_t alg = alg_arg;
9077 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009078 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009080 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009081
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9083 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009084
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9088 psa_set_key_algorithm(&attributes, alg);
9089 psa_set_key_type(&attributes, our_key_type);
9090 PSA_ASSERT(psa_import_key(&attributes,
9091 our_key_data->x, our_key_data->len,
9092 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009093
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9095 PSA_ASSERT(psa_key_derivation_key_agreement(
9096 &operation,
9097 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9098 peer_key_data->x, peer_key_data->len));
9099 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009100 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9102 PSA_KEY_DERIVATION_INPUT_INFO,
9103 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009104 }
Gilles Peskine59685592018-09-18 12:11:34 +02009105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9107 actual_output,
9108 expected_output1->len));
9109 ASSERT_COMPARE(actual_output, expected_output1->len,
9110 expected_output1->x, expected_output1->len);
9111 if (expected_output2->len != 0) {
9112 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9113 actual_output,
9114 expected_output2->len));
9115 ASSERT_COMPARE(actual_output, expected_output2->len,
9116 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009117 }
Gilles Peskine59685592018-09-18 12:11:34 +02009118
9119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009120 psa_key_derivation_abort(&operation);
9121 psa_destroy_key(our_key);
9122 PSA_DONE();
9123 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009124}
9125/* END_CASE */
9126
9127/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009128void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009129{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009130 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009131 unsigned char *output = NULL;
9132 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009133 size_t i;
9134 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009137
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 ASSERT_ALLOC(output, bytes);
9139 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009142
Gilles Peskinea50d7392018-06-21 10:22:13 +02009143 /* Run several times, to ensure that every output byte will be
9144 * nonzero at least once with overwhelming probability
9145 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 for (run = 0; run < 10; run++) {
9147 if (bytes != 0) {
9148 memset(output, 0, bytes);
9149 }
9150 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009151
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 for (i = 0; i < bytes; i++) {
9153 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009154 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009156 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009157 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009158
9159 /* Check that every byte was changed to nonzero at least once. This
9160 * validates that psa_generate_random is overwriting every byte of
9161 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 for (i = 0; i < bytes; i++) {
9163 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009164 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009165
9166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 PSA_DONE();
9168 mbedtls_free(output);
9169 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009170}
9171/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009172
9173/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009174void generate_key(int type_arg,
9175 int bits_arg,
9176 int usage_arg,
9177 int alg_arg,
9178 int expected_status_arg,
9179 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009180{
Ronald Cron5425a212020-08-04 14:58:35 +02009181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009182 psa_key_type_t type = type_arg;
9183 psa_key_usage_t usage = usage_arg;
9184 size_t bits = bits_arg;
9185 psa_algorithm_t alg = alg_arg;
9186 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009188 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009189
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009191
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 psa_set_key_usage_flags(&attributes, usage);
9193 psa_set_key_algorithm(&attributes, alg);
9194 psa_set_key_type(&attributes, type);
9195 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009196
9197 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009198 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009199
Gilles Peskine449bd832023-01-11 14:50:10 +01009200 if (is_large_key > 0) {
9201 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9202 }
9203 TEST_EQUAL(status, expected_status);
9204 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009207
9208 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9210 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9211 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009212
Gilles Peskine818ca122018-06-20 18:16:48 +02009213 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009217
9218exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009219 /*
9220 * Key attributes may have been returned by psa_get_key_attributes()
9221 * thus reset them as required.
9222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 psa_destroy_key(key);
9226 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009227}
9228/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009229
Ronald Cronee414c72021-03-18 18:50:08 +01009230/* 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 +01009231void generate_key_rsa(int bits_arg,
9232 data_t *e_arg,
9233 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009234{
Ronald Cron5425a212020-08-04 14:58:35 +02009235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009236 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009237 size_t bits = bits_arg;
9238 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9239 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9240 psa_status_t expected_status = expected_status_arg;
9241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9242 uint8_t *exported = NULL;
9243 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009245 size_t exported_length = SIZE_MAX;
9246 uint8_t *e_read_buffer = NULL;
9247 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009249 size_t e_read_length = SIZE_MAX;
9250
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 if (e_arg->len == 0 ||
9252 (e_arg->len == 3 &&
9253 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009254 is_default_public_exponent = 1;
9255 e_read_size = 0;
9256 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 ASSERT_ALLOC(e_read_buffer, e_read_size);
9258 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009259
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009261
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 psa_set_key_usage_flags(&attributes, usage);
9263 psa_set_key_algorithm(&attributes, alg);
9264 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9265 e_arg->x, e_arg->len));
9266 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009267
9268 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9270 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009272 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009273
9274 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009275 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9276 TEST_EQUAL(psa_get_key_type(&attributes), type);
9277 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9278 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9279 e_read_buffer, e_read_size,
9280 &e_read_length));
9281 if (is_default_public_exponent) {
9282 TEST_EQUAL(e_read_length, 0);
9283 } else {
9284 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9285 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009286
9287 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009291
9292 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 PSA_ASSERT(psa_export_public_key(key,
9294 exported, exported_size,
9295 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009296 {
9297 uint8_t *p = exported;
9298 uint8_t *end = exported + exported_length;
9299 size_t len;
9300 /* RSAPublicKey ::= SEQUENCE {
9301 * modulus INTEGER, -- n
9302 * publicExponent INTEGER } -- e
9303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9305 MBEDTLS_ASN1_SEQUENCE |
9306 MBEDTLS_ASN1_CONSTRUCTED));
9307 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9308 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9309 MBEDTLS_ASN1_INTEGER));
9310 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009311 ++p;
9312 --len;
9313 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 if (e_arg->len == 0) {
9315 TEST_EQUAL(len, 3);
9316 TEST_EQUAL(p[0], 1);
9317 TEST_EQUAL(p[1], 0);
9318 TEST_EQUAL(p[2], 1);
9319 } else {
9320 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009321 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009322 }
9323
9324exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009325 /*
9326 * Key attributes may have been returned by psa_get_key_attributes() or
9327 * set by psa_set_key_domain_parameters() thus reset them as required.
9328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009330
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 psa_destroy_key(key);
9332 PSA_DONE();
9333 mbedtls_free(e_read_buffer);
9334 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009335}
9336/* END_CASE */
9337
Darryl Greend49a4992018-06-18 17:27:26 +01009338/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009339void persistent_key_load_key_from_storage(data_t *data,
9340 int type_arg, int bits_arg,
9341 int usage_flags_arg, int alg_arg,
9342 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009343{
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9347 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009348 psa_key_type_t type = type_arg;
9349 size_t bits = bits_arg;
9350 psa_key_usage_t usage_flags = usage_flags_arg;
9351 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009353 unsigned char *first_export = NULL;
9354 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009356 size_t first_exported_length;
9357 size_t second_exported_length;
9358
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9360 ASSERT_ALLOC(first_export, export_size);
9361 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009362 }
Darryl Greend49a4992018-06-18 17:27:26 +01009363
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009365
Gilles Peskine449bd832023-01-11 14:50:10 +01009366 psa_set_key_id(&attributes, key_id);
9367 psa_set_key_usage_flags(&attributes, usage_flags);
9368 psa_set_key_algorithm(&attributes, alg);
9369 psa_set_key_type(&attributes, type);
9370 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009371
Gilles Peskine449bd832023-01-11 14:50:10 +01009372 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009373 case IMPORT_KEY:
9374 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9376 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009377 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009378
Darryl Green0c6575a2018-11-07 16:05:30 +00009379 case GENERATE_KEY:
9380 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009381 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009382 break;
9383
9384 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009385#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009386 {
9387 /* Create base key */
9388 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9389 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9390 psa_set_key_usage_flags(&base_attributes,
9391 PSA_KEY_USAGE_DERIVE);
9392 psa_set_key_algorithm(&base_attributes, derive_alg);
9393 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9394 PSA_ASSERT(psa_import_key(&base_attributes,
9395 data->x, data->len,
9396 &base_key));
9397 /* Derive a key. */
9398 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9399 PSA_ASSERT(psa_key_derivation_input_key(
9400 &operation,
9401 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9402 PSA_ASSERT(psa_key_derivation_input_bytes(
9403 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9404 NULL, 0));
9405 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9406 &operation,
9407 &key));
9408 PSA_ASSERT(psa_key_derivation_abort(&operation));
9409 PSA_ASSERT(psa_destroy_key(base_key));
9410 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9411 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009412#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009414#endif
9415 break;
9416
9417 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009419 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009420 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009422
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009423 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9425 PSA_ASSERT(psa_export_key(key,
9426 first_export, export_size,
9427 &first_exported_length));
9428 if (generation_method == IMPORT_KEY) {
9429 ASSERT_COMPARE(data->x, data->len,
9430 first_export, first_exported_length);
9431 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009432 }
Darryl Greend49a4992018-06-18 17:27:26 +01009433
9434 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009436 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009437 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009438
Darryl Greend49a4992018-06-18 17:27:26 +01009439 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9441 TEST_ASSERT(mbedtls_svc_key_id_equal(
9442 psa_get_key_id(&attributes), key_id));
9443 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9444 PSA_KEY_LIFETIME_PERSISTENT);
9445 TEST_EQUAL(psa_get_key_type(&attributes), type);
9446 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9447 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9448 mbedtls_test_update_key_usage_flags(usage_flags));
9449 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009450
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009451 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9453 PSA_ASSERT(psa_export_key(key,
9454 second_export, export_size,
9455 &second_exported_length));
9456 ASSERT_COMPARE(first_export, first_exported_length,
9457 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009458 }
9459
9460 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009461 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009462 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 }
Darryl Greend49a4992018-06-18 17:27:26 +01009464
9465exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009466 /*
9467 * Key attributes may have been returned by psa_get_key_attributes()
9468 * thus reset them as required.
9469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009470 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009471
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 mbedtls_free(first_export);
9473 mbedtls_free(second_export);
9474 psa_key_derivation_abort(&operation);
9475 psa_destroy_key(base_key);
9476 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009477 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009478}
9479/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009480
Neil Armstronga557cb82022-06-10 08:58:32 +02009481/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009482void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9483 int primitive_arg, int hash_arg, int role_arg,
9484 int test_input, data_t *pw_data,
9485 int inj_err_type_arg,
9486 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009487{
9488 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9489 psa_pake_operation_t operation = psa_pake_operation_init();
9490 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009491 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009492 psa_key_type_t key_type_pw = key_type_pw_arg;
9493 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009494 psa_algorithm_t hash_alg = hash_arg;
9495 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009498 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9499 psa_status_t expected_error = expected_error_arg;
9500 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009501 unsigned char *output_buffer = NULL;
9502 size_t output_len = 0;
9503
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009505
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009506 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 PSA_PAKE_STEP_KEY_SHARE);
9508 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 if (pw_data->len > 0) {
9511 psa_set_key_usage_flags(&attributes, key_usage_pw);
9512 psa_set_key_algorithm(&attributes, alg);
9513 psa_set_key_type(&attributes, key_type_pw);
9514 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9515 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009516 }
9517
Gilles Peskine449bd832023-01-11 14:50:10 +01009518 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9519 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9520 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009521
Gilles Peskine449bd832023-01-11 14:50:10 +01009522 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009523
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9525 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9526 expected_error);
9527 PSA_ASSERT(psa_pake_abort(&operation));
9528 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9529 expected_error);
9530 PSA_ASSERT(psa_pake_abort(&operation));
9531 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9532 expected_error);
9533 PSA_ASSERT(psa_pake_abort(&operation));
9534 TEST_EQUAL(psa_pake_set_role(&operation, role),
9535 expected_error);
9536 PSA_ASSERT(psa_pake_abort(&operation));
9537 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9538 NULL, 0, NULL),
9539 expected_error);
9540 PSA_ASSERT(psa_pake_abort(&operation));
9541 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9542 expected_error);
9543 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009544 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009545 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009546
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 status = psa_pake_setup(&operation, &cipher_suite);
9548 if (status != PSA_SUCCESS) {
9549 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009550 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009551 }
9552
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9554 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9555 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009556 goto exit;
9557 }
9558
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 status = psa_pake_set_role(&operation, role);
9560 if (status != PSA_SUCCESS) {
9561 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009562 goto exit;
9563 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009564
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 if (pw_data->len > 0) {
9566 status = psa_pake_set_password_key(&operation, key);
9567 if (status != PSA_SUCCESS) {
9568 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009569 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009570 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009571 }
9572
Gilles Peskine449bd832023-01-11 14:50:10 +01009573 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9574 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9575 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009576 goto exit;
9577 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009578
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9580 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9581 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009582 goto exit;
9583 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009584
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009586 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9588 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009589 goto exit;
9590 }
9591
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009593 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009594 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9595 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009596 goto exit;
9597 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9600 PSA_PAKE_STEP_KEY_SHARE);
9601 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9602 PSA_PAKE_STEP_ZK_PUBLIC);
9603 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9604 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009605
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 if (test_input) {
9607 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9608 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9609 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009610 goto exit;
9611 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9614 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9615 output_buffer, size_zk_proof),
9616 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009617 goto exit;
9618 }
9619
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9621 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9622 output_buffer, size_zk_proof),
9623 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009624 goto exit;
9625 }
9626
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9628 output_buffer, size_key_share);
9629 if (status != PSA_SUCCESS) {
9630 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009631 goto exit;
9632 }
9633
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9635 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9636 output_buffer, size_zk_public + 1),
9637 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009638 goto exit;
9639 }
9640
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009642 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9644 output_buffer, size_zk_public + 1);
9645 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9646 output_buffer, size_zk_public),
9647 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009648 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009649 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009650 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009651 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9652 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9653 NULL, 0, NULL),
9654 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009655 goto exit;
9656 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009657
Gilles Peskine449bd832023-01-11 14:50:10 +01009658 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9659 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9660 output_buffer, buf_size, &output_len),
9661 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009662 goto exit;
9663 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009664
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9666 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9667 output_buffer, buf_size, &output_len),
9668 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009669 goto exit;
9670 }
9671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9673 output_buffer, buf_size, &output_len);
9674 if (status != PSA_SUCCESS) {
9675 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009676 goto exit;
9677 }
9678
Gilles Peskine449bd832023-01-11 14:50:10 +01009679 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009680
Gilles Peskine449bd832023-01-11 14:50:10 +01009681 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9682 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9683 output_buffer, size_zk_public - 1, &output_len),
9684 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01009685 goto exit;
9686 }
9687
Gilles Peskine449bd832023-01-11 14:50:10 +01009688 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009689 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009690 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9691 output_buffer, size_zk_public - 1, &output_len);
9692 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9693 output_buffer, buf_size, &output_len),
9694 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009695 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009696 }
9697 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009698
9699exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 PSA_ASSERT(psa_destroy_key(key));
9701 PSA_ASSERT(psa_pake_abort(&operation));
9702 mbedtls_free(output_buffer);
9703 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009704}
9705/* END_CASE */
9706
Neil Armstronga557cb82022-06-10 08:58:32 +02009707/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009708void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
9709 int client_input_first, int inject_error,
9710 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009711{
9712 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9713 psa_pake_operation_t server = psa_pake_operation_init();
9714 psa_pake_operation_t client = psa_pake_operation_init();
9715 psa_algorithm_t alg = alg_arg;
9716 psa_algorithm_t hash_alg = hash_arg;
9717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9719
Gilles Peskine449bd832023-01-11 14:50:10 +01009720 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009721
Gilles Peskine449bd832023-01-11 14:50:10 +01009722 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9723 psa_set_key_algorithm(&attributes, alg);
9724 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9725 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9726 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009727
Gilles Peskine449bd832023-01-11 14:50:10 +01009728 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9729 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9730 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009731
9732
Gilles Peskine449bd832023-01-11 14:50:10 +01009733 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9734 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009735
Gilles Peskine449bd832023-01-11 14:50:10 +01009736 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9737 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009738
Gilles Peskine449bd832023-01-11 14:50:10 +01009739 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9740 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009741
Gilles Peskine449bd832023-01-11 14:50:10 +01009742 ecjpake_do_round(alg, primitive_arg, &server, &client,
9743 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009744
Gilles Peskine449bd832023-01-11 14:50:10 +01009745 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009746 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009747 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009748
Gilles Peskine449bd832023-01-11 14:50:10 +01009749 ecjpake_do_round(alg, primitive_arg, &server, &client,
9750 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009751
9752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009753 psa_destroy_key(key);
9754 psa_pake_abort(&server);
9755 psa_pake_abort(&client);
9756 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009757}
9758/* END_CASE */
9759
9760/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009761void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
9762 int derive_alg_arg, data_t *pw_data,
9763 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009764{
9765 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9766 psa_pake_operation_t server = psa_pake_operation_init();
9767 psa_pake_operation_t client = psa_pake_operation_init();
9768 psa_algorithm_t alg = alg_arg;
9769 psa_algorithm_t hash_alg = hash_arg;
9770 psa_algorithm_t derive_alg = derive_alg_arg;
9771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9773 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009774 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009775 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009776 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009777 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009778
Gilles Peskine449bd832023-01-11 14:50:10 +01009779 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009780
Gilles Peskine449bd832023-01-11 14:50:10 +01009781 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9782 psa_set_key_algorithm(&attributes, alg);
9783 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9784 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9785 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009786
Gilles Peskine449bd832023-01-11 14:50:10 +01009787 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9788 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9789 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009790
Neil Armstrong1e855602022-06-15 11:32:11 +02009791 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009792 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
9793 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02009794
Gilles Peskine449bd832023-01-11 14:50:10 +01009795 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
9796 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
9797 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
9798 PSA_KEY_DERIVATION_INPUT_SEED,
9799 (const uint8_t *) "", 0));
9800 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
9801 PSA_KEY_DERIVATION_INPUT_SEED,
9802 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02009803 }
9804
Gilles Peskine449bd832023-01-11 14:50:10 +01009805 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9806 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009807
Gilles Peskine449bd832023-01-11 14:50:10 +01009808 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9809 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009810
Gilles Peskine449bd832023-01-11 14:50:10 +01009811 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9812 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009813
Gilles Peskine449bd832023-01-11 14:50:10 +01009814 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
9815 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9816 PSA_ERROR_BAD_STATE);
9817 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9818 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009819 goto exit;
9820 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009821
Neil Armstrongf983caf2022-06-15 15:27:48 +02009822 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009823 ecjpake_do_round(alg, primitive_arg, &server, &client,
9824 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009825
Gilles Peskine449bd832023-01-11 14:50:10 +01009826 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
9827 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9828 PSA_ERROR_BAD_STATE);
9829 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9830 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009831 goto exit;
9832 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009833
Neil Armstrongf983caf2022-06-15 15:27:48 +02009834 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 ecjpake_do_round(alg, primitive_arg, &server, &client,
9836 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009837
Gilles Peskine449bd832023-01-11 14:50:10 +01009838 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
9839 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009840
9841exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009842 psa_key_derivation_abort(&server_derive);
9843 psa_key_derivation_abort(&client_derive);
9844 psa_destroy_key(key);
9845 psa_pake_abort(&server);
9846 psa_pake_abort(&client);
9847 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009848}
9849/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009850
9851/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009852void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009853{
9854 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9855 const size_t bits = 256;
9856 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01009857 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009858 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01009859 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009860
9861 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9862 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009863 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9864 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
9865 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9866 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009867 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01009868 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9869 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009870
9871 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01009872 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9873 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
9874 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9875 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
9876 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9877 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009878
9879 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01009880 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9881 PSA_PAKE_OUTPUT_MAX_SIZE);
9882 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9883 PSA_PAKE_OUTPUT_MAX_SIZE);
9884 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9885 PSA_PAKE_OUTPUT_MAX_SIZE);
9886 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9887 PSA_PAKE_INPUT_MAX_SIZE);
9888 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9889 PSA_PAKE_INPUT_MAX_SIZE);
9890 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9891 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009892}
9893/* END_CASE */