blob: 21965cfca1f423c1bb6f601541cfad802bf5e4c7 [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,
Paul Elliott0c683352022-12-16 19:16:56 +00006450 data_t *output_data, int max_ops,
6451 int min_completes, int max_completes)
Paul Elliott712d5122022-12-07 14:03:10 +00006452{
6453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6454 psa_key_type_t key_type = key_type_arg;
6455 psa_algorithm_t alg = alg_arg;
6456 size_t key_bits;
6457 unsigned char *signature = NULL;
6458 size_t signature_size;
6459 size_t signature_length = 0xdeadbeef;
6460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6461 psa_status_t status = PSA_OPERATION_INCOMPLETE;
6462 size_t num_ops = 0;
6463 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006464 size_t num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006465 psa_sign_hash_interruptible_operation_t operation =
6466 psa_sign_hash_interruptible_operation_init();
6467
6468 PSA_ASSERT(psa_crypto_init());
6469
6470 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6471 psa_set_key_algorithm(&attributes, alg);
6472 psa_set_key_type(&attributes, key_type);
6473
6474 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6475 &key));
6476 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6477 key_bits = psa_get_key_bits(&attributes);
6478
6479 /* Allocate a buffer which has the size advertised by the
6480 * library. */
6481 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6482 key_bits, alg);
6483 TEST_ASSERT(signature_size != 0);
6484 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6485 ASSERT_ALLOC(signature, signature_size);
6486
Paul Elliott0c683352022-12-16 19:16:56 +00006487 psa_interruptible_set_max_ops(max_ops);
6488
Paul Elliott712d5122022-12-07 14:03:10 +00006489 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6490 TEST_ASSERT(num_ops_prior == 0);
6491
6492 /* Start performing the signature. */
6493 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6494 input_data->x, input_data->len));
6495
6496 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6497 TEST_ASSERT(num_ops_prior == 0);
6498
6499 /* Continue performing the signature until complete. */
6500 while (status == PSA_OPERATION_INCOMPLETE) {
6501 status = psa_sign_hash_complete(&operation, signature, signature_size,
6502 &signature_length);
6503
Paul Elliott0c683352022-12-16 19:16:56 +00006504 num_completes++;
6505
Paul Elliott712d5122022-12-07 14:03:10 +00006506 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6507 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00006508 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006509
Paul Elliott712d5122022-12-07 14:03:10 +00006510 num_ops_prior = num_ops;
6511 }
6512 }
6513
6514 TEST_ASSERT(status == PSA_SUCCESS);
6515
Paul Elliott0c683352022-12-16 19:16:56 +00006516 TEST_LE_U(min_completes, num_completes);
6517 TEST_LE_U(num_completes, max_completes);
6518
Paul Elliott712d5122022-12-07 14:03:10 +00006519 /* Verify that the signature is what is expected. */
6520 ASSERT_COMPARE(output_data->x, output_data->len,
6521 signature, signature_length);
6522
6523 PSA_ASSERT(psa_sign_hash_abort(&operation));
6524
Paul Elliott59ad9452022-12-18 15:09:02 +00006525 num_ops = psa_sign_hash_get_num_ops(&operation);
6526 TEST_ASSERT(num_ops == 0);
6527
Paul Elliott712d5122022-12-07 14:03:10 +00006528exit:
6529
6530 /*
6531 * Key attributes may have been returned by psa_get_key_attributes()
6532 * thus reset them as required.
6533 */
6534 psa_reset_key_attributes(&attributes);
6535
6536 psa_destroy_key(key);
6537 mbedtls_free(signature);
6538 PSA_DONE();
6539}
6540/* END_CASE */
6541
Gilles Peskine20035e32018-02-03 22:44:14 +01006542/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006543void sign_hash_fail(int key_type_arg, data_t *key_data,
6544 int alg_arg, data_t *input_data,
6545 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006546{
Ronald Cron5425a212020-08-04 14:58:35 +02006547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006548 psa_key_type_t key_type = key_type_arg;
6549 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006550 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006551 psa_status_t actual_status;
6552 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006553 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006554 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6562 psa_set_key_algorithm(&attributes, alg);
6563 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006564
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6566 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006567
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 actual_status = psa_sign_hash(key, alg,
6569 input_data->x, input_data->len,
6570 signature, signature_size,
6571 &signature_length);
6572 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006573 /* The value of *signature_length is unspecified on error, but
6574 * whatever it is, it should be less than signature_size, so that
6575 * if the caller tries to read *signature_length bytes without
6576 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006578
6579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 psa_reset_key_attributes(&attributes);
6581 psa_destroy_key(key);
6582 mbedtls_free(signature);
6583 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006584}
6585/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006586
Paul Elliott91007972022-12-16 12:21:24 +00006587/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6588void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6589 int alg_arg, data_t *input_data,
6590 int signature_size_arg,
6591 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006592 int expected_complete_status_arg,
6593 int max_ops, int min_completes,
6594 int max_completes)
Paul Elliott91007972022-12-16 12:21:24 +00006595{
6596 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6597 psa_key_type_t key_type = key_type_arg;
6598 psa_algorithm_t alg = alg_arg;
6599 size_t signature_size = signature_size_arg;
6600 psa_status_t actual_status;
6601 psa_status_t expected_start_status = expected_start_status_arg;
6602 psa_status_t expected_complete_status = expected_complete_status_arg;
6603 unsigned char *signature = NULL;
6604 size_t signature_length = 0xdeadbeef;
6605 size_t num_ops = 0;
6606 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006607 size_t num_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00006608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6609 psa_sign_hash_interruptible_operation_t operation =
6610 psa_sign_hash_interruptible_operation_init();
6611
6612 ASSERT_ALLOC(signature, signature_size);
6613
6614 PSA_ASSERT(psa_crypto_init());
6615
6616 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6617 psa_set_key_algorithm(&attributes, alg);
6618 psa_set_key_type(&attributes, key_type);
6619
6620 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6621 &key));
6622
Paul Elliott0c683352022-12-16 19:16:56 +00006623 psa_interruptible_set_max_ops(max_ops);
6624
Paul Elliott91007972022-12-16 12:21:24 +00006625 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6626 TEST_ASSERT(num_ops_prior == 0);
6627
6628 /* Start performing the signature. */
6629 actual_status = psa_sign_hash_start(&operation, key, alg,
6630 input_data->x, input_data->len);
6631
6632 TEST_EQUAL(actual_status, expected_start_status);
6633
6634 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6635 TEST_ASSERT(num_ops_prior == 0);
6636
6637 actual_status = PSA_OPERATION_INCOMPLETE;
6638
6639 /* Continue performing the signature until complete. */
6640 while (actual_status == PSA_OPERATION_INCOMPLETE) {
6641 actual_status = psa_sign_hash_complete(&operation, signature,
6642 signature_size,
6643 &signature_length);
6644
Paul Elliott0c683352022-12-16 19:16:56 +00006645 num_completes++;
6646
Paul Elliott91007972022-12-16 12:21:24 +00006647 /* If the psa_sign_hash_start() failed, psa_sign_hash_complete()
6648 * should also fail with bad state. */
6649 if (expected_start_status != PSA_SUCCESS) {
6650 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6651 } else if (actual_status != PSA_OPERATION_INCOMPLETE) {
6652 TEST_EQUAL(actual_status, expected_complete_status);
6653 } else {
6654 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00006655 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006656
Paul Elliott91007972022-12-16 12:21:24 +00006657 num_ops_prior = num_ops;
6658 }
6659 }
6660
6661 PSA_ASSERT(psa_sign_hash_abort(&operation));
6662
Paul Elliott59ad9452022-12-18 15:09:02 +00006663 num_ops = psa_sign_hash_get_num_ops(&operation);
6664 TEST_ASSERT(num_ops == 0);
6665
Paul Elliott91007972022-12-16 12:21:24 +00006666 /* The value of *signature_length is unspecified on error, but
6667 * whatever it is, it should be less than signature_size, so that
6668 * if the caller tries to read *signature_length bytes without
6669 * checking the error code then they don't overflow a buffer. */
6670 TEST_LE_U(signature_length, signature_size);
6671
Paul Elliott0c683352022-12-16 19:16:56 +00006672 TEST_LE_U(min_completes, num_completes);
6673 TEST_LE_U(num_completes, max_completes);
6674
Paul Elliott91007972022-12-16 12:21:24 +00006675exit:
6676 psa_reset_key_attributes(&attributes);
6677 psa_destroy_key(key);
6678 mbedtls_free(signature);
6679 PSA_DONE();
6680}
6681/* END_CASE */
6682
mohammad16038cc1cee2018-03-28 01:21:33 +03006683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006684void sign_verify_hash(int key_type_arg, data_t *key_data,
6685 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006686{
Ronald Cron5425a212020-08-04 14:58:35 +02006687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006688 psa_key_type_t key_type = key_type_arg;
6689 psa_algorithm_t alg = alg_arg;
6690 size_t key_bits;
6691 unsigned char *signature = NULL;
6692 size_t signature_size;
6693 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006694 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006695
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006697
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6699 psa_set_key_algorithm(&attributes, alg);
6700 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6703 &key));
6704 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6705 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006706
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006707 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006708 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6710 key_bits, alg);
6711 TEST_ASSERT(signature_size != 0);
6712 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6713 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006714
6715 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 PSA_ASSERT(psa_sign_hash(key, alg,
6717 input_data->x, input_data->len,
6718 signature, signature_size,
6719 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006720 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 TEST_LE_U(signature_length, signature_size);
6722 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006723
6724 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 PSA_ASSERT(psa_verify_hash(key, alg,
6726 input_data->x, input_data->len,
6727 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006730 /* Flip a bit in the input and verify that the signature is now
6731 * detected as invalid. Flip a bit at the beginning, not at the end,
6732 * because ECDSA may ignore the last few bits of the input. */
6733 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 TEST_EQUAL(psa_verify_hash(key, alg,
6735 input_data->x, input_data->len,
6736 signature, signature_length),
6737 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006738 }
6739
6740exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006741 /*
6742 * Key attributes may have been returned by psa_get_key_attributes()
6743 * thus reset them as required.
6744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006746
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 psa_destroy_key(key);
6748 mbedtls_free(signature);
6749 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006750}
6751/* END_CASE */
6752
Paul Elliott712d5122022-12-07 14:03:10 +00006753/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6754void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006755 int alg_arg, data_t *input_data,
6756 int max_ops, int min_completes,
6757 int max_completes)
Paul Elliott712d5122022-12-07 14:03:10 +00006758{
6759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6760 psa_key_type_t key_type = key_type_arg;
6761 psa_algorithm_t alg = alg_arg;
6762 size_t key_bits;
6763 unsigned char *signature = NULL;
6764 size_t signature_size;
6765 size_t signature_length = 0xdeadbeef;
6766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6767 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliott0c683352022-12-16 19:16:56 +00006768 size_t num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006769 psa_sign_hash_interruptible_operation_t sign_operation =
6770 psa_sign_hash_interruptible_operation_init();
6771 psa_verify_hash_interruptible_operation_t verify_operation =
6772 psa_verify_hash_interruptible_operation_init();
6773
6774 PSA_ASSERT(psa_crypto_init());
6775
Paul Elliott0c683352022-12-16 19:16:56 +00006776 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6777 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006778 psa_set_key_algorithm(&attributes, alg);
6779 psa_set_key_type(&attributes, key_type);
6780
6781 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6782 &key));
6783 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6784 key_bits = psa_get_key_bits(&attributes);
6785
6786 /* Allocate a buffer which has the size advertised by the
6787 * library. */
6788 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6789 key_bits, alg);
6790 TEST_ASSERT(signature_size != 0);
6791 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6792 ASSERT_ALLOC(signature, signature_size);
6793
Paul Elliott0c683352022-12-16 19:16:56 +00006794 psa_interruptible_set_max_ops(max_ops);
6795
Paul Elliott712d5122022-12-07 14:03:10 +00006796 /* Start performing the signature. */
6797 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6798 input_data->x, input_data->len));
6799
6800 /* Continue performing the signature until complete. */
6801 while (status == PSA_OPERATION_INCOMPLETE) {
6802
Paul Elliott0c683352022-12-16 19:16:56 +00006803 status = psa_sign_hash_complete(&sign_operation, signature,
6804 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006805 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006806
6807 num_completes++;
Paul Elliott712d5122022-12-07 14:03:10 +00006808 }
6809
6810 TEST_ASSERT(status == PSA_SUCCESS);
6811
Paul Elliott0c683352022-12-16 19:16:56 +00006812 TEST_LE_U(min_completes, num_completes);
6813 TEST_LE_U(num_completes, max_completes);
6814
Paul Elliott712d5122022-12-07 14:03:10 +00006815 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6816
6817 /* Check that the signature length looks sensible. */
6818 TEST_LE_U(signature_length, signature_size);
6819 TEST_ASSERT(signature_length > 0);
6820
Paul Elliott0c683352022-12-16 19:16:56 +00006821 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006822 status = PSA_OPERATION_INCOMPLETE;
6823
6824 /* Start verification. */
6825 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6826 input_data->x, input_data->len,
6827 signature, signature_length));
6828
6829 /* Continue performing the signature until complete. */
6830 while (status == PSA_OPERATION_INCOMPLETE) {
6831 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00006832
6833 num_completes++;
Paul Elliott712d5122022-12-07 14:03:10 +00006834 }
6835
6836 TEST_ASSERT(status == PSA_SUCCESS);
6837
Paul Elliott0c683352022-12-16 19:16:56 +00006838 TEST_LE_U(min_completes, num_completes);
6839 TEST_LE_U(num_completes, max_completes);
6840
Paul Elliott712d5122022-12-07 14:03:10 +00006841 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6842
6843 verify_operation = psa_verify_hash_interruptible_operation_init();
6844
6845 if (input_data->len != 0) {
6846 /* Flip a bit in the input and verify that the signature is now
6847 * detected as invalid. Flip a bit at the beginning, not at the end,
6848 * because ECDSA may ignore the last few bits of the input. */
6849 input_data->x[0] ^= 1;
6850
6851 status = PSA_OPERATION_INCOMPLETE;
6852
6853 /* Start verification. */
6854 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6855 input_data->x, input_data->len,
6856 signature, signature_length));
6857
6858 /* Continue performing the signature until complete. */
6859 while (status == PSA_OPERATION_INCOMPLETE) {
6860 status = psa_verify_hash_complete(&verify_operation);
6861 }
6862
6863 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
6864 }
6865
6866 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6867
6868exit:
6869 /*
6870 * Key attributes may have been returned by psa_get_key_attributes()
6871 * thus reset them as required.
6872 */
6873 psa_reset_key_attributes(&attributes);
6874
6875 psa_destroy_key(key);
6876 mbedtls_free(signature);
6877 PSA_DONE();
6878}
6879/* END_CASE */
6880
Gilles Peskine9911b022018-06-29 17:30:48 +02006881/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006882void verify_hash(int key_type_arg, data_t *key_data,
6883 int alg_arg, data_t *hash_data,
6884 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006885{
Ronald Cron5425a212020-08-04 14:58:35 +02006886 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006887 psa_key_type_t key_type = key_type_arg;
6888 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006890
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006894
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6896 psa_set_key_algorithm(&attributes, alg);
6897 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006898
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6900 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 PSA_ASSERT(psa_verify_hash(key, alg,
6903 hash_data->x, hash_data->len,
6904 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006905
itayzafrir5c753392018-05-08 11:18:38 +03006906exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 psa_reset_key_attributes(&attributes);
6908 psa_destroy_key(key);
6909 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006910}
6911/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006912
Paul Elliott712d5122022-12-07 14:03:10 +00006913/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6914void verify_hash_interruptible(int key_type_arg, data_t *key_data,
6915 int alg_arg, data_t *hash_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006916 data_t *signature_data, int max_ops,
6917 int min_completes, int max_completes)
Paul Elliott712d5122022-12-07 14:03:10 +00006918{
6919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6920 psa_key_type_t key_type = key_type_arg;
6921 psa_algorithm_t alg = alg_arg;
6922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6923 psa_status_t status = PSA_OPERATION_INCOMPLETE;
6924 size_t num_ops = 0;
6925 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006926 size_t num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006927 psa_verify_hash_interruptible_operation_t operation =
6928 psa_verify_hash_interruptible_operation_init();
6929
6930 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
6931
6932 PSA_ASSERT(psa_crypto_init());
6933
6934 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6935 psa_set_key_algorithm(&attributes, alg);
6936 psa_set_key_type(&attributes, key_type);
6937
6938 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6939 &key));
6940
Paul Elliott0c683352022-12-16 19:16:56 +00006941 psa_interruptible_set_max_ops(max_ops);
6942
Paul Elliott712d5122022-12-07 14:03:10 +00006943 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
6944
6945 TEST_ASSERT(num_ops_prior == 0);
6946
6947 /* Start verification. */
6948 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
6949 hash_data->x, hash_data->len,
6950 signature_data->x, signature_data->len)
6951 );
6952
6953 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
6954
6955 TEST_ASSERT(num_ops_prior == 0);
6956
6957 /* Continue performing the signature until complete. */
6958 while (status == PSA_OPERATION_INCOMPLETE) {
6959 status = psa_verify_hash_complete(&operation);
6960
Paul Elliott0c683352022-12-16 19:16:56 +00006961 num_completes++;
6962
Paul Elliott712d5122022-12-07 14:03:10 +00006963 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6964 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00006965 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006966
Paul Elliott712d5122022-12-07 14:03:10 +00006967 num_ops_prior = num_ops;
6968 }
6969 }
6970
6971 TEST_ASSERT(status == PSA_SUCCESS);
6972
Paul Elliott0c683352022-12-16 19:16:56 +00006973 TEST_LE_U(min_completes, num_completes);
6974 TEST_LE_U(num_completes, max_completes);
6975
Paul Elliott712d5122022-12-07 14:03:10 +00006976 PSA_ASSERT(psa_verify_hash_abort(&operation));
6977
Paul Elliott59ad9452022-12-18 15:09:02 +00006978 num_ops = psa_verify_hash_get_num_ops(&operation);
6979 TEST_ASSERT(num_ops == 0);
6980
Paul Elliott712d5122022-12-07 14:03:10 +00006981exit:
6982 psa_reset_key_attributes(&attributes);
6983 psa_destroy_key(key);
6984 PSA_DONE();
6985}
6986/* END_CASE */
6987
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006988/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006989void verify_hash_fail(int key_type_arg, data_t *key_data,
6990 int alg_arg, data_t *hash_data,
6991 data_t *signature_data,
6992 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006993{
Ronald Cron5425a212020-08-04 14:58:35 +02006994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006995 psa_key_type_t key_type = key_type_arg;
6996 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006997 psa_status_t actual_status;
6998 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007000
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007002
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7004 psa_set_key_algorithm(&attributes, alg);
7005 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007006
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7008 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007009
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 actual_status = psa_verify_hash(key, alg,
7011 hash_data->x, hash_data->len,
7012 signature_data->x, signature_data->len);
7013 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007014
7015exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 psa_reset_key_attributes(&attributes);
7017 psa_destroy_key(key);
7018 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007019}
7020/* END_CASE */
7021
Paul Elliott91007972022-12-16 12:21:24 +00007022/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7023void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7024 int alg_arg, data_t *hash_data,
7025 data_t *signature_data,
7026 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007027 int expected_complete_status_arg,
7028 int max_ops, int min_completes,
7029 int max_completes)
Paul Elliott91007972022-12-16 12:21:24 +00007030{
7031 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7032 psa_key_type_t key_type = key_type_arg;
7033 psa_algorithm_t alg = alg_arg;
7034 psa_status_t actual_status;
7035 psa_status_t expected_start_status = expected_start_status_arg;
7036 psa_status_t expected_complete_status = expected_complete_status_arg;
7037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7038 size_t num_ops = 0;
7039 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007040 size_t num_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007041 psa_verify_hash_interruptible_operation_t operation =
7042 psa_verify_hash_interruptible_operation_init();
7043
7044 PSA_ASSERT(psa_crypto_init());
7045
7046 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7047 psa_set_key_algorithm(&attributes, alg);
7048 psa_set_key_type(&attributes, key_type);
7049
7050 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7051 &key));
7052
Paul Elliott0c683352022-12-16 19:16:56 +00007053 psa_interruptible_set_max_ops(max_ops);
7054
Paul Elliott91007972022-12-16 12:21:24 +00007055 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7056 TEST_ASSERT(num_ops_prior == 0);
7057
7058 /* Start verification. */
7059 actual_status = psa_verify_hash_start(&operation, key, alg,
7060 hash_data->x, hash_data->len,
7061 signature_data->x,
7062 signature_data->len);
7063
7064 TEST_EQUAL(actual_status, expected_start_status);
7065
7066 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7067 TEST_ASSERT(num_ops_prior == 0);
7068
7069 actual_status = PSA_OPERATION_INCOMPLETE;
7070
7071 /* Continue performing the signature until complete. */
7072 while (actual_status == PSA_OPERATION_INCOMPLETE) {
7073 actual_status = psa_verify_hash_complete(&operation);
7074
Paul Elliott0c683352022-12-16 19:16:56 +00007075 num_completes++;
7076
Paul Elliott91007972022-12-16 12:21:24 +00007077 /* If the psa_verify_hash_start() failed,
7078 * psa_verify_hash_complete() should also fail with bad state.*/
7079 if (expected_start_status != PSA_SUCCESS) {
7080 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7081 } else if (actual_status != PSA_OPERATION_INCOMPLETE) {
7082 TEST_EQUAL(actual_status, expected_complete_status);
7083 } else {
7084 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00007085 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007086
Paul Elliott91007972022-12-16 12:21:24 +00007087 num_ops_prior = num_ops;
7088 }
7089 }
7090
Paul Elliott0c683352022-12-16 19:16:56 +00007091 TEST_LE_U(min_completes, num_completes);
7092 TEST_LE_U(num_completes, max_completes);
7093
Paul Elliott91007972022-12-16 12:21:24 +00007094 PSA_ASSERT(psa_verify_hash_abort(&operation));
7095
Paul Elliott59ad9452022-12-18 15:09:02 +00007096 num_ops = psa_verify_hash_get_num_ops(&operation);
7097 TEST_ASSERT(num_ops == 0);
7098
Paul Elliott91007972022-12-16 12:21:24 +00007099exit:
7100 psa_reset_key_attributes(&attributes);
7101 psa_destroy_key(key);
7102 PSA_DONE();
7103}
7104/* END_CASE */
7105
Paul Elliott20a36062022-12-18 13:21:25 +00007106/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7107void hash_interruptible_state_test(int key_type_arg, data_t *key_data,
7108 int alg_arg, data_t *input_data)
7109{
7110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7111 psa_key_type_t key_type = key_type_arg;
7112 psa_algorithm_t alg = alg_arg;
7113 size_t key_bits;
7114 unsigned char *signature = NULL;
7115 size_t signature_size;
7116 size_t signature_length = 0xdeadbeef;
7117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7118 psa_sign_hash_interruptible_operation_t sign_operation =
7119 psa_sign_hash_interruptible_operation_init();
7120 psa_verify_hash_interruptible_operation_t verify_operation =
7121 psa_verify_hash_interruptible_operation_init();
7122
7123 PSA_ASSERT(psa_crypto_init());
7124
7125 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7126 PSA_KEY_USAGE_VERIFY_HASH);
7127 psa_set_key_algorithm(&attributes, alg);
7128 psa_set_key_type(&attributes, key_type);
7129
7130 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7131 &key));
7132 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7133 key_bits = psa_get_key_bits(&attributes);
7134
7135 /* Allocate a buffer which has the size advertised by the
7136 * library. */
7137 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7138 key_bits, alg);
7139 TEST_ASSERT(signature_size != 0);
7140 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7141 ASSERT_ALLOC(signature, signature_size);
7142
7143 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7144
7145 /* --- Attempt completes prior to starts --- */
7146 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7147 signature_size,
7148 &signature_length),
7149 PSA_ERROR_BAD_STATE);
7150
7151 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7152
7153 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7154 PSA_ERROR_BAD_STATE);
7155
7156 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7157
7158 /* --- Aborts in all other places. --- */
7159 psa_sign_hash_abort(&sign_operation);
7160
7161 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7162 input_data->x, input_data->len));
7163
7164 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7165
7166 psa_interruptible_set_max_ops(1);
7167
7168 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7169 input_data->x, input_data->len));
7170
7171 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7172 signature_size,
7173 &signature_length),
7174 PSA_OPERATION_INCOMPLETE);
7175
7176 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7177
7178 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7179
7180 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7181 input_data->x, input_data->len));
7182
7183 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7184 signature_size,
7185 &signature_length));
7186
7187 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7188
7189 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7190
7191 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7192 input_data->x, input_data->len,
7193 signature, signature_length));
7194
7195 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7196
7197 psa_interruptible_set_max_ops(1);
7198
7199 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7200 input_data->x, input_data->len,
7201 signature, signature_length));
7202
7203 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7204 PSA_OPERATION_INCOMPLETE);
7205
7206 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7207
7208 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7209
7210 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7211 input_data->x, input_data->len,
7212 signature, signature_length));
7213
7214 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7215
7216 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7217
7218 /* --- Attempt double starts. --- */
7219
7220 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7221 input_data->x, input_data->len));
7222
7223 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7224 input_data->x, input_data->len),
7225 PSA_ERROR_BAD_STATE);
7226
7227 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7228
7229 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7230 input_data->x, input_data->len,
7231 signature, signature_length));
7232
7233 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7234 input_data->x, input_data->len,
7235 signature, signature_length),
7236 PSA_ERROR_BAD_STATE);
7237
7238 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7239
7240 /* --- Ensure changing the max ops mid operation works (operation should
7241 * complete successfully after setting max ops to unlimited --- */
7242 psa_interruptible_set_max_ops(1);
7243
7244 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7245 input_data->x, input_data->len));
7246
7247 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7248 signature_size,
7249 &signature_length),
7250 PSA_OPERATION_INCOMPLETE);
7251
7252 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7253
7254 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7255 signature_size,
7256 &signature_length));
7257
7258 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7259
7260 psa_interruptible_set_max_ops(1);
7261
7262 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7263 input_data->x, input_data->len,
7264 signature, signature_length));
7265
7266 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7267 PSA_OPERATION_INCOMPLETE);
7268
7269 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7270
7271 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7272
7273 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7274
7275 /* --- Change function inputs mid run, to cause an error (sign only,
7276 * verify passes all inputs to start. --- */
7277
7278 psa_interruptible_set_max_ops(1);
7279
7280 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7281 input_data->x, input_data->len));
7282
7283 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7284 signature_size,
7285 &signature_length),
7286 PSA_OPERATION_INCOMPLETE);
7287
7288 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7289 0,
7290 &signature_length),
7291 PSA_ERROR_BUFFER_TOO_SMALL);
7292
7293 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7294
7295exit:
7296 /*
7297 * Key attributes may have been returned by psa_get_key_attributes()
7298 * thus reset them as required.
7299 */
7300 psa_reset_key_attributes(&attributes);
7301
7302 psa_destroy_key(key);
7303 mbedtls_free(signature);
7304 PSA_DONE();
7305}
7306/* END_CASE */
7307
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007308/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007309void sign_message_deterministic(int key_type_arg,
7310 data_t *key_data,
7311 int alg_arg,
7312 data_t *input_data,
7313 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007314{
7315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7316 psa_key_type_t key_type = key_type_arg;
7317 psa_algorithm_t alg = alg_arg;
7318 size_t key_bits;
7319 unsigned char *signature = NULL;
7320 size_t signature_size;
7321 size_t signature_length = 0xdeadbeef;
7322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7323
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007325
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7327 psa_set_key_algorithm(&attributes, alg);
7328 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007329
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7331 &key));
7332 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7333 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7336 TEST_ASSERT(signature_size != 0);
7337 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7338 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007339
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 PSA_ASSERT(psa_sign_message(key, alg,
7341 input_data->x, input_data->len,
7342 signature, signature_size,
7343 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007344
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 ASSERT_COMPARE(output_data->x, output_data->len,
7346 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007347
7348exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007350
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 psa_destroy_key(key);
7352 mbedtls_free(signature);
7353 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007354
7355}
7356/* END_CASE */
7357
7358/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007359void sign_message_fail(int key_type_arg,
7360 data_t *key_data,
7361 int alg_arg,
7362 data_t *input_data,
7363 int signature_size_arg,
7364 int expected_status_arg)
7365{
7366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7367 psa_key_type_t key_type = key_type_arg;
7368 psa_algorithm_t alg = alg_arg;
7369 size_t signature_size = signature_size_arg;
7370 psa_status_t actual_status;
7371 psa_status_t expected_status = expected_status_arg;
7372 unsigned char *signature = NULL;
7373 size_t signature_length = 0xdeadbeef;
7374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7375
7376 ASSERT_ALLOC(signature, signature_size);
7377
7378 PSA_ASSERT(psa_crypto_init());
7379
7380 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7381 psa_set_key_algorithm(&attributes, alg);
7382 psa_set_key_type(&attributes, key_type);
7383
7384 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7385 &key));
7386
7387 actual_status = psa_sign_message(key, alg,
7388 input_data->x, input_data->len,
7389 signature, signature_size,
7390 &signature_length);
7391 TEST_EQUAL(actual_status, expected_status);
7392 /* The value of *signature_length is unspecified on error, but
7393 * whatever it is, it should be less than signature_size, so that
7394 * if the caller tries to read *signature_length bytes without
7395 * checking the error code then they don't overflow a buffer. */
7396 TEST_LE_U(signature_length, signature_size);
7397
7398exit:
7399 psa_reset_key_attributes(&attributes);
7400 psa_destroy_key(key);
7401 mbedtls_free(signature);
7402 PSA_DONE();
7403}
7404/* END_CASE */
7405
7406/* BEGIN_CASE */
7407void sign_verify_message(int key_type_arg,
7408 data_t *key_data,
7409 int alg_arg,
7410 data_t *input_data)
7411{
7412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7413 psa_key_type_t key_type = key_type_arg;
7414 psa_algorithm_t alg = alg_arg;
7415 size_t key_bits;
7416 unsigned char *signature = NULL;
7417 size_t signature_size;
7418 size_t signature_length = 0xdeadbeef;
7419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7420
7421 PSA_ASSERT(psa_crypto_init());
7422
7423 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7424 PSA_KEY_USAGE_VERIFY_MESSAGE);
7425 psa_set_key_algorithm(&attributes, alg);
7426 psa_set_key_type(&attributes, key_type);
7427
7428 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7429 &key));
7430 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7431 key_bits = psa_get_key_bits(&attributes);
7432
7433 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7434 TEST_ASSERT(signature_size != 0);
7435 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7436 ASSERT_ALLOC(signature, signature_size);
7437
7438 PSA_ASSERT(psa_sign_message(key, alg,
7439 input_data->x, input_data->len,
7440 signature, signature_size,
7441 &signature_length));
7442 TEST_LE_U(signature_length, signature_size);
7443 TEST_ASSERT(signature_length > 0);
7444
7445 PSA_ASSERT(psa_verify_message(key, alg,
7446 input_data->x, input_data->len,
7447 signature, signature_length));
7448
7449 if (input_data->len != 0) {
7450 /* Flip a bit in the input and verify that the signature is now
7451 * detected as invalid. Flip a bit at the beginning, not at the end,
7452 * because ECDSA may ignore the last few bits of the input. */
7453 input_data->x[0] ^= 1;
7454 TEST_EQUAL(psa_verify_message(key, alg,
7455 input_data->x, input_data->len,
7456 signature, signature_length),
7457 PSA_ERROR_INVALID_SIGNATURE);
7458 }
7459
7460exit:
7461 psa_reset_key_attributes(&attributes);
7462
7463 psa_destroy_key(key);
7464 mbedtls_free(signature);
7465 PSA_DONE();
7466}
7467/* END_CASE */
7468
7469/* BEGIN_CASE */
7470void verify_message(int key_type_arg,
7471 data_t *key_data,
7472 int alg_arg,
7473 data_t *input_data,
7474 data_t *signature_data)
7475{
7476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7477 psa_key_type_t key_type = key_type_arg;
7478 psa_algorithm_t alg = alg_arg;
7479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7480
7481 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7482
7483 PSA_ASSERT(psa_crypto_init());
7484
7485 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7486 psa_set_key_algorithm(&attributes, alg);
7487 psa_set_key_type(&attributes, key_type);
7488
7489 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7490 &key));
7491
7492 PSA_ASSERT(psa_verify_message(key, alg,
7493 input_data->x, input_data->len,
7494 signature_data->x, signature_data->len));
7495
7496exit:
7497 psa_reset_key_attributes(&attributes);
7498 psa_destroy_key(key);
7499 PSA_DONE();
7500}
7501/* END_CASE */
7502
7503/* BEGIN_CASE */
7504void verify_message_fail(int key_type_arg,
7505 data_t *key_data,
7506 int alg_arg,
7507 data_t *hash_data,
7508 data_t *signature_data,
7509 int expected_status_arg)
7510{
7511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7512 psa_key_type_t key_type = key_type_arg;
7513 psa_algorithm_t alg = alg_arg;
7514 psa_status_t actual_status;
7515 psa_status_t expected_status = expected_status_arg;
7516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7517
7518 PSA_ASSERT(psa_crypto_init());
7519
7520 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7521 psa_set_key_algorithm(&attributes, alg);
7522 psa_set_key_type(&attributes, key_type);
7523
7524 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7525 &key));
7526
7527 actual_status = psa_verify_message(key, alg,
7528 hash_data->x, hash_data->len,
7529 signature_data->x,
7530 signature_data->len);
7531 TEST_EQUAL(actual_status, expected_status);
7532
7533exit:
7534 psa_reset_key_attributes(&attributes);
7535 psa_destroy_key(key);
7536 PSA_DONE();
7537}
7538/* END_CASE */
7539
7540/* BEGIN_CASE */
7541void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02007542 data_t *key_data,
7543 int alg_arg,
7544 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 data_t *label,
7546 int expected_output_length_arg,
7547 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02007548{
Ronald Cron5425a212020-08-04 14:58:35 +02007549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007550 psa_key_type_t key_type = key_type_arg;
7551 psa_algorithm_t alg = alg_arg;
7552 size_t expected_output_length = expected_output_length_arg;
7553 size_t key_bits;
7554 unsigned char *output = NULL;
7555 size_t output_size;
7556 size_t output_length = ~0;
7557 psa_status_t actual_status;
7558 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007560
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007562
Gilles Peskine656896e2018-06-29 19:12:28 +02007563 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
7565 psa_set_key_algorithm(&attributes, alg);
7566 psa_set_key_type(&attributes, key_type);
7567 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7568 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02007569
7570 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7572 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007573
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7575 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7576 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02007577
7578 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 actual_status = psa_asymmetric_encrypt(key, alg,
7580 input_data->x, input_data->len,
7581 label->x, label->len,
7582 output, output_size,
7583 &output_length);
7584 TEST_EQUAL(actual_status, expected_status);
7585 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine656896e2018-06-29 19:12:28 +02007586
Gilles Peskine68428122018-06-30 18:42:41 +02007587 /* If the label is empty, the test framework puts a non-null pointer
7588 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007590 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 if (output_size != 0) {
7592 memset(output, 0, output_size);
7593 }
7594 actual_status = psa_asymmetric_encrypt(key, alg,
7595 input_data->x, input_data->len,
7596 NULL, label->len,
7597 output, output_size,
7598 &output_length);
7599 TEST_EQUAL(actual_status, expected_status);
7600 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007601 }
7602
Gilles Peskine656896e2018-06-29 19:12:28 +02007603exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007604 /*
7605 * Key attributes may have been returned by psa_get_key_attributes()
7606 * thus reset them as required.
7607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 psa_destroy_key(key);
7611 mbedtls_free(output);
7612 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02007613}
7614/* END_CASE */
7615
7616/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007617void asymmetric_encrypt_decrypt(int key_type_arg,
7618 data_t *key_data,
7619 int alg_arg,
7620 data_t *input_data,
7621 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007622{
Ronald Cron5425a212020-08-04 14:58:35 +02007623 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007624 psa_key_type_t key_type = key_type_arg;
7625 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007626 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007627 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007628 size_t output_size;
7629 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007630 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007631 size_t output2_size;
7632 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007634
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
7638 psa_set_key_algorithm(&attributes, alg);
7639 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007640
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7642 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007643
7644 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7646 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007647
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7649 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7650 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007651
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007652 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 TEST_LE_U(output2_size,
7654 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
7655 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7656 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007657
Gilles Peskineeebd7382018-06-08 18:11:54 +02007658 /* We test encryption by checking that encrypt-then-decrypt gives back
7659 * the original plaintext because of the non-optional random
7660 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
7662 input_data->x, input_data->len,
7663 label->x, label->len,
7664 output, output_size,
7665 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007666 /* We don't know what ciphertext length to expect, but check that
7667 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007669
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7671 output, output_length,
7672 label->x, label->len,
7673 output2, output2_size,
7674 &output2_length));
7675 ASSERT_COMPARE(input_data->x, input_data->len,
7676 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007677
7678exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007679 /*
7680 * Key attributes may have been returned by psa_get_key_attributes()
7681 * thus reset them as required.
7682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007684
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 psa_destroy_key(key);
7686 mbedtls_free(output);
7687 mbedtls_free(output2);
7688 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007689}
7690/* END_CASE */
7691
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007692/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007693void asymmetric_decrypt(int key_type_arg,
7694 data_t *key_data,
7695 int alg_arg,
7696 data_t *input_data,
7697 data_t *label,
7698 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007699{
Ronald Cron5425a212020-08-04 14:58:35 +02007700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007701 psa_key_type_t key_type = key_type_arg;
7702 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007703 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007704 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007705 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007706 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007708
Gilles Peskine449bd832023-01-11 14:50:10 +01007709 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007710
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7712 psa_set_key_algorithm(&attributes, alg);
7713 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007714
Gilles Peskine449bd832023-01-11 14:50:10 +01007715 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7716 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007717
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7719 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007720
7721 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7723 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7724 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007725
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7727 input_data->x, input_data->len,
7728 label->x, label->len,
7729 output,
7730 output_size,
7731 &output_length));
7732 ASSERT_COMPARE(expected_data->x, expected_data->len,
7733 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007734
Gilles Peskine68428122018-06-30 18:42:41 +02007735 /* If the label is empty, the test framework puts a non-null pointer
7736 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007738 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 if (output_size != 0) {
7740 memset(output, 0, output_size);
7741 }
7742 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7743 input_data->x, input_data->len,
7744 NULL, label->len,
7745 output,
7746 output_size,
7747 &output_length));
7748 ASSERT_COMPARE(expected_data->x, expected_data->len,
7749 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007750 }
7751
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 psa_reset_key_attributes(&attributes);
7754 psa_destroy_key(key);
7755 mbedtls_free(output);
7756 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007757}
7758/* END_CASE */
7759
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007760/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007761void asymmetric_decrypt_fail(int key_type_arg,
7762 data_t *key_data,
7763 int alg_arg,
7764 data_t *input_data,
7765 data_t *label,
7766 int output_size_arg,
7767 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007768{
Ronald Cron5425a212020-08-04 14:58:35 +02007769 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007770 psa_key_type_t key_type = key_type_arg;
7771 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007772 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007773 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007774 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007775 psa_status_t actual_status;
7776 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007778
Gilles Peskine449bd832023-01-11 14:50:10 +01007779 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007780
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007782
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7784 psa_set_key_algorithm(&attributes, alg);
7785 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007786
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7788 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007789
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 actual_status = psa_asymmetric_decrypt(key, alg,
7791 input_data->x, input_data->len,
7792 label->x, label->len,
7793 output, output_size,
7794 &output_length);
7795 TEST_EQUAL(actual_status, expected_status);
7796 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007797
Gilles Peskine68428122018-06-30 18:42:41 +02007798 /* If the label is empty, the test framework puts a non-null pointer
7799 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007800 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007801 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 if (output_size != 0) {
7803 memset(output, 0, output_size);
7804 }
7805 actual_status = psa_asymmetric_decrypt(key, alg,
7806 input_data->x, input_data->len,
7807 NULL, label->len,
7808 output, output_size,
7809 &output_length);
7810 TEST_EQUAL(actual_status, expected_status);
7811 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02007812 }
7813
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007814exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 psa_reset_key_attributes(&attributes);
7816 psa_destroy_key(key);
7817 mbedtls_free(output);
7818 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007819}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007820/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007821
7822/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007823void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00007824{
7825 /* Test each valid way of initializing the object, except for `= {0}`, as
7826 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7827 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007828 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007829 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007831 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7832 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007833
Gilles Peskine449bd832023-01-11 14:50:10 +01007834 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007835
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007836 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
7838 PSA_ERROR_BAD_STATE);
7839 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
7840 PSA_ERROR_BAD_STATE);
7841 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
7842 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007843
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007844 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007845 PSA_ASSERT(psa_key_derivation_abort(&func));
7846 PSA_ASSERT(psa_key_derivation_abort(&init));
7847 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007848}
7849/* END_CASE */
7850
Janos Follath16de4a42019-06-13 16:32:24 +01007851/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007852void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02007853{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007854 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007855 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007856 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007857
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02007859
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7861 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02007862
7863exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 psa_key_derivation_abort(&operation);
7865 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02007866}
7867/* END_CASE */
7868
Janos Follathaf3c2a02019-06-12 12:34:34 +01007869/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007870void derive_set_capacity(int alg_arg, int capacity_arg,
7871 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01007872{
7873 psa_algorithm_t alg = alg_arg;
7874 size_t capacity = capacity_arg;
7875 psa_status_t expected_status = expected_status_arg;
7876 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7877
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01007879
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01007881
Gilles Peskine449bd832023-01-11 14:50:10 +01007882 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
7883 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01007884
7885exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007886 psa_key_derivation_abort(&operation);
7887 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01007888}
7889/* END_CASE */
7890
7891/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007892void derive_input(int alg_arg,
7893 int step_arg1, int key_type_arg1, data_t *input1,
7894 int expected_status_arg1,
7895 int step_arg2, int key_type_arg2, data_t *input2,
7896 int expected_status_arg2,
7897 int step_arg3, int key_type_arg3, data_t *input3,
7898 int expected_status_arg3,
7899 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01007900{
7901 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
7903 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
7904 psa_status_t expected_statuses[] = { expected_status_arg1,
7905 expected_status_arg2,
7906 expected_status_arg3 };
7907 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02007908 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7909 MBEDTLS_SVC_KEY_ID_INIT,
7910 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007911 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7913 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007914 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007915 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007916 psa_status_t expected_output_status = expected_output_status_arg;
7917 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007918
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01007920
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7922 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007923
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01007925
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
7927 mbedtls_test_set_step(i);
7928 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02007929 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
7931 psa_set_key_type(&attributes, key_types[i]);
7932 PSA_ASSERT(psa_import_key(&attributes,
7933 inputs[i]->x, inputs[i]->len,
7934 &keys[i]));
7935 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
7936 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007937 // When taking a private key as secret input, use key agreement
7938 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
7940 &operation, keys[i]),
7941 expected_statuses[i]);
7942 } else {
7943 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
7944 keys[i]),
7945 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007946 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 } else {
7948 TEST_EQUAL(psa_key_derivation_input_bytes(
7949 &operation, steps[i],
7950 inputs[i]->x, inputs[i]->len),
7951 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007952 }
7953 }
7954
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 if (output_key_type != PSA_KEY_TYPE_NONE) {
7956 psa_reset_key_attributes(&attributes);
7957 psa_set_key_type(&attributes, output_key_type);
7958 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007959 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 psa_key_derivation_output_key(&attributes, &operation,
7961 &output_key);
7962 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007963 uint8_t buffer[1];
7964 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 psa_key_derivation_output_bytes(&operation,
7966 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007967 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007968 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007969
Janos Follathaf3c2a02019-06-12 12:34:34 +01007970exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 psa_key_derivation_abort(&operation);
7972 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
7973 psa_destroy_key(keys[i]);
7974 }
7975 psa_destroy_key(output_key);
7976 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01007977}
7978/* END_CASE */
7979
Janos Follathd958bb72019-07-03 15:02:16 +01007980/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007981void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007982{
Janos Follathd958bb72019-07-03 15:02:16 +01007983 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007984 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007985 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007986 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007987 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01007989 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007991 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007993 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7994 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007997
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007999
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8001 psa_set_key_algorithm(&attributes, alg);
8002 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 PSA_ASSERT(psa_import_key(&attributes,
8005 key_data, sizeof(key_data),
8006 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008007
8008 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008009 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8010 input1, input1_length,
8011 input2, input2_length,
8012 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008013 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008015
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008016 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008017 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8018 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008019
Gilles Peskine449bd832023-01-11 14:50:10 +01008020 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008021
Gilles Peskine449bd832023-01-11 14:50:10 +01008022 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8023 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008024
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008026 psa_key_derivation_abort(&operation);
8027 psa_destroy_key(key);
8028 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008029}
8030/* END_CASE */
8031
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008032/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008033void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008034{
8035 uint8_t output_buffer[16];
8036 size_t buffer_size = 16;
8037 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008038 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008039
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8041 output_buffer, buffer_size)
8042 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008043
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8045 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008046
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008048
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8050 output_buffer, buffer_size)
8051 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008052
Gilles Peskine449bd832023-01-11 14:50:10 +01008053 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8054 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008055
8056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008058}
8059/* END_CASE */
8060
8061/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008062void derive_output(int alg_arg,
8063 int step1_arg, data_t *input1, int expected_status_arg1,
8064 int step2_arg, data_t *input2, int expected_status_arg2,
8065 int step3_arg, data_t *input3, int expected_status_arg3,
8066 int step4_arg, data_t *input4, int expected_status_arg4,
8067 data_t *key_agreement_peer_key,
8068 int requested_capacity_arg,
8069 data_t *expected_output1,
8070 data_t *expected_output2,
8071 int other_key_input_type,
8072 int key_input_type,
8073 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008074{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008075 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8077 data_t *inputs[] = { input1, input2, input3, input4 };
8078 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8079 MBEDTLS_SVC_KEY_ID_INIT,
8080 MBEDTLS_SVC_KEY_ID_INIT,
8081 MBEDTLS_SVC_KEY_ID_INIT };
8082 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8083 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008084 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008085 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008086 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008087 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008088 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008089 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008090 size_t output_buffer_size = 0;
8091 uint8_t *output_buffer = NULL;
8092 size_t expected_capacity;
8093 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008094 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8095 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8096 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8097 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008098 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008099 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008100 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008101
Gilles Peskine449bd832023-01-11 14:50:10 +01008102 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8103 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008104 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 }
8106 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008107 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008110 ASSERT_ALLOC(output_buffer, output_buffer_size);
8111 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008112
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008113 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8115 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8116 requested_capacity));
8117 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8118 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008119 case 0:
8120 break;
8121 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008123 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 TEST_EQUAL(psa_key_derivation_input_bytes(
8125 &operation, steps[i],
8126 inputs[i]->x, inputs[i]->len),
8127 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008128
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008132 break;
8133 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8135 psa_set_key_algorithm(&attributes1, alg);
8136 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008137
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 PSA_ASSERT(psa_import_key(&attributes1,
8139 inputs[i]->x, inputs[i]->len,
8140 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008141
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8143 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8144 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8145 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008146 }
8147
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8149 steps[i],
8150 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008151 break;
8152 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008154 break;
8155 }
8156 break;
8157 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008158 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008159 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008160 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8161 steps[i],
8162 inputs[i]->x,
8163 inputs[i]->len),
8164 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008165 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008166 case 1: // input key, type DERIVE
8167 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8169 psa_set_key_algorithm(&attributes2, alg);
8170 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008171
8172 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008173 if (other_key_input_type == 11) {
8174 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8175 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008176
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 PSA_ASSERT(psa_import_key(&attributes2,
8178 inputs[i]->x, inputs[i]->len,
8179 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008180
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8182 steps[i],
8183 keys[i]),
8184 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008185 break;
8186 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008187 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8188 psa_set_key_algorithm(&attributes3, alg);
8189 psa_set_key_type(&attributes3,
8190 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008191
Gilles Peskine449bd832023-01-11 14:50:10 +01008192 PSA_ASSERT(psa_import_key(&attributes3,
8193 inputs[i]->x, inputs[i]->len,
8194 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008195
Gilles Peskine449bd832023-01-11 14:50:10 +01008196 TEST_EQUAL(psa_key_derivation_key_agreement(
8197 &operation,
8198 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8199 keys[i], key_agreement_peer_key->x,
8200 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008201 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008202 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008204 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008205 }
8206
Gilles Peskine449bd832023-01-11 14:50:10 +01008207 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008210 break;
8211 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008212 TEST_EQUAL(psa_key_derivation_input_bytes(
8213 &operation, steps[i],
8214 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008215
Gilles Peskine449bd832023-01-11 14:50:10 +01008216 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008219 break;
8220 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008221 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8224 &current_capacity));
8225 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008226 expected_capacity = requested_capacity;
8227
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008229 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8230
8231 /* For output key derivation secret must be provided using
8232 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008234 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008236
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8238 psa_set_key_algorithm(&attributes4, alg);
8239 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8240 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008241
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8243 &derived_key), expected_status);
8244 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008245 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008247 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 status = psa_key_derivation_output_bytes(&operation,
8249 output_buffer, output_sizes[i]);
8250 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008251 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 TEST_ASSERT(status == PSA_SUCCESS ||
8253 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008254 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 } else if (expected_capacity == 0 ||
8256 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008257 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008259 expected_capacity = 0;
8260 continue;
8261 }
8262 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 PSA_ASSERT(status);
8264 if (output_sizes[i] != 0) {
8265 ASSERT_COMPARE(output_buffer, output_sizes[i],
8266 expected_outputs[i], output_sizes[i]);
8267 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008268 /* Check the operation status. */
8269 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8271 &current_capacity));
8272 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008273 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008274 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008276
8277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 mbedtls_free(output_buffer);
8279 psa_key_derivation_abort(&operation);
8280 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8281 psa_destroy_key(keys[i]);
8282 }
8283 psa_destroy_key(derived_key);
8284 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008285}
8286/* END_CASE */
8287
8288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008289void derive_full(int alg_arg,
8290 data_t *key_data,
8291 data_t *input1,
8292 data_t *input2,
8293 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008294{
Ronald Cron5425a212020-08-04 14:58:35 +02008295 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008296 psa_algorithm_t alg = alg_arg;
8297 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008298 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008299 unsigned char output_buffer[16];
8300 size_t expected_capacity = requested_capacity;
8301 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008303
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008305
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8307 psa_set_key_algorithm(&attributes, alg);
8308 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008309
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8311 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008312
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8314 input1->x, input1->len,
8315 input2->x, input2->len,
8316 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008317 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008319
Gilles Peskine449bd832023-01-11 14:50:10 +01008320 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8321 &current_capacity));
8322 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008323
8324 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008325 while (current_capacity > 0) {
8326 size_t read_size = sizeof(output_buffer);
8327 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008328 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 }
8330 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8331 output_buffer,
8332 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008333 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008334 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8335 &current_capacity));
8336 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008337 }
8338
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008339 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8341 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008342
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008344
8345exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 psa_key_derivation_abort(&operation);
8347 psa_destroy_key(key);
8348 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008349}
8350/* END_CASE */
8351
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008352/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008353void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8354 int derivation_step,
8355 int capacity, int expected_capacity_status_arg,
8356 data_t *expected_output,
8357 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008358{
8359 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8360 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008361 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008362 uint8_t *output_buffer = NULL;
8363 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008364 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8365 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8366 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008367
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 ASSERT_ALLOC(output_buffer, expected_output->len);
8369 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008370
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8372 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8373 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8376 step, input->x, input->len),
8377 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008378
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008382
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8384 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008385
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 TEST_EQUAL(status, expected_output_status);
8387 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8388 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8389 expected_output->len);
8390 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008391
8392exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 mbedtls_free(output_buffer);
8394 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008395 PSA_DONE();
8396}
8397/* END_CASE */
8398
Janos Follathe60c9052019-07-03 13:51:30 +01008399/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008400void derive_key_exercise(int alg_arg,
8401 data_t *key_data,
8402 data_t *input1,
8403 data_t *input2,
8404 int derived_type_arg,
8405 int derived_bits_arg,
8406 int derived_usage_arg,
8407 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008408{
Ronald Cron5425a212020-08-04 14:58:35 +02008409 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8410 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008411 psa_algorithm_t alg = alg_arg;
8412 psa_key_type_t derived_type = derived_type_arg;
8413 size_t derived_bits = derived_bits_arg;
8414 psa_key_usage_t derived_usage = derived_usage_arg;
8415 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008417 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008419 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008420
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008422
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8424 psa_set_key_algorithm(&attributes, alg);
8425 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8426 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8427 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008428
8429 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8431 input1->x, input1->len,
8432 input2->x, input2->len,
8433 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 }
Janos Follathe60c9052019-07-03 13:51:30 +01008436
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 psa_set_key_usage_flags(&attributes, derived_usage);
8438 psa_set_key_algorithm(&attributes, derived_alg);
8439 psa_set_key_type(&attributes, derived_type);
8440 psa_set_key_bits(&attributes, derived_bits);
8441 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8442 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008443
8444 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8446 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8447 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008448
8449 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008451 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008453
8454exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008455 /*
8456 * Key attributes may have been returned by psa_get_key_attributes()
8457 * thus reset them as required.
8458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008460
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 psa_key_derivation_abort(&operation);
8462 psa_destroy_key(base_key);
8463 psa_destroy_key(derived_key);
8464 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008465}
8466/* END_CASE */
8467
Janos Follath42fd8882019-07-03 14:17:09 +01008468/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008469void derive_key_export(int alg_arg,
8470 data_t *key_data,
8471 data_t *input1,
8472 data_t *input2,
8473 int bytes1_arg,
8474 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008475{
Ronald Cron5425a212020-08-04 14:58:35 +02008476 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8477 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008478 psa_algorithm_t alg = alg_arg;
8479 size_t bytes1 = bytes1_arg;
8480 size_t bytes2 = bytes2_arg;
8481 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008482 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008483 uint8_t *output_buffer = NULL;
8484 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008485 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8486 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008487 size_t length;
8488
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 ASSERT_ALLOC(output_buffer, capacity);
8490 ASSERT_ALLOC(export_buffer, capacity);
8491 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008492
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8494 psa_set_key_algorithm(&base_attributes, alg);
8495 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8496 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8497 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008498
8499 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8501 input1->x, input1->len,
8502 input2->x, input2->len,
8503 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008504 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 }
Janos Follath42fd8882019-07-03 14:17:09 +01008506
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8508 output_buffer,
8509 capacity));
8510 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008511
8512 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8514 input1->x, input1->len,
8515 input2->x, input2->len,
8516 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008517 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 }
Janos Follath42fd8882019-07-03 14:17:09 +01008519
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8521 psa_set_key_algorithm(&derived_attributes, 0);
8522 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
8523 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
8524 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8525 &derived_key));
8526 PSA_ASSERT(psa_export_key(derived_key,
8527 export_buffer, bytes1,
8528 &length));
8529 TEST_EQUAL(length, bytes1);
8530 PSA_ASSERT(psa_destroy_key(derived_key));
8531 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
8532 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8533 &derived_key));
8534 PSA_ASSERT(psa_export_key(derived_key,
8535 export_buffer + bytes1, bytes2,
8536 &length));
8537 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008538
8539 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
8541 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008542
8543exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 mbedtls_free(output_buffer);
8545 mbedtls_free(export_buffer);
8546 psa_key_derivation_abort(&operation);
8547 psa_destroy_key(base_key);
8548 psa_destroy_key(derived_key);
8549 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008550}
8551/* END_CASE */
8552
8553/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008554void derive_key_type(int alg_arg,
8555 data_t *key_data,
8556 data_t *input1,
8557 data_t *input2,
8558 int key_type_arg, int bits_arg,
8559 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008560{
8561 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8562 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8563 const psa_algorithm_t alg = alg_arg;
8564 const psa_key_type_t key_type = key_type_arg;
8565 const size_t bits = bits_arg;
8566 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8567 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008569 uint8_t *export_buffer = NULL;
8570 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8571 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8572 size_t export_length;
8573
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 ASSERT_ALLOC(export_buffer, export_buffer_size);
8575 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008576
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8578 psa_set_key_algorithm(&base_attributes, alg);
8579 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8580 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8581 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008582
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008584 &operation, base_key, alg,
8585 input1->x, input1->len,
8586 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008588 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008590
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8592 psa_set_key_algorithm(&derived_attributes, 0);
8593 psa_set_key_type(&derived_attributes, key_type);
8594 psa_set_key_bits(&derived_attributes, bits);
8595 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8596 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 PSA_ASSERT(psa_export_key(derived_key,
8599 export_buffer, export_buffer_size,
8600 &export_length));
8601 ASSERT_COMPARE(export_buffer, export_length,
8602 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008603
8604exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 mbedtls_free(export_buffer);
8606 psa_key_derivation_abort(&operation);
8607 psa_destroy_key(base_key);
8608 psa_destroy_key(derived_key);
8609 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008610}
8611/* END_CASE */
8612
8613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008614void derive_key(int alg_arg,
8615 data_t *key_data, data_t *input1, data_t *input2,
8616 int type_arg, int bits_arg,
8617 int expected_status_arg,
8618 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02008619{
Ronald Cron5425a212020-08-04 14:58:35 +02008620 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8621 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008622 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008623 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008624 size_t bits = bits_arg;
8625 psa_status_t expected_status = expected_status_arg;
8626 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8627 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8628 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8629
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02008631
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8633 psa_set_key_algorithm(&base_attributes, alg);
8634 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8635 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8636 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02008637
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8639 input1->x, input1->len,
8640 input2->x, input2->len,
8641 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02008642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 }
Gilles Peskinec744d992019-07-30 17:26:54 +02008644
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8646 psa_set_key_algorithm(&derived_attributes, 0);
8647 psa_set_key_type(&derived_attributes, type);
8648 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008649
8650 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008651 psa_key_derivation_output_key(&derived_attributes,
8652 &operation,
8653 &derived_key);
8654 if (is_large_output > 0) {
8655 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8656 }
8657 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02008658
8659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008660 psa_key_derivation_abort(&operation);
8661 psa_destroy_key(base_key);
8662 psa_destroy_key(derived_key);
8663 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02008664}
8665/* END_CASE */
8666
8667/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008668void key_agreement_setup(int alg_arg,
8669 int our_key_type_arg, int our_key_alg_arg,
8670 data_t *our_key_data, data_t *peer_key_data,
8671 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02008672{
Ronald Cron5425a212020-08-04 14:58:35 +02008673 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008674 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008675 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008676 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008677 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008679 psa_status_t expected_status = expected_status_arg;
8680 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008681
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02008683
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8685 psa_set_key_algorithm(&attributes, our_key_alg);
8686 psa_set_key_type(&attributes, our_key_type);
8687 PSA_ASSERT(psa_import_key(&attributes,
8688 our_key_data->x, our_key_data->len,
8689 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008690
Gilles Peskine77f40d82019-04-11 21:27:06 +02008691 /* The tests currently include inputs that should fail at either step.
8692 * Test cases that fail at the setup step should be changed to call
8693 * key_derivation_setup instead, and this function should be renamed
8694 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 status = psa_key_derivation_setup(&operation, alg);
8696 if (status == PSA_SUCCESS) {
8697 TEST_EQUAL(psa_key_derivation_key_agreement(
8698 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8699 our_key,
8700 peer_key_data->x, peer_key_data->len),
8701 expected_status);
8702 } else {
8703 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008704 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008705
8706exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 psa_key_derivation_abort(&operation);
8708 psa_destroy_key(our_key);
8709 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008710}
8711/* END_CASE */
8712
8713/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008714void raw_key_agreement(int alg_arg,
8715 int our_key_type_arg, data_t *our_key_data,
8716 data_t *peer_key_data,
8717 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008718{
Ronald Cron5425a212020-08-04 14:58:35 +02008719 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008720 psa_algorithm_t alg = alg_arg;
8721 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008723 unsigned char *output = NULL;
8724 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008725 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008726
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02008728
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8730 psa_set_key_algorithm(&attributes, alg);
8731 psa_set_key_type(&attributes, our_key_type);
8732 PSA_ASSERT(psa_import_key(&attributes,
8733 our_key_data->x, our_key_data->len,
8734 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02008735
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
8737 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008738
Gilles Peskine992bee82022-04-13 23:25:52 +02008739 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01008740 TEST_LE_U(expected_output->len,
8741 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
8742 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
8743 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02008744
8745 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01008746 ASSERT_ALLOC(output, expected_output->len);
8747 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8748 peer_key_data->x, peer_key_data->len,
8749 output, expected_output->len,
8750 &output_length));
8751 ASSERT_COMPARE(output, output_length,
8752 expected_output->x, expected_output->len);
8753 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008754 output = NULL;
8755 output_length = ~0;
8756
8757 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 ASSERT_ALLOC(output, expected_output->len + 1);
8759 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8760 peer_key_data->x, peer_key_data->len,
8761 output, expected_output->len + 1,
8762 &output_length));
8763 ASSERT_COMPARE(output, output_length,
8764 expected_output->x, expected_output->len);
8765 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008766 output = NULL;
8767 output_length = ~0;
8768
8769 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 ASSERT_ALLOC(output, expected_output->len - 1);
8771 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
8772 peer_key_data->x, peer_key_data->len,
8773 output, expected_output->len - 1,
8774 &output_length),
8775 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02008776 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 TEST_LE_U(output_length, expected_output->len - 1);
8778 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008779 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008780
8781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 mbedtls_free(output);
8783 psa_destroy_key(our_key);
8784 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02008785}
8786/* END_CASE */
8787
8788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008789void key_agreement_capacity(int alg_arg,
8790 int our_key_type_arg, data_t *our_key_data,
8791 data_t *peer_key_data,
8792 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02008793{
Ronald Cron5425a212020-08-04 14:58:35 +02008794 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008795 psa_algorithm_t alg = alg_arg;
8796 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008797 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008799 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008800 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008801
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008803
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8805 psa_set_key_algorithm(&attributes, alg);
8806 psa_set_key_type(&attributes, our_key_type);
8807 PSA_ASSERT(psa_import_key(&attributes,
8808 our_key_data->x, our_key_data->len,
8809 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008810
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8812 PSA_ASSERT(psa_key_derivation_key_agreement(
8813 &operation,
8814 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8815 peer_key_data->x, peer_key_data->len));
8816 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008817 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8819 PSA_KEY_DERIVATION_INPUT_INFO,
8820 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008821 }
Gilles Peskine59685592018-09-18 12:11:34 +02008822
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008823 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 PSA_ASSERT(psa_key_derivation_get_capacity(
8825 &operation, &actual_capacity));
8826 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02008827
Gilles Peskinebf491972018-10-25 22:36:12 +02008828 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 while (actual_capacity > sizeof(output)) {
8830 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8831 output, sizeof(output)));
8832 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02008833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8835 output, actual_capacity));
8836 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
8837 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02008838
Gilles Peskine59685592018-09-18 12:11:34 +02008839exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 psa_key_derivation_abort(&operation);
8841 psa_destroy_key(our_key);
8842 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02008843}
8844/* END_CASE */
8845
8846/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008847void key_agreement_output(int alg_arg,
8848 int our_key_type_arg, data_t *our_key_data,
8849 data_t *peer_key_data,
8850 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02008851{
Ronald Cron5425a212020-08-04 14:58:35 +02008852 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008853 psa_algorithm_t alg = alg_arg;
8854 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008855 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008857 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
8860 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02008861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008863
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8865 psa_set_key_algorithm(&attributes, alg);
8866 psa_set_key_type(&attributes, our_key_type);
8867 PSA_ASSERT(psa_import_key(&attributes,
8868 our_key_data->x, our_key_data->len,
8869 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8872 PSA_ASSERT(psa_key_derivation_key_agreement(
8873 &operation,
8874 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8875 peer_key_data->x, peer_key_data->len));
8876 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008877 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8879 PSA_KEY_DERIVATION_INPUT_INFO,
8880 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008881 }
Gilles Peskine59685592018-09-18 12:11:34 +02008882
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8884 actual_output,
8885 expected_output1->len));
8886 ASSERT_COMPARE(actual_output, expected_output1->len,
8887 expected_output1->x, expected_output1->len);
8888 if (expected_output2->len != 0) {
8889 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8890 actual_output,
8891 expected_output2->len));
8892 ASSERT_COMPARE(actual_output, expected_output2->len,
8893 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008894 }
Gilles Peskine59685592018-09-18 12:11:34 +02008895
8896exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 psa_key_derivation_abort(&operation);
8898 psa_destroy_key(our_key);
8899 PSA_DONE();
8900 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02008901}
8902/* END_CASE */
8903
8904/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008905void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02008906{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008907 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008908 unsigned char *output = NULL;
8909 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008910 size_t i;
8911 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008912
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00008914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 ASSERT_ALLOC(output, bytes);
8916 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02008917
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02008919
Gilles Peskinea50d7392018-06-21 10:22:13 +02008920 /* Run several times, to ensure that every output byte will be
8921 * nonzero at least once with overwhelming probability
8922 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 for (run = 0; run < 10; run++) {
8924 if (bytes != 0) {
8925 memset(output, 0, bytes);
8926 }
8927 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02008928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 for (i = 0; i < bytes; i++) {
8930 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02008931 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008933 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008934 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008935
8936 /* Check that every byte was changed to nonzero at least once. This
8937 * validates that psa_generate_random is overwriting every byte of
8938 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 for (i = 0; i < bytes; i++) {
8940 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02008941 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008942
8943exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 PSA_DONE();
8945 mbedtls_free(output);
8946 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02008947}
8948/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008949
8950/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008951void generate_key(int type_arg,
8952 int bits_arg,
8953 int usage_arg,
8954 int alg_arg,
8955 int expected_status_arg,
8956 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02008957{
Ronald Cron5425a212020-08-04 14:58:35 +02008958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008959 psa_key_type_t type = type_arg;
8960 psa_key_usage_t usage = usage_arg;
8961 size_t bits = bits_arg;
8962 psa_algorithm_t alg = alg_arg;
8963 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008965 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008966
Gilles Peskine449bd832023-01-11 14:50:10 +01008967 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02008968
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 psa_set_key_usage_flags(&attributes, usage);
8970 psa_set_key_algorithm(&attributes, alg);
8971 psa_set_key_type(&attributes, type);
8972 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008973
8974 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008975 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008976
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 if (is_large_key > 0) {
8978 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8979 }
8980 TEST_EQUAL(status, expected_status);
8981 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008982 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008984
8985 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
8987 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
8988 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008989
Gilles Peskine818ca122018-06-20 18:16:48 +02008990 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02008992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008994
8995exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008996 /*
8997 * Key attributes may have been returned by psa_get_key_attributes()
8998 * thus reset them as required.
8999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 psa_destroy_key(key);
9003 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009004}
9005/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009006
Ronald Cronee414c72021-03-18 18:50:08 +01009007/* 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 +01009008void generate_key_rsa(int bits_arg,
9009 data_t *e_arg,
9010 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009011{
Ronald Cron5425a212020-08-04 14:58:35 +02009012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009013 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009014 size_t bits = bits_arg;
9015 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9016 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9017 psa_status_t expected_status = expected_status_arg;
9018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9019 uint8_t *exported = NULL;
9020 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009022 size_t exported_length = SIZE_MAX;
9023 uint8_t *e_read_buffer = NULL;
9024 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009026 size_t e_read_length = SIZE_MAX;
9027
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 if (e_arg->len == 0 ||
9029 (e_arg->len == 3 &&
9030 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009031 is_default_public_exponent = 1;
9032 e_read_size = 0;
9033 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 ASSERT_ALLOC(e_read_buffer, e_read_size);
9035 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009036
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009038
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 psa_set_key_usage_flags(&attributes, usage);
9040 psa_set_key_algorithm(&attributes, alg);
9041 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9042 e_arg->x, e_arg->len));
9043 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009044
9045 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9047 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009048 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009050
9051 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9053 TEST_EQUAL(psa_get_key_type(&attributes), type);
9054 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9055 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9056 e_read_buffer, e_read_size,
9057 &e_read_length));
9058 if (is_default_public_exponent) {
9059 TEST_EQUAL(e_read_length, 0);
9060 } else {
9061 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9062 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009063
9064 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009068
9069 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009070 PSA_ASSERT(psa_export_public_key(key,
9071 exported, exported_size,
9072 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009073 {
9074 uint8_t *p = exported;
9075 uint8_t *end = exported + exported_length;
9076 size_t len;
9077 /* RSAPublicKey ::= SEQUENCE {
9078 * modulus INTEGER, -- n
9079 * publicExponent INTEGER } -- e
9080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9082 MBEDTLS_ASN1_SEQUENCE |
9083 MBEDTLS_ASN1_CONSTRUCTED));
9084 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9085 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9086 MBEDTLS_ASN1_INTEGER));
9087 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009088 ++p;
9089 --len;
9090 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009091 if (e_arg->len == 0) {
9092 TEST_EQUAL(len, 3);
9093 TEST_EQUAL(p[0], 1);
9094 TEST_EQUAL(p[1], 0);
9095 TEST_EQUAL(p[2], 1);
9096 } else {
9097 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009098 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009099 }
9100
9101exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009102 /*
9103 * Key attributes may have been returned by psa_get_key_attributes() or
9104 * set by psa_set_key_domain_parameters() thus reset them as required.
9105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009107
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 psa_destroy_key(key);
9109 PSA_DONE();
9110 mbedtls_free(e_read_buffer);
9111 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009112}
9113/* END_CASE */
9114
Darryl Greend49a4992018-06-18 17:27:26 +01009115/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009116void persistent_key_load_key_from_storage(data_t *data,
9117 int type_arg, int bits_arg,
9118 int usage_flags_arg, int alg_arg,
9119 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009120{
Gilles Peskine449bd832023-01-11 14:50:10 +01009121 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9124 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009125 psa_key_type_t type = type_arg;
9126 size_t bits = bits_arg;
9127 psa_key_usage_t usage_flags = usage_flags_arg;
9128 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009129 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009130 unsigned char *first_export = NULL;
9131 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009133 size_t first_exported_length;
9134 size_t second_exported_length;
9135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9137 ASSERT_ALLOC(first_export, export_size);
9138 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009139 }
Darryl Greend49a4992018-06-18 17:27:26 +01009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009142
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 psa_set_key_id(&attributes, key_id);
9144 psa_set_key_usage_flags(&attributes, usage_flags);
9145 psa_set_key_algorithm(&attributes, alg);
9146 psa_set_key_type(&attributes, type);
9147 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009148
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009150 case IMPORT_KEY:
9151 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9153 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009154 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009155
Darryl Green0c6575a2018-11-07 16:05:30 +00009156 case GENERATE_KEY:
9157 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009159 break;
9160
9161 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009162#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 {
9164 /* Create base key */
9165 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9166 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9167 psa_set_key_usage_flags(&base_attributes,
9168 PSA_KEY_USAGE_DERIVE);
9169 psa_set_key_algorithm(&base_attributes, derive_alg);
9170 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9171 PSA_ASSERT(psa_import_key(&base_attributes,
9172 data->x, data->len,
9173 &base_key));
9174 /* Derive a key. */
9175 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9176 PSA_ASSERT(psa_key_derivation_input_key(
9177 &operation,
9178 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9179 PSA_ASSERT(psa_key_derivation_input_bytes(
9180 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9181 NULL, 0));
9182 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9183 &operation,
9184 &key));
9185 PSA_ASSERT(psa_key_derivation_abort(&operation));
9186 PSA_ASSERT(psa_destroy_key(base_key));
9187 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9188 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009189#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009191#endif
9192 break;
9193
9194 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009195 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009196 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009198 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009199
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009200 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9202 PSA_ASSERT(psa_export_key(key,
9203 first_export, export_size,
9204 &first_exported_length));
9205 if (generation_method == IMPORT_KEY) {
9206 ASSERT_COMPARE(data->x, data->len,
9207 first_export, first_exported_length);
9208 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009209 }
Darryl Greend49a4992018-06-18 17:27:26 +01009210
9211 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009213 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009215
Darryl Greend49a4992018-06-18 17:27:26 +01009216 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9218 TEST_ASSERT(mbedtls_svc_key_id_equal(
9219 psa_get_key_id(&attributes), key_id));
9220 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9221 PSA_KEY_LIFETIME_PERSISTENT);
9222 TEST_EQUAL(psa_get_key_type(&attributes), type);
9223 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9224 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9225 mbedtls_test_update_key_usage_flags(usage_flags));
9226 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009227
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009228 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9230 PSA_ASSERT(psa_export_key(key,
9231 second_export, export_size,
9232 &second_exported_length));
9233 ASSERT_COMPARE(first_export, first_exported_length,
9234 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009235 }
9236
9237 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 }
Darryl Greend49a4992018-06-18 17:27:26 +01009241
9242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009243 /*
9244 * Key attributes may have been returned by psa_get_key_attributes()
9245 * thus reset them as required.
9246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009247 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009248
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 mbedtls_free(first_export);
9250 mbedtls_free(second_export);
9251 psa_key_derivation_abort(&operation);
9252 psa_destroy_key(base_key);
9253 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009254 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009255}
9256/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009257
Neil Armstronga557cb82022-06-10 08:58:32 +02009258/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009259void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9260 int primitive_arg, int hash_arg, int role_arg,
9261 int test_input, data_t *pw_data,
9262 int inj_err_type_arg,
9263 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009264{
9265 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9266 psa_pake_operation_t operation = psa_pake_operation_init();
9267 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009268 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009269 psa_key_type_t key_type_pw = key_type_pw_arg;
9270 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009271 psa_algorithm_t hash_alg = hash_arg;
9272 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009275 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9276 psa_status_t expected_error = expected_error_arg;
9277 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009278 unsigned char *output_buffer = NULL;
9279 size_t output_len = 0;
9280
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009282
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009283 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 PSA_PAKE_STEP_KEY_SHARE);
9285 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009286
Gilles Peskine449bd832023-01-11 14:50:10 +01009287 if (pw_data->len > 0) {
9288 psa_set_key_usage_flags(&attributes, key_usage_pw);
9289 psa_set_key_algorithm(&attributes, alg);
9290 psa_set_key_type(&attributes, key_type_pw);
9291 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9292 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009293 }
9294
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9296 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9297 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009298
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009300
Gilles Peskine449bd832023-01-11 14:50:10 +01009301 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9302 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9303 expected_error);
9304 PSA_ASSERT(psa_pake_abort(&operation));
9305 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9306 expected_error);
9307 PSA_ASSERT(psa_pake_abort(&operation));
9308 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9309 expected_error);
9310 PSA_ASSERT(psa_pake_abort(&operation));
9311 TEST_EQUAL(psa_pake_set_role(&operation, role),
9312 expected_error);
9313 PSA_ASSERT(psa_pake_abort(&operation));
9314 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9315 NULL, 0, NULL),
9316 expected_error);
9317 PSA_ASSERT(psa_pake_abort(&operation));
9318 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9319 expected_error);
9320 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009321 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009322 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009323
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 status = psa_pake_setup(&operation, &cipher_suite);
9325 if (status != PSA_SUCCESS) {
9326 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009327 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009328 }
9329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9331 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9332 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009333 goto exit;
9334 }
9335
Gilles Peskine449bd832023-01-11 14:50:10 +01009336 status = psa_pake_set_role(&operation, role);
9337 if (status != PSA_SUCCESS) {
9338 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009339 goto exit;
9340 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009341
Gilles Peskine449bd832023-01-11 14:50:10 +01009342 if (pw_data->len > 0) {
9343 status = psa_pake_set_password_key(&operation, key);
9344 if (status != PSA_SUCCESS) {
9345 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009346 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009347 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009348 }
9349
Gilles Peskine449bd832023-01-11 14:50:10 +01009350 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9351 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9352 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009353 goto exit;
9354 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009355
Gilles Peskine449bd832023-01-11 14:50:10 +01009356 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9357 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9358 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009359 goto exit;
9360 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009361
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009363 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9365 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009366 goto exit;
9367 }
9368
Gilles Peskine449bd832023-01-11 14:50:10 +01009369 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009370 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9372 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009373 goto exit;
9374 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009375
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9377 PSA_PAKE_STEP_KEY_SHARE);
9378 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9379 PSA_PAKE_STEP_ZK_PUBLIC);
9380 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9381 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009382
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 if (test_input) {
9384 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9385 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9386 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009387 goto exit;
9388 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009389
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9391 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9392 output_buffer, size_zk_proof),
9393 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009394 goto exit;
9395 }
9396
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9398 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9399 output_buffer, size_zk_proof),
9400 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009401 goto exit;
9402 }
9403
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9405 output_buffer, size_key_share);
9406 if (status != PSA_SUCCESS) {
9407 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009408 goto exit;
9409 }
9410
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9412 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9413 output_buffer, size_zk_public + 1),
9414 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009415 goto exit;
9416 }
9417
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009419 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9421 output_buffer, size_zk_public + 1);
9422 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9423 output_buffer, size_zk_public),
9424 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009425 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009426 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009427 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9429 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9430 NULL, 0, NULL),
9431 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009432 goto exit;
9433 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009434
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9436 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9437 output_buffer, buf_size, &output_len),
9438 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009439 goto exit;
9440 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009441
Gilles Peskine449bd832023-01-11 14:50:10 +01009442 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9443 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9444 output_buffer, buf_size, &output_len),
9445 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009446 goto exit;
9447 }
9448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9450 output_buffer, buf_size, &output_len);
9451 if (status != PSA_SUCCESS) {
9452 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009453 goto exit;
9454 }
9455
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009457
Gilles Peskine449bd832023-01-11 14:50:10 +01009458 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9459 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9460 output_buffer, size_zk_public - 1, &output_len),
9461 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01009462 goto exit;
9463 }
9464
Gilles Peskine449bd832023-01-11 14:50:10 +01009465 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009466 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9468 output_buffer, size_zk_public - 1, &output_len);
9469 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9470 output_buffer, buf_size, &output_len),
9471 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009472 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009473 }
9474 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009475
9476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 PSA_ASSERT(psa_destroy_key(key));
9478 PSA_ASSERT(psa_pake_abort(&operation));
9479 mbedtls_free(output_buffer);
9480 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009481}
9482/* END_CASE */
9483
Neil Armstronga557cb82022-06-10 08:58:32 +02009484/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009485void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
9486 int client_input_first, int inject_error,
9487 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009488{
9489 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9490 psa_pake_operation_t server = psa_pake_operation_init();
9491 psa_pake_operation_t client = psa_pake_operation_init();
9492 psa_algorithm_t alg = alg_arg;
9493 psa_algorithm_t hash_alg = hash_arg;
9494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9496
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009498
Gilles Peskine449bd832023-01-11 14:50:10 +01009499 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9500 psa_set_key_algorithm(&attributes, alg);
9501 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9502 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9503 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009504
Gilles Peskine449bd832023-01-11 14:50:10 +01009505 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9506 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9507 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009508
9509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9511 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009512
Gilles Peskine449bd832023-01-11 14:50:10 +01009513 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9514 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009515
Gilles Peskine449bd832023-01-11 14:50:10 +01009516 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9517 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009518
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 ecjpake_do_round(alg, primitive_arg, &server, &client,
9520 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009521
Gilles Peskine449bd832023-01-11 14:50:10 +01009522 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 ecjpake_do_round(alg, primitive_arg, &server, &client,
9527 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009528
9529exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009530 psa_destroy_key(key);
9531 psa_pake_abort(&server);
9532 psa_pake_abort(&client);
9533 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009534}
9535/* END_CASE */
9536
9537/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009538void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
9539 int derive_alg_arg, data_t *pw_data,
9540 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009541{
9542 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9543 psa_pake_operation_t server = psa_pake_operation_init();
9544 psa_pake_operation_t client = psa_pake_operation_init();
9545 psa_algorithm_t alg = alg_arg;
9546 psa_algorithm_t hash_alg = hash_arg;
9547 psa_algorithm_t derive_alg = derive_alg_arg;
9548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9550 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009552 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009554 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009555
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009557
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9559 psa_set_key_algorithm(&attributes, alg);
9560 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9561 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9562 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009563
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9565 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9566 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009567
Neil Armstrong1e855602022-06-15 11:32:11 +02009568 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
9570 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02009571
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
9573 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
9574 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
9575 PSA_KEY_DERIVATION_INPUT_SEED,
9576 (const uint8_t *) "", 0));
9577 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
9578 PSA_KEY_DERIVATION_INPUT_SEED,
9579 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02009580 }
9581
Gilles Peskine449bd832023-01-11 14:50:10 +01009582 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9583 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009584
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9586 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009587
Gilles Peskine449bd832023-01-11 14:50:10 +01009588 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9589 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009590
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
9592 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9593 PSA_ERROR_BAD_STATE);
9594 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9595 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009596 goto exit;
9597 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009598
Neil Armstrongf983caf2022-06-15 15:27:48 +02009599 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009600 ecjpake_do_round(alg, primitive_arg, &server, &client,
9601 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
9604 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9605 PSA_ERROR_BAD_STATE);
9606 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9607 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009608 goto exit;
9609 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009610
Neil Armstrongf983caf2022-06-15 15:27:48 +02009611 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 ecjpake_do_round(alg, primitive_arg, &server, &client,
9613 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009614
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
9616 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009617
9618exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 psa_key_derivation_abort(&server_derive);
9620 psa_key_derivation_abort(&client_derive);
9621 psa_destroy_key(key);
9622 psa_pake_abort(&server);
9623 psa_pake_abort(&client);
9624 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009625}
9626/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009627
9628/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009629void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009630{
9631 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9632 const size_t bits = 256;
9633 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009635 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01009636 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009637
9638 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9639 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009640 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9641 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
9642 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9643 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009644 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9646 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009647
9648 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01009649 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9650 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
9651 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9652 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
9653 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9654 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009655
9656 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01009657 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9658 PSA_PAKE_OUTPUT_MAX_SIZE);
9659 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9660 PSA_PAKE_OUTPUT_MAX_SIZE);
9661 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9662 PSA_PAKE_OUTPUT_MAX_SIZE);
9663 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9664 PSA_PAKE_INPUT_MAX_SIZE);
9665 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9666 PSA_PAKE_INPUT_MAX_SIZE);
9667 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9668 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009669}
9670/* END_CASE */