blob: d5141790d79818ce1ddb7d5e89914c5373b5af9f [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
Gilles Peskinee59236f2018-01-27 23:32:46 +01001223/* END_HEADER */
1224
1225/* BEGIN_DEPENDENCIES
1226 * depends_on:MBEDTLS_PSA_CRYPTO_C
1227 * END_DEPENDENCIES
1228 */
1229
1230/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001232{
1233 size_t max_truncated_mac_size =
1234 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1235
1236 /* Check that the length for a truncated MAC always fits in the algorithm
1237 * encoding. The shifted mask is the maximum truncated value. The
1238 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244void import_with_policy(int type_arg,
1245 int usage_arg, int alg_arg,
1246 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001247{
1248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1249 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001251 psa_key_type_t type = type_arg;
1252 psa_key_usage_t usage = usage_arg;
1253 psa_algorithm_t alg = alg_arg;
1254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001256 psa_status_t status;
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 psa_set_key_type(&attributes, type);
1261 psa_set_key_usage_flags(&attributes, usage);
1262 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 status = psa_import_key(&attributes,
1265 key_material, sizeof(key_material),
1266 &key);
1267 TEST_EQUAL(status, expected_status);
1268 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1273 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1274 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1275 mbedtls_test_update_key_usage_flags(usage));
1276 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1277 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 PSA_ASSERT(psa_destroy_key(key));
1280 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001281
1282exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001283 /*
1284 * Key attributes may have been returned by psa_get_key_attributes()
1285 * thus reset them as required.
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 psa_destroy_key(key);
1290 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291}
1292/* END_CASE */
1293
1294/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295void import_with_data(data_t *data, int type_arg,
1296 int attr_bits_arg,
1297 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001298{
1299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1300 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001302 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001303 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001304 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 psa_set_key_type(&attributes, type);
1310 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 status = psa_import_key(&attributes, data->x, data->len, &key);
1313 TEST_EQUAL(status, expected_status);
1314 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1319 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1320 if (attr_bits != 0) {
1321 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1322 }
1323 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 PSA_ASSERT(psa_destroy_key(key));
1326 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327
1328exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001329 /*
1330 * Key attributes may have been returned by psa_get_key_attributes()
1331 * thus reset them as required.
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 psa_destroy_key(key);
1336 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337}
1338/* END_CASE */
1339
1340/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001341/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342void import_large_key(int type_arg, int byte_size_arg,
1343 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001344{
1345 psa_key_type_t type = type_arg;
1346 size_t byte_size = byte_size_arg;
1347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1348 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001350 psa_status_t status;
1351 uint8_t *buffer = NULL;
1352 size_t buffer_size = byte_size + 1;
1353 size_t n;
1354
Steven Cooreman69967ce2021-01-18 18:01:08 +01001355 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001356 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1358 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001361
1362 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1364 psa_set_key_type(&attributes, type);
1365 status = psa_import_key(&attributes, buffer, byte_size, &key);
1366 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1367 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (status == PSA_SUCCESS) {
1370 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1371 TEST_EQUAL(psa_get_key_type(&attributes), type);
1372 TEST_EQUAL(psa_get_key_bits(&attributes),
1373 PSA_BYTES_TO_BITS(byte_size));
1374 ASSERT_NO_SLOT_NUMBER(&attributes);
1375 memset(buffer, 0, byte_size + 1);
1376 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1377 for (n = 0; n < byte_size; n++) {
1378 TEST_EQUAL(buffer[n], 'K');
1379 }
1380 for (n = byte_size; n < buffer_size; n++) {
1381 TEST_EQUAL(buffer[n], 0);
1382 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001383 }
1384
1385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001386 /*
1387 * Key attributes may have been returned by psa_get_key_attributes()
1388 * thus reset them as required.
1389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 psa_destroy_key(key);
1393 PSA_DONE();
1394 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001395}
1396/* END_CASE */
1397
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001398/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001399/* Import an RSA key with a valid structure (but not valid numbers
1400 * inside, beyond having sensible size and parity). This is expected to
1401 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001403{
Ronald Cron5425a212020-08-04 14:58:35 +02001404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001405 size_t bits = bits_arg;
1406 psa_status_t expected_status = expected_status_arg;
1407 psa_status_t status;
1408 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001409 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001410 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001412 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001413 unsigned char *p;
1414 int ret;
1415 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 PSA_ASSERT(psa_crypto_init());
1419 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1422 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001423 length = ret;
1424
1425 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 psa_set_key_type(&attributes, type);
1427 status = psa_import_key(&attributes, p, length, &key);
1428 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (status == PSA_SUCCESS) {
1431 PSA_ASSERT(psa_destroy_key(key));
1432 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001433
1434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_free(buffer);
1436 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001437}
1438/* END_CASE */
1439
1440/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441void import_export(data_t *data,
1442 int type_arg,
1443 int usage_arg, int alg_arg,
1444 int lifetime_arg,
1445 int expected_bits,
1446 int export_size_delta,
1447 int expected_export_status_arg,
1448 /*whether reexport must give the original input exactly*/
1449 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001450{
Ronald Cron5425a212020-08-04 14:58:35 +02001451 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001452 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001453 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001454 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301456 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001457 unsigned char *exported = NULL;
1458 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001459 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001460 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001461 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001463 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464
Moran Pekercb088e72018-07-17 17:36:59 +03001465 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 ASSERT_ALLOC(exported, export_size);
1467 if (!canonical_input) {
1468 ASSERT_ALLOC(reexported, export_size);
1469 }
1470 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 psa_set_key_lifetime(&attributes, lifetime);
1473 psa_set_key_usage_flags(&attributes, usage_arg);
1474 psa_set_key_algorithm(&attributes, alg);
1475 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001476
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479
1480 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1482 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1483 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1484 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001485
1486 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 status = psa_export_key(key, exported, export_size, &exported_length);
1488 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001489
1490 /* The exported length must be set by psa_export_key() to a value between 0
1491 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1493 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1494 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1497 export_size - exported_length));
1498 if (status != PSA_SUCCESS) {
1499 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001501 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502
Gilles Peskineea38a922021-02-13 00:05:16 +01001503 /* Run sanity checks on the exported key. For non-canonical inputs,
1504 * this validates the canonical representations. For canonical inputs,
1505 * this doesn't directly validate the implementation, but it still helps
1506 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (!psa_key_lifetime_is_external(lifetime)) {
1508 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 }
Archana4d7ae1d2021-07-07 02:50:22 +05301511 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (canonical_input) {
1514 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1515 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001516 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1518 &key2));
1519 PSA_ASSERT(psa_export_key(key2,
1520 reexported,
1521 export_size,
1522 &reexported_length));
1523 ASSERT_COMPARE(exported, exported_length,
1524 reexported, reexported_length);
1525 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 TEST_LE_U(exported_length,
1528 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1529 psa_get_key_bits(&got_attributes)));
1530 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531
1532destroy:
1533 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 PSA_ASSERT(psa_destroy_key(key));
1535 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536
1537exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001538 /*
1539 * Key attributes may have been returned by psa_get_key_attributes()
1540 * thus reset them as required.
1541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_reset_key_attributes(&got_attributes);
1543 psa_destroy_key(key);
1544 mbedtls_free(exported);
1545 mbedtls_free(reexported);
1546 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547}
1548/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001549
Moran Pekerf709f4a2018-06-06 17:26:04 +03001550/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551void import_export_public_key(data_t *data,
1552 int type_arg, // key pair or public key
1553 int alg_arg,
1554 int lifetime_arg,
1555 int export_size_delta,
1556 int expected_export_status_arg,
1557 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001558{
Ronald Cron5425a212020-08-04 14:58:35 +02001559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001560 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001561 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001562 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001563 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301564 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001565 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001566 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001567 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 psa_set_key_lifetime(&attributes, lifetime);
1573 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1574 psa_set_key_algorithm(&attributes, alg);
1575 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001576
1577 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001579
Gilles Peskine49c25912018-10-29 15:15:31 +01001580 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 ASSERT_ALLOC(exported, export_size);
1582 status = psa_export_public_key(key,
1583 exported, export_size,
1584 &exported_length);
1585 TEST_EQUAL(status, expected_export_status);
1586 if (status == PSA_SUCCESS) {
1587 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001588 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1590 bits = psa_get_key_bits(&attributes);
1591 TEST_LE_U(expected_public_key->len,
1592 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1593 TEST_LE_U(expected_public_key->len,
1594 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1595 TEST_LE_U(expected_public_key->len,
1596 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1597 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1598 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001599 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001600exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001601 /*
1602 * Key attributes may have been returned by psa_get_key_attributes()
1603 * thus reset them as required.
1604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 mbedtls_free(exported);
1608 psa_destroy_key(key);
1609 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001610}
1611/* END_CASE */
1612
Gilles Peskine20035e32018-02-03 22:44:14 +01001613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void import_and_exercise_key(data_t *data,
1615 int type_arg,
1616 int bits_arg,
1617 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001618{
Ronald Cron5425a212020-08-04 14:58:35 +02001619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001620 psa_key_type_t type = type_arg;
1621 size_t bits = bits_arg;
1622 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001625 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 psa_set_key_usage_flags(&attributes, usage);
1630 psa_set_key_algorithm(&attributes, alg);
1631 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001632
1633 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001635
1636 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1638 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1639 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001640
1641 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 PSA_ASSERT(psa_destroy_key(key));
1647 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001648
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001650 /*
1651 * Key attributes may have been returned by psa_get_key_attributes()
1652 * thus reset them as required.
1653 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 psa_reset_key_attributes(&attributes);
1657 psa_destroy_key(key);
1658 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001659}
1660/* END_CASE */
1661
1662/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void effective_key_attributes(int type_arg, int expected_type_arg,
1664 int bits_arg, int expected_bits_arg,
1665 int usage_arg, int expected_usage_arg,
1666 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001667{
Ronald Cron5425a212020-08-04 14:58:35 +02001668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001669 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001670 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001671 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001672 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001673 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001674 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001675 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001676 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 psa_set_key_usage_flags(&attributes, usage);
1682 psa_set_key_algorithm(&attributes, alg);
1683 psa_set_key_type(&attributes, key_type);
1684 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 PSA_ASSERT(psa_generate_key(&attributes, &key));
1687 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1690 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1691 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1692 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1693 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001694
1695exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001696 /*
1697 * Key attributes may have been returned by psa_get_key_attributes()
1698 * thus reset them as required.
1699 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 psa_destroy_key(key);
1703 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001704}
1705/* END_CASE */
1706
1707/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708void check_key_policy(int type_arg, int bits_arg,
1709 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001710{
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1712 usage_arg,
1713 mbedtls_test_update_key_usage_flags(usage_arg),
1714 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001715 goto exit;
1716}
1717/* END_CASE */
1718
1719/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001721{
1722 /* Test each valid way of initializing the object, except for `= {0}`, as
1723 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1724 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001725 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001727 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1728 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1733 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1734 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 TEST_EQUAL(psa_get_key_type(&func), 0);
1737 TEST_EQUAL(psa_get_key_type(&init), 0);
1738 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 TEST_EQUAL(psa_get_key_bits(&func), 0);
1741 TEST_EQUAL(psa_get_key_bits(&init), 0);
1742 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1745 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1746 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1749 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1750 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001751}
1752/* END_CASE */
1753
1754/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755void mac_key_policy(int policy_usage_arg,
1756 int policy_alg_arg,
1757 int key_type_arg,
1758 data_t *key_data,
1759 int exercise_alg_arg,
1760 int expected_status_sign_arg,
1761 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001762{
Ronald Cron5425a212020-08-04 14:58:35 +02001763 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001765 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001766 psa_key_type_t key_type = key_type_arg;
1767 psa_algorithm_t policy_alg = policy_alg_arg;
1768 psa_algorithm_t exercise_alg = exercise_alg_arg;
1769 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001771 psa_status_t expected_status_sign = expected_status_sign_arg;
1772 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001773 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 psa_set_key_usage_flags(&attributes, policy_usage);
1778 psa_set_key_algorithm(&attributes, policy_alg);
1779 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1782 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1785 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1788 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001789
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001790 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001792 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1794 input, 128,
1795 mac, PSA_MAC_MAX_SIZE, &mac_len),
1796 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001797
Neil Armstrong3af9b972022-02-07 12:20:21 +01001798 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 PSA_ASSERT(psa_mac_abort(&operation));
1800 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1801 if (status == PSA_SUCCESS) {
1802 status = psa_mac_update(&operation, input, 128);
1803 if (status == PSA_SUCCESS) {
1804 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1805 &mac_len),
1806 expected_status_sign);
1807 } else {
1808 TEST_EQUAL(status, expected_status_sign);
1809 }
1810 } else {
1811 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001814
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001815 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 status = psa_mac_verify(key, exercise_alg, input, 128,
1817 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1820 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1821 } else {
1822 TEST_EQUAL(status, expected_status_verify);
1823 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001824
Neil Armstrong3af9b972022-02-07 12:20:21 +01001825 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1827 if (status == PSA_SUCCESS) {
1828 status = psa_mac_update(&operation, input, 128);
1829 if (status == PSA_SUCCESS) {
1830 status = psa_mac_verify_finish(&operation, mac, mac_len);
1831 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1832 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1833 } else {
1834 TEST_EQUAL(status, expected_status_verify);
1835 }
1836 } else {
1837 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001838 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 } else {
1840 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001841 }
1842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 memset(mac, 0, sizeof(mac));
1846 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1847 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001848
1849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 psa_mac_abort(&operation);
1851 psa_destroy_key(key);
1852 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001853}
1854/* END_CASE */
1855
1856/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857void cipher_key_policy(int policy_usage_arg,
1858 int policy_alg,
1859 int key_type,
1860 data_t *key_data,
1861 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862{
Ronald Cron5425a212020-08-04 14:58:35 +02001863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001865 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001866 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001867 size_t output_buffer_size = 0;
1868 size_t input_buffer_size = 0;
1869 size_t output_length = 0;
1870 uint8_t *output = NULL;
1871 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 psa_status_t status;
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1875 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1876 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 ASSERT_ALLOC(input, input_buffer_size);
1879 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 psa_set_key_usage_flags(&attributes, policy_usage);
1884 psa_set_key_algorithm(&attributes, policy_alg);
1885 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1888 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001890 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 TEST_EQUAL(policy_usage,
1892 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001893
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001894 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1896 output, output_buffer_size,
1897 &output_length);
1898 if (policy_alg == exercise_alg &&
1899 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1900 PSA_ASSERT(status);
1901 } else {
1902 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1903 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001904
1905 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1907 if (policy_alg == exercise_alg &&
1908 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1909 PSA_ASSERT(status);
1910 } else {
1911 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1912 }
1913 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001915 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1917 input, input_buffer_size,
1918 &output_length);
1919 if (policy_alg == exercise_alg &&
1920 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1921 PSA_ASSERT(status);
1922 } else {
1923 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1924 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001925
1926 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1928 if (policy_alg == exercise_alg &&
1929 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1930 PSA_ASSERT(status);
1931 } else {
1932 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1933 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
1935exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 psa_cipher_abort(&operation);
1937 mbedtls_free(input);
1938 mbedtls_free(output);
1939 psa_destroy_key(key);
1940 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945void aead_key_policy(int policy_usage_arg,
1946 int policy_alg,
1947 int key_type,
1948 data_t *key_data,
1949 int nonce_length_arg,
1950 int tag_length_arg,
1951 int exercise_alg,
1952 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953{
Ronald Cron5425a212020-08-04 14:58:35 +02001954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001956 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001957 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001959 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961 size_t nonce_length = nonce_length_arg;
1962 unsigned char tag[16];
1963 size_t tag_length = tag_length_arg;
1964 size_t output_length;
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 TEST_LE_U(nonce_length, sizeof(nonce));
1967 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 psa_set_key_usage_flags(&attributes, policy_usage);
1972 psa_set_key_algorithm(&attributes, policy_alg);
1973 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1976 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001978 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 TEST_EQUAL(policy_usage,
1980 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001981
Neil Armstrong752d8112022-02-07 14:51:11 +01001982 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_aead_encrypt(key, exercise_alg,
1984 nonce, nonce_length,
1985 NULL, 0,
1986 NULL, 0,
1987 tag, tag_length,
1988 &output_length);
1989 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1990 TEST_EQUAL(status, expected_status);
1991 } else {
1992 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1993 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994
Neil Armstrong752d8112022-02-07 14:51:11 +01001995 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
1997 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1998 TEST_EQUAL(status, expected_status);
1999 } else {
2000 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2001 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002002
2003 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 memset(tag, 0, sizeof(tag));
2005 status = psa_aead_decrypt(key, exercise_alg,
2006 nonce, nonce_length,
2007 NULL, 0,
2008 tag, tag_length,
2009 NULL, 0,
2010 &output_length);
2011 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2012 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2013 } else if (expected_status == PSA_SUCCESS) {
2014 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2015 } else {
2016 TEST_EQUAL(status, expected_status);
2017 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018
Neil Armstrong752d8112022-02-07 14:51:11 +01002019 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 PSA_ASSERT(psa_aead_abort(&operation));
2021 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2022 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2023 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2024 } else {
2025 TEST_EQUAL(status, expected_status);
2026 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002027
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 PSA_ASSERT(psa_aead_abort(&operation));
2030 psa_destroy_key(key);
2031 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032}
2033/* END_CASE */
2034
2035/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036void asymmetric_encryption_key_policy(int policy_usage_arg,
2037 int policy_alg,
2038 int key_type,
2039 data_t *key_data,
2040 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041{
Ronald Cron5425a212020-08-04 14:58:35 +02002042 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002044 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045 psa_status_t status;
2046 size_t key_bits;
2047 size_t buffer_length;
2048 unsigned char *buffer = NULL;
2049 size_t output_length;
2050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 psa_set_key_usage_flags(&attributes, policy_usage);
2054 psa_set_key_algorithm(&attributes, policy_alg);
2055 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2058 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002060 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 TEST_EQUAL(policy_usage,
2062 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2065 key_bits = psa_get_key_bits(&attributes);
2066 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2067 exercise_alg);
2068 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_asymmetric_encrypt(key, exercise_alg,
2071 NULL, 0,
2072 NULL, 0,
2073 buffer, buffer_length,
2074 &output_length);
2075 if (policy_alg == exercise_alg &&
2076 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2077 PSA_ASSERT(status);
2078 } else {
2079 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2080 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (buffer_length != 0) {
2083 memset(buffer, 0, buffer_length);
2084 }
2085 status = psa_asymmetric_decrypt(key, exercise_alg,
2086 buffer, buffer_length,
2087 NULL, 0,
2088 buffer, buffer_length,
2089 &output_length);
2090 if (policy_alg == exercise_alg &&
2091 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2092 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2093 } else {
2094 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2095 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096
2097exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002098 /*
2099 * Key attributes may have been returned by psa_get_key_attributes()
2100 * thus reset them as required.
2101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 psa_destroy_key(key);
2105 PSA_DONE();
2106 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107}
2108/* END_CASE */
2109
2110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002111void asymmetric_signature_key_policy(int policy_usage_arg,
2112 int policy_alg,
2113 int key_type,
2114 data_t *key_data,
2115 int exercise_alg,
2116 int payload_length_arg,
2117 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118{
Ronald Cron5425a212020-08-04 14:58:35 +02002119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002121 psa_key_usage_t policy_usage = policy_usage_arg;
2122 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002125 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2126 * compatible with the policy and `payload_length_arg` is supposed to be
2127 * a valid input length to sign. If `payload_length_arg <= 0`,
2128 * `exercise_alg` is supposed to be forbidden by the policy. */
2129 int compatible_alg = payload_length_arg > 0;
2130 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 size_t signature_length;
2133
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002134 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002135 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 TEST_EQUAL(expected_usage,
2137 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 psa_set_key_usage_flags(&attributes, policy_usage);
2142 psa_set_key_algorithm(&attributes, policy_alg);
2143 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2146 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 status = psa_sign_hash(key, exercise_alg,
2151 payload, payload_length,
2152 signature, sizeof(signature),
2153 &signature_length);
2154 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2155 PSA_ASSERT(status);
2156 } else {
2157 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2158 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 memset(signature, 0, sizeof(signature));
2161 status = psa_verify_hash(key, exercise_alg,
2162 payload, payload_length,
2163 signature, sizeof(signature));
2164 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2165 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2166 } else {
2167 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2168 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2171 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2172 status = psa_sign_message(key, exercise_alg,
2173 payload, payload_length,
2174 signature, sizeof(signature),
2175 &signature_length);
2176 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2177 PSA_ASSERT(status);
2178 } else {
2179 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2180 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 memset(signature, 0, sizeof(signature));
2183 status = psa_verify_message(key, exercise_alg,
2184 payload, payload_length,
2185 signature, sizeof(signature));
2186 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2187 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2188 } else {
2189 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2190 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002191 }
2192
Gilles Peskined5b33222018-06-18 22:20:03 +02002193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 psa_destroy_key(key);
2195 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002196}
2197/* END_CASE */
2198
Janos Follathba3fab92019-06-11 14:50:16 +01002199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200void derive_key_policy(int policy_usage,
2201 int policy_alg,
2202 int key_type,
2203 data_t *key_data,
2204 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002205{
Ronald Cron5425a212020-08-04 14:58:35 +02002206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002208 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002209 psa_status_t status;
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 psa_set_key_usage_flags(&attributes, policy_usage);
2214 psa_set_key_algorithm(&attributes, policy_alg);
2215 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2218 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2223 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2224 PSA_ASSERT(psa_key_derivation_input_bytes(
2225 &operation,
2226 PSA_KEY_DERIVATION_INPUT_SEED,
2227 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002228 }
Janos Follathba3fab92019-06-11 14:50:16 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_key_derivation_input_key(&operation,
2231 PSA_KEY_DERIVATION_INPUT_SECRET,
2232 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (policy_alg == exercise_alg &&
2235 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2236 PSA_ASSERT(status);
2237 } else {
2238 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2239 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002240
2241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 psa_key_derivation_abort(&operation);
2243 psa_destroy_key(key);
2244 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245}
2246/* END_CASE */
2247
2248/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249void agreement_key_policy(int policy_usage,
2250 int policy_alg,
2251 int key_type_arg,
2252 data_t *key_data,
2253 int exercise_alg,
2254 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002255{
Ronald Cron5425a212020-08-04 14:58:35 +02002256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002258 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002260 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002261 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 psa_set_key_usage_flags(&attributes, policy_usage);
2266 psa_set_key_algorithm(&attributes, policy_alg);
2267 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2270 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2273 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002276
2277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 psa_key_derivation_abort(&operation);
2279 psa_destroy_key(key);
2280 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002285void key_policy_alg2(int key_type_arg, data_t *key_data,
2286 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002287{
Ronald Cron5425a212020-08-04 14:58:35 +02002288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002289 psa_key_type_t key_type = key_type_arg;
2290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2291 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2292 psa_key_usage_t usage = usage_arg;
2293 psa_algorithm_t alg = alg_arg;
2294 psa_algorithm_t alg2 = alg2_arg;
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 psa_set_key_usage_flags(&attributes, usage);
2299 psa_set_key_algorithm(&attributes, alg);
2300 psa_set_key_enrollment_algorithm(&attributes, alg2);
2301 psa_set_key_type(&attributes, key_type);
2302 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2303 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002304
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002305 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 usage = mbedtls_test_update_key_usage_flags(usage);
2307 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2308 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2309 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2310 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
2315 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002318
2319exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002320 /*
2321 * Key attributes may have been returned by psa_get_key_attributes()
2322 * thus reset them as required.
2323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 psa_destroy_key(key);
2327 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002328}
2329/* END_CASE */
2330
2331/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332void raw_agreement_key_policy(int policy_usage,
2333 int policy_alg,
2334 int key_type_arg,
2335 data_t *key_data,
2336 int exercise_alg,
2337 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002341 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002342 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002343 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002344 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 psa_set_key_usage_flags(&attributes, policy_usage);
2349 psa_set_key_algorithm(&attributes, policy_alg);
2350 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2353 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002358
2359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 psa_key_derivation_abort(&operation);
2361 psa_destroy_key(key);
2362 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363}
2364/* END_CASE */
2365
2366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367void copy_success(int source_usage_arg,
2368 int source_alg_arg, int source_alg2_arg,
2369 unsigned int source_lifetime_arg,
2370 int type_arg, data_t *material,
2371 int copy_attributes,
2372 int target_usage_arg,
2373 int target_alg_arg, int target_alg2_arg,
2374 unsigned int target_lifetime_arg,
2375 int expected_usage_arg,
2376 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002377{
Gilles Peskineca25db92019-04-19 11:43:08 +02002378 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2379 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002380 psa_key_usage_t expected_usage = expected_usage_arg;
2381 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002382 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302383 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2384 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002385 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2386 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002387 uint8_t *export_buffer = NULL;
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002390
Gilles Peskineca25db92019-04-19 11:43:08 +02002391 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2393 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2394 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2395 psa_set_key_type(&source_attributes, type_arg);
2396 psa_set_key_lifetime(&source_attributes, source_lifetime);
2397 PSA_ASSERT(psa_import_key(&source_attributes,
2398 material->x, material->len,
2399 &source_key));
2400 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002401
Gilles Peskineca25db92019-04-19 11:43:08 +02002402 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002404 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (target_usage_arg != -1) {
2409 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2410 }
2411 if (target_alg_arg != -1) {
2412 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2413 }
2414 if (target_alg2_arg != -1) {
2415 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2416 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002417
Archana8a180362021-07-05 02:18:48 +05302418
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 PSA_ASSERT(psa_copy_key(source_key,
2421 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002422
2423 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002425
2426 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2428 TEST_EQUAL(psa_get_key_type(&source_attributes),
2429 psa_get_key_type(&target_attributes));
2430 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2431 psa_get_key_bits(&target_attributes));
2432 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2433 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2434 TEST_EQUAL(expected_alg2,
2435 psa_get_key_enrollment_algorithm(&target_attributes));
2436 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002437 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 ASSERT_ALLOC(export_buffer, material->len);
2439 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2440 material->len, &length));
2441 ASSERT_COMPARE(material->x, material->len,
2442 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (!psa_key_lifetime_is_external(target_lifetime)) {
2446 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302447 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 }
2449 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 }
Archana8a180362021-07-05 02:18:48 +05302452 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002455
2456exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002457 /*
2458 * Source and target key attributes may have been returned by
2459 * psa_get_key_attributes() thus reset them as required.
2460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 psa_reset_key_attributes(&source_attributes);
2462 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 PSA_DONE();
2465 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002466}
2467/* END_CASE */
2468
2469/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470void copy_fail(int source_usage_arg,
2471 int source_alg_arg, int source_alg2_arg,
2472 int source_lifetime_arg,
2473 int type_arg, data_t *material,
2474 int target_type_arg, int target_bits_arg,
2475 int target_usage_arg,
2476 int target_alg_arg, int target_alg2_arg,
2477 int target_id_arg, int target_lifetime_arg,
2478 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002479{
2480 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2481 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002482 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2483 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002487
2488 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2490 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2491 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2492 psa_set_key_type(&source_attributes, type_arg);
2493 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2494 PSA_ASSERT(psa_import_key(&source_attributes,
2495 material->x, material->len,
2496 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002497
2498 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 psa_set_key_id(&target_attributes, key_id);
2500 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2501 psa_set_key_type(&target_attributes, target_type_arg);
2502 psa_set_key_bits(&target_attributes, target_bits_arg);
2503 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2504 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2505 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002506
2507 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 TEST_EQUAL(psa_copy_key(source_key,
2509 &target_attributes, &target_key),
2510 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002513
Gilles Peskine4a644642019-05-03 17:14:08 +02002514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_reset_key_attributes(&source_attributes);
2516 psa_reset_key_attributes(&target_attributes);
2517 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002518}
2519/* END_CASE */
2520
2521/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002523{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002524 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002525 /* Test each valid way of initializing the object, except for `= {0}`, as
2526 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2527 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002528 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002530 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2531 psa_hash_operation_t zero;
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002534
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002535 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2537 PSA_ERROR_BAD_STATE);
2538 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2539 PSA_ERROR_BAD_STATE);
2540 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2541 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002542
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002543 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 PSA_ASSERT(psa_hash_abort(&func));
2545 PSA_ASSERT(psa_hash_abort(&init));
2546 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551void hash_setup(int alg_arg,
2552 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553{
2554 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002555 uint8_t *output = NULL;
2556 size_t output_size = 0;
2557 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002558 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002559 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560 psa_status_t status;
2561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563
Neil Armstrongedb20862022-02-07 15:47:44 +01002564 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 output_size = PSA_HASH_LENGTH(alg);
2566 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 status = psa_hash_compute(alg, NULL, 0,
2569 output, output_size, &output_length);
2570 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002571
2572 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_hash_setup(&operation, alg);
2574 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002575
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002576 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002578
2579 /* If setup failed, reproduce the failure, so as to
2580 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (status != PSA_SUCCESS) {
2582 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2583 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002584
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002585 /* Now the operation object should be reusable. */
2586#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2588 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002589#endif
2590
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002591exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 mbedtls_free(output);
2593 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002594}
2595/* END_CASE */
2596
2597/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598void hash_compute_fail(int alg_arg, data_t *input,
2599 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002600{
2601 psa_algorithm_t alg = alg_arg;
2602 uint8_t *output = NULL;
2603 size_t output_size = output_size_arg;
2604 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002605 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002606 psa_status_t expected_status = expected_status_arg;
2607 psa_status_t status;
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002612
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002613 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 status = psa_hash_compute(alg, input->x, input->len,
2615 output, output_size, &output_length);
2616 TEST_EQUAL(status, expected_status);
2617 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002618
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002619 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 status = psa_hash_setup(&operation, alg);
2621 if (status == PSA_SUCCESS) {
2622 status = psa_hash_update(&operation, input->x, input->len);
2623 if (status == PSA_SUCCESS) {
2624 status = psa_hash_finish(&operation, output, output_size,
2625 &output_length);
2626 if (status == PSA_SUCCESS) {
2627 TEST_LE_U(output_length, output_size);
2628 } else {
2629 TEST_EQUAL(status, expected_status);
2630 }
2631 } else {
2632 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002633 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 } else {
2635 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002636 }
2637
Gilles Peskine0a749c82019-11-28 19:33:58 +01002638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 PSA_ASSERT(psa_hash_abort(&operation));
2640 mbedtls_free(output);
2641 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002642}
2643/* END_CASE */
2644
2645/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646void hash_compare_fail(int alg_arg, data_t *input,
2647 data_t *reference_hash,
2648 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002649{
2650 psa_algorithm_t alg = alg_arg;
2651 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002652 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002653 psa_status_t status;
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002656
Neil Armstrong55a1be12022-02-07 11:23:20 +01002657 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 status = psa_hash_compare(alg, input->x, input->len,
2659 reference_hash->x, reference_hash->len);
2660 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002661
Neil Armstrong55a1be12022-02-07 11:23:20 +01002662 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_hash_setup(&operation, alg);
2664 if (status == PSA_SUCCESS) {
2665 status = psa_hash_update(&operation, input->x, input->len);
2666 if (status == PSA_SUCCESS) {
2667 status = psa_hash_verify(&operation, reference_hash->x,
2668 reference_hash->len);
2669 TEST_EQUAL(status, expected_status);
2670 } else {
2671 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 } else {
2674 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002675 }
2676
Gilles Peskine88e08462020-01-28 20:43:00 +01002677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 PSA_ASSERT(psa_hash_abort(&operation));
2679 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002680}
2681/* END_CASE */
2682
2683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002684void hash_compute_compare(int alg_arg, data_t *input,
2685 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002686{
2687 psa_algorithm_t alg = alg_arg;
2688 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2689 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002690 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002691 size_t i;
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002694
Neil Armstrongca30a002022-02-07 11:40:23 +01002695 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2697 output, PSA_HASH_LENGTH(alg),
2698 &output_length));
2699 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2700 ASSERT_COMPARE(output, output_length,
2701 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002702
Neil Armstrongca30a002022-02-07 11:40:23 +01002703 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 PSA_ASSERT(psa_hash_setup(&operation, alg));
2705 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2706 PSA_ASSERT(psa_hash_finish(&operation, output,
2707 PSA_HASH_LENGTH(alg),
2708 &output_length));
2709 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2710 ASSERT_COMPARE(output, output_length,
2711 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002712
2713 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2715 output, sizeof(output),
2716 &output_length));
2717 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2718 ASSERT_COMPARE(output, output_length,
2719 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002720
Neil Armstrongca30a002022-02-07 11:40:23 +01002721 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 PSA_ASSERT(psa_hash_setup(&operation, alg));
2723 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2724 PSA_ASSERT(psa_hash_finish(&operation, output,
2725 sizeof(output), &output_length));
2726 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2727 ASSERT_COMPARE(output, output_length,
2728 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002729
2730 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2732 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002733
Neil Armstrongca30a002022-02-07 11:40:23 +01002734 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 PSA_ASSERT(psa_hash_setup(&operation, alg));
2736 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2737 PSA_ASSERT(psa_hash_verify(&operation, output,
2738 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002739
2740 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2742 output, output_length + 1),
2743 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002744
Neil Armstrongca30a002022-02-07 11:40:23 +01002745 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 PSA_ASSERT(psa_hash_setup(&operation, alg));
2747 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2748 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2749 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002750
2751 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2753 output, output_length - 1),
2754 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002755
Neil Armstrongca30a002022-02-07 11:40:23 +01002756 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 PSA_ASSERT(psa_hash_setup(&operation, alg));
2758 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2759 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2760 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002761
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 for (i = 0; i < output_length; i++) {
2764 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002766
2767 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2769 output, output_length),
2770 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002771
2772 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 PSA_ASSERT(psa_hash_setup(&operation, alg));
2774 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2775 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2776 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002777
Gilles Peskine0a749c82019-11-28 19:33:58 +01002778 output[i] ^= 1;
2779 }
2780
2781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 PSA_ASSERT(psa_hash_abort(&operation));
2783 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002784}
2785/* END_CASE */
2786
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002787/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002788void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002789{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002790 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002791 unsigned char input[] = "";
2792 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002793 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002794 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2795 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2797 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002798 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002799 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002800 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002803
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002804 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 PSA_ASSERT(psa_hash_setup(&operation, alg));
2806 ASSERT_OPERATION_IS_ACTIVE(operation);
2807 TEST_EQUAL(psa_hash_setup(&operation, alg),
2808 PSA_ERROR_BAD_STATE);
2809 ASSERT_OPERATION_IS_INACTIVE(operation);
2810 PSA_ASSERT(psa_hash_abort(&operation));
2811 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002812
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002813 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2815 PSA_ERROR_BAD_STATE);
2816 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002817
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002818 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002820 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 ASSERT_OPERATION_IS_ACTIVE(operation);
2822 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2823 PSA_ERROR_BAD_STATE);
2824 ASSERT_OPERATION_IS_INACTIVE(operation);
2825 PSA_ASSERT(psa_hash_abort(&operation));
2826 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002827
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002828 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 PSA_ASSERT(psa_hash_setup(&operation, alg));
2830 PSA_ASSERT(psa_hash_finish(&operation,
2831 hash, sizeof(hash), &hash_len));
2832 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2833 PSA_ERROR_BAD_STATE);
2834 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002835
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002836 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 TEST_EQUAL(psa_hash_verify(&operation,
2838 valid_hash, sizeof(valid_hash)),
2839 PSA_ERROR_BAD_STATE);
2840 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002841
2842 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ASSERT(psa_hash_setup(&operation, alg));
2844 PSA_ASSERT(psa_hash_finish(&operation,
2845 hash, sizeof(hash), &hash_len));
2846 TEST_EQUAL(psa_hash_verify(&operation,
2847 valid_hash, sizeof(valid_hash)),
2848 PSA_ERROR_BAD_STATE);
2849 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002850
2851 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 PSA_ASSERT(psa_hash_setup(&operation, alg));
2853 ASSERT_OPERATION_IS_ACTIVE(operation);
2854 PSA_ASSERT(psa_hash_verify(&operation,
2855 valid_hash, sizeof(valid_hash)));
2856 ASSERT_OPERATION_IS_INACTIVE(operation);
2857 TEST_EQUAL(psa_hash_verify(&operation,
2858 valid_hash, sizeof(valid_hash)),
2859 PSA_ERROR_BAD_STATE);
2860 ASSERT_OPERATION_IS_INACTIVE(operation);
2861 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862
2863 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 TEST_EQUAL(psa_hash_finish(&operation,
2865 hash, sizeof(hash), &hash_len),
2866 PSA_ERROR_BAD_STATE);
2867 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002868
2869 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 PSA_ASSERT(psa_hash_setup(&operation, alg));
2871 PSA_ASSERT(psa_hash_finish(&operation,
2872 hash, sizeof(hash), &hash_len));
2873 TEST_EQUAL(psa_hash_finish(&operation,
2874 hash, sizeof(hash), &hash_len),
2875 PSA_ERROR_BAD_STATE);
2876 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002877
2878 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 PSA_ASSERT(psa_hash_setup(&operation, alg));
2880 PSA_ASSERT(psa_hash_verify(&operation,
2881 valid_hash, sizeof(valid_hash)));
2882 TEST_EQUAL(psa_hash_finish(&operation,
2883 hash, sizeof(hash), &hash_len),
2884 PSA_ERROR_BAD_STATE);
2885 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002886
2887exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002889}
2890/* END_CASE */
2891
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002892/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002894{
2895 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002896 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2897 * appended to it */
2898 unsigned char hash[] = {
2899 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2900 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2902 };
2903 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002904 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002907
itayzafrir27e69452018-11-01 14:26:34 +02002908 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 PSA_ASSERT(psa_hash_setup(&operation, alg));
2910 ASSERT_OPERATION_IS_ACTIVE(operation);
2911 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2912 PSA_ERROR_INVALID_SIGNATURE);
2913 ASSERT_OPERATION_IS_INACTIVE(operation);
2914 PSA_ASSERT(psa_hash_abort(&operation));
2915 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002916
itayzafrir27e69452018-11-01 14:26:34 +02002917 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 PSA_ASSERT(psa_hash_setup(&operation, alg));
2919 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2920 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002921
itayzafrir27e69452018-11-01 14:26:34 +02002922 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 PSA_ASSERT(psa_hash_setup(&operation, alg));
2924 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2925 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002926
itayzafrirec93d302018-10-18 18:01:10 +03002927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002929}
2930/* END_CASE */
2931
Ronald Cronee414c72021-03-18 18:50:08 +01002932/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002934{
2935 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002936 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002938 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002939 size_t hash_len;
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002942
itayzafrir58028322018-10-25 10:22:01 +03002943 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 PSA_ASSERT(psa_hash_setup(&operation, alg));
2945 TEST_EQUAL(psa_hash_finish(&operation,
2946 hash, expected_size - 1, &hash_len),
2947 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002948
2949exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002951}
2952/* END_CASE */
2953
Ronald Cronee414c72021-03-18 18:50:08 +01002954/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002955void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002956{
2957 psa_algorithm_t alg = PSA_ALG_SHA_256;
2958 unsigned char hash[PSA_HASH_MAX_SIZE];
2959 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2960 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2961 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2962 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2963 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2964 size_t hash_len;
2965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_crypto_init());
2967 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
2970 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
2971 PSA_ASSERT(psa_hash_finish(&op_finished,
2972 hash, sizeof(hash), &hash_len));
2973 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
2974 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
2977 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
2980 PSA_ASSERT(psa_hash_finish(&op_init,
2981 hash, sizeof(hash), &hash_len));
2982 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
2983 PSA_ASSERT(psa_hash_finish(&op_finished,
2984 hash, sizeof(hash), &hash_len));
2985 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
2986 PSA_ASSERT(psa_hash_finish(&op_aborted,
2987 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002988
2989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 psa_hash_abort(&op_source);
2991 psa_hash_abort(&op_init);
2992 psa_hash_abort(&op_setup);
2993 psa_hash_abort(&op_finished);
2994 psa_hash_abort(&op_aborted);
2995 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002996}
2997/* END_CASE */
2998
Ronald Cronee414c72021-03-18 18:50:08 +01002999/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003001{
3002 psa_algorithm_t alg = PSA_ALG_SHA_256;
3003 unsigned char hash[PSA_HASH_MAX_SIZE];
3004 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3005 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3006 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3007 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3008 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3009 size_t hash_len;
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3014 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3015 PSA_ASSERT(psa_hash_finish(&op_finished,
3016 hash, sizeof(hash), &hash_len));
3017 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3018 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3021 PSA_ASSERT(psa_hash_finish(&op_target,
3022 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3025 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3026 PSA_ERROR_BAD_STATE);
3027 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3028 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003029
3030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 psa_hash_abort(&op_target);
3032 psa_hash_abort(&op_init);
3033 psa_hash_abort(&op_setup);
3034 psa_hash_abort(&op_finished);
3035 psa_hash_abort(&op_aborted);
3036 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037}
3038/* END_CASE */
3039
itayzafrir58028322018-10-25 10:22:01 +03003040/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003041void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003042{
Jaeden Amero252ef282019-02-15 14:05:35 +00003043 const uint8_t input[1] = { 0 };
3044
Jaeden Amero769ce272019-01-04 11:48:03 +00003045 /* Test each valid way of initializing the object, except for `= {0}`, as
3046 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3047 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003048 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003050 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3051 psa_mac_operation_t zero;
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003054
Jaeden Amero252ef282019-02-15 14:05:35 +00003055 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 TEST_EQUAL(psa_mac_update(&func,
3057 input, sizeof(input)),
3058 PSA_ERROR_BAD_STATE);
3059 TEST_EQUAL(psa_mac_update(&init,
3060 input, sizeof(input)),
3061 PSA_ERROR_BAD_STATE);
3062 TEST_EQUAL(psa_mac_update(&zero,
3063 input, sizeof(input)),
3064 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003065
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003066 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ASSERT(psa_mac_abort(&func));
3068 PSA_ASSERT(psa_mac_abort(&init));
3069 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003070}
3071/* END_CASE */
3072
3073/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074void mac_setup(int key_type_arg,
3075 data_t *key,
3076 int alg_arg,
3077 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003078{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003079 psa_key_type_t key_type = key_type_arg;
3080 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003081 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003082 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003083 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3084#if defined(KNOWN_SUPPORTED_MAC_ALG)
3085 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3086#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3091 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 }
3094 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003095
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003096 /* The operation object should be reusable. */
3097#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3099 smoke_test_key_data,
3100 sizeof(smoke_test_key_data),
3101 KNOWN_SUPPORTED_MAC_ALG,
3102 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 }
3105 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003106#endif
3107
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003110}
3111/* END_CASE */
3112
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003113/* 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 +01003114void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003115{
Ronald Cron5425a212020-08-04 14:58:35 +02003116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003117 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3118 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003119 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003120 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3121 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3123 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3126 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3127 size_t sign_mac_length = 0;
3128 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3129 const uint8_t verify_mac[] = {
3130 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3131 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3133 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 PSA_ASSERT(psa_crypto_init());
3136 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3137 psa_set_key_algorithm(&attributes, alg);
3138 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3141 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003142
Jaeden Amero252ef282019-02-15 14:05:35 +00003143 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3145 PSA_ERROR_BAD_STATE);
3146 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003147
3148 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3150 &sign_mac_length),
3151 PSA_ERROR_BAD_STATE);
3152 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003153
3154 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 TEST_EQUAL(psa_mac_verify_finish(&operation,
3156 verify_mac, sizeof(verify_mac)),
3157 PSA_ERROR_BAD_STATE);
3158 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003159
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003160 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3162 ASSERT_OPERATION_IS_ACTIVE(operation);
3163 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3164 PSA_ERROR_BAD_STATE);
3165 ASSERT_OPERATION_IS_INACTIVE(operation);
3166 PSA_ASSERT(psa_mac_abort(&operation));
3167 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003168
Jaeden Amero252ef282019-02-15 14:05:35 +00003169 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3171 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3172 PSA_ASSERT(psa_mac_sign_finish(&operation,
3173 sign_mac, sizeof(sign_mac),
3174 &sign_mac_length));
3175 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3176 PSA_ERROR_BAD_STATE);
3177 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003178
3179 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3181 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3182 PSA_ASSERT(psa_mac_verify_finish(&operation,
3183 verify_mac, sizeof(verify_mac)));
3184 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3185 PSA_ERROR_BAD_STATE);
3186 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003187
3188 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3190 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3191 PSA_ASSERT(psa_mac_sign_finish(&operation,
3192 sign_mac, sizeof(sign_mac),
3193 &sign_mac_length));
3194 TEST_EQUAL(psa_mac_sign_finish(&operation,
3195 sign_mac, sizeof(sign_mac),
3196 &sign_mac_length),
3197 PSA_ERROR_BAD_STATE);
3198 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003199
3200 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3202 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3203 PSA_ASSERT(psa_mac_verify_finish(&operation,
3204 verify_mac, sizeof(verify_mac)));
3205 TEST_EQUAL(psa_mac_verify_finish(&operation,
3206 verify_mac, sizeof(verify_mac)),
3207 PSA_ERROR_BAD_STATE);
3208 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003209
3210 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3212 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3213 ASSERT_OPERATION_IS_ACTIVE(operation);
3214 TEST_EQUAL(psa_mac_verify_finish(&operation,
3215 verify_mac, sizeof(verify_mac)),
3216 PSA_ERROR_BAD_STATE);
3217 ASSERT_OPERATION_IS_INACTIVE(operation);
3218 PSA_ASSERT(psa_mac_abort(&operation));
3219 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003220
3221 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3223 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3224 ASSERT_OPERATION_IS_ACTIVE(operation);
3225 TEST_EQUAL(psa_mac_sign_finish(&operation,
3226 sign_mac, sizeof(sign_mac),
3227 &sign_mac_length),
3228 PSA_ERROR_BAD_STATE);
3229 ASSERT_OPERATION_IS_INACTIVE(operation);
3230 PSA_ASSERT(psa_mac_abort(&operation));
3231 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003234
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003237}
3238/* END_CASE */
3239
3240/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241void mac_sign_verify_multi(int key_type_arg,
3242 data_t *key_data,
3243 int alg_arg,
3244 data_t *input,
3245 int is_verify,
3246 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003247{
3248 size_t data_part_len = 0;
3249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003251 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (mac_multipart_internal_func(key_type_arg, key_data,
3255 alg_arg,
3256 input, data_part_len,
3257 expected_mac,
3258 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003259 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003261
3262 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (mac_multipart_internal_func(key_type_arg, key_data,
3266 alg_arg,
3267 input, data_part_len,
3268 expected_mac,
3269 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003270 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003272 }
3273
3274 /* Goto is required to silence warnings about unused labels, as we
3275 * don't actually do any test assertions in this function. */
3276 goto exit;
3277}
3278/* END_CASE */
3279
3280/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281void mac_sign(int key_type_arg,
3282 data_t *key_data,
3283 int alg_arg,
3284 data_t *input,
3285 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003286{
Ronald Cron5425a212020-08-04 14:58:35 +02003287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003288 psa_key_type_t key_type = key_type_arg;
3289 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003290 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003292 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003293 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003295 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003296 const size_t output_sizes_to_test[] = {
3297 0,
3298 1,
3299 expected_mac->len - 1,
3300 expected_mac->len,
3301 expected_mac->len + 1,
3302 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003303
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003305 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3311 psa_set_key_algorithm(&attributes, alg);
3312 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3315 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003318 const size_t output_size = output_sizes_to_test[i];
3319 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 (output_size >= expected_mac->len ? PSA_SUCCESS :
3321 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 mbedtls_test_set_step(output_size);
3324 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003325
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003326 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 TEST_EQUAL(psa_mac_compute(key, alg,
3328 input->x, input->len,
3329 actual_mac, output_size, &mac_length),
3330 expected_status);
3331 if (expected_status == PSA_SUCCESS) {
3332 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3333 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003334 }
3335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 if (output_size > 0) {
3337 memset(actual_mac, 0, output_size);
3338 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003339
3340 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3342 PSA_ASSERT(psa_mac_update(&operation,
3343 input->x, input->len));
3344 TEST_EQUAL(psa_mac_sign_finish(&operation,
3345 actual_mac, output_size,
3346 &mac_length),
3347 expected_status);
3348 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 if (expected_status == PSA_SUCCESS) {
3351 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3352 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003353 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003355 actual_mac = NULL;
3356 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003357
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 psa_mac_abort(&operation);
3360 psa_destroy_key(key);
3361 PSA_DONE();
3362 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363}
3364/* END_CASE */
3365
3366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367void mac_verify(int key_type_arg,
3368 data_t *key_data,
3369 int alg_arg,
3370 data_t *input,
3371 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003372{
Ronald Cron5425a212020-08-04 14:58:35 +02003373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003374 psa_key_type_t key_type = key_type_arg;
3375 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003376 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003378 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3385 psa_set_key_algorithm(&attributes, alg);
3386 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3389 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003390
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003391 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3393 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003394
3395 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3397 PSA_ASSERT(psa_mac_update(&operation,
3398 input->x, input->len));
3399 PSA_ASSERT(psa_mac_verify_finish(&operation,
3400 expected_mac->x,
3401 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003403 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 TEST_EQUAL(psa_mac_verify(key, alg,
3405 input->x, input->len,
3406 expected_mac->x,
3407 expected_mac->len - 1),
3408 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003409
3410 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3412 PSA_ASSERT(psa_mac_update(&operation,
3413 input->x, input->len));
3414 TEST_EQUAL(psa_mac_verify_finish(&operation,
3415 expected_mac->x,
3416 expected_mac->len - 1),
3417 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003418
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003419 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3421 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3422 TEST_EQUAL(psa_mac_verify(key, alg,
3423 input->x, input->len,
3424 perturbed_mac, expected_mac->len + 1),
3425 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003426
3427 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3429 PSA_ASSERT(psa_mac_update(&operation,
3430 input->x, input->len));
3431 TEST_EQUAL(psa_mac_verify_finish(&operation,
3432 perturbed_mac,
3433 expected_mac->len + 1),
3434 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003435
3436 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 for (size_t i = 0; i < expected_mac->len; i++) {
3438 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003439 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 TEST_EQUAL(psa_mac_verify(key, alg,
3442 input->x, input->len,
3443 perturbed_mac, expected_mac->len),
3444 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3447 PSA_ASSERT(psa_mac_update(&operation,
3448 input->x, input->len));
3449 TEST_EQUAL(psa_mac_verify_finish(&operation,
3450 perturbed_mac,
3451 expected_mac->len),
3452 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003453 perturbed_mac[i] ^= 1;
3454 }
3455
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 psa_mac_abort(&operation);
3458 psa_destroy_key(key);
3459 PSA_DONE();
3460 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003461}
3462/* END_CASE */
3463
3464/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003466{
Jaeden Ameroab439972019-02-15 14:12:05 +00003467 const uint8_t input[1] = { 0 };
3468 unsigned char output[1] = { 0 };
3469 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003470 /* Test each valid way of initializing the object, except for `= {0}`, as
3471 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3472 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003473 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003475 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3476 psa_cipher_operation_t zero;
3477
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003479
Jaeden Ameroab439972019-02-15 14:12:05 +00003480 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 TEST_EQUAL(psa_cipher_update(&func,
3482 input, sizeof(input),
3483 output, sizeof(output),
3484 &output_length),
3485 PSA_ERROR_BAD_STATE);
3486 TEST_EQUAL(psa_cipher_update(&init,
3487 input, sizeof(input),
3488 output, sizeof(output),
3489 &output_length),
3490 PSA_ERROR_BAD_STATE);
3491 TEST_EQUAL(psa_cipher_update(&zero,
3492 input, sizeof(input),
3493 output, sizeof(output),
3494 &output_length),
3495 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003496
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003497 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 PSA_ASSERT(psa_cipher_abort(&func));
3499 PSA_ASSERT(psa_cipher_abort(&init));
3500 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003501}
3502/* END_CASE */
3503
3504/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003505void cipher_setup(int key_type_arg,
3506 data_t *key,
3507 int alg_arg,
3508 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003509{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003510 psa_key_type_t key_type = key_type_arg;
3511 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003512 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003513 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003514 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003515#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003516 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3517#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003518
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3522 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 }
3525 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003526
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003527 /* The operation object should be reusable. */
3528#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3530 smoke_test_key_data,
3531 sizeof(smoke_test_key_data),
3532 KNOWN_SUPPORTED_CIPHER_ALG,
3533 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003534 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 }
3536 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003537#endif
3538
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 psa_cipher_abort(&operation);
3541 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003542}
3543/* END_CASE */
3544
Ronald Cronee414c72021-03-18 18:50:08 +01003545/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003546void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003547{
Ronald Cron5425a212020-08-04 14:58:35 +02003548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003549 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3550 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003552 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003553 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003554 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003555 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 0xaa, 0xaa, 0xaa, 0xaa
3557 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003558 const uint8_t text[] = {
3559 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 0xbb, 0xbb, 0xbb, 0xbb
3561 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003562 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003563 size_t length = 0;
3564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 PSA_ASSERT(psa_crypto_init());
3566 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3567 psa_set_key_algorithm(&attributes, alg);
3568 psa_set_key_type(&attributes, key_type);
3569 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3570 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003571
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003572 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3574 ASSERT_OPERATION_IS_ACTIVE(operation);
3575 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3576 PSA_ERROR_BAD_STATE);
3577 ASSERT_OPERATION_IS_INACTIVE(operation);
3578 PSA_ASSERT(psa_cipher_abort(&operation));
3579 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003580
3581 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3583 ASSERT_OPERATION_IS_ACTIVE(operation);
3584 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3585 PSA_ERROR_BAD_STATE);
3586 ASSERT_OPERATION_IS_INACTIVE(operation);
3587 PSA_ASSERT(psa_cipher_abort(&operation));
3588 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003589
Jaeden Ameroab439972019-02-15 14:12:05 +00003590 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3592 buffer, sizeof(buffer),
3593 &length),
3594 PSA_ERROR_BAD_STATE);
3595 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003596
3597 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3599 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3600 buffer, sizeof(buffer),
3601 &length));
3602 ASSERT_OPERATION_IS_ACTIVE(operation);
3603 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3604 buffer, sizeof(buffer),
3605 &length),
3606 PSA_ERROR_BAD_STATE);
3607 ASSERT_OPERATION_IS_INACTIVE(operation);
3608 PSA_ASSERT(psa_cipher_abort(&operation));
3609 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003610
3611 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3613 PSA_ASSERT(psa_cipher_set_iv(&operation,
3614 iv, sizeof(iv)));
3615 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3616 buffer, sizeof(buffer),
3617 &length),
3618 PSA_ERROR_BAD_STATE);
3619 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003620
3621 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 TEST_EQUAL(psa_cipher_set_iv(&operation,
3623 iv, sizeof(iv)),
3624 PSA_ERROR_BAD_STATE);
3625 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003626
3627 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3629 PSA_ASSERT(psa_cipher_set_iv(&operation,
3630 iv, sizeof(iv)));
3631 ASSERT_OPERATION_IS_ACTIVE(operation);
3632 TEST_EQUAL(psa_cipher_set_iv(&operation,
3633 iv, sizeof(iv)),
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 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3641 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3642 buffer, sizeof(buffer),
3643 &length));
3644 TEST_EQUAL(psa_cipher_set_iv(&operation,
3645 iv, sizeof(iv)),
3646 PSA_ERROR_BAD_STATE);
3647 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003648
3649 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 TEST_EQUAL(psa_cipher_update(&operation,
3651 text, sizeof(text),
3652 buffer, sizeof(buffer),
3653 &length),
3654 PSA_ERROR_BAD_STATE);
3655 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003656
3657 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3659 ASSERT_OPERATION_IS_ACTIVE(operation);
3660 TEST_EQUAL(psa_cipher_update(&operation,
3661 text, sizeof(text),
3662 buffer, sizeof(buffer),
3663 &length),
3664 PSA_ERROR_BAD_STATE);
3665 ASSERT_OPERATION_IS_INACTIVE(operation);
3666 PSA_ASSERT(psa_cipher_abort(&operation));
3667 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003668
3669 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3671 PSA_ASSERT(psa_cipher_set_iv(&operation,
3672 iv, sizeof(iv)));
3673 PSA_ASSERT(psa_cipher_finish(&operation,
3674 buffer, sizeof(buffer), &length));
3675 TEST_EQUAL(psa_cipher_update(&operation,
3676 text, sizeof(text),
3677 buffer, sizeof(buffer),
3678 &length),
3679 PSA_ERROR_BAD_STATE);
3680 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003681
3682 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 TEST_EQUAL(psa_cipher_finish(&operation,
3684 buffer, sizeof(buffer), &length),
3685 PSA_ERROR_BAD_STATE);
3686 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003687
3688 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003690 /* Not calling update means we are encrypting an empty buffer, which is OK
3691 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 ASSERT_OPERATION_IS_ACTIVE(operation);
3693 TEST_EQUAL(psa_cipher_finish(&operation,
3694 buffer, sizeof(buffer), &length),
3695 PSA_ERROR_BAD_STATE);
3696 ASSERT_OPERATION_IS_INACTIVE(operation);
3697 PSA_ASSERT(psa_cipher_abort(&operation));
3698 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003699
3700 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3702 PSA_ASSERT(psa_cipher_set_iv(&operation,
3703 iv, sizeof(iv)));
3704 PSA_ASSERT(psa_cipher_finish(&operation,
3705 buffer, sizeof(buffer), &length));
3706 TEST_EQUAL(psa_cipher_finish(&operation,
3707 buffer, sizeof(buffer), &length),
3708 PSA_ERROR_BAD_STATE);
3709 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003710
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003712
Jaeden Ameroab439972019-02-15 14:12:05 +00003713exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 psa_cipher_abort(&operation);
3715 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003716}
3717/* END_CASE */
3718
3719/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003720void cipher_encrypt_fail(int alg_arg,
3721 int key_type_arg,
3722 data_t *key_data,
3723 data_t *input,
3724 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003725{
Ronald Cron5425a212020-08-04 14:58:35 +02003726 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003727 psa_status_t status;
3728 psa_key_type_t key_type = key_type_arg;
3729 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003730 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003732 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3733 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003734 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003735 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003736 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003737 size_t function_output_length;
3738 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3740
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 if (PSA_ERROR_BAD_STATE != expected_status) {
3742 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003743
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3745 psa_set_key_algorithm(&attributes, alg);
3746 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003747
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3749 input->len);
3750 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003751
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3753 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003754 }
3755
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003756 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3758 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003759
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003761
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003762 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 status = psa_cipher_encrypt_setup(&operation, key, alg);
3764 if (status == PSA_SUCCESS) {
3765 if (alg != PSA_ALG_ECB_NO_PADDING) {
3766 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3767 iv, iv_size,
3768 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003769 }
3770
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 status = psa_cipher_update(&operation, input->x, input->len,
3772 output, output_buffer_size,
3773 &function_output_length);
3774 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003775 output_length += function_output_length;
3776
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 status = psa_cipher_finish(&operation, output + output_length,
3778 output_buffer_size - output_length,
3779 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003780
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 TEST_EQUAL(status, expected_status);
3782 } else {
3783 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003784 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 } else {
3786 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003787 }
3788
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 psa_cipher_abort(&operation);
3791 mbedtls_free(output);
3792 psa_destroy_key(key);
3793 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003794}
3795/* END_CASE */
3796
3797/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003798void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3799 data_t *input, int iv_length,
3800 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003801{
3802 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3803 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3805 size_t output_buffer_size = 0;
3806 unsigned char *output = NULL;
3807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3809 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3814 psa_set_key_algorithm(&attributes, alg);
3815 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3818 &key));
3819 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3820 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3821 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003822
3823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 psa_cipher_abort(&operation);
3825 mbedtls_free(output);
3826 psa_destroy_key(key);
3827 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003828}
3829/* END_CASE */
3830
3831/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003832void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3833 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003834{
3835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3836 psa_key_type_t key_type = key_type_arg;
3837 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003838 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3839 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003840 unsigned char *output = NULL;
3841 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003842 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003846
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003847 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 TEST_LE_U(ciphertext->len,
3849 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3850 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3851 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3852 TEST_LE_U(plaintext->len,
3853 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3854 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3855 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003856
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003857
3858 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 psa_set_key_usage_flags(&attributes,
3860 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3861 psa_set_key_algorithm(&attributes, alg);
3862 psa_set_key_type(&attributes, key_type);
3863 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3864 &key));
3865 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3866 plaintext->len);
3867 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003868
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003869 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3871 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3872 PSA_ERROR_BAD_STATE);
3873 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3874 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3875 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003876
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003877 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3879 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3880 &length),
3881 PSA_ERROR_BAD_STATE);
3882 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3883 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3884 &length),
3885 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003886
Gilles Peskine286c3142022-04-20 17:09:38 +02003887 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003889 output_length = 0;
3890 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 PSA_ASSERT(psa_cipher_update(&operation,
3892 plaintext->x, plaintext->len,
3893 output, output_buffer_size,
3894 &length));
3895 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003896 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 PSA_ASSERT(psa_cipher_finish(&operation,
3898 mbedtls_buffer_offset(output, output_length),
3899 output_buffer_size - output_length,
3900 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003901 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3903 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003904
Gilles Peskine286c3142022-04-20 17:09:38 +02003905 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003907 output_length = 0;
3908 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 PSA_ASSERT(psa_cipher_update(&operation,
3910 ciphertext->x, ciphertext->len,
3911 output, output_buffer_size,
3912 &length));
3913 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003914 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 PSA_ASSERT(psa_cipher_finish(&operation,
3916 mbedtls_buffer_offset(output, output_length),
3917 output_buffer_size - output_length,
3918 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003919 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 ASSERT_COMPARE(plaintext->x, plaintext->len,
3921 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003922
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003923 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003924 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3926 output, output_buffer_size,
3927 &output_length));
3928 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3929 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003930
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003931 /* One-shot decryption */
3932 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3934 output, output_buffer_size,
3935 &output_length));
3936 ASSERT_COMPARE(plaintext->x, plaintext->len,
3937 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003938
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003939exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 PSA_ASSERT(psa_cipher_abort(&operation));
3941 mbedtls_free(output);
3942 psa_cipher_abort(&operation);
3943 psa_destroy_key(key);
3944 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003945}
3946/* END_CASE */
3947
3948/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003950{
3951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3952 psa_algorithm_t alg = alg_arg;
3953 psa_key_type_t key_type = key_type_arg;
3954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3955 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3956 psa_status_t status;
3957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01003959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3961 psa_set_key_algorithm(&attributes, alg);
3962 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01003963
3964 /* Usage of either of these two size macros would cause divide by zero
3965 * with incorrect key types previously. Input length should be irrelevant
3966 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
3968 0);
3969 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01003970
3971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3973 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01003974
3975 /* Should fail due to invalid alg type (to support invalid key type).
3976 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01003980
3981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 psa_cipher_abort(&operation);
3983 psa_destroy_key(key);
3984 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01003985}
3986/* END_CASE */
3987
3988/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003989void cipher_encrypt_validation(int alg_arg,
3990 int key_type_arg,
3991 data_t *key_data,
3992 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993{
3994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3995 psa_key_type_t key_type = key_type_arg;
3996 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003998 unsigned char *output1 = NULL;
3999 size_t output1_buffer_size = 0;
4000 size_t output1_length = 0;
4001 unsigned char *output2 = NULL;
4002 size_t output2_buffer_size = 0;
4003 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004004 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004005 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004009
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4011 psa_set_key_algorithm(&attributes, alg);
4012 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4015 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4016 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4017 ASSERT_ALLOC(output1, output1_buffer_size);
4018 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004019
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4021 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004022
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004023 /* The one-shot cipher encryption uses generated iv so validating
4024 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4026 output1_buffer_size, &output1_length));
4027 TEST_LE_U(output1_length,
4028 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4029 TEST_LE_U(output1_length,
4030 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4033 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_cipher_update(&operation,
4036 input->x, input->len,
4037 output2, output2_buffer_size,
4038 &function_output_length));
4039 TEST_LE_U(function_output_length,
4040 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4041 TEST_LE_U(function_output_length,
4042 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004043 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 PSA_ASSERT(psa_cipher_finish(&operation,
4046 output2 + output2_length,
4047 output2_buffer_size - output2_length,
4048 &function_output_length));
4049 TEST_LE_U(function_output_length,
4050 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4051 TEST_LE_U(function_output_length,
4052 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004053 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 PSA_ASSERT(psa_cipher_abort(&operation));
4056 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4057 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004058
Gilles Peskine50e586b2018-06-08 14:28:46 +02004059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 psa_cipher_abort(&operation);
4061 mbedtls_free(output1);
4062 mbedtls_free(output2);
4063 psa_destroy_key(key);
4064 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004065}
4066/* END_CASE */
4067
4068/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004069void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4070 data_t *key_data, data_t *iv,
4071 data_t *input,
4072 int first_part_size_arg,
4073 int output1_length_arg, int output2_length_arg,
4074 data_t *expected_output,
4075 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076{
Ronald Cron5425a212020-08-04 14:58:35 +02004077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004078 psa_key_type_t key_type = key_type_arg;
4079 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004080 psa_status_t status;
4081 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004082 size_t first_part_size = first_part_size_arg;
4083 size_t output1_length = output1_length_arg;
4084 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004085 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004086 size_t output_buffer_size = 0;
4087 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004088 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004089 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4095 psa_set_key_algorithm(&attributes, alg);
4096 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4099 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (iv->len > 0) {
4104 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004105 }
4106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4108 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4109 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 TEST_LE_U(first_part_size, input->len);
4112 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4113 output, output_buffer_size,
4114 &function_output_length));
4115 TEST_ASSERT(function_output_length == output1_length);
4116 TEST_LE_U(function_output_length,
4117 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4118 TEST_LE_U(function_output_length,
4119 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004120 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (first_part_size < input->len) {
4123 PSA_ASSERT(psa_cipher_update(&operation,
4124 input->x + first_part_size,
4125 input->len - first_part_size,
4126 (output_buffer_size == 0 ? NULL :
4127 output + total_output_length),
4128 output_buffer_size - total_output_length,
4129 &function_output_length));
4130 TEST_ASSERT(function_output_length == output2_length);
4131 TEST_LE_U(function_output_length,
4132 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4133 alg,
4134 input->len - first_part_size));
4135 TEST_LE_U(function_output_length,
4136 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004137 total_output_length += function_output_length;
4138 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 status = psa_cipher_finish(&operation,
4141 (output_buffer_size == 0 ? NULL :
4142 output + total_output_length),
4143 output_buffer_size - total_output_length,
4144 &function_output_length);
4145 TEST_LE_U(function_output_length,
4146 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4147 TEST_LE_U(function_output_length,
4148 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004149 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (expected_status == PSA_SUCCESS) {
4153 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 ASSERT_COMPARE(expected_output->x, expected_output->len,
4156 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004157 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158
4159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 psa_cipher_abort(&operation);
4161 mbedtls_free(output);
4162 psa_destroy_key(key);
4163 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004164}
4165/* END_CASE */
4166
4167/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004168void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4169 data_t *key_data, data_t *iv,
4170 data_t *input,
4171 int first_part_size_arg,
4172 int output1_length_arg, int output2_length_arg,
4173 data_t *expected_output,
4174 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004175{
Ronald Cron5425a212020-08-04 14:58:35 +02004176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004177 psa_key_type_t key_type = key_type_arg;
4178 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004179 psa_status_t status;
4180 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004181 size_t first_part_size = first_part_size_arg;
4182 size_t output1_length = output1_length_arg;
4183 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004184 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004185 size_t output_buffer_size = 0;
4186 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004187 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004188 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4194 psa_set_key_algorithm(&attributes, alg);
4195 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4198 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 if (iv->len > 0) {
4203 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004204 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4207 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4208 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 TEST_LE_U(first_part_size, input->len);
4211 PSA_ASSERT(psa_cipher_update(&operation,
4212 input->x, first_part_size,
4213 output, output_buffer_size,
4214 &function_output_length));
4215 TEST_ASSERT(function_output_length == output1_length);
4216 TEST_LE_U(function_output_length,
4217 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4218 TEST_LE_U(function_output_length,
4219 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004220 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (first_part_size < input->len) {
4223 PSA_ASSERT(psa_cipher_update(&operation,
4224 input->x + first_part_size,
4225 input->len - first_part_size,
4226 (output_buffer_size == 0 ? NULL :
4227 output + total_output_length),
4228 output_buffer_size - total_output_length,
4229 &function_output_length));
4230 TEST_ASSERT(function_output_length == output2_length);
4231 TEST_LE_U(function_output_length,
4232 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4233 alg,
4234 input->len - first_part_size));
4235 TEST_LE_U(function_output_length,
4236 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004237 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004238 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 status = psa_cipher_finish(&operation,
4241 (output_buffer_size == 0 ? NULL :
4242 output + total_output_length),
4243 output_buffer_size - total_output_length,
4244 &function_output_length);
4245 TEST_LE_U(function_output_length,
4246 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4247 TEST_LE_U(function_output_length,
4248 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004249 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (expected_status == PSA_SUCCESS) {
4253 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 ASSERT_COMPARE(expected_output->x, expected_output->len,
4256 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257 }
4258
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 psa_cipher_abort(&operation);
4261 mbedtls_free(output);
4262 psa_destroy_key(key);
4263 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004264}
4265/* END_CASE */
4266
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004268void cipher_decrypt_fail(int alg_arg,
4269 int key_type_arg,
4270 data_t *key_data,
4271 data_t *iv,
4272 data_t *input_arg,
4273 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004274{
4275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4276 psa_status_t status;
4277 psa_key_type_t key_type = key_type_arg;
4278 psa_algorithm_t alg = alg_arg;
4279 psa_status_t expected_status = expected_status_arg;
4280 unsigned char *input = NULL;
4281 size_t input_buffer_size = 0;
4282 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004283 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004284 size_t output_buffer_size = 0;
4285 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004286 size_t function_output_length;
4287 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (PSA_ERROR_BAD_STATE != expected_status) {
4291 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4294 psa_set_key_algorithm(&attributes, alg);
4295 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4298 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004299 }
4300
4301 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4303 if (input_buffer_size > 0) {
4304 ASSERT_ALLOC(input, input_buffer_size);
4305 memcpy(input, iv->x, iv->len);
4306 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004307 }
4308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4310 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004311
Neil Armstrong66a479f2022-02-07 15:41:19 +01004312 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4314 output_buffer_size, &output_length);
4315 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004316
Neil Armstrong66a479f2022-02-07 15:41:19 +01004317 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 status = psa_cipher_decrypt_setup(&operation, key, alg);
4319 if (status == PSA_SUCCESS) {
4320 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4321 input_arg->len) +
4322 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4323 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (iv->len > 0) {
4326 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (status != PSA_SUCCESS) {
4329 TEST_EQUAL(status, expected_status);
4330 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004331 }
4332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 if (status == PSA_SUCCESS) {
4334 status = psa_cipher_update(&operation,
4335 input_arg->x, input_arg->len,
4336 output_multi, output_buffer_size,
4337 &function_output_length);
4338 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004339 output_length = function_output_length;
4340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 status = psa_cipher_finish(&operation,
4342 output_multi + output_length,
4343 output_buffer_size - output_length,
4344 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 TEST_EQUAL(status, expected_status);
4347 } else {
4348 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004349 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 } else {
4351 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 } else {
4354 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 }
4356
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 psa_cipher_abort(&operation);
4359 mbedtls_free(input);
4360 mbedtls_free(output);
4361 mbedtls_free(output_multi);
4362 psa_destroy_key(key);
4363 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004364}
4365/* END_CASE */
4366
4367/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004368void cipher_decrypt(int alg_arg,
4369 int key_type_arg,
4370 data_t *key_data,
4371 data_t *iv,
4372 data_t *input_arg,
4373 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004374{
4375 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4376 psa_key_type_t key_type = key_type_arg;
4377 psa_algorithm_t alg = alg_arg;
4378 unsigned char *input = NULL;
4379 size_t input_buffer_size = 0;
4380 unsigned char *output = NULL;
4381 size_t output_buffer_size = 0;
4382 size_t output_length = 0;
4383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4388 psa_set_key_algorithm(&attributes, alg);
4389 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004390
4391 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4393 if (input_buffer_size > 0) {
4394 ASSERT_ALLOC(input, input_buffer_size);
4395 memcpy(input, iv->x, iv->len);
4396 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004397 }
4398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4400 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4403 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4406 output_buffer_size, &output_length));
4407 TEST_LE_U(output_length,
4408 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4409 TEST_LE_U(output_length,
4410 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 ASSERT_COMPARE(expected_output->x, expected_output->len,
4413 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 mbedtls_free(input);
4416 mbedtls_free(output);
4417 psa_destroy_key(key);
4418 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004419}
4420/* END_CASE */
4421
4422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004423void cipher_verify_output(int alg_arg,
4424 int key_type_arg,
4425 data_t *key_data,
4426 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004427{
Ronald Cron5425a212020-08-04 14:58:35 +02004428 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004429 psa_key_type_t key_type = key_type_arg;
4430 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004431 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004432 size_t output1_size = 0;
4433 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004434 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004435 size_t output2_size = 0;
4436 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4442 psa_set_key_algorithm(&attributes, alg);
4443 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4446 &key));
4447 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4448 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4451 output1, output1_size,
4452 &output1_length));
4453 TEST_LE_U(output1_length,
4454 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4455 TEST_LE_U(output1_length,
4456 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004457
4458 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4462 output2, output2_size,
4463 &output2_length));
4464 TEST_LE_U(output2_length,
4465 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4466 TEST_LE_U(output2_length,
4467 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004470
4471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 mbedtls_free(output1);
4473 mbedtls_free(output2);
4474 psa_destroy_key(key);
4475 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004476}
4477/* END_CASE */
4478
4479/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480void cipher_verify_output_multipart(int alg_arg,
4481 int key_type_arg,
4482 data_t *key_data,
4483 data_t *input,
4484 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004485{
Ronald Cron5425a212020-08-04 14:58:35 +02004486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004487 psa_key_type_t key_type = key_type_arg;
4488 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004489 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004491 size_t iv_size = 16;
4492 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004493 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004494 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004495 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004496 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004497 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004498 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004499 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004500 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4501 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4507 psa_set_key_algorithm(&attributes, alg);
4508 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4511 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4514 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (alg != PSA_ALG_ECB_NO_PADDING) {
4517 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4518 iv, iv_size,
4519 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004520 }
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4523 TEST_LE_U(output1_buffer_size,
4524 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4525 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4530 output1, output1_buffer_size,
4531 &function_output_length));
4532 TEST_LE_U(function_output_length,
4533 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4534 TEST_LE_U(function_output_length,
4535 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004536 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 PSA_ASSERT(psa_cipher_update(&operation1,
4539 input->x + first_part_size,
4540 input->len - first_part_size,
4541 output1, output1_buffer_size,
4542 &function_output_length));
4543 TEST_LE_U(function_output_length,
4544 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4545 alg,
4546 input->len - first_part_size));
4547 TEST_LE_U(function_output_length,
4548 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004549 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 PSA_ASSERT(psa_cipher_finish(&operation1,
4552 output1 + output1_length,
4553 output1_buffer_size - output1_length,
4554 &function_output_length));
4555 TEST_LE_U(function_output_length,
4556 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4557 TEST_LE_U(function_output_length,
4558 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004559 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004562
Gilles Peskine048b7f02018-06-08 14:20:49 +02004563 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 TEST_LE_U(output2_buffer_size,
4565 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4566 TEST_LE_U(output2_buffer_size,
4567 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4568 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if (iv_length > 0) {
4571 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4572 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004573 }
Moran Pekerded84402018-06-06 16:36:50 +03004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4576 output2, output2_buffer_size,
4577 &function_output_length));
4578 TEST_LE_U(function_output_length,
4579 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4580 TEST_LE_U(function_output_length,
4581 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004582 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 PSA_ASSERT(psa_cipher_update(&operation2,
4585 output1 + first_part_size,
4586 output1_length - first_part_size,
4587 output2, output2_buffer_size,
4588 &function_output_length));
4589 TEST_LE_U(function_output_length,
4590 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4591 alg,
4592 output1_length - first_part_size));
4593 TEST_LE_U(function_output_length,
4594 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004595 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 PSA_ASSERT(psa_cipher_finish(&operation2,
4598 output2 + output2_length,
4599 output2_buffer_size - output2_length,
4600 &function_output_length));
4601 TEST_LE_U(function_output_length,
4602 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004610
4611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 psa_cipher_abort(&operation1);
4613 psa_cipher_abort(&operation2);
4614 mbedtls_free(output1);
4615 mbedtls_free(output2);
4616 psa_destroy_key(key);
4617 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004618}
4619/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004620
Gilles Peskine20035e32018-02-03 22:44:14 +01004621/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004622void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4623 int alg_arg,
4624 data_t *nonce,
4625 data_t *additional_data,
4626 data_t *input_data,
4627 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004628{
Ronald Cron5425a212020-08-04 14:58:35 +02004629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004630 psa_key_type_t key_type = key_type_arg;
4631 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004632 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004633 unsigned char *output_data = NULL;
4634 size_t output_size = 0;
4635 size_t output_length = 0;
4636 unsigned char *output_data2 = NULL;
4637 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004638 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004639 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4645 psa_set_key_algorithm(&attributes, alg);
4646 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4649 &key));
4650 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4651 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4654 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004655 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4656 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4658 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4659 TEST_EQUAL(output_size,
4660 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4661 TEST_LE_U(output_size,
4662 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 status = psa_aead_encrypt(key, alg,
4667 nonce->x, nonce->len,
4668 additional_data->x,
4669 additional_data->len,
4670 input_data->x, input_data->len,
4671 output_data, output_size,
4672 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004673
4674 /* If the operation is not supported, just skip and not fail in case the
4675 * encryption involves a common limitation of cryptography hardwares and
4676 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 if (status == PSA_ERROR_NOT_SUPPORTED) {
4678 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4679 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004680 }
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (PSA_SUCCESS == expected_result) {
4685 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004686
Gilles Peskine003a4a92019-05-14 16:09:40 +02004687 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4688 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 TEST_EQUAL(input_data->len,
4690 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 TEST_LE_U(input_data->len,
4693 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 TEST_EQUAL(psa_aead_decrypt(key, alg,
4696 nonce->x, nonce->len,
4697 additional_data->x,
4698 additional_data->len,
4699 output_data, output_length,
4700 output_data2, output_length,
4701 &output_length2),
4702 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 ASSERT_COMPARE(input_data->x, input_data->len,
4705 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004707
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 psa_destroy_key(key);
4710 mbedtls_free(output_data);
4711 mbedtls_free(output_data2);
4712 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004713}
4714/* END_CASE */
4715
4716/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004717void aead_encrypt(int key_type_arg, data_t *key_data,
4718 int alg_arg,
4719 data_t *nonce,
4720 data_t *additional_data,
4721 data_t *input_data,
4722 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004723{
Ronald Cron5425a212020-08-04 14:58:35 +02004724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725 psa_key_type_t key_type = key_type_arg;
4726 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004727 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004728 unsigned char *output_data = NULL;
4729 size_t output_size = 0;
4730 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004732 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4737 psa_set_key_algorithm(&attributes, alg);
4738 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4741 &key));
4742 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4743 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4746 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004747 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4748 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 TEST_EQUAL(output_size,
4750 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4751 TEST_LE_U(output_size,
4752 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4753 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 status = psa_aead_encrypt(key, alg,
4756 nonce->x, nonce->len,
4757 additional_data->x, additional_data->len,
4758 input_data->x, input_data->len,
4759 output_data, output_size,
4760 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004761
Ronald Cron28a45ed2021-02-09 20:35:42 +01004762 /* If the operation is not supported, just skip and not fail in case the
4763 * encryption involves a common limitation of cryptography hardwares and
4764 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (status == PSA_ERROR_NOT_SUPPORTED) {
4766 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4767 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004768 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 PSA_ASSERT(status);
4771 ASSERT_COMPARE(expected_result->x, expected_result->len,
4772 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004773
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 psa_destroy_key(key);
4776 mbedtls_free(output_data);
4777 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004778}
4779/* END_CASE */
4780
4781/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004782void aead_decrypt(int key_type_arg, data_t *key_data,
4783 int alg_arg,
4784 data_t *nonce,
4785 data_t *additional_data,
4786 data_t *input_data,
4787 data_t *expected_data,
4788 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789{
Ronald Cron5425a212020-08-04 14:58:35 +02004790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791 psa_key_type_t key_type = key_type_arg;
4792 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004793 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794 unsigned char *output_data = NULL;
4795 size_t output_size = 0;
4796 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004798 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004799 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4804 psa_set_key_algorithm(&attributes, alg);
4805 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4808 &key));
4809 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4810 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4813 alg);
4814 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4815 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4817 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 TEST_EQUAL(output_size,
4819 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4820 TEST_LE_U(output_size,
4821 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 status = psa_aead_decrypt(key, alg,
4826 nonce->x, nonce->len,
4827 additional_data->x,
4828 additional_data->len,
4829 input_data->x, input_data->len,
4830 output_data, output_size,
4831 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004832
Ronald Cron28a45ed2021-02-09 20:35:42 +01004833 /* If the operation is not supported, just skip and not fail in case the
4834 * decryption involves a common limitation of cryptography hardwares and
4835 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (status == PSA_ERROR_NOT_SUPPORTED) {
4837 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4838 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004839 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (expected_result == PSA_SUCCESS) {
4844 ASSERT_COMPARE(expected_data->x, expected_data->len,
4845 output_data, output_length);
4846 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004847
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 psa_destroy_key(key);
4850 mbedtls_free(output_data);
4851 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852}
4853/* END_CASE */
4854
4855/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004856void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4857 int alg_arg,
4858 data_t *nonce,
4859 data_t *additional_data,
4860 data_t *input_data,
4861 int do_set_lengths,
4862 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004863{
Paul Elliottd3f82412021-06-16 16:52:21 +01004864 size_t ad_part_len = 0;
4865 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004866 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4869 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (do_set_lengths) {
4872 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004873 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004875 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004877 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004878
4879 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (!aead_multipart_internal_func(key_type_arg, key_data,
4881 alg_arg, nonce,
4882 additional_data,
4883 ad_part_len,
4884 input_data, -1,
4885 set_lengths_method,
4886 expected_output,
4887 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004888 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004889 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 /* length(0) part, length(ad_part_len) part, length(0) part... */
4892 mbedtls_test_set_step(1000 + ad_part_len);
4893
4894 if (!aead_multipart_internal_func(key_type_arg, key_data,
4895 alg_arg, nonce,
4896 additional_data,
4897 ad_part_len,
4898 input_data, -1,
4899 set_lengths_method,
4900 expected_output,
4901 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004902 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 }
4904 }
4905
4906 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4907 /* Split data into length(data_part_len) parts. */
4908 mbedtls_test_set_step(2000 + data_part_len);
4909
4910 if (do_set_lengths) {
4911 if (data_part_len & 0x01) {
4912 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4913 } else {
4914 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4915 }
4916 }
4917
4918 if (!aead_multipart_internal_func(key_type_arg, key_data,
4919 alg_arg, nonce,
4920 additional_data, -1,
4921 input_data, data_part_len,
4922 set_lengths_method,
4923 expected_output,
4924 1, 0)) {
4925 break;
4926 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004927
4928 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 if (!aead_multipart_internal_func(key_type_arg, key_data,
4932 alg_arg, nonce,
4933 additional_data, -1,
4934 input_data, data_part_len,
4935 set_lengths_method,
4936 expected_output,
4937 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004938 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004940 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004941
Paul Elliott8fc45162021-06-23 16:06:01 +01004942 /* Goto is required to silence warnings about unused labels, as we
4943 * don't actually do any test assertions in this function. */
4944 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004945}
4946/* END_CASE */
4947
4948/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4950 int alg_arg,
4951 data_t *nonce,
4952 data_t *additional_data,
4953 data_t *input_data,
4954 int do_set_lengths,
4955 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004956{
Paul Elliottd3f82412021-06-16 16:52:21 +01004957 size_t ad_part_len = 0;
4958 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004959 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004962 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 if (do_set_lengths) {
4966 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004967 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004969 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 if (!aead_multipart_internal_func(key_type_arg, key_data,
4974 alg_arg, nonce,
4975 additional_data,
4976 ad_part_len,
4977 input_data, -1,
4978 set_lengths_method,
4979 expected_output,
4980 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004981 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983
4984 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 if (!aead_multipart_internal_func(key_type_arg, key_data,
4988 alg_arg, nonce,
4989 additional_data,
4990 ad_part_len,
4991 input_data, -1,
4992 set_lengths_method,
4993 expected_output,
4994 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004995 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005000 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (do_set_lengths) {
5004 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005009 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 if (!aead_multipart_internal_func(key_type_arg, key_data,
5012 alg_arg, nonce,
5013 additional_data, -1,
5014 input_data, data_part_len,
5015 set_lengths_method,
5016 expected_output,
5017 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020
5021 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 if (!aead_multipart_internal_func(key_type_arg, key_data,
5025 alg_arg, nonce,
5026 additional_data, -1,
5027 input_data, data_part_len,
5028 set_lengths_method,
5029 expected_output,
5030 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005033 }
5034
Paul Elliott8fc45162021-06-23 16:06:01 +01005035 /* Goto is required to silence warnings about unused labels, as we
5036 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005037 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038}
5039/* END_CASE */
5040
5041/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005042void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5043 int alg_arg,
5044 int nonce_length,
5045 int expected_nonce_length_arg,
5046 data_t *additional_data,
5047 data_t *input_data,
5048 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005049{
5050
5051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5052 psa_key_type_t key_type = key_type_arg;
5053 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005054 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005055 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5057 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005058 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005059 size_t actual_nonce_length = 0;
5060 size_t expected_nonce_length = expected_nonce_length_arg;
5061 unsigned char *output = NULL;
5062 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005063 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005064 size_t ciphertext_size = 0;
5065 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005066 size_t tag_length = 0;
5067 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5072 psa_set_key_algorithm(&attributes, alg);
5073 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5076 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005091
5092 /* If the operation is not supported, just skip and not fail in case the
5093 * encryption involves a common limitation of cryptography hardwares and
5094 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (status == PSA_ERROR_NOT_SUPPORTED) {
5096 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5097 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005098 }
5099
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5103 nonce_length,
5104 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (expected_status == PSA_SUCCESS) {
5111 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5112 alg));
5113 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005118 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5120 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5123 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5126 output, output_size,
5127 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5130 &ciphertext_length, tag_buffer,
5131 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
5134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 psa_destroy_key(key);
5136 mbedtls_free(output);
5137 mbedtls_free(ciphertext);
5138 psa_aead_abort(&operation);
5139 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140}
5141/* END_CASE */
5142
5143/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005144void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5145 int alg_arg,
5146 int nonce_length_arg,
5147 int set_lengths_method_arg,
5148 data_t *additional_data,
5149 data_t *input_data,
5150 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005151{
5152
5153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5154 psa_key_type_t key_type = key_type_arg;
5155 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005156 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005157 uint8_t *nonce_buffer = NULL;
5158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5159 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5160 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005161 unsigned char *output = NULL;
5162 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005163 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005164 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005165 size_t ciphertext_size = 0;
5166 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005167 size_t tag_length = 0;
5168 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005169 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005170 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5175 psa_set_key_algorithm(&attributes, alg);
5176 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5179 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005194
5195 /* If the operation is not supported, just skip and not fail in case the
5196 * encryption involves a common limitation of cryptography hardwares and
5197 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (status == PSA_ERROR_NOT_SUPPORTED) {
5199 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5200 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005201 }
5202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005204
Paul Elliott4023ffd2021-09-10 16:21:22 +01005205 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (nonce_length_arg == -1) {
5207 /* Arbitrary size buffer, to test zero length valid buffer. */
5208 ASSERT_ALLOC(nonce_buffer, 4);
5209 nonce_length = 0;
5210 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005211 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 nonce_length = (size_t) nonce_length_arg;
5213 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (nonce_buffer) {
5216 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005217 nonce_buffer[index] = 'a' + index;
5218 }
Paul Elliott66696b52021-08-16 18:42:41 +01005219 }
Paul Elliott863864a2021-07-23 17:28:31 +01005220 }
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5223 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5224 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005225 }
5226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 if (expected_status == PSA_SUCCESS) {
5232 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5233 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5234 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005237 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 }
Paul Elliott863864a2021-07-23 17:28:31 +01005239
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005240 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5242 additional_data->len),
5243 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5246 output, output_size,
5247 &ciphertext_length),
5248 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5251 &ciphertext_length, tag_buffer,
5252 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5253 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005254 }
5255
5256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 psa_destroy_key(key);
5258 mbedtls_free(output);
5259 mbedtls_free(ciphertext);
5260 mbedtls_free(nonce_buffer);
5261 psa_aead_abort(&operation);
5262 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005263}
5264/* END_CASE */
5265
5266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005268 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005269 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005270 data_t *nonce,
5271 data_t *additional_data,
5272 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005274{
5275
5276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5277 psa_key_type_t key_type = key_type_arg;
5278 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005279 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5281 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5282 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005283 unsigned char *output = NULL;
5284 unsigned char *ciphertext = NULL;
5285 size_t output_size = output_size_arg;
5286 size_t ciphertext_size = 0;
5287 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005288 size_t tag_length = 0;
5289 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5294 psa_set_key_algorithm(&attributes, alg);
5295 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5298 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005309
5310 /* If the operation is not supported, just skip and not fail in case the
5311 * encryption involves a common limitation of cryptography hardwares and
5312 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (status == PSA_ERROR_NOT_SUPPORTED) {
5314 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5315 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005316 }
5317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5321 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5326 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 status = psa_aead_update(&operation, input_data->x, input_data->len,
5329 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005332
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005334 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5336 &ciphertext_length, tag_buffer,
5337 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005338 }
5339
5340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 psa_destroy_key(key);
5342 mbedtls_free(output);
5343 mbedtls_free(ciphertext);
5344 psa_aead_abort(&operation);
5345 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005346}
5347/* END_CASE */
5348
Paul Elliott91b021e2021-07-23 18:52:31 +01005349/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005350void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5351 int alg_arg,
5352 int finish_ciphertext_size_arg,
5353 int tag_size_arg,
5354 data_t *nonce,
5355 data_t *additional_data,
5356 data_t *input_data,
5357 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005358{
5359
5360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5361 psa_key_type_t key_type = key_type_arg;
5362 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005363 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5365 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5366 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005367 unsigned char *ciphertext = NULL;
5368 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005369 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005370 size_t ciphertext_size = 0;
5371 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5373 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005374 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5379 psa_set_key_algorithm(&attributes, alg);
5380 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5383 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005396
5397 /* If the operation is not supported, just skip and not fail in case the
5398 * encryption involves a common limitation of cryptography hardwares and
5399 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if (status == PSA_ERROR_NOT_SUPPORTED) {
5401 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5402 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005403 }
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5410 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5413 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5416 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005417
5418 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 status = psa_aead_finish(&operation, finish_ciphertext,
5420 finish_ciphertext_size,
5421 &ciphertext_length, tag_buffer,
5422 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005425
5426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 psa_destroy_key(key);
5428 mbedtls_free(ciphertext);
5429 mbedtls_free(finish_ciphertext);
5430 mbedtls_free(tag_buffer);
5431 psa_aead_abort(&operation);
5432 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005433}
5434/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005435
5436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437void aead_multipart_verify(int key_type_arg, data_t *key_data,
5438 int alg_arg,
5439 data_t *nonce,
5440 data_t *additional_data,
5441 data_t *input_data,
5442 data_t *tag,
5443 int tag_usage_arg,
5444 int expected_setup_status_arg,
5445 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005446{
5447 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5448 psa_key_type_t key_type = key_type_arg;
5449 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005450 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5452 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5453 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005454 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005455 unsigned char *plaintext = NULL;
5456 unsigned char *finish_plaintext = NULL;
5457 size_t plaintext_size = 0;
5458 size_t plaintext_length = 0;
5459 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005460 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005461 unsigned char *tag_buffer = NULL;
5462 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5467 psa_set_key_algorithm(&attributes, alg);
5468 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5471 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5476 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005485
5486 /* If the operation is not supported, just skip and not fail in case the
5487 * encryption involves a common limitation of cryptography hardwares and
5488 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (status == PSA_ERROR_NOT_SUPPORTED) {
5490 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5491 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 }
Paul Elliott9961a662021-09-17 19:19:02 +01005498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 status = psa_aead_set_lengths(&operation, additional_data->len,
5504 input_data->len);
5505 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5508 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5511 input_data->len,
5512 plaintext, plaintext_size,
5513 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005516 tag_buffer = tag->x;
5517 tag_size = tag->len;
5518 }
5519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 status = psa_aead_verify(&operation, finish_plaintext,
5521 verify_plaintext_size,
5522 &plaintext_length,
5523 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005526
5527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_destroy_key(key);
5529 mbedtls_free(plaintext);
5530 mbedtls_free(finish_plaintext);
5531 psa_aead_abort(&operation);
5532 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005533}
5534/* END_CASE */
5535
Paul Elliott9961a662021-09-17 19:19:02 +01005536/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005537void aead_multipart_setup(int key_type_arg, data_t *key_data,
5538 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005539{
5540 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5541 psa_key_type_t key_type = key_type_arg;
5542 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005543 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5545 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5546 psa_status_t expected_status = expected_status_arg;
5547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 psa_set_key_usage_flags(&attributes,
5551 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5552 psa_set_key_algorithm(&attributes, alg);
5553 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5556 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005567
5568exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 psa_destroy_key(key);
5570 psa_aead_abort(&operation);
5571 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005572}
5573/* END_CASE */
5574
5575/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5577 int alg_arg,
5578 data_t *nonce,
5579 data_t *additional_data,
5580 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005581{
5582 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5583 psa_key_type_t key_type = key_type_arg;
5584 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005585 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005586 unsigned char *output_data = NULL;
5587 unsigned char *final_data = NULL;
5588 size_t output_size = 0;
5589 size_t finish_output_size = 0;
5590 size_t output_length = 0;
5591 size_t key_bits = 0;
5592 size_t tag_length = 0;
5593 size_t tag_size = 0;
5594 size_t nonce_length = 0;
5595 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5596 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5597 size_t output_part_length = 0;
5598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 psa_set_key_usage_flags(&attributes,
5603 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5604 psa_set_key_algorithm(&attributes, alg);
5605 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5608 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5611 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005622
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005626
5627 /* Test all operations error without calling setup first. */
5628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5630 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5635 PSA_AEAD_NONCE_MAX_SIZE,
5636 &nonce_length),
5637 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005640
Paul Elliott481be342021-07-16 17:38:47 +01005641 /* ------------------------------------------------------- */
5642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5644 input_data->len),
5645 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005648
Paul Elliott481be342021-07-16 17:38:47 +01005649 /* ------------------------------------------------------- */
5650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5652 additional_data->len),
5653 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005656
Paul Elliott481be342021-07-16 17:38:47 +01005657 /* ------------------------------------------------------- */
5658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5660 input_data->len, output_data,
5661 output_size, &output_length),
5662 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Paul Elliott481be342021-07-16 17:38:47 +01005666 /* ------------------------------------------------------- */
5667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5669 finish_output_size,
5670 &output_part_length,
5671 tag_buffer, tag_length,
5672 &tag_size),
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_verify(&operation, final_data,
5680 finish_output_size,
5681 &output_part_length,
5682 tag_buffer,
5683 tag_length),
5684 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
5688 /* Test for double setups. */
5689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5693 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Paul Elliott481be342021-07-16 17:38:47 +01005697 /* ------------------------------------------------------- */
5698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5702 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Paul Elliott374a2be2021-07-16 17:53:40 +01005706 /* ------------------------------------------------------- */
5707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5711 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005714
5715 /* ------------------------------------------------------- */
5716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5720 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005723
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724 /* Test for not setting a nonce. */
5725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5729 additional_data->len),
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 Elliott7f628422021-09-01 12:08:29 +01005734 /* ------------------------------------------------------- */
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5739 input_data->len, output_data,
5740 output_size, &output_length),
5741 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005744
Paul Elliottbdc2c682021-09-21 18:37:10 +01005745 /* ------------------------------------------------------- */
5746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5750 finish_output_size,
5751 &output_part_length,
5752 tag_buffer, tag_length,
5753 &tag_size),
5754 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005757
5758 /* ------------------------------------------------------- */
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5763 finish_output_size,
5764 &output_part_length,
5765 tag_buffer,
5766 tag_length),
5767 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005770
Paul Elliottc23a9a02021-06-21 18:32:46 +01005771 /* Test for double setting nonce. */
5772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5778 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005781
Paul Elliott374a2be2021-07-16 17:53:40 +01005782 /* Test for double generating nonce. */
5783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5787 PSA_AEAD_NONCE_MAX_SIZE,
5788 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5791 PSA_AEAD_NONCE_MAX_SIZE,
5792 &nonce_length),
5793 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005794
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005797
5798 /* Test for generate nonce then set and vice versa */
5799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5803 PSA_AEAD_NONCE_MAX_SIZE,
5804 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5807 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005810
Andrzej Kurekad837522021-12-15 15:28:49 +01005811 /* Test for generating nonce after calling set lengths */
5812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5816 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5819 PSA_AEAD_NONCE_MAX_SIZE,
5820 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005823
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005824 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 if (operation.alg == PSA_ALG_CCM) {
5829 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5830 input_data->len),
5831 PSA_ERROR_INVALID_ARGUMENT);
5832 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5833 PSA_AEAD_NONCE_MAX_SIZE,
5834 &nonce_length),
5835 PSA_ERROR_BAD_STATE);
5836 } else {
5837 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5838 input_data->len));
5839 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5840 PSA_AEAD_NONCE_MAX_SIZE,
5841 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005842 }
5843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005845
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005846 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005847#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5851 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5852 input_data->len),
5853 PSA_ERROR_INVALID_ARGUMENT);
5854 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5855 PSA_AEAD_NONCE_MAX_SIZE,
5856 &nonce_length),
5857 PSA_ERROR_BAD_STATE);
5858 } else {
5859 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5860 input_data->len));
5861 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5862 PSA_AEAD_NONCE_MAX_SIZE,
5863 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005864 }
5865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005867#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005868
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005869 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5874 PSA_AEAD_NONCE_MAX_SIZE,
5875 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (operation.alg == PSA_ALG_CCM) {
5878 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5879 input_data->len),
5880 PSA_ERROR_INVALID_ARGUMENT);
5881 } else {
5882 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5883 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005884 }
5885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005887
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005888 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005889 /* Test for setting nonce after calling set lengths */
5890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5894 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005899
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005900 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 if (operation.alg == PSA_ALG_CCM) {
5905 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5906 input_data->len),
5907 PSA_ERROR_INVALID_ARGUMENT);
5908 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5909 PSA_ERROR_BAD_STATE);
5910 } else {
5911 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5912 input_data->len));
5913 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005914 }
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005917
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005918 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005919#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5923 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5924 input_data->len),
5925 PSA_ERROR_INVALID_ARGUMENT);
5926 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5927 PSA_ERROR_BAD_STATE);
5928 } else {
5929 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5930 input_data->len));
5931 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005932 }
5933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005935#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005936
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005937 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 if (operation.alg == PSA_ALG_CCM) {
5944 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5945 input_data->len),
5946 PSA_ERROR_INVALID_ARGUMENT);
5947 } else {
5948 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5949 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005950 }
5951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005953
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005954 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00005955#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 if (operation.alg == PSA_ALG_GCM) {
5959 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5960 SIZE_MAX),
5961 PSA_ERROR_INVALID_ARGUMENT);
5962 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5963 PSA_ERROR_BAD_STATE);
5964 } else if (operation.alg != PSA_ALG_CCM) {
5965 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5966 SIZE_MAX));
5967 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005968 }
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00005971#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005972
Tom Cosgrove1797b052022-12-04 17:19:59 +00005973 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00005974#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 if (operation.alg == PSA_ALG_GCM) {
5980 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5981 SIZE_MAX),
5982 PSA_ERROR_INVALID_ARGUMENT);
5983 } else if (operation.alg != PSA_ALG_CCM) {
5984 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5985 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005986 }
5987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00005989#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005990
5991 /* ------------------------------------------------------- */
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5998 PSA_AEAD_NONCE_MAX_SIZE,
5999 &nonce_length),
6000 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006003
Paul Elliott7220cae2021-06-22 17:25:57 +01006004 /* Test for generating nonce in decrypt setup. */
6005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6009 PSA_AEAD_NONCE_MAX_SIZE,
6010 &nonce_length),
6011 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006014
Paul Elliottc23a9a02021-06-21 18:32:46 +01006015 /* Test for setting lengths twice. */
6016
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6022 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6025 input_data->len),
6026 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006029
Andrzej Kurekad837522021-12-15 15:28:49 +01006030 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6039 additional_data->len),
6040 PSA_ERROR_BAD_STATE);
6041 } else {
6042 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6043 additional_data->len));
6044
6045 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6046 input_data->len),
6047 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006048 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006050
6051 /* ------------------------------------------------------- */
6052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (operation.alg == PSA_ALG_CCM) {
6058 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6059 input_data->len, output_data,
6060 output_size, &output_length),
6061 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 } else {
6064 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6065 input_data->len, output_data,
6066 output_size, &output_length));
6067
6068 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6069 input_data->len),
6070 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006073
6074 /* ------------------------------------------------------- */
6075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (operation.alg == PSA_ALG_CCM) {
6081 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6082 finish_output_size,
6083 &output_part_length,
6084 tag_buffer, tag_length,
6085 &tag_size));
6086 } else {
6087 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6088 finish_output_size,
6089 &output_part_length,
6090 tag_buffer, tag_length,
6091 &tag_size));
6092
6093 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6094 input_data->len),
6095 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006096 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006098
6099 /* Test for setting lengths after generating nonce + already starting data. */
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6104 PSA_AEAD_NONCE_MAX_SIZE,
6105 &nonce_length));
6106 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6109 additional_data->len),
6110 PSA_ERROR_BAD_STATE);
6111 } else {
6112 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6113 additional_data->len));
6114
6115 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6116 input_data->len),
6117 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006120
6121 /* ------------------------------------------------------- */
6122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6126 PSA_AEAD_NONCE_MAX_SIZE,
6127 &nonce_length));
6128 if (operation.alg == PSA_ALG_CCM) {
6129 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6130 input_data->len, output_data,
6131 output_size, &output_length),
6132 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 } else {
6135 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6136 input_data->len, output_data,
6137 output_size, &output_length));
6138
6139 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6140 input_data->len),
6141 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006142 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006144
6145 /* ------------------------------------------------------- */
6146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6150 PSA_AEAD_NONCE_MAX_SIZE,
6151 &nonce_length));
6152 if (operation.alg == PSA_ALG_CCM) {
6153 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6154 finish_output_size,
6155 &output_part_length,
6156 tag_buffer, tag_length,
6157 &tag_size));
6158 } else {
6159 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6160 finish_output_size,
6161 &output_part_length,
6162 tag_buffer, tag_length,
6163 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6166 input_data->len),
6167 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006168 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006170
Paul Elliott243080c2021-07-21 19:01:17 +01006171 /* Test for not sending any additional data or data after setting non zero
6172 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6179 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6182 finish_output_size,
6183 &output_part_length,
6184 tag_buffer, tag_length,
6185 &tag_size),
6186 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006189
Paul Elliott243080c2021-07-21 19:01:17 +01006190 /* Test for not sending any additional data or data after setting non-zero
6191 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6198 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6201 finish_output_size,
6202 &output_part_length,
6203 tag_buffer,
6204 tag_length),
6205 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006208
Paul Elliott243080c2021-07-21 19:01:17 +01006209 /* Test for not sending any additional data after setting a non-zero length
6210 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6217 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6220 input_data->len, output_data,
6221 output_size, &output_length),
6222 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006225
Paul Elliottf94bd992021-09-19 18:15:59 +01006226 /* Test for not sending any data after setting a non-zero length for it.*/
6227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6233 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6236 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6239 finish_output_size,
6240 &output_part_length,
6241 tag_buffer, tag_length,
6242 &tag_size),
6243 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006246
Paul Elliottb0450fe2021-09-01 15:06:26 +01006247 /* Test for sending too much additional data after setting lengths. */
6248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006254
6255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6257 additional_data->len),
6258 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006261
Paul Elliotta2a09b02021-09-22 14:56:40 +01006262 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6269 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6272 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6275 1),
6276 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006279
Paul Elliottb0450fe2021-09-01 15:06:26 +01006280 /* Test for sending too much data after setting lengths. */
6281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6289 input_data->len, output_data,
6290 output_size, &output_length),
6291 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006294
Paul Elliotta2a09b02021-09-22 14:56:40 +01006295 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6302 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6305 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6308 input_data->len, output_data,
6309 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6312 1, output_data,
6313 output_size, &output_length),
6314 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006317
Paul Elliottc23a9a02021-06-21 18:32:46 +01006318 /* Test sending additional data after data. */
6319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 if (operation.alg != PSA_ALG_CCM) {
6325 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6326 input_data->len, output_data,
6327 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6330 additional_data->len),
6331 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006332 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006334
Paul Elliott534d0b42021-06-22 19:15:20 +01006335 /* Test calling finish on decryption. */
6336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6342 finish_output_size,
6343 &output_part_length,
6344 tag_buffer, tag_length,
6345 &tag_size),
6346 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006349
6350 /* Test calling verify on encryption. */
6351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6357 finish_output_size,
6358 &output_part_length,
6359 tag_buffer,
6360 tag_length),
6361 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006364
6365
Paul Elliottc23a9a02021-06-21 18:32:46 +01006366exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 psa_destroy_key(key);
6368 psa_aead_abort(&operation);
6369 mbedtls_free(output_data);
6370 mbedtls_free(final_data);
6371 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006372}
6373/* END_CASE */
6374
6375/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006376void signature_size(int type_arg,
6377 int bits,
6378 int alg_arg,
6379 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006380{
6381 psa_key_type_t type = type_arg;
6382 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006386
Gilles Peskinee59236f2018-01-27 23:32:46 +01006387exit:
6388 ;
6389}
6390/* END_CASE */
6391
6392/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006393void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6394 int alg_arg, data_t *input_data,
6395 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006396{
Ronald Cron5425a212020-08-04 14:58:35 +02006397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006398 psa_key_type_t key_type = key_type_arg;
6399 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006400 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006401 unsigned char *signature = NULL;
6402 size_t signature_size;
6403 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6409 psa_set_key_algorithm(&attributes, alg);
6410 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6413 &key));
6414 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6415 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006416
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006417 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006418 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6420 key_bits, alg);
6421 TEST_ASSERT(signature_size != 0);
6422 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6423 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006424
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006425 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 PSA_ASSERT(psa_sign_hash(key, alg,
6427 input_data->x, input_data->len,
6428 signature, signature_size,
6429 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006430 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 ASSERT_COMPARE(output_data->x, output_data->len,
6432 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006433
6434exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006435 /*
6436 * Key attributes may have been returned by psa_get_key_attributes()
6437 * thus reset them as required.
6438 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 psa_destroy_key(key);
6442 mbedtls_free(signature);
6443 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006444}
6445/* END_CASE */
6446
Paul Elliott712d5122022-12-07 14:03:10 +00006447/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6448void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6449 int alg_arg, data_t *input_data,
6450 data_t *output_data)
6451{
6452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6453 psa_key_type_t key_type = key_type_arg;
6454 psa_algorithm_t alg = alg_arg;
6455 size_t key_bits;
6456 unsigned char *signature = NULL;
6457 size_t signature_size;
6458 size_t signature_length = 0xdeadbeef;
6459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6460 psa_status_t status = PSA_OPERATION_INCOMPLETE;
6461 size_t num_ops = 0;
6462 size_t num_ops_prior = 0;
6463 psa_sign_hash_interruptible_operation_t operation =
6464 psa_sign_hash_interruptible_operation_init();
6465
6466 PSA_ASSERT(psa_crypto_init());
6467
6468 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6469 psa_set_key_algorithm(&attributes, alg);
6470 psa_set_key_type(&attributes, key_type);
6471
6472 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6473 &key));
6474 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6475 key_bits = psa_get_key_bits(&attributes);
6476
6477 /* Allocate a buffer which has the size advertised by the
6478 * library. */
6479 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6480 key_bits, alg);
6481 TEST_ASSERT(signature_size != 0);
6482 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6483 ASSERT_ALLOC(signature, signature_size);
6484
6485 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6486 TEST_ASSERT(num_ops_prior == 0);
6487
6488 /* Start performing the signature. */
6489 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6490 input_data->x, input_data->len));
6491
6492 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6493 TEST_ASSERT(num_ops_prior == 0);
6494
6495 /* Continue performing the signature until complete. */
6496 while (status == PSA_OPERATION_INCOMPLETE) {
6497 status = psa_sign_hash_complete(&operation, signature, signature_size,
6498 &signature_length);
6499
6500 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6501 num_ops = psa_sign_hash_get_num_ops(&operation);
6502
6503 TEST_ASSERT(num_ops > num_ops_prior);
6504 num_ops_prior = num_ops;
6505 }
6506 }
6507
6508 TEST_ASSERT(status == PSA_SUCCESS);
6509
6510 /* Verify that the signature is what is expected. */
6511 ASSERT_COMPARE(output_data->x, output_data->len,
6512 signature, signature_length);
6513
6514 PSA_ASSERT(psa_sign_hash_abort(&operation));
6515
6516exit:
6517
6518 /*
6519 * Key attributes may have been returned by psa_get_key_attributes()
6520 * thus reset them as required.
6521 */
6522 psa_reset_key_attributes(&attributes);
6523
6524 psa_destroy_key(key);
6525 mbedtls_free(signature);
6526 PSA_DONE();
6527}
6528/* END_CASE */
6529
Gilles Peskine20035e32018-02-03 22:44:14 +01006530/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006531void sign_hash_fail(int key_type_arg, data_t *key_data,
6532 int alg_arg, data_t *input_data,
6533 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006534{
Ronald Cron5425a212020-08-04 14:58:35 +02006535 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006536 psa_key_type_t key_type = key_type_arg;
6537 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006538 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006539 psa_status_t actual_status;
6540 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006541 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006542 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006544
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6550 psa_set_key_algorithm(&attributes, alg);
6551 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6554 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 actual_status = psa_sign_hash(key, alg,
6557 input_data->x, input_data->len,
6558 signature, signature_size,
6559 &signature_length);
6560 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006561 /* The value of *signature_length is unspecified on error, but
6562 * whatever it is, it should be less than signature_size, so that
6563 * if the caller tries to read *signature_length bytes without
6564 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006566
6567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 psa_reset_key_attributes(&attributes);
6569 psa_destroy_key(key);
6570 mbedtls_free(signature);
6571 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006572}
6573/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006574
Paul Elliott91007972022-12-16 12:21:24 +00006575/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6576void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6577 int alg_arg, data_t *input_data,
6578 int signature_size_arg,
6579 int expected_start_status_arg,
6580 int expected_complete_status_arg)
6581{
6582 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6583 psa_key_type_t key_type = key_type_arg;
6584 psa_algorithm_t alg = alg_arg;
6585 size_t signature_size = signature_size_arg;
6586 psa_status_t actual_status;
6587 psa_status_t expected_start_status = expected_start_status_arg;
6588 psa_status_t expected_complete_status = expected_complete_status_arg;
6589 unsigned char *signature = NULL;
6590 size_t signature_length = 0xdeadbeef;
6591 size_t num_ops = 0;
6592 size_t num_ops_prior = 0;
6593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6594 psa_sign_hash_interruptible_operation_t operation =
6595 psa_sign_hash_interruptible_operation_init();
6596
6597 ASSERT_ALLOC(signature, signature_size);
6598
6599 PSA_ASSERT(psa_crypto_init());
6600
6601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6602 psa_set_key_algorithm(&attributes, alg);
6603 psa_set_key_type(&attributes, key_type);
6604
6605 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6606 &key));
6607
6608 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6609 TEST_ASSERT(num_ops_prior == 0);
6610
6611 /* Start performing the signature. */
6612 actual_status = psa_sign_hash_start(&operation, key, alg,
6613 input_data->x, input_data->len);
6614
6615 TEST_EQUAL(actual_status, expected_start_status);
6616
6617 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6618 TEST_ASSERT(num_ops_prior == 0);
6619
6620 actual_status = PSA_OPERATION_INCOMPLETE;
6621
6622 /* Continue performing the signature until complete. */
6623 while (actual_status == PSA_OPERATION_INCOMPLETE) {
6624 actual_status = psa_sign_hash_complete(&operation, signature,
6625 signature_size,
6626 &signature_length);
6627
6628 /* If the psa_sign_hash_start() failed, psa_sign_hash_complete()
6629 * should also fail with bad state. */
6630 if (expected_start_status != PSA_SUCCESS) {
6631 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6632 } else if (actual_status != PSA_OPERATION_INCOMPLETE) {
6633 TEST_EQUAL(actual_status, expected_complete_status);
6634 } else {
6635 num_ops = psa_sign_hash_get_num_ops(&operation);
6636
6637 TEST_ASSERT(num_ops > num_ops_prior);
6638 num_ops_prior = num_ops;
6639 }
6640 }
6641
6642 PSA_ASSERT(psa_sign_hash_abort(&operation));
6643
6644 /* The value of *signature_length is unspecified on error, but
6645 * whatever it is, it should be less than signature_size, so that
6646 * if the caller tries to read *signature_length bytes without
6647 * checking the error code then they don't overflow a buffer. */
6648 TEST_LE_U(signature_length, signature_size);
6649
6650exit:
6651 psa_reset_key_attributes(&attributes);
6652 psa_destroy_key(key);
6653 mbedtls_free(signature);
6654 PSA_DONE();
6655}
6656/* END_CASE */
6657
mohammad16038cc1cee2018-03-28 01:21:33 +03006658/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006659void sign_verify_hash(int key_type_arg, data_t *key_data,
6660 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006661{
Ronald Cron5425a212020-08-04 14:58:35 +02006662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006663 psa_key_type_t key_type = key_type_arg;
6664 psa_algorithm_t alg = alg_arg;
6665 size_t key_bits;
6666 unsigned char *signature = NULL;
6667 size_t signature_size;
6668 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6674 psa_set_key_algorithm(&attributes, alg);
6675 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6678 &key));
6679 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6680 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006681
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006682 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006683 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6685 key_bits, alg);
6686 TEST_ASSERT(signature_size != 0);
6687 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6688 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006689
6690 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 PSA_ASSERT(psa_sign_hash(key, alg,
6692 input_data->x, input_data->len,
6693 signature, signature_size,
6694 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006695 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 TEST_LE_U(signature_length, signature_size);
6697 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006698
6699 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 PSA_ASSERT(psa_verify_hash(key, alg,
6701 input_data->x, input_data->len,
6702 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006705 /* Flip a bit in the input and verify that the signature is now
6706 * detected as invalid. Flip a bit at the beginning, not at the end,
6707 * because ECDSA may ignore the last few bits of the input. */
6708 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 TEST_EQUAL(psa_verify_hash(key, alg,
6710 input_data->x, input_data->len,
6711 signature, signature_length),
6712 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006713 }
6714
6715exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006716 /*
6717 * Key attributes may have been returned by psa_get_key_attributes()
6718 * thus reset them as required.
6719 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006721
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 psa_destroy_key(key);
6723 mbedtls_free(signature);
6724 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006725}
6726/* END_CASE */
6727
Paul Elliott712d5122022-12-07 14:03:10 +00006728/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6729void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
6730 int alg_arg, data_t *input_data)
6731{
6732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6733 psa_key_type_t key_type = key_type_arg;
6734 psa_algorithm_t alg = alg_arg;
6735 size_t key_bits;
6736 unsigned char *signature = NULL;
6737 size_t signature_size;
6738 size_t signature_length = 0xdeadbeef;
6739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6740 psa_status_t status = PSA_OPERATION_INCOMPLETE;
6741 psa_sign_hash_interruptible_operation_t sign_operation =
6742 psa_sign_hash_interruptible_operation_init();
6743 psa_verify_hash_interruptible_operation_t verify_operation =
6744 psa_verify_hash_interruptible_operation_init();
6745
6746 PSA_ASSERT(psa_crypto_init());
6747
6748 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6749 psa_set_key_algorithm(&attributes, alg);
6750 psa_set_key_type(&attributes, key_type);
6751
6752 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6753 &key));
6754 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6755 key_bits = psa_get_key_bits(&attributes);
6756
6757 /* Allocate a buffer which has the size advertised by the
6758 * library. */
6759 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6760 key_bits, alg);
6761 TEST_ASSERT(signature_size != 0);
6762 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6763 ASSERT_ALLOC(signature, signature_size);
6764
6765 /* Start performing the signature. */
6766 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6767 input_data->x, input_data->len));
6768
6769 /* Continue performing the signature until complete. */
6770 while (status == PSA_OPERATION_INCOMPLETE) {
6771
6772 status = psa_sign_hash_complete(&sign_operation, signature, signature_size,
6773 &signature_length);
6774 }
6775
6776 TEST_ASSERT(status == PSA_SUCCESS);
6777
6778 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6779
6780 /* Check that the signature length looks sensible. */
6781 TEST_LE_U(signature_length, signature_size);
6782 TEST_ASSERT(signature_length > 0);
6783
6784 status = PSA_OPERATION_INCOMPLETE;
6785
6786 /* Start verification. */
6787 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6788 input_data->x, input_data->len,
6789 signature, signature_length));
6790
6791 /* Continue performing the signature until complete. */
6792 while (status == PSA_OPERATION_INCOMPLETE) {
6793 status = psa_verify_hash_complete(&verify_operation);
6794 }
6795
6796 TEST_ASSERT(status == PSA_SUCCESS);
6797
6798 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6799
6800 verify_operation = psa_verify_hash_interruptible_operation_init();
6801
6802 if (input_data->len != 0) {
6803 /* Flip a bit in the input and verify that the signature is now
6804 * detected as invalid. Flip a bit at the beginning, not at the end,
6805 * because ECDSA may ignore the last few bits of the input. */
6806 input_data->x[0] ^= 1;
6807
6808 status = PSA_OPERATION_INCOMPLETE;
6809
6810 /* Start verification. */
6811 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6812 input_data->x, input_data->len,
6813 signature, signature_length));
6814
6815 /* Continue performing the signature until complete. */
6816 while (status == PSA_OPERATION_INCOMPLETE) {
6817 status = psa_verify_hash_complete(&verify_operation);
6818 }
6819
6820 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
6821 }
6822
6823 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6824
6825exit:
6826 /*
6827 * Key attributes may have been returned by psa_get_key_attributes()
6828 * thus reset them as required.
6829 */
6830 psa_reset_key_attributes(&attributes);
6831
6832 psa_destroy_key(key);
6833 mbedtls_free(signature);
6834 PSA_DONE();
6835}
6836/* END_CASE */
6837
Gilles Peskine9911b022018-06-29 17:30:48 +02006838/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839void verify_hash(int key_type_arg, data_t *key_data,
6840 int alg_arg, data_t *hash_data,
6841 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006842{
Ronald Cron5425a212020-08-04 14:58:35 +02006843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006844 psa_key_type_t key_type = key_type_arg;
6845 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006849
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006851
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6853 psa_set_key_algorithm(&attributes, alg);
6854 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006855
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6857 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 PSA_ASSERT(psa_verify_hash(key, alg,
6860 hash_data->x, hash_data->len,
6861 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006862
itayzafrir5c753392018-05-08 11:18:38 +03006863exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 psa_reset_key_attributes(&attributes);
6865 psa_destroy_key(key);
6866 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006867}
6868/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006869
Paul Elliott712d5122022-12-07 14:03:10 +00006870/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6871void verify_hash_interruptible(int key_type_arg, data_t *key_data,
6872 int alg_arg, data_t *hash_data,
6873 data_t *signature_data)
6874{
6875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6876 psa_key_type_t key_type = key_type_arg;
6877 psa_algorithm_t alg = alg_arg;
6878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6879 psa_status_t status = PSA_OPERATION_INCOMPLETE;
6880 size_t num_ops = 0;
6881 size_t num_ops_prior = 0;
6882 psa_verify_hash_interruptible_operation_t operation =
6883 psa_verify_hash_interruptible_operation_init();
6884
6885 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
6886
6887 PSA_ASSERT(psa_crypto_init());
6888
6889 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6890 psa_set_key_algorithm(&attributes, alg);
6891 psa_set_key_type(&attributes, key_type);
6892
6893 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6894 &key));
6895
6896 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
6897
6898 TEST_ASSERT(num_ops_prior == 0);
6899
6900 /* Start verification. */
6901 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
6902 hash_data->x, hash_data->len,
6903 signature_data->x, signature_data->len)
6904 );
6905
6906 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
6907
6908 TEST_ASSERT(num_ops_prior == 0);
6909
6910 /* Continue performing the signature until complete. */
6911 while (status == PSA_OPERATION_INCOMPLETE) {
6912 status = psa_verify_hash_complete(&operation);
6913
6914 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6915 num_ops = psa_verify_hash_get_num_ops(&operation);
6916
6917 TEST_ASSERT(num_ops > num_ops_prior);
6918 num_ops_prior = num_ops;
6919 }
6920 }
6921
6922 TEST_ASSERT(status == PSA_SUCCESS);
6923
6924 PSA_ASSERT(psa_verify_hash_abort(&operation));
6925
6926exit:
6927 psa_reset_key_attributes(&attributes);
6928 psa_destroy_key(key);
6929 PSA_DONE();
6930}
6931/* END_CASE */
6932
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006933/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006934void verify_hash_fail(int key_type_arg, data_t *key_data,
6935 int alg_arg, data_t *hash_data,
6936 data_t *signature_data,
6937 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006938{
Ronald Cron5425a212020-08-04 14:58:35 +02006939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006940 psa_key_type_t key_type = key_type_arg;
6941 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006942 psa_status_t actual_status;
6943 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6949 psa_set_key_algorithm(&attributes, alg);
6950 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6953 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006954
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 actual_status = psa_verify_hash(key, alg,
6956 hash_data->x, hash_data->len,
6957 signature_data->x, signature_data->len);
6958 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006959
6960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 psa_reset_key_attributes(&attributes);
6962 psa_destroy_key(key);
6963 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006964}
6965/* END_CASE */
6966
Paul Elliott91007972022-12-16 12:21:24 +00006967/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6968void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6969 int alg_arg, data_t *hash_data,
6970 data_t *signature_data,
6971 int expected_start_status_arg,
6972 int expected_complete_status_arg)
6973{
6974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6975 psa_key_type_t key_type = key_type_arg;
6976 psa_algorithm_t alg = alg_arg;
6977 psa_status_t actual_status;
6978 psa_status_t expected_start_status = expected_start_status_arg;
6979 psa_status_t expected_complete_status = expected_complete_status_arg;
6980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6981 size_t num_ops = 0;
6982 size_t num_ops_prior = 0;
6983 psa_verify_hash_interruptible_operation_t operation =
6984 psa_verify_hash_interruptible_operation_init();
6985
6986 PSA_ASSERT(psa_crypto_init());
6987
6988 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6989 psa_set_key_algorithm(&attributes, alg);
6990 psa_set_key_type(&attributes, key_type);
6991
6992 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6993 &key));
6994
6995 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
6996 TEST_ASSERT(num_ops_prior == 0);
6997
6998 /* Start verification. */
6999 actual_status = psa_verify_hash_start(&operation, key, alg,
7000 hash_data->x, hash_data->len,
7001 signature_data->x,
7002 signature_data->len);
7003
7004 TEST_EQUAL(actual_status, expected_start_status);
7005
7006 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7007 TEST_ASSERT(num_ops_prior == 0);
7008
7009 actual_status = PSA_OPERATION_INCOMPLETE;
7010
7011 /* Continue performing the signature until complete. */
7012 while (actual_status == PSA_OPERATION_INCOMPLETE) {
7013 actual_status = psa_verify_hash_complete(&operation);
7014
7015 /* If the psa_verify_hash_start() failed,
7016 * psa_verify_hash_complete() should also fail with bad state.*/
7017 if (expected_start_status != PSA_SUCCESS) {
7018 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7019 } else if (actual_status != PSA_OPERATION_INCOMPLETE) {
7020 TEST_EQUAL(actual_status, expected_complete_status);
7021 } else {
7022 num_ops = psa_verify_hash_get_num_ops(&operation);
7023
7024 TEST_ASSERT(num_ops > num_ops_prior);
7025 num_ops_prior = num_ops;
7026 }
7027 }
7028
7029 PSA_ASSERT(psa_verify_hash_abort(&operation));
7030
7031exit:
7032 psa_reset_key_attributes(&attributes);
7033 psa_destroy_key(key);
7034 PSA_DONE();
7035}
7036/* END_CASE */
7037
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007038/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007039void sign_message_deterministic(int key_type_arg,
7040 data_t *key_data,
7041 int alg_arg,
7042 data_t *input_data,
7043 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007044{
7045 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7046 psa_key_type_t key_type = key_type_arg;
7047 psa_algorithm_t alg = alg_arg;
7048 size_t key_bits;
7049 unsigned char *signature = NULL;
7050 size_t signature_size;
7051 size_t signature_length = 0xdeadbeef;
7052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007055
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7057 psa_set_key_algorithm(&attributes, alg);
7058 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7061 &key));
7062 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7063 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7066 TEST_ASSERT(signature_size != 0);
7067 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7068 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007069
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 PSA_ASSERT(psa_sign_message(key, alg,
7071 input_data->x, input_data->len,
7072 signature, signature_size,
7073 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 ASSERT_COMPARE(output_data->x, output_data->len,
7076 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007077
7078exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 psa_destroy_key(key);
7082 mbedtls_free(signature);
7083 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007084
7085}
7086/* END_CASE */
7087
7088/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007089void sign_message_fail(int key_type_arg,
7090 data_t *key_data,
7091 int alg_arg,
7092 data_t *input_data,
7093 int signature_size_arg,
7094 int expected_status_arg)
7095{
7096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7097 psa_key_type_t key_type = key_type_arg;
7098 psa_algorithm_t alg = alg_arg;
7099 size_t signature_size = signature_size_arg;
7100 psa_status_t actual_status;
7101 psa_status_t expected_status = expected_status_arg;
7102 unsigned char *signature = NULL;
7103 size_t signature_length = 0xdeadbeef;
7104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7105
7106 ASSERT_ALLOC(signature, signature_size);
7107
7108 PSA_ASSERT(psa_crypto_init());
7109
7110 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7111 psa_set_key_algorithm(&attributes, alg);
7112 psa_set_key_type(&attributes, key_type);
7113
7114 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7115 &key));
7116
7117 actual_status = psa_sign_message(key, alg,
7118 input_data->x, input_data->len,
7119 signature, signature_size,
7120 &signature_length);
7121 TEST_EQUAL(actual_status, expected_status);
7122 /* The value of *signature_length is unspecified on error, but
7123 * whatever it is, it should be less than signature_size, so that
7124 * if the caller tries to read *signature_length bytes without
7125 * checking the error code then they don't overflow a buffer. */
7126 TEST_LE_U(signature_length, signature_size);
7127
7128exit:
7129 psa_reset_key_attributes(&attributes);
7130 psa_destroy_key(key);
7131 mbedtls_free(signature);
7132 PSA_DONE();
7133}
7134/* END_CASE */
7135
7136/* BEGIN_CASE */
7137void sign_verify_message(int key_type_arg,
7138 data_t *key_data,
7139 int alg_arg,
7140 data_t *input_data)
7141{
7142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7143 psa_key_type_t key_type = key_type_arg;
7144 psa_algorithm_t alg = alg_arg;
7145 size_t key_bits;
7146 unsigned char *signature = NULL;
7147 size_t signature_size;
7148 size_t signature_length = 0xdeadbeef;
7149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7150
7151 PSA_ASSERT(psa_crypto_init());
7152
7153 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7154 PSA_KEY_USAGE_VERIFY_MESSAGE);
7155 psa_set_key_algorithm(&attributes, alg);
7156 psa_set_key_type(&attributes, key_type);
7157
7158 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7159 &key));
7160 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7161 key_bits = psa_get_key_bits(&attributes);
7162
7163 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7164 TEST_ASSERT(signature_size != 0);
7165 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7166 ASSERT_ALLOC(signature, signature_size);
7167
7168 PSA_ASSERT(psa_sign_message(key, alg,
7169 input_data->x, input_data->len,
7170 signature, signature_size,
7171 &signature_length));
7172 TEST_LE_U(signature_length, signature_size);
7173 TEST_ASSERT(signature_length > 0);
7174
7175 PSA_ASSERT(psa_verify_message(key, alg,
7176 input_data->x, input_data->len,
7177 signature, signature_length));
7178
7179 if (input_data->len != 0) {
7180 /* Flip a bit in the input and verify that the signature is now
7181 * detected as invalid. Flip a bit at the beginning, not at the end,
7182 * because ECDSA may ignore the last few bits of the input. */
7183 input_data->x[0] ^= 1;
7184 TEST_EQUAL(psa_verify_message(key, alg,
7185 input_data->x, input_data->len,
7186 signature, signature_length),
7187 PSA_ERROR_INVALID_SIGNATURE);
7188 }
7189
7190exit:
7191 psa_reset_key_attributes(&attributes);
7192
7193 psa_destroy_key(key);
7194 mbedtls_free(signature);
7195 PSA_DONE();
7196}
7197/* END_CASE */
7198
7199/* BEGIN_CASE */
7200void verify_message(int key_type_arg,
7201 data_t *key_data,
7202 int alg_arg,
7203 data_t *input_data,
7204 data_t *signature_data)
7205{
7206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7207 psa_key_type_t key_type = key_type_arg;
7208 psa_algorithm_t alg = alg_arg;
7209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7210
7211 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7212
7213 PSA_ASSERT(psa_crypto_init());
7214
7215 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7216 psa_set_key_algorithm(&attributes, alg);
7217 psa_set_key_type(&attributes, key_type);
7218
7219 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7220 &key));
7221
7222 PSA_ASSERT(psa_verify_message(key, alg,
7223 input_data->x, input_data->len,
7224 signature_data->x, signature_data->len));
7225
7226exit:
7227 psa_reset_key_attributes(&attributes);
7228 psa_destroy_key(key);
7229 PSA_DONE();
7230}
7231/* END_CASE */
7232
7233/* BEGIN_CASE */
7234void verify_message_fail(int key_type_arg,
7235 data_t *key_data,
7236 int alg_arg,
7237 data_t *hash_data,
7238 data_t *signature_data,
7239 int expected_status_arg)
7240{
7241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7242 psa_key_type_t key_type = key_type_arg;
7243 psa_algorithm_t alg = alg_arg;
7244 psa_status_t actual_status;
7245 psa_status_t expected_status = expected_status_arg;
7246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7247
7248 PSA_ASSERT(psa_crypto_init());
7249
7250 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7251 psa_set_key_algorithm(&attributes, alg);
7252 psa_set_key_type(&attributes, key_type);
7253
7254 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7255 &key));
7256
7257 actual_status = psa_verify_message(key, alg,
7258 hash_data->x, hash_data->len,
7259 signature_data->x,
7260 signature_data->len);
7261 TEST_EQUAL(actual_status, expected_status);
7262
7263exit:
7264 psa_reset_key_attributes(&attributes);
7265 psa_destroy_key(key);
7266 PSA_DONE();
7267}
7268/* END_CASE */
7269
7270/* BEGIN_CASE */
7271void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02007272 data_t *key_data,
7273 int alg_arg,
7274 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 data_t *label,
7276 int expected_output_length_arg,
7277 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02007278{
Ronald Cron5425a212020-08-04 14:58:35 +02007279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007280 psa_key_type_t key_type = key_type_arg;
7281 psa_algorithm_t alg = alg_arg;
7282 size_t expected_output_length = expected_output_length_arg;
7283 size_t key_bits;
7284 unsigned char *output = NULL;
7285 size_t output_size;
7286 size_t output_length = ~0;
7287 psa_status_t actual_status;
7288 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007290
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007292
Gilles Peskine656896e2018-06-29 19:12:28 +02007293 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
7295 psa_set_key_algorithm(&attributes, alg);
7296 psa_set_key_type(&attributes, key_type);
7297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7298 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02007299
7300 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7302 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007303
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7305 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7306 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02007307
7308 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 actual_status = psa_asymmetric_encrypt(key, alg,
7310 input_data->x, input_data->len,
7311 label->x, label->len,
7312 output, output_size,
7313 &output_length);
7314 TEST_EQUAL(actual_status, expected_status);
7315 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine656896e2018-06-29 19:12:28 +02007316
Gilles Peskine68428122018-06-30 18:42:41 +02007317 /* If the label is empty, the test framework puts a non-null pointer
7318 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007320 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (output_size != 0) {
7322 memset(output, 0, output_size);
7323 }
7324 actual_status = psa_asymmetric_encrypt(key, alg,
7325 input_data->x, input_data->len,
7326 NULL, label->len,
7327 output, output_size,
7328 &output_length);
7329 TEST_EQUAL(actual_status, expected_status);
7330 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007331 }
7332
Gilles Peskine656896e2018-06-29 19:12:28 +02007333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007334 /*
7335 * Key attributes may have been returned by psa_get_key_attributes()
7336 * thus reset them as required.
7337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007339
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 psa_destroy_key(key);
7341 mbedtls_free(output);
7342 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02007343}
7344/* END_CASE */
7345
7346/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007347void asymmetric_encrypt_decrypt(int key_type_arg,
7348 data_t *key_data,
7349 int alg_arg,
7350 data_t *input_data,
7351 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007352{
Ronald Cron5425a212020-08-04 14:58:35 +02007353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007354 psa_key_type_t key_type = key_type_arg;
7355 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007356 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007357 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007358 size_t output_size;
7359 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007360 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007361 size_t output2_size;
7362 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007366
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
7368 psa_set_key_algorithm(&attributes, alg);
7369 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007370
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7372 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007373
7374 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7376 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007377
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7379 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7380 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007381
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007382 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 TEST_LE_U(output2_size,
7384 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
7385 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7386 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007387
Gilles Peskineeebd7382018-06-08 18:11:54 +02007388 /* We test encryption by checking that encrypt-then-decrypt gives back
7389 * the original plaintext because of the non-optional random
7390 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
7392 input_data->x, input_data->len,
7393 label->x, label->len,
7394 output, output_size,
7395 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007396 /* We don't know what ciphertext length to expect, but check that
7397 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007399
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7401 output, output_length,
7402 label->x, label->len,
7403 output2, output2_size,
7404 &output2_length));
7405 ASSERT_COMPARE(input_data->x, input_data->len,
7406 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007407
7408exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007409 /*
7410 * Key attributes may have been returned by psa_get_key_attributes()
7411 * thus reset them as required.
7412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007414
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 psa_destroy_key(key);
7416 mbedtls_free(output);
7417 mbedtls_free(output2);
7418 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007419}
7420/* END_CASE */
7421
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007423void asymmetric_decrypt(int key_type_arg,
7424 data_t *key_data,
7425 int alg_arg,
7426 data_t *input_data,
7427 data_t *label,
7428 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007429{
Ronald Cron5425a212020-08-04 14:58:35 +02007430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007431 psa_key_type_t key_type = key_type_arg;
7432 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007433 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007434 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007435 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007436 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007438
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7442 psa_set_key_algorithm(&attributes, alg);
7443 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7446 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007447
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7449 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007450
7451 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7453 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7454 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007455
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7457 input_data->x, input_data->len,
7458 label->x, label->len,
7459 output,
7460 output_size,
7461 &output_length));
7462 ASSERT_COMPARE(expected_data->x, expected_data->len,
7463 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007464
Gilles Peskine68428122018-06-30 18:42:41 +02007465 /* If the label is empty, the test framework puts a non-null pointer
7466 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007468 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 if (output_size != 0) {
7470 memset(output, 0, output_size);
7471 }
7472 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7473 input_data->x, input_data->len,
7474 NULL, label->len,
7475 output,
7476 output_size,
7477 &output_length));
7478 ASSERT_COMPARE(expected_data->x, expected_data->len,
7479 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007480 }
7481
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 psa_reset_key_attributes(&attributes);
7484 psa_destroy_key(key);
7485 mbedtls_free(output);
7486 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007487}
7488/* END_CASE */
7489
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007490/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007491void asymmetric_decrypt_fail(int key_type_arg,
7492 data_t *key_data,
7493 int alg_arg,
7494 data_t *input_data,
7495 data_t *label,
7496 int output_size_arg,
7497 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007498{
Ronald Cron5425a212020-08-04 14:58:35 +02007499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007500 psa_key_type_t key_type = key_type_arg;
7501 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007502 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007503 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007504 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007505 psa_status_t actual_status;
7506 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007510
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007512
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7514 psa_set_key_algorithm(&attributes, alg);
7515 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7518 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 actual_status = psa_asymmetric_decrypt(key, alg,
7521 input_data->x, input_data->len,
7522 label->x, label->len,
7523 output, output_size,
7524 &output_length);
7525 TEST_EQUAL(actual_status, expected_status);
7526 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007527
Gilles Peskine68428122018-06-30 18:42:41 +02007528 /* If the label is empty, the test framework puts a non-null pointer
7529 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007531 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (output_size != 0) {
7533 memset(output, 0, output_size);
7534 }
7535 actual_status = psa_asymmetric_decrypt(key, alg,
7536 input_data->x, input_data->len,
7537 NULL, label->len,
7538 output, output_size,
7539 &output_length);
7540 TEST_EQUAL(actual_status, expected_status);
7541 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02007542 }
7543
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007544exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 psa_reset_key_attributes(&attributes);
7546 psa_destroy_key(key);
7547 mbedtls_free(output);
7548 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007549}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007550/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007551
7552/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007553void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00007554{
7555 /* Test each valid way of initializing the object, except for `= {0}`, as
7556 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7557 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007558 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007559 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007561 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7562 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007563
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007565
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007566 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
7568 PSA_ERROR_BAD_STATE);
7569 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
7570 PSA_ERROR_BAD_STATE);
7571 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
7572 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007573
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007574 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 PSA_ASSERT(psa_key_derivation_abort(&func));
7576 PSA_ASSERT(psa_key_derivation_abort(&init));
7577 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007578}
7579/* END_CASE */
7580
Janos Follath16de4a42019-06-13 16:32:24 +01007581/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007582void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02007583{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007584 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007585 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007586 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02007589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7591 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02007592
7593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 psa_key_derivation_abort(&operation);
7595 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02007596}
7597/* END_CASE */
7598
Janos Follathaf3c2a02019-06-12 12:34:34 +01007599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007600void derive_set_capacity(int alg_arg, int capacity_arg,
7601 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01007602{
7603 psa_algorithm_t alg = alg_arg;
7604 size_t capacity = capacity_arg;
7605 psa_status_t expected_status = expected_status_arg;
7606 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7607
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01007609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01007611
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
7613 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01007614
7615exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 psa_key_derivation_abort(&operation);
7617 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01007618}
7619/* END_CASE */
7620
7621/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007622void derive_input(int alg_arg,
7623 int step_arg1, int key_type_arg1, data_t *input1,
7624 int expected_status_arg1,
7625 int step_arg2, int key_type_arg2, data_t *input2,
7626 int expected_status_arg2,
7627 int step_arg3, int key_type_arg3, data_t *input3,
7628 int expected_status_arg3,
7629 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01007630{
7631 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
7633 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
7634 psa_status_t expected_statuses[] = { expected_status_arg1,
7635 expected_status_arg2,
7636 expected_status_arg3 };
7637 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02007638 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7639 MBEDTLS_SVC_KEY_ID_INIT,
7640 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007641 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7643 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007644 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007645 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007646 psa_status_t expected_output_status = expected_output_status_arg;
7647 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007648
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01007650
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7652 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01007655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
7657 mbedtls_test_set_step(i);
7658 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02007659 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
7661 psa_set_key_type(&attributes, key_types[i]);
7662 PSA_ASSERT(psa_import_key(&attributes,
7663 inputs[i]->x, inputs[i]->len,
7664 &keys[i]));
7665 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
7666 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007667 // When taking a private key as secret input, use key agreement
7668 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
7670 &operation, keys[i]),
7671 expected_statuses[i]);
7672 } else {
7673 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
7674 keys[i]),
7675 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007676 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 } else {
7678 TEST_EQUAL(psa_key_derivation_input_bytes(
7679 &operation, steps[i],
7680 inputs[i]->x, inputs[i]->len),
7681 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007682 }
7683 }
7684
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 if (output_key_type != PSA_KEY_TYPE_NONE) {
7686 psa_reset_key_attributes(&attributes);
7687 psa_set_key_type(&attributes, output_key_type);
7688 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007689 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 psa_key_derivation_output_key(&attributes, &operation,
7691 &output_key);
7692 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007693 uint8_t buffer[1];
7694 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 psa_key_derivation_output_bytes(&operation,
7696 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007697 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007699
Janos Follathaf3c2a02019-06-12 12:34:34 +01007700exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 psa_key_derivation_abort(&operation);
7702 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
7703 psa_destroy_key(keys[i]);
7704 }
7705 psa_destroy_key(output_key);
7706 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01007707}
7708/* END_CASE */
7709
Janos Follathd958bb72019-07-03 15:02:16 +01007710/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007711void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007712{
Janos Follathd958bb72019-07-03 15:02:16 +01007713 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007715 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007716 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007717 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01007719 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007721 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007723 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7724 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007727
Gilles Peskine449bd832023-01-11 14:50:10 +01007728 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007729
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7731 psa_set_key_algorithm(&attributes, alg);
7732 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007733
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 PSA_ASSERT(psa_import_key(&attributes,
7735 key_data, sizeof(key_data),
7736 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007737
7738 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
7740 input1, input1_length,
7741 input2, input2_length,
7742 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01007743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007745
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007746 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7748 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007749
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007751
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
7753 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007754
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007756 psa_key_derivation_abort(&operation);
7757 psa_destroy_key(key);
7758 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007759}
7760/* END_CASE */
7761
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007762/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007763void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007764{
7765 uint8_t output_buffer[16];
7766 size_t buffer_size = 16;
7767 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007768 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007769
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
7771 output_buffer, buffer_size)
7772 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007773
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
7775 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007776
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007778
Gilles Peskine449bd832023-01-11 14:50:10 +01007779 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
7780 output_buffer, buffer_size)
7781 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007782
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
7784 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007785
7786exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007788}
7789/* END_CASE */
7790
7791/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007792void derive_output(int alg_arg,
7793 int step1_arg, data_t *input1, int expected_status_arg1,
7794 int step2_arg, data_t *input2, int expected_status_arg2,
7795 int step3_arg, data_t *input3, int expected_status_arg3,
7796 int step4_arg, data_t *input4, int expected_status_arg4,
7797 data_t *key_agreement_peer_key,
7798 int requested_capacity_arg,
7799 data_t *expected_output1,
7800 data_t *expected_output2,
7801 int other_key_input_type,
7802 int key_input_type,
7803 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007804{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007805 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
7807 data_t *inputs[] = { input1, input2, input3, input4 };
7808 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7809 MBEDTLS_SVC_KEY_ID_INIT,
7810 MBEDTLS_SVC_KEY_ID_INIT,
7811 MBEDTLS_SVC_KEY_ID_INIT };
7812 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
7813 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007814 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007815 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007816 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007818 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007820 size_t output_buffer_size = 0;
7821 uint8_t *output_buffer = NULL;
7822 size_t expected_capacity;
7823 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007824 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7825 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7826 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7827 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007828 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007829 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007830 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007831
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
7833 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007834 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 }
7836 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007837 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007839 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007840 ASSERT_ALLOC(output_buffer, output_buffer_size);
7841 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007842
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007843 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
7845 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
7846 requested_capacity));
7847 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
7848 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02007849 case 0:
7850 break;
7851 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007852 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007853 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 TEST_EQUAL(psa_key_derivation_input_bytes(
7855 &operation, steps[i],
7856 inputs[i]->x, inputs[i]->len),
7857 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007858
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007860 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007862 break;
7863 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
7865 psa_set_key_algorithm(&attributes1, alg);
7866 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007867
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 PSA_ASSERT(psa_import_key(&attributes1,
7869 inputs[i]->x, inputs[i]->len,
7870 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007871
Gilles Peskine449bd832023-01-11 14:50:10 +01007872 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
7873 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
7874 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
7875 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007876 }
7877
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 PSA_ASSERT(psa_key_derivation_input_key(&operation,
7879 steps[i],
7880 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007881 break;
7882 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007883 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007884 break;
7885 }
7886 break;
7887 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007889 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
7891 steps[i],
7892 inputs[i]->x,
7893 inputs[i]->len),
7894 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007895 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007896 case 1: // input key, type DERIVE
7897 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
7899 psa_set_key_algorithm(&attributes2, alg);
7900 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007901
7902 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 if (other_key_input_type == 11) {
7904 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
7905 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 PSA_ASSERT(psa_import_key(&attributes2,
7908 inputs[i]->x, inputs[i]->len,
7909 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007910
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 TEST_EQUAL(psa_key_derivation_input_key(&operation,
7912 steps[i],
7913 keys[i]),
7914 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007915 break;
7916 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
7918 psa_set_key_algorithm(&attributes3, alg);
7919 psa_set_key_type(&attributes3,
7920 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007921
Gilles Peskine449bd832023-01-11 14:50:10 +01007922 PSA_ASSERT(psa_import_key(&attributes3,
7923 inputs[i]->x, inputs[i]->len,
7924 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007925
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 TEST_EQUAL(psa_key_derivation_key_agreement(
7927 &operation,
7928 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7929 keys[i], key_agreement_peer_key->x,
7930 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007931 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007932 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007933 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007934 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007935 }
7936
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007938 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007940 break;
7941 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 TEST_EQUAL(psa_key_derivation_input_bytes(
7943 &operation, steps[i],
7944 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02007945
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02007947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007949 break;
7950 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007951 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007952
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
7954 &current_capacity));
7955 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007956 expected_capacity = requested_capacity;
7957
Gilles Peskine449bd832023-01-11 14:50:10 +01007958 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007959 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7960
7961 /* For output key derivation secret must be provided using
7962 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007963 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007964 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007966
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
7968 psa_set_key_algorithm(&attributes4, alg);
7969 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
7970 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007971
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
7973 &derived_key), expected_status);
7974 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007975 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007977 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007978 status = psa_key_derivation_output_bytes(&operation,
7979 output_buffer, output_sizes[i]);
7980 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007981 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 TEST_ASSERT(status == PSA_SUCCESS ||
7983 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007984 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 } else if (expected_capacity == 0 ||
7986 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007987 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007989 expected_capacity = 0;
7990 continue;
7991 }
7992 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 PSA_ASSERT(status);
7994 if (output_sizes[i] != 0) {
7995 ASSERT_COMPARE(output_buffer, output_sizes[i],
7996 expected_outputs[i], output_sizes[i]);
7997 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007998 /* Check the operation status. */
7999 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8001 &current_capacity));
8002 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008003 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008006
8007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008008 mbedtls_free(output_buffer);
8009 psa_key_derivation_abort(&operation);
8010 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8011 psa_destroy_key(keys[i]);
8012 }
8013 psa_destroy_key(derived_key);
8014 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008015}
8016/* END_CASE */
8017
8018/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008019void derive_full(int alg_arg,
8020 data_t *key_data,
8021 data_t *input1,
8022 data_t *input2,
8023 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008024{
Ronald Cron5425a212020-08-04 14:58:35 +02008025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008026 psa_algorithm_t alg = alg_arg;
8027 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008028 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008029 unsigned char output_buffer[16];
8030 size_t expected_capacity = requested_capacity;
8031 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008033
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008035
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8037 psa_set_key_algorithm(&attributes, alg);
8038 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008039
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8041 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008042
Gilles Peskine449bd832023-01-11 14:50:10 +01008043 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8044 input1->x, input1->len,
8045 input2->x, input2->len,
8046 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008047 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008049
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8051 &current_capacity));
8052 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008053
8054 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008055 while (current_capacity > 0) {
8056 size_t read_size = sizeof(output_buffer);
8057 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008058 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 }
8060 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8061 output_buffer,
8062 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008063 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008064 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8065 &current_capacity));
8066 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008067 }
8068
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008069 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008070 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8071 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008072
Gilles Peskine449bd832023-01-11 14:50:10 +01008073 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008074
8075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 psa_key_derivation_abort(&operation);
8077 psa_destroy_key(key);
8078 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008079}
8080/* END_CASE */
8081
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008082/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008083void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8084 int derivation_step,
8085 int capacity, int expected_capacity_status_arg,
8086 data_t *expected_output,
8087 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008088{
8089 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8090 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008091 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008092 uint8_t *output_buffer = NULL;
8093 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008094 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8095 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8096 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008097
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 ASSERT_ALLOC(output_buffer, expected_output->len);
8099 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008100
Gilles Peskine449bd832023-01-11 14:50:10 +01008101 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8102 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8103 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008104
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8106 step, input->x, input->len),
8107 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008108
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008112
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8114 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008115
Gilles Peskine449bd832023-01-11 14:50:10 +01008116 TEST_EQUAL(status, expected_output_status);
8117 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8118 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8119 expected_output->len);
8120 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008121
8122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 mbedtls_free(output_buffer);
8124 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008125 PSA_DONE();
8126}
8127/* END_CASE */
8128
Janos Follathe60c9052019-07-03 13:51:30 +01008129/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008130void derive_key_exercise(int alg_arg,
8131 data_t *key_data,
8132 data_t *input1,
8133 data_t *input2,
8134 int derived_type_arg,
8135 int derived_bits_arg,
8136 int derived_usage_arg,
8137 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008138{
Ronald Cron5425a212020-08-04 14:58:35 +02008139 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8140 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008141 psa_algorithm_t alg = alg_arg;
8142 psa_key_type_t derived_type = derived_type_arg;
8143 size_t derived_bits = derived_bits_arg;
8144 psa_key_usage_t derived_usage = derived_usage_arg;
8145 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008147 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008149 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008150
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008152
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8154 psa_set_key_algorithm(&attributes, alg);
8155 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8156 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8157 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008158
8159 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008160 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8161 input1->x, input1->len,
8162 input2->x, input2->len,
8163 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 }
Janos Follathe60c9052019-07-03 13:51:30 +01008166
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 psa_set_key_usage_flags(&attributes, derived_usage);
8168 psa_set_key_algorithm(&attributes, derived_alg);
8169 psa_set_key_type(&attributes, derived_type);
8170 psa_set_key_bits(&attributes, derived_bits);
8171 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8172 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008173
8174 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008175 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8176 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8177 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008178
8179 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008180 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008183
8184exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008185 /*
8186 * Key attributes may have been returned by psa_get_key_attributes()
8187 * thus reset them as required.
8188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008189 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008190
Gilles Peskine449bd832023-01-11 14:50:10 +01008191 psa_key_derivation_abort(&operation);
8192 psa_destroy_key(base_key);
8193 psa_destroy_key(derived_key);
8194 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008195}
8196/* END_CASE */
8197
Janos Follath42fd8882019-07-03 14:17:09 +01008198/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008199void derive_key_export(int alg_arg,
8200 data_t *key_data,
8201 data_t *input1,
8202 data_t *input2,
8203 int bytes1_arg,
8204 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008205{
Ronald Cron5425a212020-08-04 14:58:35 +02008206 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8207 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008208 psa_algorithm_t alg = alg_arg;
8209 size_t bytes1 = bytes1_arg;
8210 size_t bytes2 = bytes2_arg;
8211 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008212 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008213 uint8_t *output_buffer = NULL;
8214 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008215 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8216 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008217 size_t length;
8218
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 ASSERT_ALLOC(output_buffer, capacity);
8220 ASSERT_ALLOC(export_buffer, capacity);
8221 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8224 psa_set_key_algorithm(&base_attributes, alg);
8225 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8226 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8227 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008228
8229 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8231 input1->x, input1->len,
8232 input2->x, input2->len,
8233 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 }
Janos Follath42fd8882019-07-03 14:17:09 +01008236
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8238 output_buffer,
8239 capacity));
8240 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008241
8242 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8244 input1->x, input1->len,
8245 input2->x, input2->len,
8246 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008247 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 }
Janos Follath42fd8882019-07-03 14:17:09 +01008249
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8251 psa_set_key_algorithm(&derived_attributes, 0);
8252 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
8253 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
8254 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8255 &derived_key));
8256 PSA_ASSERT(psa_export_key(derived_key,
8257 export_buffer, bytes1,
8258 &length));
8259 TEST_EQUAL(length, bytes1);
8260 PSA_ASSERT(psa_destroy_key(derived_key));
8261 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
8262 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8263 &derived_key));
8264 PSA_ASSERT(psa_export_key(derived_key,
8265 export_buffer + bytes1, bytes2,
8266 &length));
8267 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008268
8269 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
8271 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008272
8273exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 mbedtls_free(output_buffer);
8275 mbedtls_free(export_buffer);
8276 psa_key_derivation_abort(&operation);
8277 psa_destroy_key(base_key);
8278 psa_destroy_key(derived_key);
8279 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008280}
8281/* END_CASE */
8282
8283/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008284void derive_key_type(int alg_arg,
8285 data_t *key_data,
8286 data_t *input1,
8287 data_t *input2,
8288 int key_type_arg, int bits_arg,
8289 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008290{
8291 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8292 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8293 const psa_algorithm_t alg = alg_arg;
8294 const psa_key_type_t key_type = key_type_arg;
8295 const size_t bits = bits_arg;
8296 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8297 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008299 uint8_t *export_buffer = NULL;
8300 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8301 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8302 size_t export_length;
8303
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 ASSERT_ALLOC(export_buffer, export_buffer_size);
8305 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008306
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8308 psa_set_key_algorithm(&base_attributes, alg);
8309 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8310 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8311 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008312
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008314 &operation, base_key, alg,
8315 input1->x, input1->len,
8316 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008318 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008320
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8322 psa_set_key_algorithm(&derived_attributes, 0);
8323 psa_set_key_type(&derived_attributes, key_type);
8324 psa_set_key_bits(&derived_attributes, bits);
8325 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8326 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008327
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 PSA_ASSERT(psa_export_key(derived_key,
8329 export_buffer, export_buffer_size,
8330 &export_length));
8331 ASSERT_COMPARE(export_buffer, export_length,
8332 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008333
8334exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 mbedtls_free(export_buffer);
8336 psa_key_derivation_abort(&operation);
8337 psa_destroy_key(base_key);
8338 psa_destroy_key(derived_key);
8339 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008340}
8341/* END_CASE */
8342
8343/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008344void derive_key(int alg_arg,
8345 data_t *key_data, data_t *input1, data_t *input2,
8346 int type_arg, int bits_arg,
8347 int expected_status_arg,
8348 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02008349{
Ronald Cron5425a212020-08-04 14:58:35 +02008350 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8351 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008352 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008353 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008354 size_t bits = bits_arg;
8355 psa_status_t expected_status = expected_status_arg;
8356 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8357 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8358 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8359
Gilles Peskine449bd832023-01-11 14:50:10 +01008360 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02008361
Gilles Peskine449bd832023-01-11 14:50:10 +01008362 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8363 psa_set_key_algorithm(&base_attributes, alg);
8364 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8365 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8366 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02008367
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8369 input1->x, input1->len,
8370 input2->x, input2->len,
8371 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02008372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 }
Gilles Peskinec744d992019-07-30 17:26:54 +02008374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8376 psa_set_key_algorithm(&derived_attributes, 0);
8377 psa_set_key_type(&derived_attributes, type);
8378 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008379
8380 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 psa_key_derivation_output_key(&derived_attributes,
8382 &operation,
8383 &derived_key);
8384 if (is_large_output > 0) {
8385 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8386 }
8387 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02008388
8389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 psa_key_derivation_abort(&operation);
8391 psa_destroy_key(base_key);
8392 psa_destroy_key(derived_key);
8393 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02008394}
8395/* END_CASE */
8396
8397/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008398void key_agreement_setup(int alg_arg,
8399 int our_key_type_arg, int our_key_alg_arg,
8400 data_t *our_key_data, data_t *peer_key_data,
8401 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02008402{
Ronald Cron5425a212020-08-04 14:58:35 +02008403 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008404 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008405 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008406 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008407 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008409 psa_status_t expected_status = expected_status_arg;
8410 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008411
Gilles Peskine449bd832023-01-11 14:50:10 +01008412 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02008413
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8415 psa_set_key_algorithm(&attributes, our_key_alg);
8416 psa_set_key_type(&attributes, our_key_type);
8417 PSA_ASSERT(psa_import_key(&attributes,
8418 our_key_data->x, our_key_data->len,
8419 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008420
Gilles Peskine77f40d82019-04-11 21:27:06 +02008421 /* The tests currently include inputs that should fail at either step.
8422 * Test cases that fail at the setup step should be changed to call
8423 * key_derivation_setup instead, and this function should be renamed
8424 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008425 status = psa_key_derivation_setup(&operation, alg);
8426 if (status == PSA_SUCCESS) {
8427 TEST_EQUAL(psa_key_derivation_key_agreement(
8428 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8429 our_key,
8430 peer_key_data->x, peer_key_data->len),
8431 expected_status);
8432 } else {
8433 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008434 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008435
8436exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 psa_key_derivation_abort(&operation);
8438 psa_destroy_key(our_key);
8439 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008440}
8441/* END_CASE */
8442
8443/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008444void raw_key_agreement(int alg_arg,
8445 int our_key_type_arg, data_t *our_key_data,
8446 data_t *peer_key_data,
8447 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008448{
Ronald Cron5425a212020-08-04 14:58:35 +02008449 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008450 psa_algorithm_t alg = alg_arg;
8451 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008453 unsigned char *output = NULL;
8454 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008455 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008456
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02008458
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8460 psa_set_key_algorithm(&attributes, alg);
8461 psa_set_key_type(&attributes, our_key_type);
8462 PSA_ASSERT(psa_import_key(&attributes,
8463 our_key_data->x, our_key_data->len,
8464 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02008465
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
8467 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008468
Gilles Peskine992bee82022-04-13 23:25:52 +02008469 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 TEST_LE_U(expected_output->len,
8471 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
8472 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
8473 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02008474
8475 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 ASSERT_ALLOC(output, expected_output->len);
8477 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8478 peer_key_data->x, peer_key_data->len,
8479 output, expected_output->len,
8480 &output_length));
8481 ASSERT_COMPARE(output, output_length,
8482 expected_output->x, expected_output->len);
8483 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008484 output = NULL;
8485 output_length = ~0;
8486
8487 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 ASSERT_ALLOC(output, expected_output->len + 1);
8489 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8490 peer_key_data->x, peer_key_data->len,
8491 output, expected_output->len + 1,
8492 &output_length));
8493 ASSERT_COMPARE(output, output_length,
8494 expected_output->x, expected_output->len);
8495 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008496 output = NULL;
8497 output_length = ~0;
8498
8499 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 ASSERT_ALLOC(output, expected_output->len - 1);
8501 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
8502 peer_key_data->x, peer_key_data->len,
8503 output, expected_output->len - 1,
8504 &output_length),
8505 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02008506 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 TEST_LE_U(output_length, expected_output->len - 1);
8508 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008509 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008510
8511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 mbedtls_free(output);
8513 psa_destroy_key(our_key);
8514 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02008515}
8516/* END_CASE */
8517
8518/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008519void key_agreement_capacity(int alg_arg,
8520 int our_key_type_arg, data_t *our_key_data,
8521 data_t *peer_key_data,
8522 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02008523{
Ronald Cron5425a212020-08-04 14:58:35 +02008524 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008525 psa_algorithm_t alg = alg_arg;
8526 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008527 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008529 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008530 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008531
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008533
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8535 psa_set_key_algorithm(&attributes, alg);
8536 psa_set_key_type(&attributes, our_key_type);
8537 PSA_ASSERT(psa_import_key(&attributes,
8538 our_key_data->x, our_key_data->len,
8539 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008540
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8542 PSA_ASSERT(psa_key_derivation_key_agreement(
8543 &operation,
8544 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8545 peer_key_data->x, peer_key_data->len));
8546 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008547 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8549 PSA_KEY_DERIVATION_INPUT_INFO,
8550 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008551 }
Gilles Peskine59685592018-09-18 12:11:34 +02008552
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008553 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 PSA_ASSERT(psa_key_derivation_get_capacity(
8555 &operation, &actual_capacity));
8556 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02008557
Gilles Peskinebf491972018-10-25 22:36:12 +02008558 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008559 while (actual_capacity > sizeof(output)) {
8560 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8561 output, sizeof(output)));
8562 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02008563 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8565 output, actual_capacity));
8566 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
8567 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02008568
Gilles Peskine59685592018-09-18 12:11:34 +02008569exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 psa_key_derivation_abort(&operation);
8571 psa_destroy_key(our_key);
8572 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02008573}
8574/* END_CASE */
8575
8576/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008577void key_agreement_output(int alg_arg,
8578 int our_key_type_arg, data_t *our_key_data,
8579 data_t *peer_key_data,
8580 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02008581{
Ronald Cron5425a212020-08-04 14:58:35 +02008582 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008583 psa_algorithm_t alg = alg_arg;
8584 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008585 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008587 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008588
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
8590 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02008591
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008593
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8595 psa_set_key_algorithm(&attributes, alg);
8596 psa_set_key_type(&attributes, our_key_type);
8597 PSA_ASSERT(psa_import_key(&attributes,
8598 our_key_data->x, our_key_data->len,
8599 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008600
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8602 PSA_ASSERT(psa_key_derivation_key_agreement(
8603 &operation,
8604 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8605 peer_key_data->x, peer_key_data->len));
8606 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008607 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8609 PSA_KEY_DERIVATION_INPUT_INFO,
8610 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008611 }
Gilles Peskine59685592018-09-18 12:11:34 +02008612
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8614 actual_output,
8615 expected_output1->len));
8616 ASSERT_COMPARE(actual_output, expected_output1->len,
8617 expected_output1->x, expected_output1->len);
8618 if (expected_output2->len != 0) {
8619 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8620 actual_output,
8621 expected_output2->len));
8622 ASSERT_COMPARE(actual_output, expected_output2->len,
8623 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008624 }
Gilles Peskine59685592018-09-18 12:11:34 +02008625
8626exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 psa_key_derivation_abort(&operation);
8628 psa_destroy_key(our_key);
8629 PSA_DONE();
8630 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02008631}
8632/* END_CASE */
8633
8634/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008635void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02008636{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008637 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008638 unsigned char *output = NULL;
8639 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008640 size_t i;
8641 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008642
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00008644
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 ASSERT_ALLOC(output, bytes);
8646 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02008647
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02008649
Gilles Peskinea50d7392018-06-21 10:22:13 +02008650 /* Run several times, to ensure that every output byte will be
8651 * nonzero at least once with overwhelming probability
8652 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 for (run = 0; run < 10; run++) {
8654 if (bytes != 0) {
8655 memset(output, 0, bytes);
8656 }
8657 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02008658
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 for (i = 0; i < bytes; i++) {
8660 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02008661 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008663 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008664 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008665
8666 /* Check that every byte was changed to nonzero at least once. This
8667 * validates that psa_generate_random is overwriting every byte of
8668 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 for (i = 0; i < bytes; i++) {
8670 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02008671 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008672
8673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 PSA_DONE();
8675 mbedtls_free(output);
8676 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02008677}
8678/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008679
8680/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008681void generate_key(int type_arg,
8682 int bits_arg,
8683 int usage_arg,
8684 int alg_arg,
8685 int expected_status_arg,
8686 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02008687{
Ronald Cron5425a212020-08-04 14:58:35 +02008688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008689 psa_key_type_t type = type_arg;
8690 psa_key_usage_t usage = usage_arg;
8691 size_t bits = bits_arg;
8692 psa_algorithm_t alg = alg_arg;
8693 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008694 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008695 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008696
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 psa_set_key_usage_flags(&attributes, usage);
8700 psa_set_key_algorithm(&attributes, alg);
8701 psa_set_key_type(&attributes, type);
8702 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008703
8704 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008706
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 if (is_large_key > 0) {
8708 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8709 }
8710 TEST_EQUAL(status, expected_status);
8711 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008712 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008714
8715 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
8717 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
8718 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008719
Gilles Peskine818ca122018-06-20 18:16:48 +02008720 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02008722 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008724
8725exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008726 /*
8727 * Key attributes may have been returned by psa_get_key_attributes()
8728 * thus reset them as required.
8729 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008731
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 psa_destroy_key(key);
8733 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02008734}
8735/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008736
Ronald Cronee414c72021-03-18 18:50:08 +01008737/* 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 +01008738void generate_key_rsa(int bits_arg,
8739 data_t *e_arg,
8740 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02008741{
Ronald Cron5425a212020-08-04 14:58:35 +02008742 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008743 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008744 size_t bits = bits_arg;
8745 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8746 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8747 psa_status_t expected_status = expected_status_arg;
8748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8749 uint8_t *exported = NULL;
8750 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008752 size_t exported_length = SIZE_MAX;
8753 uint8_t *e_read_buffer = NULL;
8754 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008756 size_t e_read_length = SIZE_MAX;
8757
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 if (e_arg->len == 0 ||
8759 (e_arg->len == 3 &&
8760 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008761 is_default_public_exponent = 1;
8762 e_read_size = 0;
8763 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 ASSERT_ALLOC(e_read_buffer, e_read_size);
8765 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02008768
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 psa_set_key_usage_flags(&attributes, usage);
8770 psa_set_key_algorithm(&attributes, alg);
8771 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
8772 e_arg->x, e_arg->len));
8773 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008774
8775 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
8777 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008780
8781 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8783 TEST_EQUAL(psa_get_key_type(&attributes), type);
8784 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
8785 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
8786 e_read_buffer, e_read_size,
8787 &e_read_length));
8788 if (is_default_public_exponent) {
8789 TEST_EQUAL(e_read_length, 0);
8790 } else {
8791 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
8792 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008793
8794 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008796 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008798
8799 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 PSA_ASSERT(psa_export_public_key(key,
8801 exported, exported_size,
8802 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02008803 {
8804 uint8_t *p = exported;
8805 uint8_t *end = exported + exported_length;
8806 size_t len;
8807 /* RSAPublicKey ::= SEQUENCE {
8808 * modulus INTEGER, -- n
8809 * publicExponent INTEGER } -- e
8810 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
8812 MBEDTLS_ASN1_SEQUENCE |
8813 MBEDTLS_ASN1_CONSTRUCTED));
8814 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
8815 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
8816 MBEDTLS_ASN1_INTEGER));
8817 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008818 ++p;
8819 --len;
8820 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 if (e_arg->len == 0) {
8822 TEST_EQUAL(len, 3);
8823 TEST_EQUAL(p[0], 1);
8824 TEST_EQUAL(p[1], 0);
8825 TEST_EQUAL(p[2], 1);
8826 } else {
8827 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008828 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008829 }
8830
8831exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008832 /*
8833 * Key attributes may have been returned by psa_get_key_attributes() or
8834 * set by psa_set_key_domain_parameters() thus reset them as required.
8835 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008837
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 psa_destroy_key(key);
8839 PSA_DONE();
8840 mbedtls_free(e_read_buffer);
8841 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008842}
8843/* END_CASE */
8844
Darryl Greend49a4992018-06-18 17:27:26 +01008845/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01008846void persistent_key_load_key_from_storage(data_t *data,
8847 int type_arg, int bits_arg,
8848 int usage_flags_arg, int alg_arg,
8849 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01008850{
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8854 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008855 psa_key_type_t type = type_arg;
8856 size_t bits = bits_arg;
8857 psa_key_usage_t usage_flags = usage_flags_arg;
8858 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008859 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008860 unsigned char *first_export = NULL;
8861 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01008863 size_t first_exported_length;
8864 size_t second_exported_length;
8865
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8867 ASSERT_ALLOC(first_export, export_size);
8868 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008869 }
Darryl Greend49a4992018-06-18 17:27:26 +01008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01008872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 psa_set_key_id(&attributes, key_id);
8874 psa_set_key_usage_flags(&attributes, usage_flags);
8875 psa_set_key_algorithm(&attributes, alg);
8876 psa_set_key_type(&attributes, type);
8877 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01008878
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00008880 case IMPORT_KEY:
8881 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
8883 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00008884 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008885
Darryl Green0c6575a2018-11-07 16:05:30 +00008886 case GENERATE_KEY:
8887 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00008889 break;
8890
8891 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008892#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 {
8894 /* Create base key */
8895 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
8896 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8897 psa_set_key_usage_flags(&base_attributes,
8898 PSA_KEY_USAGE_DERIVE);
8899 psa_set_key_algorithm(&base_attributes, derive_alg);
8900 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8901 PSA_ASSERT(psa_import_key(&base_attributes,
8902 data->x, data->len,
8903 &base_key));
8904 /* Derive a key. */
8905 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
8906 PSA_ASSERT(psa_key_derivation_input_key(
8907 &operation,
8908 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
8909 PSA_ASSERT(psa_key_derivation_input_bytes(
8910 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
8911 NULL, 0));
8912 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
8913 &operation,
8914 &key));
8915 PSA_ASSERT(psa_key_derivation_abort(&operation));
8916 PSA_ASSERT(psa_destroy_key(base_key));
8917 base_key = MBEDTLS_SVC_KEY_ID_INIT;
8918 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008919#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008921#endif
8922 break;
8923
8924 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008926 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008927 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01008929
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008930 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8932 PSA_ASSERT(psa_export_key(key,
8933 first_export, export_size,
8934 &first_exported_length));
8935 if (generation_method == IMPORT_KEY) {
8936 ASSERT_COMPARE(data->x, data->len,
8937 first_export, first_exported_length);
8938 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008939 }
Darryl Greend49a4992018-06-18 17:27:26 +01008940
8941 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008943 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01008945
Darryl Greend49a4992018-06-18 17:27:26 +01008946 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8948 TEST_ASSERT(mbedtls_svc_key_id_equal(
8949 psa_get_key_id(&attributes), key_id));
8950 TEST_EQUAL(psa_get_key_lifetime(&attributes),
8951 PSA_KEY_LIFETIME_PERSISTENT);
8952 TEST_EQUAL(psa_get_key_type(&attributes), type);
8953 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
8954 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
8955 mbedtls_test_update_key_usage_flags(usage_flags));
8956 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01008957
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008958 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8960 PSA_ASSERT(psa_export_key(key,
8961 second_export, export_size,
8962 &second_exported_length));
8963 ASSERT_COMPARE(first_export, first_exported_length,
8964 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00008965 }
8966
8967 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00008969 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 }
Darryl Greend49a4992018-06-18 17:27:26 +01008971
8972exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008973 /*
8974 * Key attributes may have been returned by psa_get_key_attributes()
8975 * thus reset them as required.
8976 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 mbedtls_free(first_export);
8980 mbedtls_free(second_export);
8981 psa_key_derivation_abort(&operation);
8982 psa_destroy_key(base_key);
8983 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008984 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008985}
8986/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008987
Neil Armstronga557cb82022-06-10 08:58:32 +02008988/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008989void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8990 int primitive_arg, int hash_arg, int role_arg,
8991 int test_input, data_t *pw_data,
8992 int inj_err_type_arg,
8993 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008994{
8995 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8996 psa_pake_operation_t operation = psa_pake_operation_init();
8997 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008998 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008999 psa_key_type_t key_type_pw = key_type_pw_arg;
9000 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009001 psa_algorithm_t hash_alg = hash_arg;
9002 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009003 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9004 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009005 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9006 psa_status_t expected_error = expected_error_arg;
9007 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009008 unsigned char *output_buffer = NULL;
9009 size_t output_len = 0;
9010
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009012
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009013 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 PSA_PAKE_STEP_KEY_SHARE);
9015 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009016
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 if (pw_data->len > 0) {
9018 psa_set_key_usage_flags(&attributes, key_usage_pw);
9019 psa_set_key_algorithm(&attributes, alg);
9020 psa_set_key_type(&attributes, key_type_pw);
9021 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9022 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009023 }
9024
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9026 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9027 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009028
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9032 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9033 expected_error);
9034 PSA_ASSERT(psa_pake_abort(&operation));
9035 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9036 expected_error);
9037 PSA_ASSERT(psa_pake_abort(&operation));
9038 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9039 expected_error);
9040 PSA_ASSERT(psa_pake_abort(&operation));
9041 TEST_EQUAL(psa_pake_set_role(&operation, role),
9042 expected_error);
9043 PSA_ASSERT(psa_pake_abort(&operation));
9044 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9045 NULL, 0, NULL),
9046 expected_error);
9047 PSA_ASSERT(psa_pake_abort(&operation));
9048 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9049 expected_error);
9050 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009051 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009052 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009053
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 status = psa_pake_setup(&operation, &cipher_suite);
9055 if (status != PSA_SUCCESS) {
9056 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009057 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009058 }
9059
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9061 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9062 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009063 goto exit;
9064 }
9065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 status = psa_pake_set_role(&operation, role);
9067 if (status != PSA_SUCCESS) {
9068 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009069 goto exit;
9070 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009071
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 if (pw_data->len > 0) {
9073 status = psa_pake_set_password_key(&operation, key);
9074 if (status != PSA_SUCCESS) {
9075 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009076 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009077 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009078 }
9079
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9081 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9082 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009083 goto exit;
9084 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009085
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9087 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9088 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009089 goto exit;
9090 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009093 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9095 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009096 goto exit;
9097 }
9098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009100 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9102 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009103 goto exit;
9104 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9107 PSA_PAKE_STEP_KEY_SHARE);
9108 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9109 PSA_PAKE_STEP_ZK_PUBLIC);
9110 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9111 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009112
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 if (test_input) {
9114 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9115 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9116 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009117 goto exit;
9118 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009119
Gilles Peskine449bd832023-01-11 14:50:10 +01009120 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9121 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9122 output_buffer, size_zk_proof),
9123 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009124 goto exit;
9125 }
9126
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9128 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9129 output_buffer, size_zk_proof),
9130 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009131 goto exit;
9132 }
9133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9135 output_buffer, size_key_share);
9136 if (status != PSA_SUCCESS) {
9137 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009138 goto exit;
9139 }
9140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9142 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9143 output_buffer, size_zk_public + 1),
9144 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009145 goto exit;
9146 }
9147
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009149 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9151 output_buffer, size_zk_public + 1);
9152 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9153 output_buffer, size_zk_public),
9154 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009155 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009156 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009157 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9159 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9160 NULL, 0, NULL),
9161 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009162 goto exit;
9163 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009164
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9166 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9167 output_buffer, buf_size, &output_len),
9168 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009169 goto exit;
9170 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9173 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9174 output_buffer, buf_size, &output_len),
9175 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009176 goto exit;
9177 }
9178
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9180 output_buffer, buf_size, &output_len);
9181 if (status != PSA_SUCCESS) {
9182 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009183 goto exit;
9184 }
9185
Gilles Peskine449bd832023-01-11 14:50:10 +01009186 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009187
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9189 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9190 output_buffer, size_zk_public - 1, &output_len),
9191 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01009192 goto exit;
9193 }
9194
Gilles Peskine449bd832023-01-11 14:50:10 +01009195 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009196 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009197 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9198 output_buffer, size_zk_public - 1, &output_len);
9199 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9200 output_buffer, buf_size, &output_len),
9201 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009202 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009203 }
9204 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009205
9206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 PSA_ASSERT(psa_destroy_key(key));
9208 PSA_ASSERT(psa_pake_abort(&operation));
9209 mbedtls_free(output_buffer);
9210 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009211}
9212/* END_CASE */
9213
Neil Armstronga557cb82022-06-10 08:58:32 +02009214/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009215void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
9216 int client_input_first, int inject_error,
9217 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009218{
9219 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9220 psa_pake_operation_t server = psa_pake_operation_init();
9221 psa_pake_operation_t client = psa_pake_operation_init();
9222 psa_algorithm_t alg = alg_arg;
9223 psa_algorithm_t hash_alg = hash_arg;
9224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9226
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9230 psa_set_key_algorithm(&attributes, alg);
9231 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9232 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9233 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9236 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9237 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009238
9239
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9241 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009242
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9244 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009245
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9247 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009248
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 ecjpake_do_round(alg, primitive_arg, &server, &client,
9250 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009251
Gilles Peskine449bd832023-01-11 14:50:10 +01009252 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009253 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009255
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 ecjpake_do_round(alg, primitive_arg, &server, &client,
9257 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009258
9259exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 psa_destroy_key(key);
9261 psa_pake_abort(&server);
9262 psa_pake_abort(&client);
9263 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009264}
9265/* END_CASE */
9266
9267/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009268void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
9269 int derive_alg_arg, data_t *pw_data,
9270 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009271{
9272 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9273 psa_pake_operation_t server = psa_pake_operation_init();
9274 psa_pake_operation_t client = psa_pake_operation_init();
9275 psa_algorithm_t alg = alg_arg;
9276 psa_algorithm_t hash_alg = hash_arg;
9277 psa_algorithm_t derive_alg = derive_alg_arg;
9278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9280 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009282 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009283 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009284 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009285
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009287
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9289 psa_set_key_algorithm(&attributes, alg);
9290 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9291 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9292 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009293
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9295 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9296 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009297
Neil Armstrong1e855602022-06-15 11:32:11 +02009298 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
9300 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02009301
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
9303 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
9304 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
9305 PSA_KEY_DERIVATION_INPUT_SEED,
9306 (const uint8_t *) "", 0));
9307 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
9308 PSA_KEY_DERIVATION_INPUT_SEED,
9309 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02009310 }
9311
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9313 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009314
Gilles Peskine449bd832023-01-11 14:50:10 +01009315 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9316 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009317
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9319 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009320
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
9322 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9323 PSA_ERROR_BAD_STATE);
9324 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9325 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009326 goto exit;
9327 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009328
Neil Armstrongf983caf2022-06-15 15:27:48 +02009329 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 ecjpake_do_round(alg, primitive_arg, &server, &client,
9331 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009332
Gilles Peskine449bd832023-01-11 14:50:10 +01009333 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
9334 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9335 PSA_ERROR_BAD_STATE);
9336 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9337 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009338 goto exit;
9339 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009340
Neil Armstrongf983caf2022-06-15 15:27:48 +02009341 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009342 ecjpake_do_round(alg, primitive_arg, &server, &client,
9343 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009344
Gilles Peskine449bd832023-01-11 14:50:10 +01009345 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
9346 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009347
9348exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009349 psa_key_derivation_abort(&server_derive);
9350 psa_key_derivation_abort(&client_derive);
9351 psa_destroy_key(key);
9352 psa_pake_abort(&server);
9353 psa_pake_abort(&client);
9354 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009355}
9356/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009357
9358/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009359void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009360{
9361 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9362 const size_t bits = 256;
9363 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009365 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01009366 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009367
9368 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9369 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9371 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
9372 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9373 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009374 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9376 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009377
9378 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01009379 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9380 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
9381 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9382 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
9383 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9384 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009385
9386 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9388 PSA_PAKE_OUTPUT_MAX_SIZE);
9389 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9390 PSA_PAKE_OUTPUT_MAX_SIZE);
9391 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9392 PSA_PAKE_OUTPUT_MAX_SIZE);
9393 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9394 PSA_PAKE_INPUT_MAX_SIZE);
9395 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9396 PSA_PAKE_INPUT_MAX_SIZE);
9397 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9398 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009399}
9400/* END_CASE */