blob: 5f6aa42d5ccb91f217d197fa9ea60d49da031e3e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
Gilles Peskine8e94efe2021-02-13 00:25:53 +010016#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010017#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010018#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053019#if defined(PSA_CRYPTO_DRIVER_TEST)
20#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053021#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
22#else
23#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053024#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020025#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010026
Gilles Peskine4023c012021-05-27 13:21:20 +020027/* If this comes up, it's a bug in the test code or in the test data. */
28#define UNUSED 0xdeadbeef
29
Dave Rodgman647791d2021-06-23 12:49:59 +010030/* Assert that an operation is (not) active.
31 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010032#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
33#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010034
Przemek Stekiel7c795482022-11-15 22:26:12 +010035#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010036int ecjpake_operation_setup(psa_pake_operation_t *operation,
37 psa_pake_cipher_suite_t *cipher_suite,
38 psa_pake_role_t role,
39 mbedtls_svc_key_id_t key,
40 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010041{
Gilles Peskine449bd832023-01-11 14:50:10 +010042 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (key_available) {
49 PSA_ASSERT(psa_pake_set_password_key(operation, key));
50 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010051 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010052exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010053 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010054}
55#endif
56
Jaeden Amerof24c7f82018-06-27 17:20:43 +010057/** An invalid export length that will never be set by psa_export_key(). */
58static const size_t INVALID_EXPORT_LENGTH = ~0U;
59
Gilles Peskinea7aa4422018-08-14 15:17:54 +020060/** Test if a buffer contains a constant byte value.
61 *
62 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020063 *
64 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020065 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 * \param size Size of the buffer in bytes.
67 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020068 * \return 1 if the buffer is all-bits-zero.
69 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072{
73 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 for (i = 0; i < size; i++) {
75 if (((unsigned char *) buffer)[i] != c) {
76 return 0;
77 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020078 }
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010081#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020082/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010083static int asn1_write_10x(unsigned char **p,
84 unsigned char *start,
85 size_t bits,
86 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020087{
88 int ret;
89 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (bits == 0) {
91 return MBEDTLS_ERR_ASN1_INVALID_DATA;
92 }
93 if (bits <= 8 && x >= 1 << (bits - 1)) {
94 return MBEDTLS_ERR_ASN1_INVALID_DATA;
95 }
96 if (*p < start || *p - start < (ptrdiff_t) len) {
97 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
98 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020099 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 (*p)[len-1] = x;
101 if (bits % 8 == 0) {
102 (*p)[1] |= 1;
103 } else {
104 (*p)[0] |= 1 << (bits % 8);
105 }
106 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
108 MBEDTLS_ASN1_INTEGER));
109 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200110}
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112static int construct_fake_rsa_key(unsigned char *buffer,
113 size_t buffer_size,
114 unsigned char **p,
115 size_t bits,
116 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200117{
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200119 int ret;
120 int len = 0;
121 /* Construct something that looks like a DER encoding of
122 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
123 * RSAPrivateKey ::= SEQUENCE {
124 * version Version,
125 * modulus INTEGER, -- n
126 * publicExponent INTEGER, -- e
127 * privateExponent INTEGER, -- d
128 * prime1 INTEGER, -- p
129 * prime2 INTEGER, -- q
130 * exponent1 INTEGER, -- d mod (p-1)
131 * exponent2 INTEGER, -- d mod (q-1)
132 * coefficient INTEGER, -- (inverse of q) mod p
133 * otherPrimeInfos OtherPrimeInfos OPTIONAL
134 * }
135 * Or, for a public key, the same structure with only
136 * version, modulus and publicExponent.
137 */
138 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (keypair) {
140 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
141 asn1_write_10x(p, buffer, half_bits, 1));
142 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
143 asn1_write_10x(p, buffer, half_bits, 1));
144 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* q */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
149 asn1_write_10x(p, buffer, half_bits, 3));
150 MBEDTLS_ASN1_CHK_ADD(len, /* d */
151 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
154 asn1_write_10x(p, buffer, 17, 1));
155 MBEDTLS_ASN1_CHK_ADD(len, /* n */
156 asn1_write_10x(p, buffer, bits, 1));
157 if (keypair) {
158 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
159 mbedtls_asn1_write_int(p, buffer, 0));
160 }
161 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200162 {
163 const unsigned char tag =
164 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200168}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100169#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int exercise_mac_setup(psa_key_type_t key_type,
172 const unsigned char *key_bytes,
173 size_t key_length,
174 psa_algorithm_t alg,
175 psa_mac_operation_t *operation,
176 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100177{
Ronald Cron5425a212020-08-04 14:58:35 +0200178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
182 psa_set_key_algorithm(&attributes, alg);
183 psa_set_key_type(&attributes, key_type);
184 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100189 /* If setup failed, reproduce the failure, so that the caller can
190 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (*status != PSA_SUCCESS) {
192 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 psa_destroy_key(key);
196 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197
198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201}
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203int exercise_cipher_setup(psa_key_type_t key_type,
204 const unsigned char *key_bytes,
205 size_t key_length,
206 psa_algorithm_t alg,
207 psa_cipher_operation_t *operation,
208 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100209{
Ronald Cron5425a212020-08-04 14:58:35 +0200210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
214 psa_set_key_algorithm(&attributes, alg);
215 psa_set_key_type(&attributes, key_type);
216 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100219 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 /* If setup failed, reproduce the failure, so that the caller can
222 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (*status != PSA_SUCCESS) {
224 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
225 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 psa_destroy_key(key);
229 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230
231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234}
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237{
238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 uint8_t buffer[1];
241 size_t length;
242 int ok = 0;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 psa_set_key_id(&attributes, key_id);
245 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
246 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
247 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
248 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
249 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
254 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
255 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
256 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
257 TEST_EQUAL(psa_get_key_type(&attributes), 0);
258 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
261 PSA_ERROR_INVALID_HANDLE);
262 TEST_EQUAL(psa_export_public_key(key,
263 buffer, sizeof(buffer), &length),
264 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200265
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200266 ok = 1;
267
268exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100269 /*
270 * Key attributes may have been returned by psa_get_key_attributes()
271 * thus reset them as required.
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200276}
277
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200278/* Assert that a key isn't reported as having a slot number. */
279#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200281 do \
282 { \
283 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 TEST_EQUAL(psa_get_key_slot_number( \
285 attributes, \
286 &ASSERT_NO_SLOT_NUMBER_slot_number), \
287 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200288 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200290#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291#define ASSERT_NO_SLOT_NUMBER(attributes) \
292 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
294
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100295/* An overapproximation of the amount of storage needed for a key of the
296 * given type and with the given content. The API doesn't make it easy
297 * to find a good value for the size. The current implementation doesn't
298 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299#define KEY_BITS_FROM_DATA(type, data) \
300 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301
Darryl Green0c6575a2018-11-07 16:05:30 +0000302typedef enum {
303 IMPORT_KEY = 0,
304 GENERATE_KEY = 1,
305 DERIVE_KEY = 2
306} generate_method;
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100309 DO_NOT_SET_LENGTHS = 0,
310 SET_LENGTHS_BEFORE_NONCE = 1,
311 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100312} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100315 USE_NULL_TAG = 0,
316 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100317} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319/*!
320 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321 * \param key_type_arg Type of key passed in
322 * \param key_data The encryption / decryption key data
323 * \param alg_arg The type of algorithm used
324 * \param nonce Nonce data
325 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100326 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 * feed additional data in to be encrypted /
328 * decrypted. If -1, no chunking.
329 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100330 * \param data_part_len_arg If not -1, the length of chunks to feed
331 * the data in to be encrypted / decrypted. If
332 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100333 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100334 * expected here, this controls whether or not
335 * to set lengths, and in what order with
336 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100339 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100340 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
344 int alg_arg,
345 data_t *nonce,
346 data_t *additional_data,
347 int ad_part_len_arg,
348 data_t *input_data,
349 int data_part_len_arg,
350 set_lengths_method_t set_lengths_method,
351 data_t *expected_output,
352 int is_encrypt,
353 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100354{
355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
356 psa_key_type_t key_type = key_type_arg;
357 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100358 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100359 unsigned char *output_data = NULL;
360 unsigned char *part_data = NULL;
361 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100363 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100364 size_t output_size = 0;
365 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 size_t output_length = 0;
367 size_t key_bits = 0;
368 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100369 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_length = 0;
371 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100372 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100373 size_t ad_part_len = 0;
374 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
377 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
378
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (is_encrypt) {
385 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
386 } else {
387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100389
390 psa_set_key_algorithm(&attributes, alg);
391 psa_set_key_type(&attributes, key_type);
392
393 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
394 &key));
395
396 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
397 key_bits = psa_get_key_bits(&attributes);
398
399 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
400
401 if (is_encrypt) {
402 /* Tag gets written at end of buffer. */
403 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
404 (input_data->len +
405 tag_length));
406 data_true_size = input_data->len;
407 } else {
408 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
409 (input_data->len -
410 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100411
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100412 /* Do not want to attempt to decrypt tag. */
413 data_true_size = input_data->len - tag_length;
414 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (is_encrypt) {
419 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
420 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
421 } else {
422 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
423 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100424 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (is_encrypt) {
429 status = psa_aead_encrypt_setup(&operation, key, alg);
430 } else {
431 status = psa_aead_decrypt_setup(&operation, key, alg);
432 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
434 /* If the operation is not supported, just skip and not fail in case the
435 * encryption involves a common limitation of cryptography hardwares and
436 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (status == PSA_ERROR_NOT_SUPPORTED) {
438 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
439 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
445 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
446 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
447 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
448 data_true_size));
449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
450 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
451 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
454 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100459 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100462 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 part_offset += part_length, part_count++) {
464 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100465 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100467 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100469 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 }
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 PSA_ASSERT(psa_aead_update_ad(&operation,
473 additional_data->x + part_offset,
474 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100475
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
480 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 data_part_len = (size_t) data_part_len_arg;
486 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
487 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100492 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 part_offset += part_length, part_count++) {
494 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100495 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if ((data_true_size - part_offset) < data_part_len) {
497 part_length = (data_true_size - part_offset);
498 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 PSA_ASSERT(psa_aead_update(&operation,
503 (input_data->x + part_offset),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (output_data && output_part_length) {
509 memcpy((output_data + output_length), part_data,
510 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 }
512
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 output_length += output_part_length;
514 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100516 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
518 data_true_size, output_data,
519 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (is_encrypt) {
523 PSA_ASSERT(psa_aead_finish(&operation, final_data,
524 final_output_size,
525 &output_part_length,
526 tag_buffer, tag_length,
527 &tag_size));
528 } else {
529 PSA_ASSERT(psa_aead_verify(&operation, final_data,
530 final_output_size,
531 &output_part_length,
532 (input_data->x + data_true_size),
533 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (output_data && output_part_length) {
537 memcpy((output_data + output_length), final_data,
538 output_part_length);
539 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100540
541 output_length += output_part_length;
542
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100543
544 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
545 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (is_encrypt) {
547 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (output_data && tag_length) {
550 memcpy((output_data + output_length), tag_buffer,
551 tag_length);
552 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553
554 output_length += tag_length;
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 TEST_EQUAL(output_length,
557 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
558 input_data->len));
559 TEST_LE_U(output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
561 } else {
562 TEST_EQUAL(output_length,
563 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
564 input_data->len));
565 TEST_LE_U(output_length,
566 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100567 }
568
Paul Elliottd3f82412021-06-16 16:52:21 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 ASSERT_COMPARE(expected_output->x, expected_output->len,
571 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100572
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100574 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 psa_destroy_key(key);
578 psa_aead_abort(&operation);
579 mbedtls_free(output_data);
580 mbedtls_free(part_data);
581 mbedtls_free(final_data);
582 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100585}
586
Neil Armstrong4766f992022-02-28 16:23:59 +0100587/*!
588 * \brief Internal Function for MAC multipart tests.
589 * \param key_type_arg Type of key passed in
590 * \param key_data The encryption / decryption key data
591 * \param alg_arg The type of algorithm used
592 * \param input_data Data to encrypt / decrypt
593 * \param data_part_len_arg If not -1, the length of chunks to feed
594 * the data in to be encrypted / decrypted. If
595 * -1, no chunking
596 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000597 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100598 * \param do_zero_parts If non-zero, interleave zero length chunks
599 * with normal length chunks.
600 * \return int Zero on failure, non-zero on success.
601 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
603 int alg_arg,
604 data_t *input_data,
605 int data_part_len_arg,
606 data_t *expected_output,
607 int is_verify,
608 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100609{
610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
611 psa_key_type_t key_type = key_type_arg;
612 psa_algorithm_t alg = alg_arg;
613 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
614 unsigned char mac[PSA_MAC_MAX_SIZE];
615 size_t part_offset = 0;
616 size_t part_length = 0;
617 size_t data_part_len = 0;
618 size_t mac_len = 0;
619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
621
622 int test_ok = 0;
623 size_t part_count = 0;
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (is_verify) {
628 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
629 } else {
630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
631 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 psa_set_key_algorithm(&attributes, alg);
634 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
637 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (is_verify) {
640 status = psa_mac_verify_setup(&operation, key, alg);
641 } else {
642 status = psa_mac_sign_setup(&operation, key, alg);
643 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100648 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100652 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 part_offset += part_length, part_count++) {
654 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else if ((input_data->len - part_offset) < data_part_len) {
657 part_length = (input_data->len - part_offset);
658 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_length = data_part_len;
660 }
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 PSA_ASSERT(psa_mac_update(&operation,
663 (input_data->x + part_offset),
664 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100667 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
669 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (is_verify) {
673 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
674 expected_output->len));
675 } else {
676 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
677 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 ASSERT_COMPARE(expected_output->x, expected_output->len,
680 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100681 }
682
683 test_ok = 1;
684
685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 psa_destroy_key(key);
687 psa_mac_abort(&operation);
688 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100691}
692
Neil Armstrong75673ab2022-06-15 17:39:01 +0200693#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
695 psa_pake_operation_t *server,
696 psa_pake_operation_t *client,
697 int client_input_first,
698 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200699{
700 unsigned char *buffer0 = NULL, *buffer1 = NULL;
701 size_t buffer_length = (
702 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
703 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
704 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200705 /* The output should be exactly this size according to the spec */
706 const size_t expected_size_key_share =
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
708 /* The output should be exactly this size according to the spec */
709 const size_t expected_size_zk_public =
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
711 /* The output can be smaller: the spec allows stripping leading zeroes */
712 const size_t max_expected_size_zk_proof =
713 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200714 size_t buffer0_off = 0;
715 size_t buffer1_off = 0;
716 size_t s_g1_len, s_g2_len, s_a_len;
717 size_t s_g1_off, s_g2_off, s_a_off;
718 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
719 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
720 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
721 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
722 size_t c_g1_len, c_g2_len, c_a_len;
723 size_t c_g1_off, c_g2_off, c_a_off;
724 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
725 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
726 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
727 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
728 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200729 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 ASSERT_ALLOC(buffer0, buffer_length);
732 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735 case 1:
736 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
738 buffer0 + buffer0_off,
739 512 - buffer0_off, &s_g1_len));
740 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200741 s_g1_off = buffer0_off;
742 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
744 buffer0 + buffer0_off,
745 512 - buffer0_off, &s_x1_pk_len));
746 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200747 s_x1_pk_off = buffer0_off;
748 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
750 buffer0 + buffer0_off,
751 512 - buffer0_off, &s_x1_pr_len));
752 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200753 s_x1_pr_off = buffer0_off;
754 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_g2_len));
758 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200759 s_g2_off = buffer0_off;
760 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
762 buffer0 + buffer0_off,
763 512 - buffer0_off, &s_x2_pk_len));
764 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200765 s_x2_pk_off = buffer0_off;
766 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
768 buffer0 + buffer0_off,
769 512 - buffer0_off, &s_x2_pr_len));
770 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200771 s_x2_pr_off = buffer0_off;
772 buffer0_off += s_x2_pr_len;
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500775 buffer0[s_x1_pr_off + 8] ^= 1;
776 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200777 expected_status = PSA_ERROR_DATA_INVALID;
778 }
779
Neil Armstrong51009d72022-09-05 17:59:54 +0200780 /*
781 * When injecting errors in inputs, the implementation is
782 * free to detect it right away of with a delay.
783 * This permits delaying the error until the end of the input
784 * sequence, if no error appears then, this will be treated
785 * as an error.
786 */
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200789 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
791 buffer0 + s_g1_off, s_g1_len);
792 if (inject_error == 1 && status != PSA_SUCCESS) {
793 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200794 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 } else {
796 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200797 }
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
800 buffer0 + s_x1_pk_off,
801 s_x1_pk_len);
802 if (inject_error == 1 && status != PSA_SUCCESS) {
803 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 } else {
806 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200807 }
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
810 buffer0 + s_x1_pr_off,
811 s_x1_pr_len);
812 if (inject_error == 1 && status != PSA_SUCCESS) {
813 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 } else {
816 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200817 }
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
820 buffer0 + s_g2_off,
821 s_g2_len);
822 if (inject_error == 1 && status != PSA_SUCCESS) {
823 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 } else {
826 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
830 buffer0 + s_x2_pk_off,
831 s_x2_pk_len);
832 if (inject_error == 1 && status != PSA_SUCCESS) {
833 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 } else {
836 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
840 buffer0 + s_x2_pr_off,
841 s_x2_pr_len);
842 if (inject_error == 1 && status != PSA_SUCCESS) {
843 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 } else {
846 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200847 }
848
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200849 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (inject_error == 1) {
851 TEST_ASSERT(
852 !"One of the last psa_pake_input() calls should have returned the expected error.");
853 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200854 }
855
856 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
858 buffer1 + buffer1_off,
859 512 - buffer1_off, &c_g1_len));
860 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 c_g1_off = buffer1_off;
862 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
864 buffer1 + buffer1_off,
865 512 - buffer1_off, &c_x1_pk_len));
866 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200867 c_x1_pk_off = buffer1_off;
868 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
870 buffer1 + buffer1_off,
871 512 - buffer1_off, &c_x1_pr_len));
872 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200873 c_x1_pr_off = buffer1_off;
874 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
876 buffer1 + buffer1_off,
877 512 - buffer1_off, &c_g2_len));
878 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200879 c_g2_off = buffer1_off;
880 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
882 buffer1 + buffer1_off,
883 512 - buffer1_off, &c_x2_pk_len));
884 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200885 c_x2_pk_off = buffer1_off;
886 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
888 buffer1 + buffer1_off,
889 512 - buffer1_off, &c_x2_pr_len));
890 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200891 c_x2_pr_off = buffer1_off;
892 buffer1_off += c_x2_pr_len;
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
897 buffer0 + s_g1_off, s_g1_len);
898 if (inject_error == 1 && status != PSA_SUCCESS) {
899 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200900 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 } else {
902 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
906 buffer0 + s_x1_pk_off,
907 s_x1_pk_len);
908 if (inject_error == 1 && status != PSA_SUCCESS) {
909 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 } else {
912 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200913 }
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
916 buffer0 + s_x1_pr_off,
917 s_x1_pr_len);
918 if (inject_error == 1 && status != PSA_SUCCESS) {
919 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 } else {
922 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200923 }
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
926 buffer0 + s_g2_off,
927 s_g2_len);
928 if (inject_error == 1 && status != PSA_SUCCESS) {
929 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 } else {
932 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200933 }
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
936 buffer0 + s_x2_pk_off,
937 s_x2_pk_len);
938 if (inject_error == 1 && status != PSA_SUCCESS) {
939 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 } else {
942 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200943 }
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
946 buffer0 + s_x2_pr_off,
947 s_x2_pr_len);
948 if (inject_error == 1 && status != PSA_SUCCESS) {
949 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 } else {
952 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200953 }
954
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200955 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (inject_error == 1) {
957 TEST_ASSERT(
958 !"One of the last psa_pake_input() calls should have returned the expected error.");
959 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500963 buffer1[c_x1_pr_off + 12] ^= 1;
964 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200965 expected_status = PSA_ERROR_DATA_INVALID;
966 }
967
968 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
970 buffer1 + c_g1_off, c_g1_len);
971 if (inject_error == 2 && status != PSA_SUCCESS) {
972 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200973 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 } else {
975 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
979 buffer1 + c_x1_pk_off, c_x1_pk_len);
980 if (inject_error == 2 && status != PSA_SUCCESS) {
981 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200982 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 } else {
984 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200985 }
986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
988 buffer1 + c_x1_pr_off, c_x1_pr_len);
989 if (inject_error == 2 && status != PSA_SUCCESS) {
990 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200991 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 } else {
993 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200994 }
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
997 buffer1 + c_g2_off, c_g2_len);
998 if (inject_error == 2 && status != PSA_SUCCESS) {
999 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001000 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 } else {
1002 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1006 buffer1 + c_x2_pk_off, c_x2_pk_len);
1007 if (inject_error == 2 && status != PSA_SUCCESS) {
1008 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 } else {
1011 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001012 }
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1015 buffer1 + c_x2_pr_off, c_x2_pr_len);
1016 if (inject_error == 2 && status != PSA_SUCCESS) {
1017 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001018 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 } else {
1020 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001021 }
1022
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001023 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if (inject_error == 2) {
1025 TEST_ASSERT(
1026 !"One of the last psa_pake_input() calls should have returned the expected error.");
1027 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001028
1029 break;
1030
1031 case 2:
1032 /* Server second round Output */
1033 buffer0_off = 0;
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1036 buffer0 + buffer0_off,
1037 512 - buffer0_off, &s_a_len));
1038 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001039 s_a_off = buffer0_off;
1040 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1042 buffer0 + buffer0_off,
1043 512 - buffer0_off, &s_x2s_pk_len));
1044 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001045 s_x2s_pk_off = buffer0_off;
1046 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1048 buffer0 + buffer0_off,
1049 512 - buffer0_off, &s_x2s_pr_len));
1050 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001051 s_x2s_pr_off = buffer0_off;
1052 buffer0_off += s_x2s_pr_len;
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001055 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001056 expected_status = PSA_ERROR_DATA_INVALID;
1057 }
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001060 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1062 buffer0 + s_a_off, s_a_len);
1063 if (inject_error == 3 && status != PSA_SUCCESS) {
1064 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001065 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 } else {
1067 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1071 buffer0 + s_x2s_pk_off,
1072 s_x2s_pk_len);
1073 if (inject_error == 3 && status != PSA_SUCCESS) {
1074 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 } else {
1077 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001078 }
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1081 buffer0 + s_x2s_pr_off,
1082 s_x2s_pr_len);
1083 if (inject_error == 3 && status != PSA_SUCCESS) {
1084 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 } else {
1087 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001088 }
1089
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001090 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (inject_error == 3) {
1092 TEST_ASSERT(
1093 !"One of the last psa_pake_input() calls should have returned the expected error.");
1094 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001095 }
1096
1097 /* Client second round Output */
1098 buffer1_off = 0;
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1101 buffer1 + buffer1_off,
1102 512 - buffer1_off, &c_a_len));
1103 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001104 c_a_off = buffer1_off;
1105 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1107 buffer1 + buffer1_off,
1108 512 - buffer1_off, &c_x2s_pk_len));
1109 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001110 c_x2s_pk_off = buffer1_off;
1111 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1113 buffer1 + buffer1_off,
1114 512 - buffer1_off, &c_x2s_pr_len));
1115 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001116 c_x2s_pr_off = buffer1_off;
1117 buffer1_off += c_x2s_pr_len;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1122 buffer0 + s_a_off, s_a_len);
1123 if (inject_error == 3 && status != PSA_SUCCESS) {
1124 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001125 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 } else {
1127 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001128 }
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1131 buffer0 + s_x2s_pk_off,
1132 s_x2s_pk_len);
1133 if (inject_error == 3 && status != PSA_SUCCESS) {
1134 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else {
1137 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001138 }
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1141 buffer0 + s_x2s_pr_off,
1142 s_x2s_pr_len);
1143 if (inject_error == 3 && status != PSA_SUCCESS) {
1144 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 } else {
1147 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001148 }
1149
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001150 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (inject_error == 3) {
1152 TEST_ASSERT(
1153 !"One of the last psa_pake_input() calls should have returned the expected error.");
1154 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001155 }
1156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001158 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001159 expected_status = PSA_ERROR_DATA_INVALID;
1160 }
1161
1162 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1164 buffer1 + c_a_off, c_a_len);
1165 if (inject_error == 4 && status != PSA_SUCCESS) {
1166 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001167 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 } else {
1169 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1173 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1174 if (inject_error == 4 && status != PSA_SUCCESS) {
1175 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001176 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 } else {
1178 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001179 }
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1182 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1183 if (inject_error == 4 && status != PSA_SUCCESS) {
1184 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001185 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 } else {
1187 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001188 }
1189
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001190 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (inject_error == 4) {
1192 TEST_ASSERT(
1193 !"One of the last psa_pake_input() calls should have returned the expected error.");
1194 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001195
1196 break;
1197
1198 }
1199
Neil Armstrongf983caf2022-06-15 15:27:48 +02001200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_free(buffer0);
1202 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001203}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001204#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001207 INJECT_ERR_NONE = 0,
1208 INJECT_ERR_UNINITIALIZED_ACCESS,
1209 INJECT_ERR_DUPLICATE_SETUP,
1210 INJECT_ERR_INVALID_USER,
1211 INJECT_ERR_INVALID_PEER,
1212 INJECT_ERR_SET_USER,
1213 INJECT_ERR_SET_PEER,
1214 INJECT_EMPTY_IO_BUFFER,
1215 INJECT_UNKNOWN_STEP,
1216 INJECT_INVALID_FIRST_STEP,
1217 INJECT_WRONG_BUFFER_SIZE,
1218 INJECT_VALID_OPERATION_AFTER_FAILURE,
1219 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1220 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1221} ecjpake_injected_failure_t;
1222
Paul Elliott1243f932023-02-07 11:21:10 +00001223#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
1224 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
1225 defined(MBEDTLS_ECP_RESTARTABLE)
1226
Paul Elliott6f600372023-02-06 18:41:05 +00001227static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1228 psa_status_t expected_status,
1229 size_t *min_completes,
1230 size_t *max_completes)
1231{
1232
1233 /* This is slightly contrived, but we only really know that with a minimum
1234 value of max_ops that a successful operation should take more than one op
1235 to complete, and likewise that with a max_ops of
1236 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1237 if (max_ops == 0 || max_ops == 1) {
1238 /* Failure test cases will fail on the first op. */
1239 if (expected_status == PSA_SUCCESS) {
1240 *min_completes = 2;
1241 } else {
1242 *min_completes = 1;
1243 }
1244
1245 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1246 } else {
1247 *min_completes = 1;
1248 *max_completes = 1;
1249 }
1250}
Paul Elliott1243f932023-02-07 11:21:10 +00001251#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
1252 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
1253 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott6f600372023-02-06 18:41:05 +00001254
Gilles Peskinee59236f2018-01-27 23:32:46 +01001255/* END_HEADER */
1256
1257/* BEGIN_DEPENDENCIES
1258 * depends_on:MBEDTLS_PSA_CRYPTO_C
1259 * END_DEPENDENCIES
1260 */
1261
1262/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001264{
1265 size_t max_truncated_mac_size =
1266 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1267
1268 /* Check that the length for a truncated MAC always fits in the algorithm
1269 * encoding. The shifted mask is the maximum truncated value. The
1270 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001272}
1273/* END_CASE */
1274
1275/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276void import_with_policy(int type_arg,
1277 int usage_arg, int alg_arg,
1278 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001279{
1280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1281 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001283 psa_key_type_t type = type_arg;
1284 psa_key_usage_t usage = usage_arg;
1285 psa_algorithm_t alg = alg_arg;
1286 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001288 psa_status_t status;
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 psa_set_key_type(&attributes, type);
1293 psa_set_key_usage_flags(&attributes, usage);
1294 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 status = psa_import_key(&attributes,
1297 key_material, sizeof(key_material),
1298 &key);
1299 TEST_EQUAL(status, expected_status);
1300 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1305 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1306 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1307 mbedtls_test_update_key_usage_flags(usage));
1308 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1309 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 PSA_ASSERT(psa_destroy_key(key));
1312 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001313
1314exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001315 /*
1316 * Key attributes may have been returned by psa_get_key_attributes()
1317 * thus reset them as required.
1318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 psa_destroy_key(key);
1322 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001323}
1324/* END_CASE */
1325
1326/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001327void import_with_data(data_t *data, int type_arg,
1328 int attr_bits_arg,
1329 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001330{
1331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1332 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001334 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001335 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001336 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 psa_set_key_type(&attributes, type);
1342 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 status = psa_import_key(&attributes, data->x, data->len, &key);
1345 TEST_EQUAL(status, expected_status);
1346 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1351 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1352 if (attr_bits != 0) {
1353 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1354 }
1355 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 PSA_ASSERT(psa_destroy_key(key));
1358 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359
1360exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001361 /*
1362 * Key attributes may have been returned by psa_get_key_attributes()
1363 * thus reset them as required.
1364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 psa_destroy_key(key);
1368 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369}
1370/* END_CASE */
1371
1372/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001373/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374void import_large_key(int type_arg, int byte_size_arg,
1375 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001376{
1377 psa_key_type_t type = type_arg;
1378 size_t byte_size = byte_size_arg;
1379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1380 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001382 psa_status_t status;
1383 uint8_t *buffer = NULL;
1384 size_t buffer_size = byte_size + 1;
1385 size_t n;
1386
Steven Cooreman69967ce2021-01-18 18:01:08 +01001387 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001388 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1390 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001393
1394 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1396 psa_set_key_type(&attributes, type);
1397 status = psa_import_key(&attributes, buffer, byte_size, &key);
1398 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1399 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (status == PSA_SUCCESS) {
1402 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1403 TEST_EQUAL(psa_get_key_type(&attributes), type);
1404 TEST_EQUAL(psa_get_key_bits(&attributes),
1405 PSA_BYTES_TO_BITS(byte_size));
1406 ASSERT_NO_SLOT_NUMBER(&attributes);
1407 memset(buffer, 0, byte_size + 1);
1408 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1409 for (n = 0; n < byte_size; n++) {
1410 TEST_EQUAL(buffer[n], 'K');
1411 }
1412 for (n = byte_size; n < buffer_size; n++) {
1413 TEST_EQUAL(buffer[n], 0);
1414 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001415 }
1416
1417exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001418 /*
1419 * Key attributes may have been returned by psa_get_key_attributes()
1420 * thus reset them as required.
1421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 psa_destroy_key(key);
1425 PSA_DONE();
1426 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001427}
1428/* END_CASE */
1429
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001430/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001431/* Import an RSA key with a valid structure (but not valid numbers
1432 * inside, beyond having sensible size and parity). This is expected to
1433 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001434void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001435{
Ronald Cron5425a212020-08-04 14:58:35 +02001436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001437 size_t bits = bits_arg;
1438 psa_status_t expected_status = expected_status_arg;
1439 psa_status_t status;
1440 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001441 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001442 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001444 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001445 unsigned char *p;
1446 int ret;
1447 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 PSA_ASSERT(psa_crypto_init());
1451 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1454 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001455 length = ret;
1456
1457 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 psa_set_key_type(&attributes, type);
1459 status = psa_import_key(&attributes, p, length, &key);
1460 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (status == PSA_SUCCESS) {
1463 PSA_ASSERT(psa_destroy_key(key));
1464 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001465
1466exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 mbedtls_free(buffer);
1468 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001469}
1470/* END_CASE */
1471
1472/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473void import_export(data_t *data,
1474 int type_arg,
1475 int usage_arg, int alg_arg,
1476 int lifetime_arg,
1477 int expected_bits,
1478 int export_size_delta,
1479 int expected_export_status_arg,
1480 /*whether reexport must give the original input exactly*/
1481 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482{
Ronald Cron5425a212020-08-04 14:58:35 +02001483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001485 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001486 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001487 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301488 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489 unsigned char *exported = NULL;
1490 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001491 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001492 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001493 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001495 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001496
Moran Pekercb088e72018-07-17 17:36:59 +03001497 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 ASSERT_ALLOC(exported, export_size);
1499 if (!canonical_input) {
1500 ASSERT_ALLOC(reexported, export_size);
1501 }
1502 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 psa_set_key_lifetime(&attributes, lifetime);
1505 psa_set_key_usage_flags(&attributes, usage_arg);
1506 psa_set_key_algorithm(&attributes, alg);
1507 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001508
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001509 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511
1512 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1514 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1515 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1516 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001517
1518 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 status = psa_export_key(key, exported, export_size, &exported_length);
1520 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001521
1522 /* The exported length must be set by psa_export_key() to a value between 0
1523 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1525 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1526 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1529 export_size - exported_length));
1530 if (status != PSA_SUCCESS) {
1531 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001533 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534
Gilles Peskineea38a922021-02-13 00:05:16 +01001535 /* Run sanity checks on the exported key. For non-canonical inputs,
1536 * this validates the canonical representations. For canonical inputs,
1537 * this doesn't directly validate the implementation, but it still helps
1538 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (!psa_key_lifetime_is_external(lifetime)) {
1540 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301541 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 }
Archana4d7ae1d2021-07-07 02:50:22 +05301543 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (canonical_input) {
1546 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1547 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001548 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1550 &key2));
1551 PSA_ASSERT(psa_export_key(key2,
1552 reexported,
1553 export_size,
1554 &reexported_length));
1555 ASSERT_COMPARE(exported, exported_length,
1556 reexported, reexported_length);
1557 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 TEST_LE_U(exported_length,
1560 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1561 psa_get_key_bits(&got_attributes)));
1562 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563
1564destroy:
1565 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 PSA_ASSERT(psa_destroy_key(key));
1567 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568
1569exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001570 /*
1571 * Key attributes may have been returned by psa_get_key_attributes()
1572 * thus reset them as required.
1573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 psa_reset_key_attributes(&got_attributes);
1575 psa_destroy_key(key);
1576 mbedtls_free(exported);
1577 mbedtls_free(reexported);
1578 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001579}
1580/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001581
Moran Pekerf709f4a2018-06-06 17:26:04 +03001582/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001583void import_export_public_key(data_t *data,
1584 int type_arg, // key pair or public key
1585 int alg_arg,
1586 int lifetime_arg,
1587 int export_size_delta,
1588 int expected_export_status_arg,
1589 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001590{
Ronald Cron5425a212020-08-04 14:58:35 +02001591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001592 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001593 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001594 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001595 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301596 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001597 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001598 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001599 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 psa_set_key_lifetime(&attributes, lifetime);
1605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1606 psa_set_key_algorithm(&attributes, alg);
1607 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001608
1609 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001611
Gilles Peskine49c25912018-10-29 15:15:31 +01001612 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 ASSERT_ALLOC(exported, export_size);
1614 status = psa_export_public_key(key,
1615 exported, export_size,
1616 &exported_length);
1617 TEST_EQUAL(status, expected_export_status);
1618 if (status == PSA_SUCCESS) {
1619 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001620 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1622 bits = psa_get_key_bits(&attributes);
1623 TEST_LE_U(expected_public_key->len,
1624 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1625 TEST_LE_U(expected_public_key->len,
1626 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1627 TEST_LE_U(expected_public_key->len,
1628 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1629 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1630 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001631 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001633 /*
1634 * Key attributes may have been returned by psa_get_key_attributes()
1635 * thus reset them as required.
1636 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_free(exported);
1640 psa_destroy_key(key);
1641 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001642}
1643/* END_CASE */
1644
Gilles Peskine20035e32018-02-03 22:44:14 +01001645/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646void import_and_exercise_key(data_t *data,
1647 int type_arg,
1648 int bits_arg,
1649 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001650{
Ronald Cron5425a212020-08-04 14:58:35 +02001651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001652 psa_key_type_t type = type_arg;
1653 size_t bits = bits_arg;
1654 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001657 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 psa_set_key_usage_flags(&attributes, usage);
1662 psa_set_key_algorithm(&attributes, alg);
1663 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001664
1665 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001667
1668 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1670 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1671 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001672
1673 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 PSA_ASSERT(psa_destroy_key(key));
1679 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001680
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001682 /*
1683 * Key attributes may have been returned by psa_get_key_attributes()
1684 * thus reset them as required.
1685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 psa_reset_key_attributes(&attributes);
1689 psa_destroy_key(key);
1690 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691}
1692/* END_CASE */
1693
1694/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001695void effective_key_attributes(int type_arg, int expected_type_arg,
1696 int bits_arg, int expected_bits_arg,
1697 int usage_arg, int expected_usage_arg,
1698 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001699{
Ronald Cron5425a212020-08-04 14:58:35 +02001700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001701 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001702 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001703 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001704 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001705 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001706 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001707 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001708 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 psa_set_key_usage_flags(&attributes, usage);
1714 psa_set_key_algorithm(&attributes, alg);
1715 psa_set_key_type(&attributes, key_type);
1716 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 PSA_ASSERT(psa_generate_key(&attributes, &key));
1719 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1722 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1723 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1724 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1725 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001726
1727exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001728 /*
1729 * Key attributes may have been returned by psa_get_key_attributes()
1730 * thus reset them as required.
1731 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 psa_destroy_key(key);
1735 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001736}
1737/* END_CASE */
1738
1739/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001740void check_key_policy(int type_arg, int bits_arg,
1741 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001742{
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1744 usage_arg,
1745 mbedtls_test_update_key_usage_flags(usage_arg),
1746 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001747 goto exit;
1748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001752void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001753{
1754 /* Test each valid way of initializing the object, except for `= {0}`, as
1755 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1756 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001757 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001759 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1760 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1765 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1766 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 TEST_EQUAL(psa_get_key_type(&func), 0);
1769 TEST_EQUAL(psa_get_key_type(&init), 0);
1770 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 TEST_EQUAL(psa_get_key_bits(&func), 0);
1773 TEST_EQUAL(psa_get_key_bits(&init), 0);
1774 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1777 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1778 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1781 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1782 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001783}
1784/* END_CASE */
1785
1786/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787void mac_key_policy(int policy_usage_arg,
1788 int policy_alg_arg,
1789 int key_type_arg,
1790 data_t *key_data,
1791 int exercise_alg_arg,
1792 int expected_status_sign_arg,
1793 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001794{
Ronald Cron5425a212020-08-04 14:58:35 +02001795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001797 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001798 psa_key_type_t key_type = key_type_arg;
1799 psa_algorithm_t policy_alg = policy_alg_arg;
1800 psa_algorithm_t exercise_alg = exercise_alg_arg;
1801 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001802 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001803 psa_status_t expected_status_sign = expected_status_sign_arg;
1804 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001805 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 psa_set_key_usage_flags(&attributes, policy_usage);
1810 psa_set_key_algorithm(&attributes, policy_alg);
1811 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1814 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1817 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1820 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001821
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001822 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001824 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1826 input, 128,
1827 mac, PSA_MAC_MAX_SIZE, &mac_len),
1828 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001829
Neil Armstrong3af9b972022-02-07 12:20:21 +01001830 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 PSA_ASSERT(psa_mac_abort(&operation));
1832 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1833 if (status == PSA_SUCCESS) {
1834 status = psa_mac_update(&operation, input, 128);
1835 if (status == PSA_SUCCESS) {
1836 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1837 &mac_len),
1838 expected_status_sign);
1839 } else {
1840 TEST_EQUAL(status, expected_status_sign);
1841 }
1842 } else {
1843 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001844 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001846
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001847 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 status = psa_mac_verify(key, exercise_alg, input, 128,
1849 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1852 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1853 } else {
1854 TEST_EQUAL(status, expected_status_verify);
1855 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001856
Neil Armstrong3af9b972022-02-07 12:20:21 +01001857 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1859 if (status == PSA_SUCCESS) {
1860 status = psa_mac_update(&operation, input, 128);
1861 if (status == PSA_SUCCESS) {
1862 status = psa_mac_verify_finish(&operation, mac, mac_len);
1863 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1864 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1865 } else {
1866 TEST_EQUAL(status, expected_status_verify);
1867 }
1868 } else {
1869 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001870 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 } else {
1872 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001873 }
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 memset(mac, 0, sizeof(mac));
1878 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1879 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001880
1881exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 psa_mac_abort(&operation);
1883 psa_destroy_key(key);
1884 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001885}
1886/* END_CASE */
1887
1888/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001889void cipher_key_policy(int policy_usage_arg,
1890 int policy_alg,
1891 int key_type,
1892 data_t *key_data,
1893 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001894{
Ronald Cron5425a212020-08-04 14:58:35 +02001895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001897 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001898 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001899 size_t output_buffer_size = 0;
1900 size_t input_buffer_size = 0;
1901 size_t output_length = 0;
1902 uint8_t *output = NULL;
1903 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904 psa_status_t status;
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1907 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1908 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 ASSERT_ALLOC(input, input_buffer_size);
1911 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 psa_set_key_usage_flags(&attributes, policy_usage);
1916 psa_set_key_algorithm(&attributes, policy_alg);
1917 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1920 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001922 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 TEST_EQUAL(policy_usage,
1924 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001925
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001926 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1928 output, output_buffer_size,
1929 &output_length);
1930 if (policy_alg == exercise_alg &&
1931 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1932 PSA_ASSERT(status);
1933 } else {
1934 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1935 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001936
1937 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1939 if (policy_alg == exercise_alg &&
1940 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1941 PSA_ASSERT(status);
1942 } else {
1943 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1944 }
1945 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001947 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1949 input, input_buffer_size,
1950 &output_length);
1951 if (policy_alg == exercise_alg &&
1952 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1953 PSA_ASSERT(status);
1954 } else {
1955 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1956 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001957
1958 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1960 if (policy_alg == exercise_alg &&
1961 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1962 PSA_ASSERT(status);
1963 } else {
1964 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1965 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966
1967exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 psa_cipher_abort(&operation);
1969 mbedtls_free(input);
1970 mbedtls_free(output);
1971 psa_destroy_key(key);
1972 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973}
1974/* END_CASE */
1975
1976/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977void aead_key_policy(int policy_usage_arg,
1978 int policy_alg,
1979 int key_type,
1980 data_t *key_data,
1981 int nonce_length_arg,
1982 int tag_length_arg,
1983 int exercise_alg,
1984 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985{
Ronald Cron5425a212020-08-04 14:58:35 +02001986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001987 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001988 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001989 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001991 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993 size_t nonce_length = nonce_length_arg;
1994 unsigned char tag[16];
1995 size_t tag_length = tag_length_arg;
1996 size_t output_length;
1997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 TEST_LE_U(nonce_length, sizeof(nonce));
1999 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 psa_set_key_usage_flags(&attributes, policy_usage);
2004 psa_set_key_algorithm(&attributes, policy_alg);
2005 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2008 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002010 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 TEST_EQUAL(policy_usage,
2012 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002013
Neil Armstrong752d8112022-02-07 14:51:11 +01002014 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_aead_encrypt(key, exercise_alg,
2016 nonce, nonce_length,
2017 NULL, 0,
2018 NULL, 0,
2019 tag, tag_length,
2020 &output_length);
2021 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2022 TEST_EQUAL(status, expected_status);
2023 } else {
2024 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2025 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026
Neil Armstrong752d8112022-02-07 14:51:11 +01002027 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2029 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2030 TEST_EQUAL(status, expected_status);
2031 } else {
2032 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2033 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002034
2035 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 memset(tag, 0, sizeof(tag));
2037 status = psa_aead_decrypt(key, exercise_alg,
2038 nonce, nonce_length,
2039 NULL, 0,
2040 tag, tag_length,
2041 NULL, 0,
2042 &output_length);
2043 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2044 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2045 } else if (expected_status == PSA_SUCCESS) {
2046 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2047 } else {
2048 TEST_EQUAL(status, expected_status);
2049 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050
Neil Armstrong752d8112022-02-07 14:51:11 +01002051 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 PSA_ASSERT(psa_aead_abort(&operation));
2053 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2054 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2055 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2056 } else {
2057 TEST_EQUAL(status, expected_status);
2058 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002059
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002060exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 PSA_ASSERT(psa_aead_abort(&operation));
2062 psa_destroy_key(key);
2063 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064}
2065/* END_CASE */
2066
2067/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068void asymmetric_encryption_key_policy(int policy_usage_arg,
2069 int policy_alg,
2070 int key_type,
2071 data_t *key_data,
2072 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073{
Ronald Cron5425a212020-08-04 14:58:35 +02002074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002076 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077 psa_status_t status;
2078 size_t key_bits;
2079 size_t buffer_length;
2080 unsigned char *buffer = NULL;
2081 size_t output_length;
2082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 psa_set_key_usage_flags(&attributes, policy_usage);
2086 psa_set_key_algorithm(&attributes, policy_alg);
2087 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2090 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002092 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 TEST_EQUAL(policy_usage,
2094 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2097 key_bits = psa_get_key_bits(&attributes);
2098 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2099 exercise_alg);
2100 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 status = psa_asymmetric_encrypt(key, exercise_alg,
2103 NULL, 0,
2104 NULL, 0,
2105 buffer, buffer_length,
2106 &output_length);
2107 if (policy_alg == exercise_alg &&
2108 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2109 PSA_ASSERT(status);
2110 } else {
2111 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2112 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (buffer_length != 0) {
2115 memset(buffer, 0, buffer_length);
2116 }
2117 status = psa_asymmetric_decrypt(key, exercise_alg,
2118 buffer, buffer_length,
2119 NULL, 0,
2120 buffer, buffer_length,
2121 &output_length);
2122 if (policy_alg == exercise_alg &&
2123 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2124 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2125 } else {
2126 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2127 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
2129exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002130 /*
2131 * Key attributes may have been returned by psa_get_key_attributes()
2132 * thus reset them as required.
2133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 psa_destroy_key(key);
2137 PSA_DONE();
2138 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139}
2140/* END_CASE */
2141
2142/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002143void asymmetric_signature_key_policy(int policy_usage_arg,
2144 int policy_alg,
2145 int key_type,
2146 data_t *key_data,
2147 int exercise_alg,
2148 int payload_length_arg,
2149 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150{
Ronald Cron5425a212020-08-04 14:58:35 +02002151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002153 psa_key_usage_t policy_usage = policy_usage_arg;
2154 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002155 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002157 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2158 * compatible with the policy and `payload_length_arg` is supposed to be
2159 * a valid input length to sign. If `payload_length_arg <= 0`,
2160 * `exercise_alg` is supposed to be forbidden by the policy. */
2161 int compatible_alg = payload_length_arg > 0;
2162 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002164 size_t signature_length;
2165
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002166 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002167 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 TEST_EQUAL(expected_usage,
2169 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 psa_set_key_usage_flags(&attributes, policy_usage);
2174 psa_set_key_algorithm(&attributes, policy_alg);
2175 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2178 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 status = psa_sign_hash(key, exercise_alg,
2183 payload, payload_length,
2184 signature, sizeof(signature),
2185 &signature_length);
2186 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2187 PSA_ASSERT(status);
2188 } else {
2189 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2190 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 memset(signature, 0, sizeof(signature));
2193 status = psa_verify_hash(key, exercise_alg,
2194 payload, payload_length,
2195 signature, sizeof(signature));
2196 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2197 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2198 } else {
2199 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2200 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2203 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2204 status = psa_sign_message(key, exercise_alg,
2205 payload, payload_length,
2206 signature, sizeof(signature),
2207 &signature_length);
2208 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2209 PSA_ASSERT(status);
2210 } else {
2211 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2212 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 memset(signature, 0, sizeof(signature));
2215 status = psa_verify_message(key, exercise_alg,
2216 payload, payload_length,
2217 signature, sizeof(signature));
2218 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2219 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2220 } else {
2221 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2222 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002223 }
2224
Gilles Peskined5b33222018-06-18 22:20:03 +02002225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 psa_destroy_key(key);
2227 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002228}
2229/* END_CASE */
2230
Janos Follathba3fab92019-06-11 14:50:16 +01002231/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232void derive_key_policy(int policy_usage,
2233 int policy_alg,
2234 int key_type,
2235 data_t *key_data,
2236 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002237{
Ronald Cron5425a212020-08-04 14:58:35 +02002238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002240 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002241 psa_status_t status;
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 psa_set_key_usage_flags(&attributes, policy_usage);
2246 psa_set_key_algorithm(&attributes, policy_alg);
2247 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2250 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2255 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2256 PSA_ASSERT(psa_key_derivation_input_bytes(
2257 &operation,
2258 PSA_KEY_DERIVATION_INPUT_SEED,
2259 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002260 }
Janos Follathba3fab92019-06-11 14:50:16 +01002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 status = psa_key_derivation_input_key(&operation,
2263 PSA_KEY_DERIVATION_INPUT_SECRET,
2264 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (policy_alg == exercise_alg &&
2267 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2268 PSA_ASSERT(status);
2269 } else {
2270 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2271 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272
2273exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 psa_key_derivation_abort(&operation);
2275 psa_destroy_key(key);
2276 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002277}
2278/* END_CASE */
2279
2280/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281void agreement_key_policy(int policy_usage,
2282 int policy_alg,
2283 int key_type_arg,
2284 data_t *key_data,
2285 int exercise_alg,
2286 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287{
Ronald Cron5425a212020-08-04 14:58:35 +02002288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002290 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002291 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002292 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002293 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 psa_set_key_usage_flags(&attributes, policy_usage);
2298 psa_set_key_algorithm(&attributes, policy_alg);
2299 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2302 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2305 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002308
2309exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 psa_key_derivation_abort(&operation);
2311 psa_destroy_key(key);
2312 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002313}
2314/* END_CASE */
2315
2316/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002317void key_policy_alg2(int key_type_arg, data_t *key_data,
2318 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002319{
Ronald Cron5425a212020-08-04 14:58:35 +02002320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002321 psa_key_type_t key_type = key_type_arg;
2322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2323 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2324 psa_key_usage_t usage = usage_arg;
2325 psa_algorithm_t alg = alg_arg;
2326 psa_algorithm_t alg2 = alg2_arg;
2327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 psa_set_key_usage_flags(&attributes, usage);
2331 psa_set_key_algorithm(&attributes, alg);
2332 psa_set_key_enrollment_algorithm(&attributes, alg2);
2333 psa_set_key_type(&attributes, key_type);
2334 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2335 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002336
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002337 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 usage = mbedtls_test_update_key_usage_flags(usage);
2339 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2340 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2341 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2342 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002345 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 }
2347 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002348 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002350
2351exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002352 /*
2353 * Key attributes may have been returned by psa_get_key_attributes()
2354 * thus reset them as required.
2355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 psa_destroy_key(key);
2359 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002360}
2361/* END_CASE */
2362
2363/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002364void raw_agreement_key_policy(int policy_usage,
2365 int policy_alg,
2366 int key_type_arg,
2367 data_t *key_data,
2368 int exercise_alg,
2369 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002370{
Ronald Cron5425a212020-08-04 14:58:35 +02002371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002373 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002374 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002376 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 psa_set_key_usage_flags(&attributes, policy_usage);
2381 psa_set_key_algorithm(&attributes, policy_alg);
2382 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2385 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002390
2391exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 psa_key_derivation_abort(&operation);
2393 psa_destroy_key(key);
2394 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002395}
2396/* END_CASE */
2397
2398/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399void copy_success(int source_usage_arg,
2400 int source_alg_arg, int source_alg2_arg,
2401 unsigned int source_lifetime_arg,
2402 int type_arg, data_t *material,
2403 int copy_attributes,
2404 int target_usage_arg,
2405 int target_alg_arg, int target_alg2_arg,
2406 unsigned int target_lifetime_arg,
2407 int expected_usage_arg,
2408 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002409{
Gilles Peskineca25db92019-04-19 11:43:08 +02002410 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2411 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002412 psa_key_usage_t expected_usage = expected_usage_arg;
2413 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002414 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302415 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2416 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002417 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2418 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419 uint8_t *export_buffer = NULL;
2420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002422
Gilles Peskineca25db92019-04-19 11:43:08 +02002423 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2425 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2426 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2427 psa_set_key_type(&source_attributes, type_arg);
2428 psa_set_key_lifetime(&source_attributes, source_lifetime);
2429 PSA_ASSERT(psa_import_key(&source_attributes,
2430 material->x, material->len,
2431 &source_key));
2432 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002433
Gilles Peskineca25db92019-04-19 11:43:08 +02002434 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002436 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002437 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (target_usage_arg != -1) {
2441 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2442 }
2443 if (target_alg_arg != -1) {
2444 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2445 }
2446 if (target_alg2_arg != -1) {
2447 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2448 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449
Archana8a180362021-07-05 02:18:48 +05302450
Gilles Peskine57ab7212019-01-28 13:03:09 +01002451 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 PSA_ASSERT(psa_copy_key(source_key,
2453 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454
2455 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457
2458 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2460 TEST_EQUAL(psa_get_key_type(&source_attributes),
2461 psa_get_key_type(&target_attributes));
2462 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2463 psa_get_key_bits(&target_attributes));
2464 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2465 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2466 TEST_EQUAL(expected_alg2,
2467 psa_get_key_enrollment_algorithm(&target_attributes));
2468 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002469 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 ASSERT_ALLOC(export_buffer, material->len);
2471 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2472 material->len, &length));
2473 ASSERT_COMPARE(material->x, material->len,
2474 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002475 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (!psa_key_lifetime_is_external(target_lifetime)) {
2478 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302479 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 }
2481 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302482 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 }
Archana8a180362021-07-05 02:18:48 +05302484 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002487
2488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002489 /*
2490 * Source and target key attributes may have been returned by
2491 * psa_get_key_attributes() thus reset them as required.
2492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 psa_reset_key_attributes(&source_attributes);
2494 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 PSA_DONE();
2497 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002498}
2499/* END_CASE */
2500
2501/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502void copy_fail(int source_usage_arg,
2503 int source_alg_arg, int source_alg2_arg,
2504 int source_lifetime_arg,
2505 int type_arg, data_t *material,
2506 int target_type_arg, int target_bits_arg,
2507 int target_usage_arg,
2508 int target_alg_arg, int target_alg2_arg,
2509 int target_id_arg, int target_lifetime_arg,
2510 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002511{
2512 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2513 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002514 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2515 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002519
2520 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2522 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2523 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2524 psa_set_key_type(&source_attributes, type_arg);
2525 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2526 PSA_ASSERT(psa_import_key(&source_attributes,
2527 material->x, material->len,
2528 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002529
2530 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 psa_set_key_id(&target_attributes, key_id);
2532 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2533 psa_set_key_type(&target_attributes, target_type_arg);
2534 psa_set_key_bits(&target_attributes, target_bits_arg);
2535 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2536 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2537 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002538
2539 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 TEST_EQUAL(psa_copy_key(source_key,
2541 &target_attributes, &target_key),
2542 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002545
Gilles Peskine4a644642019-05-03 17:14:08 +02002546exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 psa_reset_key_attributes(&source_attributes);
2548 psa_reset_key_attributes(&target_attributes);
2549 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002550}
2551/* END_CASE */
2552
2553/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002555{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002556 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002557 /* Test each valid way of initializing the object, except for `= {0}`, as
2558 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2559 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002560 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002562 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2563 psa_hash_operation_t zero;
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002566
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002567 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2569 PSA_ERROR_BAD_STATE);
2570 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2571 PSA_ERROR_BAD_STATE);
2572 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2573 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002574
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002575 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 PSA_ASSERT(psa_hash_abort(&func));
2577 PSA_ASSERT(psa_hash_abort(&init));
2578 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002579}
2580/* END_CASE */
2581
2582/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583void hash_setup(int alg_arg,
2584 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585{
2586 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002587 uint8_t *output = NULL;
2588 size_t output_size = 0;
2589 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002590 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002591 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002592 psa_status_t status;
2593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002595
Neil Armstrongedb20862022-02-07 15:47:44 +01002596 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 output_size = PSA_HASH_LENGTH(alg);
2598 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 status = psa_hash_compute(alg, NULL, 0,
2601 output, output_size, &output_length);
2602 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002603
2604 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 status = psa_hash_setup(&operation, alg);
2606 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002608 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002610
2611 /* If setup failed, reproduce the failure, so as to
2612 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (status != PSA_SUCCESS) {
2614 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2615 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002616
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002617 /* Now the operation object should be reusable. */
2618#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2620 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002621#endif
2622
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002623exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 mbedtls_free(output);
2625 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626}
2627/* END_CASE */
2628
2629/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002630void hash_compute_fail(int alg_arg, data_t *input,
2631 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002632{
2633 psa_algorithm_t alg = alg_arg;
2634 uint8_t *output = NULL;
2635 size_t output_size = output_size_arg;
2636 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002637 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002638 psa_status_t expected_status = expected_status_arg;
2639 psa_status_t status;
2640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002644
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002645 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 status = psa_hash_compute(alg, input->x, input->len,
2647 output, output_size, &output_length);
2648 TEST_EQUAL(status, expected_status);
2649 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002650
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002651 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 status = psa_hash_setup(&operation, alg);
2653 if (status == PSA_SUCCESS) {
2654 status = psa_hash_update(&operation, input->x, input->len);
2655 if (status == PSA_SUCCESS) {
2656 status = psa_hash_finish(&operation, output, output_size,
2657 &output_length);
2658 if (status == PSA_SUCCESS) {
2659 TEST_LE_U(output_length, output_size);
2660 } else {
2661 TEST_EQUAL(status, expected_status);
2662 }
2663 } else {
2664 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002665 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 } else {
2667 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002668 }
2669
Gilles Peskine0a749c82019-11-28 19:33:58 +01002670exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 PSA_ASSERT(psa_hash_abort(&operation));
2672 mbedtls_free(output);
2673 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002674}
2675/* END_CASE */
2676
2677/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002678void hash_compare_fail(int alg_arg, data_t *input,
2679 data_t *reference_hash,
2680 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002681{
2682 psa_algorithm_t alg = alg_arg;
2683 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002684 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002685 psa_status_t status;
2686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002688
Neil Armstrong55a1be12022-02-07 11:23:20 +01002689 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 status = psa_hash_compare(alg, input->x, input->len,
2691 reference_hash->x, reference_hash->len);
2692 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002693
Neil Armstrong55a1be12022-02-07 11:23:20 +01002694 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 status = psa_hash_setup(&operation, alg);
2696 if (status == PSA_SUCCESS) {
2697 status = psa_hash_update(&operation, input->x, input->len);
2698 if (status == PSA_SUCCESS) {
2699 status = psa_hash_verify(&operation, reference_hash->x,
2700 reference_hash->len);
2701 TEST_EQUAL(status, expected_status);
2702 } else {
2703 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002704 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 } else {
2706 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002707 }
2708
Gilles Peskine88e08462020-01-28 20:43:00 +01002709exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 PSA_ASSERT(psa_hash_abort(&operation));
2711 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002712}
2713/* END_CASE */
2714
2715/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002716void hash_compute_compare(int alg_arg, data_t *input,
2717 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002718{
2719 psa_algorithm_t alg = alg_arg;
2720 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2721 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002722 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002723 size_t i;
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002726
Neil Armstrongca30a002022-02-07 11:40:23 +01002727 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2729 output, PSA_HASH_LENGTH(alg),
2730 &output_length));
2731 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2732 ASSERT_COMPARE(output, output_length,
2733 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002734
Neil Armstrongca30a002022-02-07 11:40:23 +01002735 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 PSA_ASSERT(psa_hash_setup(&operation, alg));
2737 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2738 PSA_ASSERT(psa_hash_finish(&operation, output,
2739 PSA_HASH_LENGTH(alg),
2740 &output_length));
2741 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2742 ASSERT_COMPARE(output, output_length,
2743 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002744
2745 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2747 output, sizeof(output),
2748 &output_length));
2749 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2750 ASSERT_COMPARE(output, output_length,
2751 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002752
Neil Armstrongca30a002022-02-07 11:40:23 +01002753 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 PSA_ASSERT(psa_hash_setup(&operation, alg));
2755 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2756 PSA_ASSERT(psa_hash_finish(&operation, output,
2757 sizeof(output), &output_length));
2758 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2759 ASSERT_COMPARE(output, output_length,
2760 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002761
2762 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2764 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765
Neil Armstrongca30a002022-02-07 11:40:23 +01002766 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 PSA_ASSERT(psa_hash_setup(&operation, alg));
2768 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2769 PSA_ASSERT(psa_hash_verify(&operation, output,
2770 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002771
2772 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2774 output, output_length + 1),
2775 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002776
Neil Armstrongca30a002022-02-07 11:40:23 +01002777 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 PSA_ASSERT(psa_hash_setup(&operation, alg));
2779 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2780 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2781 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002782
2783 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2785 output, output_length - 1),
2786 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002787
Neil Armstrongca30a002022-02-07 11:40:23 +01002788 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 PSA_ASSERT(psa_hash_setup(&operation, alg));
2790 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2791 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2792 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002793
Gilles Peskine0a749c82019-11-28 19:33:58 +01002794 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 for (i = 0; i < output_length; i++) {
2796 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002797 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002798
2799 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2801 output, output_length),
2802 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002803
2804 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 PSA_ASSERT(psa_hash_setup(&operation, alg));
2806 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2807 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2808 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002809
Gilles Peskine0a749c82019-11-28 19:33:58 +01002810 output[i] ^= 1;
2811 }
2812
2813exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 PSA_ASSERT(psa_hash_abort(&operation));
2815 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002816}
2817/* END_CASE */
2818
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002819/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002820void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002821{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002822 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002823 unsigned char input[] = "";
2824 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002825 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002826 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2827 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2829 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002830 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002831 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002832 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002835
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002836 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 PSA_ASSERT(psa_hash_setup(&operation, alg));
2838 ASSERT_OPERATION_IS_ACTIVE(operation);
2839 TEST_EQUAL(psa_hash_setup(&operation, alg),
2840 PSA_ERROR_BAD_STATE);
2841 ASSERT_OPERATION_IS_INACTIVE(operation);
2842 PSA_ASSERT(psa_hash_abort(&operation));
2843 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002844
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002845 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2847 PSA_ERROR_BAD_STATE);
2848 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002849
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002850 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002852 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 ASSERT_OPERATION_IS_ACTIVE(operation);
2854 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2855 PSA_ERROR_BAD_STATE);
2856 ASSERT_OPERATION_IS_INACTIVE(operation);
2857 PSA_ASSERT(psa_hash_abort(&operation));
2858 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002859
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002860 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 PSA_ASSERT(psa_hash_setup(&operation, alg));
2862 PSA_ASSERT(psa_hash_finish(&operation,
2863 hash, sizeof(hash), &hash_len));
2864 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2865 PSA_ERROR_BAD_STATE);
2866 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002867
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002868 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 TEST_EQUAL(psa_hash_verify(&operation,
2870 valid_hash, sizeof(valid_hash)),
2871 PSA_ERROR_BAD_STATE);
2872 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002873
2874 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 PSA_ASSERT(psa_hash_setup(&operation, alg));
2876 PSA_ASSERT(psa_hash_finish(&operation,
2877 hash, sizeof(hash), &hash_len));
2878 TEST_EQUAL(psa_hash_verify(&operation,
2879 valid_hash, sizeof(valid_hash)),
2880 PSA_ERROR_BAD_STATE);
2881 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002882
2883 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 PSA_ASSERT(psa_hash_setup(&operation, alg));
2885 ASSERT_OPERATION_IS_ACTIVE(operation);
2886 PSA_ASSERT(psa_hash_verify(&operation,
2887 valid_hash, sizeof(valid_hash)));
2888 ASSERT_OPERATION_IS_INACTIVE(operation);
2889 TEST_EQUAL(psa_hash_verify(&operation,
2890 valid_hash, sizeof(valid_hash)),
2891 PSA_ERROR_BAD_STATE);
2892 ASSERT_OPERATION_IS_INACTIVE(operation);
2893 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002894
2895 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 TEST_EQUAL(psa_hash_finish(&operation,
2897 hash, sizeof(hash), &hash_len),
2898 PSA_ERROR_BAD_STATE);
2899 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002900
2901 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 PSA_ASSERT(psa_hash_setup(&operation, alg));
2903 PSA_ASSERT(psa_hash_finish(&operation,
2904 hash, sizeof(hash), &hash_len));
2905 TEST_EQUAL(psa_hash_finish(&operation,
2906 hash, sizeof(hash), &hash_len),
2907 PSA_ERROR_BAD_STATE);
2908 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002909
2910 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 PSA_ASSERT(psa_hash_setup(&operation, alg));
2912 PSA_ASSERT(psa_hash_verify(&operation,
2913 valid_hash, sizeof(valid_hash)));
2914 TEST_EQUAL(psa_hash_finish(&operation,
2915 hash, sizeof(hash), &hash_len),
2916 PSA_ERROR_BAD_STATE);
2917 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002918
2919exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002921}
2922/* END_CASE */
2923
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002924/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002926{
2927 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002928 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2929 * appended to it */
2930 unsigned char hash[] = {
2931 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2932 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2934 };
2935 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002936 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002939
itayzafrir27e69452018-11-01 14:26:34 +02002940 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 PSA_ASSERT(psa_hash_setup(&operation, alg));
2942 ASSERT_OPERATION_IS_ACTIVE(operation);
2943 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2944 PSA_ERROR_INVALID_SIGNATURE);
2945 ASSERT_OPERATION_IS_INACTIVE(operation);
2946 PSA_ASSERT(psa_hash_abort(&operation));
2947 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002948
itayzafrir27e69452018-11-01 14:26:34 +02002949 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 PSA_ASSERT(psa_hash_setup(&operation, alg));
2951 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2952 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002953
itayzafrir27e69452018-11-01 14:26:34 +02002954 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 PSA_ASSERT(psa_hash_setup(&operation, alg));
2956 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2957 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002958
itayzafrirec93d302018-10-18 18:01:10 +03002959exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002961}
2962/* END_CASE */
2963
Ronald Cronee414c72021-03-18 18:50:08 +01002964/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002966{
2967 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002968 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002970 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002971 size_t hash_len;
2972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002974
itayzafrir58028322018-10-25 10:22:01 +03002975 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 PSA_ASSERT(psa_hash_setup(&operation, alg));
2977 TEST_EQUAL(psa_hash_finish(&operation,
2978 hash, expected_size - 1, &hash_len),
2979 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002980
2981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002983}
2984/* END_CASE */
2985
Ronald Cronee414c72021-03-18 18:50:08 +01002986/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002988{
2989 psa_algorithm_t alg = PSA_ALG_SHA_256;
2990 unsigned char hash[PSA_HASH_MAX_SIZE];
2991 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2992 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2993 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2994 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2995 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2996 size_t hash_len;
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 PSA_ASSERT(psa_crypto_init());
2999 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3002 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3003 PSA_ASSERT(psa_hash_finish(&op_finished,
3004 hash, sizeof(hash), &hash_len));
3005 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3006 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3009 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3012 PSA_ASSERT(psa_hash_finish(&op_init,
3013 hash, sizeof(hash), &hash_len));
3014 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3015 PSA_ASSERT(psa_hash_finish(&op_finished,
3016 hash, sizeof(hash), &hash_len));
3017 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3018 PSA_ASSERT(psa_hash_finish(&op_aborted,
3019 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003020
3021exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 psa_hash_abort(&op_source);
3023 psa_hash_abort(&op_init);
3024 psa_hash_abort(&op_setup);
3025 psa_hash_abort(&op_finished);
3026 psa_hash_abort(&op_aborted);
3027 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003028}
3029/* END_CASE */
3030
Ronald Cronee414c72021-03-18 18:50:08 +01003031/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003032void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003033{
3034 psa_algorithm_t alg = PSA_ALG_SHA_256;
3035 unsigned char hash[PSA_HASH_MAX_SIZE];
3036 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3037 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3038 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3039 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3040 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3041 size_t hash_len;
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3046 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3047 PSA_ASSERT(psa_hash_finish(&op_finished,
3048 hash, sizeof(hash), &hash_len));
3049 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3050 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3053 PSA_ASSERT(psa_hash_finish(&op_target,
3054 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3057 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3058 PSA_ERROR_BAD_STATE);
3059 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3060 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003061
3062exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 psa_hash_abort(&op_target);
3064 psa_hash_abort(&op_init);
3065 psa_hash_abort(&op_setup);
3066 psa_hash_abort(&op_finished);
3067 psa_hash_abort(&op_aborted);
3068 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003069}
3070/* END_CASE */
3071
itayzafrir58028322018-10-25 10:22:01 +03003072/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003073void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003074{
Jaeden Amero252ef282019-02-15 14:05:35 +00003075 const uint8_t input[1] = { 0 };
3076
Jaeden Amero769ce272019-01-04 11:48:03 +00003077 /* Test each valid way of initializing the object, except for `= {0}`, as
3078 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3079 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003080 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003082 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3083 psa_mac_operation_t zero;
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003086
Jaeden Amero252ef282019-02-15 14:05:35 +00003087 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 TEST_EQUAL(psa_mac_update(&func,
3089 input, sizeof(input)),
3090 PSA_ERROR_BAD_STATE);
3091 TEST_EQUAL(psa_mac_update(&init,
3092 input, sizeof(input)),
3093 PSA_ERROR_BAD_STATE);
3094 TEST_EQUAL(psa_mac_update(&zero,
3095 input, sizeof(input)),
3096 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003097
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003098 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 PSA_ASSERT(psa_mac_abort(&func));
3100 PSA_ASSERT(psa_mac_abort(&init));
3101 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003102}
3103/* END_CASE */
3104
3105/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003106void mac_setup(int key_type_arg,
3107 data_t *key,
3108 int alg_arg,
3109 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003110{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003111 psa_key_type_t key_type = key_type_arg;
3112 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003113 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003114 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003115 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3116#if defined(KNOWN_SUPPORTED_MAC_ALG)
3117 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3118#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3123 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 }
3126 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003127
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003128 /* The operation object should be reusable. */
3129#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3131 smoke_test_key_data,
3132 sizeof(smoke_test_key_data),
3133 KNOWN_SUPPORTED_MAC_ALG,
3134 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 }
3137 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003138#endif
3139
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003142}
3143/* END_CASE */
3144
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003145/* 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 +01003146void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003147{
Ronald Cron5425a212020-08-04 14:58:35 +02003148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003149 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3150 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003151 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003152 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3153 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3155 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003157 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3158 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3159 size_t sign_mac_length = 0;
3160 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3161 const uint8_t verify_mac[] = {
3162 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3163 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3165 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 PSA_ASSERT(psa_crypto_init());
3168 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3169 psa_set_key_algorithm(&attributes, alg);
3170 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3173 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003174
Jaeden Amero252ef282019-02-15 14:05:35 +00003175 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3177 PSA_ERROR_BAD_STATE);
3178 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003179
3180 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3182 &sign_mac_length),
3183 PSA_ERROR_BAD_STATE);
3184 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003185
3186 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 TEST_EQUAL(psa_mac_verify_finish(&operation,
3188 verify_mac, sizeof(verify_mac)),
3189 PSA_ERROR_BAD_STATE);
3190 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003191
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003192 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3194 ASSERT_OPERATION_IS_ACTIVE(operation);
3195 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3196 PSA_ERROR_BAD_STATE);
3197 ASSERT_OPERATION_IS_INACTIVE(operation);
3198 PSA_ASSERT(psa_mac_abort(&operation));
3199 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003200
Jaeden Amero252ef282019-02-15 14:05:35 +00003201 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3203 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3204 PSA_ASSERT(psa_mac_sign_finish(&operation,
3205 sign_mac, sizeof(sign_mac),
3206 &sign_mac_length));
3207 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3208 PSA_ERROR_BAD_STATE);
3209 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003210
3211 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3213 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3214 PSA_ASSERT(psa_mac_verify_finish(&operation,
3215 verify_mac, sizeof(verify_mac)));
3216 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3217 PSA_ERROR_BAD_STATE);
3218 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003219
3220 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3222 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3223 PSA_ASSERT(psa_mac_sign_finish(&operation,
3224 sign_mac, sizeof(sign_mac),
3225 &sign_mac_length));
3226 TEST_EQUAL(psa_mac_sign_finish(&operation,
3227 sign_mac, sizeof(sign_mac),
3228 &sign_mac_length),
3229 PSA_ERROR_BAD_STATE);
3230 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003231
3232 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3234 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3235 PSA_ASSERT(psa_mac_verify_finish(&operation,
3236 verify_mac, sizeof(verify_mac)));
3237 TEST_EQUAL(psa_mac_verify_finish(&operation,
3238 verify_mac, sizeof(verify_mac)),
3239 PSA_ERROR_BAD_STATE);
3240 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003241
3242 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3244 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3245 ASSERT_OPERATION_IS_ACTIVE(operation);
3246 TEST_EQUAL(psa_mac_verify_finish(&operation,
3247 verify_mac, sizeof(verify_mac)),
3248 PSA_ERROR_BAD_STATE);
3249 ASSERT_OPERATION_IS_INACTIVE(operation);
3250 PSA_ASSERT(psa_mac_abort(&operation));
3251 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003252
3253 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3255 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3256 ASSERT_OPERATION_IS_ACTIVE(operation);
3257 TEST_EQUAL(psa_mac_sign_finish(&operation,
3258 sign_mac, sizeof(sign_mac),
3259 &sign_mac_length),
3260 PSA_ERROR_BAD_STATE);
3261 ASSERT_OPERATION_IS_INACTIVE(operation);
3262 PSA_ASSERT(psa_mac_abort(&operation));
3263 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003266
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003269}
3270/* END_CASE */
3271
3272/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003273void mac_sign_verify_multi(int key_type_arg,
3274 data_t *key_data,
3275 int alg_arg,
3276 data_t *input,
3277 int is_verify,
3278 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003279{
3280 size_t data_part_len = 0;
3281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003283 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 if (mac_multipart_internal_func(key_type_arg, key_data,
3287 alg_arg,
3288 input, data_part_len,
3289 expected_mac,
3290 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003291 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003293
3294 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 if (mac_multipart_internal_func(key_type_arg, key_data,
3298 alg_arg,
3299 input, data_part_len,
3300 expected_mac,
3301 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003302 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003304 }
3305
3306 /* Goto is required to silence warnings about unused labels, as we
3307 * don't actually do any test assertions in this function. */
3308 goto exit;
3309}
3310/* END_CASE */
3311
3312/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003313void mac_sign(int key_type_arg,
3314 data_t *key_data,
3315 int alg_arg,
3316 data_t *input,
3317 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003318{
Ronald Cron5425a212020-08-04 14:58:35 +02003319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003320 psa_key_type_t key_type = key_type_arg;
3321 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003322 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003324 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003325 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003327 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003328 const size_t output_sizes_to_test[] = {
3329 0,
3330 1,
3331 expected_mac->len - 1,
3332 expected_mac->len,
3333 expected_mac->len + 1,
3334 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003337 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003339
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3343 psa_set_key_algorithm(&attributes, alg);
3344 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3347 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003348
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003350 const size_t output_size = output_sizes_to_test[i];
3351 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 (output_size >= expected_mac->len ? PSA_SUCCESS :
3353 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 mbedtls_test_set_step(output_size);
3356 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003357
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003358 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 TEST_EQUAL(psa_mac_compute(key, alg,
3360 input->x, input->len,
3361 actual_mac, output_size, &mac_length),
3362 expected_status);
3363 if (expected_status == PSA_SUCCESS) {
3364 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3365 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003366 }
3367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 if (output_size > 0) {
3369 memset(actual_mac, 0, output_size);
3370 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003371
3372 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3374 PSA_ASSERT(psa_mac_update(&operation,
3375 input->x, input->len));
3376 TEST_EQUAL(psa_mac_sign_finish(&operation,
3377 actual_mac, output_size,
3378 &mac_length),
3379 expected_status);
3380 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 if (expected_status == PSA_SUCCESS) {
3383 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3384 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003385 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003387 actual_mac = NULL;
3388 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003389
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003390exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 psa_mac_abort(&operation);
3392 psa_destroy_key(key);
3393 PSA_DONE();
3394 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003395}
3396/* END_CASE */
3397
3398/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003399void mac_verify(int key_type_arg,
3400 data_t *key_data,
3401 int alg_arg,
3402 data_t *input,
3403 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003404{
Ronald Cron5425a212020-08-04 14:58:35 +02003405 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003406 psa_key_type_t key_type = key_type_arg;
3407 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003408 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003410 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003413
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3417 psa_set_key_algorithm(&attributes, alg);
3418 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3421 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003422
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003423 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3425 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003426
3427 /* Verify correct MAC, 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 PSA_ASSERT(psa_mac_verify_finish(&operation,
3432 expected_mac->x,
3433 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003434
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003435 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 TEST_EQUAL(psa_mac_verify(key, alg,
3437 input->x, input->len,
3438 expected_mac->x,
3439 expected_mac->len - 1),
3440 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003441
3442 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3444 PSA_ASSERT(psa_mac_update(&operation,
3445 input->x, input->len));
3446 TEST_EQUAL(psa_mac_verify_finish(&operation,
3447 expected_mac->x,
3448 expected_mac->len - 1),
3449 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003450
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003451 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3453 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3454 TEST_EQUAL(psa_mac_verify(key, alg,
3455 input->x, input->len,
3456 perturbed_mac, expected_mac->len + 1),
3457 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003458
3459 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3461 PSA_ASSERT(psa_mac_update(&operation,
3462 input->x, input->len));
3463 TEST_EQUAL(psa_mac_verify_finish(&operation,
3464 perturbed_mac,
3465 expected_mac->len + 1),
3466 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003467
3468 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 for (size_t i = 0; i < expected_mac->len; i++) {
3470 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003471 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003472
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 TEST_EQUAL(psa_mac_verify(key, alg,
3474 input->x, input->len,
3475 perturbed_mac, expected_mac->len),
3476 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003477
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3479 PSA_ASSERT(psa_mac_update(&operation,
3480 input->x, input->len));
3481 TEST_EQUAL(psa_mac_verify_finish(&operation,
3482 perturbed_mac,
3483 expected_mac->len),
3484 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003485 perturbed_mac[i] ^= 1;
3486 }
3487
Gilles Peskine8c9def32018-02-08 10:02:12 +01003488exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003489 psa_mac_abort(&operation);
3490 psa_destroy_key(key);
3491 PSA_DONE();
3492 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003493}
3494/* END_CASE */
3495
3496/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003497void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003498{
Jaeden Ameroab439972019-02-15 14:12:05 +00003499 const uint8_t input[1] = { 0 };
3500 unsigned char output[1] = { 0 };
3501 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003502 /* Test each valid way of initializing the object, except for `= {0}`, as
3503 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3504 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003505 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003507 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3508 psa_cipher_operation_t zero;
3509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003511
Jaeden Ameroab439972019-02-15 14:12:05 +00003512 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 TEST_EQUAL(psa_cipher_update(&func,
3514 input, sizeof(input),
3515 output, sizeof(output),
3516 &output_length),
3517 PSA_ERROR_BAD_STATE);
3518 TEST_EQUAL(psa_cipher_update(&init,
3519 input, sizeof(input),
3520 output, sizeof(output),
3521 &output_length),
3522 PSA_ERROR_BAD_STATE);
3523 TEST_EQUAL(psa_cipher_update(&zero,
3524 input, sizeof(input),
3525 output, sizeof(output),
3526 &output_length),
3527 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003528
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003529 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 PSA_ASSERT(psa_cipher_abort(&func));
3531 PSA_ASSERT(psa_cipher_abort(&init));
3532 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003533}
3534/* END_CASE */
3535
3536/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003537void cipher_setup(int key_type_arg,
3538 data_t *key,
3539 int alg_arg,
3540 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003541{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003542 psa_key_type_t key_type = key_type_arg;
3543 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003544 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003545 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003546 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003547#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003548 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3549#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003550
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003552
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3554 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 }
3557 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003558
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003559 /* The operation object should be reusable. */
3560#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3562 smoke_test_key_data,
3563 sizeof(smoke_test_key_data),
3564 KNOWN_SUPPORTED_CIPHER_ALG,
3565 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 }
3568 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003569#endif
3570
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003571exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 psa_cipher_abort(&operation);
3573 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003574}
3575/* END_CASE */
3576
Ronald Cronee414c72021-03-18 18:50:08 +01003577/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003578void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003579{
Ronald Cron5425a212020-08-04 14:58:35 +02003580 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003581 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3582 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003584 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003585 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003586 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003587 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 0xaa, 0xaa, 0xaa, 0xaa
3589 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003590 const uint8_t text[] = {
3591 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 0xbb, 0xbb, 0xbb, 0xbb
3593 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003594 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003595 size_t length = 0;
3596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 PSA_ASSERT(psa_crypto_init());
3598 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3599 psa_set_key_algorithm(&attributes, alg);
3600 psa_set_key_type(&attributes, key_type);
3601 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3602 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003603
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003604 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003605 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3606 ASSERT_OPERATION_IS_ACTIVE(operation);
3607 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3608 PSA_ERROR_BAD_STATE);
3609 ASSERT_OPERATION_IS_INACTIVE(operation);
3610 PSA_ASSERT(psa_cipher_abort(&operation));
3611 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003612
3613 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3615 ASSERT_OPERATION_IS_ACTIVE(operation);
3616 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3617 PSA_ERROR_BAD_STATE);
3618 ASSERT_OPERATION_IS_INACTIVE(operation);
3619 PSA_ASSERT(psa_cipher_abort(&operation));
3620 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003621
Jaeden Ameroab439972019-02-15 14:12:05 +00003622 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003623 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3624 buffer, sizeof(buffer),
3625 &length),
3626 PSA_ERROR_BAD_STATE);
3627 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003628
3629 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3631 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3632 buffer, sizeof(buffer),
3633 &length));
3634 ASSERT_OPERATION_IS_ACTIVE(operation);
3635 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3636 buffer, sizeof(buffer),
3637 &length),
3638 PSA_ERROR_BAD_STATE);
3639 ASSERT_OPERATION_IS_INACTIVE(operation);
3640 PSA_ASSERT(psa_cipher_abort(&operation));
3641 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003642
3643 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003644 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3645 PSA_ASSERT(psa_cipher_set_iv(&operation,
3646 iv, sizeof(iv)));
3647 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3648 buffer, sizeof(buffer),
3649 &length),
3650 PSA_ERROR_BAD_STATE);
3651 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003652
3653 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 TEST_EQUAL(psa_cipher_set_iv(&operation,
3655 iv, sizeof(iv)),
3656 PSA_ERROR_BAD_STATE);
3657 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003658
3659 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003660 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3661 PSA_ASSERT(psa_cipher_set_iv(&operation,
3662 iv, sizeof(iv)));
3663 ASSERT_OPERATION_IS_ACTIVE(operation);
3664 TEST_EQUAL(psa_cipher_set_iv(&operation,
3665 iv, sizeof(iv)),
3666 PSA_ERROR_BAD_STATE);
3667 ASSERT_OPERATION_IS_INACTIVE(operation);
3668 PSA_ASSERT(psa_cipher_abort(&operation));
3669 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003670
3671 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003672 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3673 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3674 buffer, sizeof(buffer),
3675 &length));
3676 TEST_EQUAL(psa_cipher_set_iv(&operation,
3677 iv, sizeof(iv)),
3678 PSA_ERROR_BAD_STATE);
3679 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003680
3681 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 TEST_EQUAL(psa_cipher_update(&operation,
3683 text, sizeof(text),
3684 buffer, sizeof(buffer),
3685 &length),
3686 PSA_ERROR_BAD_STATE);
3687 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003688
3689 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003690 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3691 ASSERT_OPERATION_IS_ACTIVE(operation);
3692 TEST_EQUAL(psa_cipher_update(&operation,
3693 text, sizeof(text),
3694 buffer, sizeof(buffer),
3695 &length),
3696 PSA_ERROR_BAD_STATE);
3697 ASSERT_OPERATION_IS_INACTIVE(operation);
3698 PSA_ASSERT(psa_cipher_abort(&operation));
3699 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003700
3701 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003702 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3703 PSA_ASSERT(psa_cipher_set_iv(&operation,
3704 iv, sizeof(iv)));
3705 PSA_ASSERT(psa_cipher_finish(&operation,
3706 buffer, sizeof(buffer), &length));
3707 TEST_EQUAL(psa_cipher_update(&operation,
3708 text, sizeof(text),
3709 buffer, sizeof(buffer),
3710 &length),
3711 PSA_ERROR_BAD_STATE);
3712 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003713
3714 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003715 TEST_EQUAL(psa_cipher_finish(&operation,
3716 buffer, sizeof(buffer), &length),
3717 PSA_ERROR_BAD_STATE);
3718 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003719
3720 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003722 /* Not calling update means we are encrypting an empty buffer, which is OK
3723 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003724 ASSERT_OPERATION_IS_ACTIVE(operation);
3725 TEST_EQUAL(psa_cipher_finish(&operation,
3726 buffer, sizeof(buffer), &length),
3727 PSA_ERROR_BAD_STATE);
3728 ASSERT_OPERATION_IS_INACTIVE(operation);
3729 PSA_ASSERT(psa_cipher_abort(&operation));
3730 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003731
3732 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003733 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3734 PSA_ASSERT(psa_cipher_set_iv(&operation,
3735 iv, sizeof(iv)));
3736 PSA_ASSERT(psa_cipher_finish(&operation,
3737 buffer, sizeof(buffer), &length));
3738 TEST_EQUAL(psa_cipher_finish(&operation,
3739 buffer, sizeof(buffer), &length),
3740 PSA_ERROR_BAD_STATE);
3741 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003742
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003744
Jaeden Ameroab439972019-02-15 14:12:05 +00003745exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 psa_cipher_abort(&operation);
3747 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003748}
3749/* END_CASE */
3750
3751/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003752void cipher_encrypt_fail(int alg_arg,
3753 int key_type_arg,
3754 data_t *key_data,
3755 data_t *input,
3756 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003757{
Ronald Cron5425a212020-08-04 14:58:35 +02003758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003759 psa_status_t status;
3760 psa_key_type_t key_type = key_type_arg;
3761 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003762 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003764 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3765 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003766 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003767 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003768 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003769 size_t function_output_length;
3770 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3772
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 if (PSA_ERROR_BAD_STATE != expected_status) {
3774 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003775
Gilles Peskine449bd832023-01-11 14:50:10 +01003776 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3777 psa_set_key_algorithm(&attributes, alg);
3778 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003779
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3781 input->len);
3782 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003783
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3785 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003786 }
3787
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003788 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003789 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3790 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003791
Gilles Peskine449bd832023-01-11 14:50:10 +01003792 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003793
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003794 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 status = psa_cipher_encrypt_setup(&operation, key, alg);
3796 if (status == PSA_SUCCESS) {
3797 if (alg != PSA_ALG_ECB_NO_PADDING) {
3798 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3799 iv, iv_size,
3800 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003801 }
3802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 status = psa_cipher_update(&operation, input->x, input->len,
3804 output, output_buffer_size,
3805 &function_output_length);
3806 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003807 output_length += function_output_length;
3808
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 status = psa_cipher_finish(&operation, output + output_length,
3810 output_buffer_size - output_length,
3811 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 TEST_EQUAL(status, expected_status);
3814 } else {
3815 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003816 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 } else {
3818 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003819 }
3820
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003821exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 psa_cipher_abort(&operation);
3823 mbedtls_free(output);
3824 psa_destroy_key(key);
3825 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003826}
3827/* END_CASE */
3828
3829/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003830void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3831 data_t *input, int iv_length,
3832 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003833{
3834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3835 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3837 size_t output_buffer_size = 0;
3838 unsigned char *output = NULL;
3839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3841 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003842
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3846 psa_set_key_algorithm(&attributes, alg);
3847 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003848
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3850 &key));
3851 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3852 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3853 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003854
3855exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 psa_cipher_abort(&operation);
3857 mbedtls_free(output);
3858 psa_destroy_key(key);
3859 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003860}
3861/* END_CASE */
3862
3863/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003864void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3865 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003866{
3867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3868 psa_key_type_t key_type = key_type_arg;
3869 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003870 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3871 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003872 unsigned char *output = NULL;
3873 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003874 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3876
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003878
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003879 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 TEST_LE_U(ciphertext->len,
3881 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3882 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3883 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3884 TEST_LE_U(plaintext->len,
3885 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3886 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3887 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003888
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003889
3890 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 psa_set_key_usage_flags(&attributes,
3892 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3893 psa_set_key_algorithm(&attributes, alg);
3894 psa_set_key_type(&attributes, key_type);
3895 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3896 &key));
3897 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3898 plaintext->len);
3899 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003900
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003901 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3903 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3904 PSA_ERROR_BAD_STATE);
3905 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3906 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3907 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003908
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003909 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3911 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3912 &length),
3913 PSA_ERROR_BAD_STATE);
3914 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3915 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3916 &length),
3917 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003918
Gilles Peskine286c3142022-04-20 17:09:38 +02003919 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003921 output_length = 0;
3922 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 PSA_ASSERT(psa_cipher_update(&operation,
3924 plaintext->x, plaintext->len,
3925 output, output_buffer_size,
3926 &length));
3927 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003928 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 PSA_ASSERT(psa_cipher_finish(&operation,
3930 mbedtls_buffer_offset(output, output_length),
3931 output_buffer_size - output_length,
3932 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003933 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3935 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003936
Gilles Peskine286c3142022-04-20 17:09:38 +02003937 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003938 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003939 output_length = 0;
3940 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 PSA_ASSERT(psa_cipher_update(&operation,
3942 ciphertext->x, ciphertext->len,
3943 output, output_buffer_size,
3944 &length));
3945 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003946 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 PSA_ASSERT(psa_cipher_finish(&operation,
3948 mbedtls_buffer_offset(output, output_length),
3949 output_buffer_size - output_length,
3950 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003951 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 ASSERT_COMPARE(plaintext->x, plaintext->len,
3953 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003954
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003955 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003956 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3958 output, output_buffer_size,
3959 &output_length));
3960 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3961 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003962
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003963 /* One-shot decryption */
3964 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3966 output, output_buffer_size,
3967 &output_length));
3968 ASSERT_COMPARE(plaintext->x, plaintext->len,
3969 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003970
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003971exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 PSA_ASSERT(psa_cipher_abort(&operation));
3973 mbedtls_free(output);
3974 psa_cipher_abort(&operation);
3975 psa_destroy_key(key);
3976 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003977}
3978/* END_CASE */
3979
3980/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003981void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003982{
3983 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3984 psa_algorithm_t alg = alg_arg;
3985 psa_key_type_t key_type = key_type_arg;
3986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3987 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3988 psa_status_t status;
3989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01003991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3993 psa_set_key_algorithm(&attributes, alg);
3994 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01003995
3996 /* Usage of either of these two size macros would cause divide by zero
3997 * with incorrect key types previously. Input length should be irrelevant
3998 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4000 0);
4001 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004002
4003
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4005 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004006
4007 /* Should fail due to invalid alg type (to support invalid key type).
4008 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004010
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004012
4013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 psa_cipher_abort(&operation);
4015 psa_destroy_key(key);
4016 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004017}
4018/* END_CASE */
4019
4020/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004021void cipher_encrypt_validation(int alg_arg,
4022 int key_type_arg,
4023 data_t *key_data,
4024 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004025{
4026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4027 psa_key_type_t key_type = key_type_arg;
4028 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004030 unsigned char *output1 = NULL;
4031 size_t output1_buffer_size = 0;
4032 size_t output1_length = 0;
4033 unsigned char *output2 = NULL;
4034 size_t output2_buffer_size = 0;
4035 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004036 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004037 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4043 psa_set_key_algorithm(&attributes, alg);
4044 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4047 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4048 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4049 ASSERT_ALLOC(output1, output1_buffer_size);
4050 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004051
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4053 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004054
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004055 /* The one-shot cipher encryption uses generated iv so validating
4056 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4058 output1_buffer_size, &output1_length));
4059 TEST_LE_U(output1_length,
4060 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4061 TEST_LE_U(output1_length,
4062 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4065 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 PSA_ASSERT(psa_cipher_update(&operation,
4068 input->x, input->len,
4069 output2, output2_buffer_size,
4070 &function_output_length));
4071 TEST_LE_U(function_output_length,
4072 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4073 TEST_LE_U(function_output_length,
4074 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004075 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 PSA_ASSERT(psa_cipher_finish(&operation,
4078 output2 + output2_length,
4079 output2_buffer_size - output2_length,
4080 &function_output_length));
4081 TEST_LE_U(function_output_length,
4082 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4083 TEST_LE_U(function_output_length,
4084 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004085 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 PSA_ASSERT(psa_cipher_abort(&operation));
4088 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4089 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004090
Gilles Peskine50e586b2018-06-08 14:28:46 +02004091exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 psa_cipher_abort(&operation);
4093 mbedtls_free(output1);
4094 mbedtls_free(output2);
4095 psa_destroy_key(key);
4096 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004097}
4098/* END_CASE */
4099
4100/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004101void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4102 data_t *key_data, data_t *iv,
4103 data_t *input,
4104 int first_part_size_arg,
4105 int output1_length_arg, int output2_length_arg,
4106 data_t *expected_output,
4107 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004108{
Ronald Cron5425a212020-08-04 14:58:35 +02004109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004110 psa_key_type_t key_type = key_type_arg;
4111 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004112 psa_status_t status;
4113 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004114 size_t first_part_size = first_part_size_arg;
4115 size_t output1_length = output1_length_arg;
4116 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004117 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004118 size_t output_buffer_size = 0;
4119 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004120 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004121 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4127 psa_set_key_algorithm(&attributes, alg);
4128 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004129
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4131 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004132
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 if (iv->len > 0) {
4136 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004137 }
4138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4140 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4141 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 TEST_LE_U(first_part_size, input->len);
4144 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4145 output, output_buffer_size,
4146 &function_output_length));
4147 TEST_ASSERT(function_output_length == output1_length);
4148 TEST_LE_U(function_output_length,
4149 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4150 TEST_LE_U(function_output_length,
4151 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004152 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (first_part_size < input->len) {
4155 PSA_ASSERT(psa_cipher_update(&operation,
4156 input->x + first_part_size,
4157 input->len - first_part_size,
4158 (output_buffer_size == 0 ? NULL :
4159 output + total_output_length),
4160 output_buffer_size - total_output_length,
4161 &function_output_length));
4162 TEST_ASSERT(function_output_length == output2_length);
4163 TEST_LE_U(function_output_length,
4164 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4165 alg,
4166 input->len - first_part_size));
4167 TEST_LE_U(function_output_length,
4168 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004169 total_output_length += function_output_length;
4170 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 status = psa_cipher_finish(&operation,
4173 (output_buffer_size == 0 ? NULL :
4174 output + total_output_length),
4175 output_buffer_size - total_output_length,
4176 &function_output_length);
4177 TEST_LE_U(function_output_length,
4178 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4179 TEST_LE_U(function_output_length,
4180 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004181 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (expected_status == PSA_SUCCESS) {
4185 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004186
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 ASSERT_COMPARE(expected_output->x, expected_output->len,
4188 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004189 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190
4191exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 psa_cipher_abort(&operation);
4193 mbedtls_free(output);
4194 psa_destroy_key(key);
4195 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004196}
4197/* END_CASE */
4198
4199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004200void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4201 data_t *key_data, data_t *iv,
4202 data_t *input,
4203 int first_part_size_arg,
4204 int output1_length_arg, int output2_length_arg,
4205 data_t *expected_output,
4206 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004207{
Ronald Cron5425a212020-08-04 14:58:35 +02004208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004209 psa_key_type_t key_type = key_type_arg;
4210 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004211 psa_status_t status;
4212 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004213 size_t first_part_size = first_part_size_arg;
4214 size_t output1_length = output1_length_arg;
4215 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004216 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004217 size_t output_buffer_size = 0;
4218 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004219 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004220 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4226 psa_set_key_algorithm(&attributes, alg);
4227 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4230 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (iv->len > 0) {
4235 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004236 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4239 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4240 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 TEST_LE_U(first_part_size, input->len);
4243 PSA_ASSERT(psa_cipher_update(&operation,
4244 input->x, first_part_size,
4245 output, output_buffer_size,
4246 &function_output_length));
4247 TEST_ASSERT(function_output_length == output1_length);
4248 TEST_LE_U(function_output_length,
4249 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4250 TEST_LE_U(function_output_length,
4251 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004252 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 if (first_part_size < input->len) {
4255 PSA_ASSERT(psa_cipher_update(&operation,
4256 input->x + first_part_size,
4257 input->len - first_part_size,
4258 (output_buffer_size == 0 ? NULL :
4259 output + total_output_length),
4260 output_buffer_size - total_output_length,
4261 &function_output_length));
4262 TEST_ASSERT(function_output_length == output2_length);
4263 TEST_LE_U(function_output_length,
4264 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4265 alg,
4266 input->len - first_part_size));
4267 TEST_LE_U(function_output_length,
4268 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004269 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004270 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 status = psa_cipher_finish(&operation,
4273 (output_buffer_size == 0 ? NULL :
4274 output + total_output_length),
4275 output_buffer_size - total_output_length,
4276 &function_output_length);
4277 TEST_LE_U(function_output_length,
4278 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4279 TEST_LE_U(function_output_length,
4280 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004281 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 if (expected_status == PSA_SUCCESS) {
4285 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 ASSERT_COMPARE(expected_output->x, expected_output->len,
4288 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004289 }
4290
Gilles Peskine50e586b2018-06-08 14:28:46 +02004291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 psa_cipher_abort(&operation);
4293 mbedtls_free(output);
4294 psa_destroy_key(key);
4295 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004296}
4297/* END_CASE */
4298
Gilles Peskine50e586b2018-06-08 14:28:46 +02004299/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004300void cipher_decrypt_fail(int alg_arg,
4301 int key_type_arg,
4302 data_t *key_data,
4303 data_t *iv,
4304 data_t *input_arg,
4305 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004306{
4307 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4308 psa_status_t status;
4309 psa_key_type_t key_type = key_type_arg;
4310 psa_algorithm_t alg = alg_arg;
4311 psa_status_t expected_status = expected_status_arg;
4312 unsigned char *input = NULL;
4313 size_t input_buffer_size = 0;
4314 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004315 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004316 size_t output_buffer_size = 0;
4317 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004318 size_t function_output_length;
4319 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004320 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 if (PSA_ERROR_BAD_STATE != expected_status) {
4323 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4326 psa_set_key_algorithm(&attributes, alg);
4327 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004328
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4330 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004331 }
4332
4333 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4335 if (input_buffer_size > 0) {
4336 ASSERT_ALLOC(input, input_buffer_size);
4337 memcpy(input, iv->x, iv->len);
4338 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004339 }
4340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4342 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004343
Neil Armstrong66a479f2022-02-07 15:41:19 +01004344 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4346 output_buffer_size, &output_length);
4347 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004348
Neil Armstrong66a479f2022-02-07 15:41:19 +01004349 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 status = psa_cipher_decrypt_setup(&operation, key, alg);
4351 if (status == PSA_SUCCESS) {
4352 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4353 input_arg->len) +
4354 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4355 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 if (iv->len > 0) {
4358 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (status != PSA_SUCCESS) {
4361 TEST_EQUAL(status, expected_status);
4362 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004363 }
4364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 if (status == PSA_SUCCESS) {
4366 status = psa_cipher_update(&operation,
4367 input_arg->x, input_arg->len,
4368 output_multi, output_buffer_size,
4369 &function_output_length);
4370 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004371 output_length = function_output_length;
4372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 status = psa_cipher_finish(&operation,
4374 output_multi + output_length,
4375 output_buffer_size - output_length,
4376 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 TEST_EQUAL(status, expected_status);
4379 } else {
4380 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004381 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 } else {
4383 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004384 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 } else {
4386 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004387 }
4388
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 psa_cipher_abort(&operation);
4391 mbedtls_free(input);
4392 mbedtls_free(output);
4393 mbedtls_free(output_multi);
4394 psa_destroy_key(key);
4395 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004396}
4397/* END_CASE */
4398
4399/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004400void cipher_decrypt(int alg_arg,
4401 int key_type_arg,
4402 data_t *key_data,
4403 data_t *iv,
4404 data_t *input_arg,
4405 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004406{
4407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4408 psa_key_type_t key_type = key_type_arg;
4409 psa_algorithm_t alg = alg_arg;
4410 unsigned char *input = NULL;
4411 size_t input_buffer_size = 0;
4412 unsigned char *output = NULL;
4413 size_t output_buffer_size = 0;
4414 size_t output_length = 0;
4415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4420 psa_set_key_algorithm(&attributes, alg);
4421 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004422
4423 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4425 if (input_buffer_size > 0) {
4426 ASSERT_ALLOC(input, input_buffer_size);
4427 memcpy(input, iv->x, iv->len);
4428 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004429 }
4430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4432 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004433
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4435 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4438 output_buffer_size, &output_length));
4439 TEST_LE_U(output_length,
4440 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4441 TEST_LE_U(output_length,
4442 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 ASSERT_COMPARE(expected_output->x, expected_output->len,
4445 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 mbedtls_free(input);
4448 mbedtls_free(output);
4449 psa_destroy_key(key);
4450 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004451}
4452/* END_CASE */
4453
4454/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004455void cipher_verify_output(int alg_arg,
4456 int key_type_arg,
4457 data_t *key_data,
4458 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004459{
Ronald Cron5425a212020-08-04 14:58:35 +02004460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004461 psa_key_type_t key_type = key_type_arg;
4462 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004463 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004464 size_t output1_size = 0;
4465 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004466 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004467 size_t output2_size = 0;
4468 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4474 psa_set_key_algorithm(&attributes, alg);
4475 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4478 &key));
4479 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4480 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4483 output1, output1_size,
4484 &output1_length));
4485 TEST_LE_U(output1_length,
4486 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4487 TEST_LE_U(output1_length,
4488 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004489
4490 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004492
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4494 output2, output2_size,
4495 &output2_length));
4496 TEST_LE_U(output2_length,
4497 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4498 TEST_LE_U(output2_length,
4499 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004500
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004502
4503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 mbedtls_free(output1);
4505 mbedtls_free(output2);
4506 psa_destroy_key(key);
4507 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004508}
4509/* END_CASE */
4510
4511/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004512void cipher_verify_output_multipart(int alg_arg,
4513 int key_type_arg,
4514 data_t *key_data,
4515 data_t *input,
4516 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004517{
Ronald Cron5425a212020-08-04 14:58:35 +02004518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004519 psa_key_type_t key_type = key_type_arg;
4520 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004521 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004523 size_t iv_size = 16;
4524 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004525 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004526 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004527 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004528 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004529 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004530 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004531 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004532 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4533 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004535
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4539 psa_set_key_algorithm(&attributes, alg);
4540 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4543 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4546 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004547
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 if (alg != PSA_ALG_ECB_NO_PADDING) {
4549 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4550 iv, iv_size,
4551 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004552 }
4553
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4555 TEST_LE_U(output1_buffer_size,
4556 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4557 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4562 output1, output1_buffer_size,
4563 &function_output_length));
4564 TEST_LE_U(function_output_length,
4565 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4566 TEST_LE_U(function_output_length,
4567 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004568 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 PSA_ASSERT(psa_cipher_update(&operation1,
4571 input->x + first_part_size,
4572 input->len - first_part_size,
4573 output1, output1_buffer_size,
4574 &function_output_length));
4575 TEST_LE_U(function_output_length,
4576 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4577 alg,
4578 input->len - first_part_size));
4579 TEST_LE_U(function_output_length,
4580 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004581 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 PSA_ASSERT(psa_cipher_finish(&operation1,
4584 output1 + output1_length,
4585 output1_buffer_size - output1_length,
4586 &function_output_length));
4587 TEST_LE_U(function_output_length,
4588 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4589 TEST_LE_U(function_output_length,
4590 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004591 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004594
Gilles Peskine048b7f02018-06-08 14:20:49 +02004595 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 TEST_LE_U(output2_buffer_size,
4597 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4598 TEST_LE_U(output2_buffer_size,
4599 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4600 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004601
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 if (iv_length > 0) {
4603 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4604 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004605 }
Moran Pekerded84402018-06-06 16:36:50 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4608 output2, output2_buffer_size,
4609 &function_output_length));
4610 TEST_LE_U(function_output_length,
4611 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4612 TEST_LE_U(function_output_length,
4613 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004614 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 PSA_ASSERT(psa_cipher_update(&operation2,
4617 output1 + first_part_size,
4618 output1_length - first_part_size,
4619 output2, output2_buffer_size,
4620 &function_output_length));
4621 TEST_LE_U(function_output_length,
4622 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4623 alg,
4624 output1_length - first_part_size));
4625 TEST_LE_U(function_output_length,
4626 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004627 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 PSA_ASSERT(psa_cipher_finish(&operation2,
4630 output2 + output2_length,
4631 output2_buffer_size - output2_length,
4632 &function_output_length));
4633 TEST_LE_U(function_output_length,
4634 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4635 TEST_LE_U(function_output_length,
4636 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004637 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004642
4643exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 psa_cipher_abort(&operation1);
4645 psa_cipher_abort(&operation2);
4646 mbedtls_free(output1);
4647 mbedtls_free(output2);
4648 psa_destroy_key(key);
4649 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004650}
4651/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004652
Gilles Peskine20035e32018-02-03 22:44:14 +01004653/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004654void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4655 int alg_arg,
4656 data_t *nonce,
4657 data_t *additional_data,
4658 data_t *input_data,
4659 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004660{
Ronald Cron5425a212020-08-04 14:58:35 +02004661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004662 psa_key_type_t key_type = key_type_arg;
4663 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004664 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004665 unsigned char *output_data = NULL;
4666 size_t output_size = 0;
4667 size_t output_length = 0;
4668 unsigned char *output_data2 = NULL;
4669 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004670 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004671 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4677 psa_set_key_algorithm(&attributes, alg);
4678 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4681 &key));
4682 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4683 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4686 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004687 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4688 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4690 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4691 TEST_EQUAL(output_size,
4692 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4693 TEST_LE_U(output_size,
4694 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004695 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 status = psa_aead_encrypt(key, alg,
4699 nonce->x, nonce->len,
4700 additional_data->x,
4701 additional_data->len,
4702 input_data->x, input_data->len,
4703 output_data, output_size,
4704 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004705
4706 /* If the operation is not supported, just skip and not fail in case the
4707 * encryption involves a common limitation of cryptography hardwares and
4708 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if (status == PSA_ERROR_NOT_SUPPORTED) {
4710 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4711 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004712 }
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (PSA_SUCCESS == expected_result) {
4717 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004718
Gilles Peskine003a4a92019-05-14 16:09:40 +02004719 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4720 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 TEST_EQUAL(input_data->len,
4722 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 TEST_LE_U(input_data->len,
4725 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 TEST_EQUAL(psa_aead_decrypt(key, alg,
4728 nonce->x, nonce->len,
4729 additional_data->x,
4730 additional_data->len,
4731 output_data, output_length,
4732 output_data2, output_length,
4733 &output_length2),
4734 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 ASSERT_COMPARE(input_data->x, input_data->len,
4737 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004738 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004739
Gilles Peskinea1cac842018-06-11 19:33:02 +02004740exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 psa_destroy_key(key);
4742 mbedtls_free(output_data);
4743 mbedtls_free(output_data2);
4744 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004745}
4746/* END_CASE */
4747
4748/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749void aead_encrypt(int key_type_arg, data_t *key_data,
4750 int alg_arg,
4751 data_t *nonce,
4752 data_t *additional_data,
4753 data_t *input_data,
4754 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755{
Ronald Cron5425a212020-08-04 14:58:35 +02004756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004757 psa_key_type_t key_type = key_type_arg;
4758 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004759 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004760 unsigned char *output_data = NULL;
4761 size_t output_size = 0;
4762 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004763 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004764 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4769 psa_set_key_algorithm(&attributes, alg);
4770 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4773 &key));
4774 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4775 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4778 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004779 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4780 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 TEST_EQUAL(output_size,
4782 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4783 TEST_LE_U(output_size,
4784 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4785 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 status = psa_aead_encrypt(key, alg,
4788 nonce->x, nonce->len,
4789 additional_data->x, additional_data->len,
4790 input_data->x, input_data->len,
4791 output_data, output_size,
4792 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004793
Ronald Cron28a45ed2021-02-09 20:35:42 +01004794 /* If the operation is not supported, just skip and not fail in case the
4795 * encryption involves a common limitation of cryptography hardwares and
4796 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (status == PSA_ERROR_NOT_SUPPORTED) {
4798 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4799 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004800 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 PSA_ASSERT(status);
4803 ASSERT_COMPARE(expected_result->x, expected_result->len,
4804 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004805
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 psa_destroy_key(key);
4808 mbedtls_free(output_data);
4809 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004810}
4811/* END_CASE */
4812
4813/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814void aead_decrypt(int key_type_arg, data_t *key_data,
4815 int alg_arg,
4816 data_t *nonce,
4817 data_t *additional_data,
4818 data_t *input_data,
4819 data_t *expected_data,
4820 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821{
Ronald Cron5425a212020-08-04 14:58:35 +02004822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823 psa_key_type_t key_type = key_type_arg;
4824 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004825 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004826 unsigned char *output_data = NULL;
4827 size_t output_size = 0;
4828 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004829 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004830 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004831 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4836 psa_set_key_algorithm(&attributes, alg);
4837 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4840 &key));
4841 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4842 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4845 alg);
4846 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4847 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004848 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4849 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 TEST_EQUAL(output_size,
4851 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4852 TEST_LE_U(output_size,
4853 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 status = psa_aead_decrypt(key, alg,
4858 nonce->x, nonce->len,
4859 additional_data->x,
4860 additional_data->len,
4861 input_data->x, input_data->len,
4862 output_data, output_size,
4863 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004864
Ronald Cron28a45ed2021-02-09 20:35:42 +01004865 /* If the operation is not supported, just skip and not fail in case the
4866 * decryption involves a common limitation of cryptography hardwares and
4867 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (status == PSA_ERROR_NOT_SUPPORTED) {
4869 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4870 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004871 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 if (expected_result == PSA_SUCCESS) {
4876 ASSERT_COMPARE(expected_data->x, expected_data->len,
4877 output_data, output_length);
4878 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004879
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 psa_destroy_key(key);
4882 mbedtls_free(output_data);
4883 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884}
4885/* END_CASE */
4886
4887/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004888void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4889 int alg_arg,
4890 data_t *nonce,
4891 data_t *additional_data,
4892 data_t *input_data,
4893 int do_set_lengths,
4894 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004895{
Paul Elliottd3f82412021-06-16 16:52:21 +01004896 size_t ad_part_len = 0;
4897 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004898 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4901 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if (do_set_lengths) {
4904 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004905 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004907 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004909 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004910
4911 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (!aead_multipart_internal_func(key_type_arg, key_data,
4913 alg_arg, nonce,
4914 additional_data,
4915 ad_part_len,
4916 input_data, -1,
4917 set_lengths_method,
4918 expected_output,
4919 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004920 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004921 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 /* length(0) part, length(ad_part_len) part, length(0) part... */
4924 mbedtls_test_set_step(1000 + ad_part_len);
4925
4926 if (!aead_multipart_internal_func(key_type_arg, key_data,
4927 alg_arg, nonce,
4928 additional_data,
4929 ad_part_len,
4930 input_data, -1,
4931 set_lengths_method,
4932 expected_output,
4933 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004934 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 }
4936 }
4937
4938 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4939 /* Split data into length(data_part_len) parts. */
4940 mbedtls_test_set_step(2000 + data_part_len);
4941
4942 if (do_set_lengths) {
4943 if (data_part_len & 0x01) {
4944 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4945 } else {
4946 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4947 }
4948 }
4949
4950 if (!aead_multipart_internal_func(key_type_arg, key_data,
4951 alg_arg, nonce,
4952 additional_data, -1,
4953 input_data, data_part_len,
4954 set_lengths_method,
4955 expected_output,
4956 1, 0)) {
4957 break;
4958 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004959
4960 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 if (!aead_multipart_internal_func(key_type_arg, key_data,
4964 alg_arg, nonce,
4965 additional_data, -1,
4966 input_data, data_part_len,
4967 set_lengths_method,
4968 expected_output,
4969 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004970 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004973
Paul Elliott8fc45162021-06-23 16:06:01 +01004974 /* Goto is required to silence warnings about unused labels, as we
4975 * don't actually do any test assertions in this function. */
4976 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004977}
4978/* END_CASE */
4979
4980/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004981void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4982 int alg_arg,
4983 data_t *nonce,
4984 data_t *additional_data,
4985 data_t *input_data,
4986 int do_set_lengths,
4987 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004988{
Paul Elliottd3f82412021-06-16 16:52:21 +01004989 size_t ad_part_len = 0;
4990 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004991 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004994 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (do_set_lengths) {
4998 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005001 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005003 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 if (!aead_multipart_internal_func(key_type_arg, key_data,
5006 alg_arg, nonce,
5007 additional_data,
5008 ad_part_len,
5009 input_data, -1,
5010 set_lengths_method,
5011 expected_output,
5012 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005013 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005015
5016 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 if (!aead_multipart_internal_func(key_type_arg, key_data,
5020 alg_arg, nonce,
5021 additional_data,
5022 ad_part_len,
5023 input_data, -1,
5024 set_lengths_method,
5025 expected_output,
5026 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005027 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005029 }
5030
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if (do_set_lengths) {
5036 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005037 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005041 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (!aead_multipart_internal_func(key_type_arg, key_data,
5044 alg_arg, nonce,
5045 additional_data, -1,
5046 input_data, data_part_len,
5047 set_lengths_method,
5048 expected_output,
5049 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005052
5053 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (!aead_multipart_internal_func(key_type_arg, key_data,
5057 alg_arg, nonce,
5058 additional_data, -1,
5059 input_data, data_part_len,
5060 set_lengths_method,
5061 expected_output,
5062 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005065 }
5066
Paul Elliott8fc45162021-06-23 16:06:01 +01005067 /* Goto is required to silence warnings about unused labels, as we
5068 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005069 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005070}
5071/* END_CASE */
5072
5073/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005074void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5075 int alg_arg,
5076 int nonce_length,
5077 int expected_nonce_length_arg,
5078 data_t *additional_data,
5079 data_t *input_data,
5080 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005081{
5082
5083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5084 psa_key_type_t key_type = key_type_arg;
5085 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005086 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005087 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5089 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005090 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005091 size_t actual_nonce_length = 0;
5092 size_t expected_nonce_length = expected_nonce_length_arg;
5093 unsigned char *output = NULL;
5094 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005095 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005096 size_t ciphertext_size = 0;
5097 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005098 size_t tag_length = 0;
5099 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005100
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5104 psa_set_key_algorithm(&attributes, alg);
5105 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5108 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005123
5124 /* If the operation is not supported, just skip and not fail in case the
5125 * encryption involves a common limitation of cryptography hardwares and
5126 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 if (status == PSA_ERROR_NOT_SUPPORTED) {
5128 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5129 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005130 }
5131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5135 nonce_length,
5136 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 if (expected_status == PSA_SUCCESS) {
5143 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5144 alg));
5145 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5152 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5155 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5158 output, output_size,
5159 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5162 &ciphertext_length, tag_buffer,
5163 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005164 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005165
5166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 psa_destroy_key(key);
5168 mbedtls_free(output);
5169 mbedtls_free(ciphertext);
5170 psa_aead_abort(&operation);
5171 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005172}
5173/* END_CASE */
5174
5175/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005176void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5177 int alg_arg,
5178 int nonce_length_arg,
5179 int set_lengths_method_arg,
5180 data_t *additional_data,
5181 data_t *input_data,
5182 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005183{
5184
5185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5186 psa_key_type_t key_type = key_type_arg;
5187 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005188 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005189 uint8_t *nonce_buffer = NULL;
5190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5191 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5192 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005193 unsigned char *output = NULL;
5194 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005195 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005196 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005197 size_t ciphertext_size = 0;
5198 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005199 size_t tag_length = 0;
5200 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005201 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005202 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5207 psa_set_key_algorithm(&attributes, alg);
5208 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5211 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005212
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005216
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005218
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005226
5227 /* If the operation is not supported, just skip and not fail in case the
5228 * encryption involves a common limitation of cryptography hardwares and
5229 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 if (status == PSA_ERROR_NOT_SUPPORTED) {
5231 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5232 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005233 }
5234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005236
Paul Elliott4023ffd2021-09-10 16:21:22 +01005237 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 if (nonce_length_arg == -1) {
5239 /* Arbitrary size buffer, to test zero length valid buffer. */
5240 ASSERT_ALLOC(nonce_buffer, 4);
5241 nonce_length = 0;
5242 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005243 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 nonce_length = (size_t) nonce_length_arg;
5245 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 if (nonce_buffer) {
5248 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005249 nonce_buffer[index] = 'a' + index;
5250 }
Paul Elliott66696b52021-08-16 18:42:41 +01005251 }
Paul Elliott863864a2021-07-23 17:28:31 +01005252 }
5253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5255 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5256 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005257 }
5258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 if (expected_status == PSA_SUCCESS) {
5264 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5265 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5266 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005267 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005269 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 }
Paul Elliott863864a2021-07-23 17:28:31 +01005271
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005272 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5274 additional_data->len),
5275 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005276
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5278 output, output_size,
5279 &ciphertext_length),
5280 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5283 &ciphertext_length, tag_buffer,
5284 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5285 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005286 }
5287
5288exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 psa_destroy_key(key);
5290 mbedtls_free(output);
5291 mbedtls_free(ciphertext);
5292 mbedtls_free(nonce_buffer);
5293 psa_aead_abort(&operation);
5294 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005295}
5296/* END_CASE */
5297
5298/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005299void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005300 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005301 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005302 data_t *nonce,
5303 data_t *additional_data,
5304 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005306{
5307
5308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5309 psa_key_type_t key_type = key_type_arg;
5310 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005311 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5313 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5314 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005315 unsigned char *output = NULL;
5316 unsigned char *ciphertext = NULL;
5317 size_t output_size = output_size_arg;
5318 size_t ciphertext_size = 0;
5319 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005320 size_t tag_length = 0;
5321 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5326 psa_set_key_algorithm(&attributes, alg);
5327 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5330 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005331
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005337
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005341
5342 /* If the operation is not supported, just skip and not fail in case the
5343 * encryption involves a common limitation of cryptography hardwares and
5344 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 if (status == PSA_ERROR_NOT_SUPPORTED) {
5346 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5347 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005348 }
5349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5353 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5358 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 status = psa_aead_update(&operation, input_data->x, input_data->len,
5361 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005366 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5368 &ciphertext_length, tag_buffer,
5369 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005370 }
5371
5372exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 psa_destroy_key(key);
5374 mbedtls_free(output);
5375 mbedtls_free(ciphertext);
5376 psa_aead_abort(&operation);
5377 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005378}
5379/* END_CASE */
5380
Paul Elliott91b021e2021-07-23 18:52:31 +01005381/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005382void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5383 int alg_arg,
5384 int finish_ciphertext_size_arg,
5385 int tag_size_arg,
5386 data_t *nonce,
5387 data_t *additional_data,
5388 data_t *input_data,
5389 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005390{
5391
5392 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5393 psa_key_type_t key_type = key_type_arg;
5394 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005395 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5397 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5398 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005399 unsigned char *ciphertext = NULL;
5400 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005401 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005402 size_t ciphertext_size = 0;
5403 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5405 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005406 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5411 psa_set_key_algorithm(&attributes, alg);
5412 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5415 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005428
5429 /* If the operation is not supported, just skip and not fail in case the
5430 * encryption involves a common limitation of cryptography hardwares and
5431 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 if (status == PSA_ERROR_NOT_SUPPORTED) {
5433 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5434 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005435 }
5436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5442 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5445 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5448 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005449
5450 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 status = psa_aead_finish(&operation, finish_ciphertext,
5452 finish_ciphertext_size,
5453 &ciphertext_length, tag_buffer,
5454 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
5458exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 psa_destroy_key(key);
5460 mbedtls_free(ciphertext);
5461 mbedtls_free(finish_ciphertext);
5462 mbedtls_free(tag_buffer);
5463 psa_aead_abort(&operation);
5464 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005465}
5466/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005467
5468/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005469void aead_multipart_verify(int key_type_arg, data_t *key_data,
5470 int alg_arg,
5471 data_t *nonce,
5472 data_t *additional_data,
5473 data_t *input_data,
5474 data_t *tag,
5475 int tag_usage_arg,
5476 int expected_setup_status_arg,
5477 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005478{
5479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5480 psa_key_type_t key_type = key_type_arg;
5481 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005482 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5484 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5485 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005486 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005487 unsigned char *plaintext = NULL;
5488 unsigned char *finish_plaintext = NULL;
5489 size_t plaintext_size = 0;
5490 size_t plaintext_length = 0;
5491 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005492 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005493 unsigned char *tag_buffer = NULL;
5494 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005495
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005497
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5499 psa_set_key_algorithm(&attributes, alg);
5500 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005501
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5503 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005504
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5508 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005513
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005515
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005517
5518 /* If the operation is not supported, just skip and not fail in case the
5519 * encryption involves a common limitation of cryptography hardwares and
5520 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (status == PSA_ERROR_NOT_SUPPORTED) {
5522 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5523 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005524 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005528 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 }
Paul Elliott9961a662021-09-17 19:19:02 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 status = psa_aead_set_lengths(&operation, additional_data->len,
5536 input_data->len);
5537 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5540 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5543 input_data->len,
5544 plaintext, plaintext_size,
5545 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005548 tag_buffer = tag->x;
5549 tag_size = tag->len;
5550 }
5551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 status = psa_aead_verify(&operation, finish_plaintext,
5553 verify_plaintext_size,
5554 &plaintext_length,
5555 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005558
5559exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 psa_destroy_key(key);
5561 mbedtls_free(plaintext);
5562 mbedtls_free(finish_plaintext);
5563 psa_aead_abort(&operation);
5564 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005565}
5566/* END_CASE */
5567
Paul Elliott9961a662021-09-17 19:19:02 +01005568/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005569void aead_multipart_setup(int key_type_arg, data_t *key_data,
5570 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005571{
5572 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5573 psa_key_type_t key_type = key_type_arg;
5574 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005575 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5577 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5578 psa_status_t expected_status = expected_status_arg;
5579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 psa_set_key_usage_flags(&attributes,
5583 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5584 psa_set_key_algorithm(&attributes, alg);
5585 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5588 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005599
5600exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 psa_destroy_key(key);
5602 psa_aead_abort(&operation);
5603 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005604}
5605/* END_CASE */
5606
5607/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005608void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5609 int alg_arg,
5610 data_t *nonce,
5611 data_t *additional_data,
5612 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005613{
5614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5615 psa_key_type_t key_type = key_type_arg;
5616 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005617 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005618 unsigned char *output_data = NULL;
5619 unsigned char *final_data = NULL;
5620 size_t output_size = 0;
5621 size_t finish_output_size = 0;
5622 size_t output_length = 0;
5623 size_t key_bits = 0;
5624 size_t tag_length = 0;
5625 size_t tag_size = 0;
5626 size_t nonce_length = 0;
5627 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5628 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5629 size_t output_part_length = 0;
5630 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 psa_set_key_usage_flags(&attributes,
5635 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5636 psa_set_key_algorithm(&attributes, alg);
5637 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5640 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5643 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005644
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005652
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005658
5659 /* Test all operations error without calling setup first. */
5660
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
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
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5667 PSA_AEAD_NONCE_MAX_SIZE,
5668 &nonce_length),
5669 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005672
Paul Elliott481be342021-07-16 17:38:47 +01005673 /* ------------------------------------------------------- */
5674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5676 input_data->len),
5677 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005680
Paul Elliott481be342021-07-16 17:38:47 +01005681 /* ------------------------------------------------------- */
5682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5684 additional_data->len),
5685 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005688
Paul Elliott481be342021-07-16 17:38:47 +01005689 /* ------------------------------------------------------- */
5690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5692 input_data->len, output_data,
5693 output_size, &output_length),
5694 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
Paul Elliott481be342021-07-16 17:38:47 +01005698 /* ------------------------------------------------------- */
5699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5701 finish_output_size,
5702 &output_part_length,
5703 tag_buffer, tag_length,
5704 &tag_size),
5705 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005708
Paul Elliott481be342021-07-16 17:38:47 +01005709 /* ------------------------------------------------------- */
5710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5712 finish_output_size,
5713 &output_part_length,
5714 tag_buffer,
5715 tag_length),
5716 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
5720 /* Test for double setups. */
5721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5725 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728
Paul Elliott481be342021-07-16 17:38:47 +01005729 /* ------------------------------------------------------- */
5730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5734 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737
Paul Elliott374a2be2021-07-16 17:53:40 +01005738 /* ------------------------------------------------------- */
5739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5743 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005746
5747 /* ------------------------------------------------------- */
5748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5752 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005755
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756 /* Test for not setting a nonce. */
5757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5761 additional_data->len),
5762 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
Paul Elliott7f628422021-09-01 12:08:29 +01005766 /* ------------------------------------------------------- */
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5771 input_data->len, output_data,
5772 output_size, &output_length),
5773 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005776
Paul Elliottbdc2c682021-09-21 18:37:10 +01005777 /* ------------------------------------------------------- */
5778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5782 finish_output_size,
5783 &output_part_length,
5784 tag_buffer, tag_length,
5785 &tag_size),
5786 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005789
5790 /* ------------------------------------------------------- */
5791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5795 finish_output_size,
5796 &output_part_length,
5797 tag_buffer,
5798 tag_length),
5799 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005802
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803 /* Test for double setting nonce. */
5804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5810 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005813
Paul Elliott374a2be2021-07-16 17:53:40 +01005814 /* Test for double generating nonce. */
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +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));
Paul Elliott374a2be2021-07-16 17:53:40 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5823 PSA_AEAD_NONCE_MAX_SIZE,
5824 &nonce_length),
5825 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005826
5827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005829
5830 /* Test for generate nonce then set and vice versa */
5831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5835 PSA_AEAD_NONCE_MAX_SIZE,
5836 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5839 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005842
Andrzej Kurekad837522021-12-15 15:28:49 +01005843 /* Test for generating nonce after calling set lengths */
5844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5848 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5851 PSA_AEAD_NONCE_MAX_SIZE,
5852 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005855
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005856 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (operation.alg == PSA_ALG_CCM) {
5861 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5862 input_data->len),
5863 PSA_ERROR_INVALID_ARGUMENT);
5864 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5865 PSA_AEAD_NONCE_MAX_SIZE,
5866 &nonce_length),
5867 PSA_ERROR_BAD_STATE);
5868 } else {
5869 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5870 input_data->len));
5871 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5872 PSA_AEAD_NONCE_MAX_SIZE,
5873 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005874 }
5875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005877
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005878 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005879#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5883 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5884 input_data->len),
5885 PSA_ERROR_INVALID_ARGUMENT);
5886 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5887 PSA_AEAD_NONCE_MAX_SIZE,
5888 &nonce_length),
5889 PSA_ERROR_BAD_STATE);
5890 } else {
5891 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5892 input_data->len));
5893 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5894 PSA_AEAD_NONCE_MAX_SIZE,
5895 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005896 }
5897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005899#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005900
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005901 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5906 PSA_AEAD_NONCE_MAX_SIZE,
5907 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005908
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 if (operation.alg == PSA_ALG_CCM) {
5910 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5911 input_data->len),
5912 PSA_ERROR_INVALID_ARGUMENT);
5913 } else {
5914 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5915 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005916 }
5917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005919
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005920 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005921 /* Test for setting nonce after calling set lengths */
5922
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5926 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005931
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005932 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (operation.alg == PSA_ALG_CCM) {
5937 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5938 input_data->len),
5939 PSA_ERROR_INVALID_ARGUMENT);
5940 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5941 PSA_ERROR_BAD_STATE);
5942 } else {
5943 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5944 input_data->len));
5945 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005946 }
5947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005949
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005950 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005951#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5955 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5956 input_data->len),
5957 PSA_ERROR_INVALID_ARGUMENT);
5958 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5959 PSA_ERROR_BAD_STATE);
5960 } else {
5961 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5962 input_data->len));
5963 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964 }
5965
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005967#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005968
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005969 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 if (operation.alg == PSA_ALG_CCM) {
5976 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5977 input_data->len),
5978 PSA_ERROR_INVALID_ARGUMENT);
5979 } else {
5980 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5981 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005982 }
5983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005985
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005986 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00005987#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 if (operation.alg == PSA_ALG_GCM) {
5991 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5992 SIZE_MAX),
5993 PSA_ERROR_INVALID_ARGUMENT);
5994 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5995 PSA_ERROR_BAD_STATE);
5996 } else if (operation.alg != PSA_ALG_CCM) {
5997 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5998 SIZE_MAX));
5999 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006000 }
6001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006003#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006004
Tom Cosgrove1797b052022-12-04 17:19:59 +00006005 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006006#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006008
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 if (operation.alg == PSA_ALG_GCM) {
6012 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6013 SIZE_MAX),
6014 PSA_ERROR_INVALID_ARGUMENT);
6015 } else if (operation.alg != PSA_ALG_CCM) {
6016 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6017 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006018 }
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006021#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006022
6023 /* ------------------------------------------------------- */
6024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6030 PSA_AEAD_NONCE_MAX_SIZE,
6031 &nonce_length),
6032 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006035
Paul Elliott7220cae2021-06-22 17:25:57 +01006036 /* Test for generating nonce in decrypt setup. */
6037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6041 PSA_AEAD_NONCE_MAX_SIZE,
6042 &nonce_length),
6043 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006046
Paul Elliottc23a9a02021-06-21 18:32:46 +01006047 /* Test for setting lengths twice. */
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6054 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6057 input_data->len),
6058 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006061
Andrzej Kurekad837522021-12-15 15:28:49 +01006062 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6071 additional_data->len),
6072 PSA_ERROR_BAD_STATE);
6073 } else {
6074 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6075 additional_data->len));
6076
6077 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6078 input_data->len),
6079 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006080 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006082
6083 /* ------------------------------------------------------- */
6084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 if (operation.alg == PSA_ALG_CCM) {
6090 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6091 input_data->len, output_data,
6092 output_size, &output_length),
6093 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 } else {
6096 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6097 input_data->len, output_data,
6098 output_size, &output_length));
6099
6100 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6101 input_data->len),
6102 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006103 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006105
6106 /* ------------------------------------------------------- */
6107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 if (operation.alg == PSA_ALG_CCM) {
6113 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6114 finish_output_size,
6115 &output_part_length,
6116 tag_buffer, tag_length,
6117 &tag_size));
6118 } else {
6119 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6120 finish_output_size,
6121 &output_part_length,
6122 tag_buffer, tag_length,
6123 &tag_size));
6124
6125 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6126 input_data->len),
6127 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006128 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006130
6131 /* Test for setting lengths after generating nonce + already starting data. */
6132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6136 PSA_AEAD_NONCE_MAX_SIZE,
6137 &nonce_length));
6138 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6141 additional_data->len),
6142 PSA_ERROR_BAD_STATE);
6143 } else {
6144 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6145 additional_data->len));
6146
6147 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6148 input_data->len),
6149 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006152
6153 /* ------------------------------------------------------- */
6154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6158 PSA_AEAD_NONCE_MAX_SIZE,
6159 &nonce_length));
6160 if (operation.alg == PSA_ALG_CCM) {
6161 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6162 input_data->len, output_data,
6163 output_size, &output_length),
6164 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 } else {
6167 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6168 input_data->len, output_data,
6169 output_size, &output_length));
6170
6171 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6172 input_data->len),
6173 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006176
6177 /* ------------------------------------------------------- */
6178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6182 PSA_AEAD_NONCE_MAX_SIZE,
6183 &nonce_length));
6184 if (operation.alg == PSA_ALG_CCM) {
6185 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6186 finish_output_size,
6187 &output_part_length,
6188 tag_buffer, tag_length,
6189 &tag_size));
6190 } else {
6191 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6192 finish_output_size,
6193 &output_part_length,
6194 tag_buffer, tag_length,
6195 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6198 input_data->len),
6199 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006200 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006202
Paul Elliott243080c2021-07-21 19:01:17 +01006203 /* Test for not sending any additional data or data after setting non zero
6204 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6211 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6214 finish_output_size,
6215 &output_part_length,
6216 tag_buffer, tag_length,
6217 &tag_size),
6218 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006221
Paul Elliott243080c2021-07-21 19:01:17 +01006222 /* Test for not sending any additional data or data after setting non-zero
6223 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6230 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6233 finish_output_size,
6234 &output_part_length,
6235 tag_buffer,
6236 tag_length),
6237 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006240
Paul Elliott243080c2021-07-21 19:01:17 +01006241 /* Test for not sending any additional data after setting a non-zero length
6242 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6249 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6252 input_data->len, output_data,
6253 output_size, &output_length),
6254 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Paul Elliottf94bd992021-09-19 18:15:59 +01006258 /* Test for not sending any data after setting a non-zero length for it.*/
6259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6265 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6268 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6271 finish_output_size,
6272 &output_part_length,
6273 tag_buffer, tag_length,
6274 &tag_size),
6275 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006278
Paul Elliottb0450fe2021-09-01 15:06:26 +01006279 /* Test for sending too much additional data after setting lengths. */
6280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006286
6287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6289 additional_data->len),
6290 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006293
Paul Elliotta2a09b02021-09-22 14:56:40 +01006294 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6301 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6304 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6307 1),
6308 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006311
Paul Elliottb0450fe2021-09-01 15:06:26 +01006312 /* Test for sending too much data after setting lengths. */
6313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6321 input_data->len, output_data,
6322 output_size, &output_length),
6323 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006326
Paul Elliotta2a09b02021-09-22 14:56:40 +01006327 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6334 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6337 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6340 input_data->len, output_data,
6341 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6344 1, output_data,
6345 output_size, &output_length),
6346 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006349
Paul Elliottc23a9a02021-06-21 18:32:46 +01006350 /* Test sending additional data after data. */
6351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 if (operation.alg != PSA_ALG_CCM) {
6357 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6358 input_data->len, output_data,
6359 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6362 additional_data->len),
6363 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006364 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006366
Paul Elliott534d0b42021-06-22 19:15:20 +01006367 /* Test calling finish on decryption. */
6368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6374 finish_output_size,
6375 &output_part_length,
6376 tag_buffer, tag_length,
6377 &tag_size),
6378 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006381
6382 /* Test calling verify on encryption. */
6383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6389 finish_output_size,
6390 &output_part_length,
6391 tag_buffer,
6392 tag_length),
6393 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006396
6397
Paul Elliottc23a9a02021-06-21 18:32:46 +01006398exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 psa_destroy_key(key);
6400 psa_aead_abort(&operation);
6401 mbedtls_free(output_data);
6402 mbedtls_free(final_data);
6403 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006404}
6405/* END_CASE */
6406
6407/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006408void signature_size(int type_arg,
6409 int bits,
6410 int alg_arg,
6411 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006412{
6413 psa_key_type_t type = type_arg;
6414 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006418
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419exit:
6420 ;
6421}
6422/* END_CASE */
6423
6424/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006425void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6426 int alg_arg, data_t *input_data,
6427 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006428{
Ronald Cron5425a212020-08-04 14:58:35 +02006429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430 psa_key_type_t key_type = key_type_arg;
6431 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006432 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006433 unsigned char *signature = NULL;
6434 size_t signature_size;
6435 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6441 psa_set_key_algorithm(&attributes, alg);
6442 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6445 &key));
6446 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6447 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006448
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006449 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006450 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6452 key_bits, alg);
6453 TEST_ASSERT(signature_size != 0);
6454 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6455 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006456
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006457 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 PSA_ASSERT(psa_sign_hash(key, alg,
6459 input_data->x, input_data->len,
6460 signature, signature_size,
6461 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006462 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 ASSERT_COMPARE(output_data->x, output_data->len,
6464 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006465
6466exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006467 /*
6468 * Key attributes may have been returned by psa_get_key_attributes()
6469 * thus reset them as required.
6470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 psa_destroy_key(key);
6474 mbedtls_free(signature);
6475 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006476}
6477/* END_CASE */
6478
Paul Elliott712d5122022-12-07 14:03:10 +00006479/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6480void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6481 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006482 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006483{
6484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6485 psa_key_type_t key_type = key_type_arg;
6486 psa_algorithm_t alg = alg_arg;
6487 size_t key_bits;
6488 unsigned char *signature = NULL;
6489 size_t signature_size;
6490 size_t signature_length = 0xdeadbeef;
6491 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6492 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006493 uint32_t num_ops = 0;
6494 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006495 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006496 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006497 size_t min_completes = 0;
6498 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006499
Paul Elliott712d5122022-12-07 14:03:10 +00006500 psa_sign_hash_interruptible_operation_t operation =
6501 psa_sign_hash_interruptible_operation_init();
6502
6503 PSA_ASSERT(psa_crypto_init());
6504
6505 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6506 psa_set_key_algorithm(&attributes, alg);
6507 psa_set_key_type(&attributes, key_type);
6508
6509 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6510 &key));
6511 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6512 key_bits = psa_get_key_bits(&attributes);
6513
6514 /* Allocate a buffer which has the size advertised by the
6515 * library. */
6516 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6517 key_bits, alg);
6518 TEST_ASSERT(signature_size != 0);
6519 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6520 ASSERT_ALLOC(signature, signature_size);
6521
Paul Elliott0c683352022-12-16 19:16:56 +00006522 psa_interruptible_set_max_ops(max_ops);
6523
Paul Elliott6f600372023-02-06 18:41:05 +00006524 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6525 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006526
Paul Elliott712d5122022-12-07 14:03:10 +00006527 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6528 TEST_ASSERT(num_ops_prior == 0);
6529
6530 /* Start performing the signature. */
6531 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6532 input_data->x, input_data->len));
6533
6534 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6535 TEST_ASSERT(num_ops_prior == 0);
6536
6537 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006538 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006539 status = psa_sign_hash_complete(&operation, signature, signature_size,
6540 &signature_length);
6541
Paul Elliott0c683352022-12-16 19:16:56 +00006542 num_completes++;
6543
Paul Elliott712d5122022-12-07 14:03:10 +00006544 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6545 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00006546 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006547
Paul Elliott712d5122022-12-07 14:03:10 +00006548 num_ops_prior = num_ops;
6549 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006550 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006551
6552 TEST_ASSERT(status == PSA_SUCCESS);
6553
Paul Elliott0c683352022-12-16 19:16:56 +00006554 TEST_LE_U(min_completes, num_completes);
6555 TEST_LE_U(num_completes, max_completes);
6556
Paul Elliott712d5122022-12-07 14:03:10 +00006557 /* Verify that the signature is what is expected. */
6558 ASSERT_COMPARE(output_data->x, output_data->len,
6559 signature, signature_length);
6560
6561 PSA_ASSERT(psa_sign_hash_abort(&operation));
6562
Paul Elliott59ad9452022-12-18 15:09:02 +00006563 num_ops = psa_sign_hash_get_num_ops(&operation);
6564 TEST_ASSERT(num_ops == 0);
6565
Paul Elliott712d5122022-12-07 14:03:10 +00006566exit:
6567
6568 /*
6569 * Key attributes may have been returned by psa_get_key_attributes()
6570 * thus reset them as required.
6571 */
6572 psa_reset_key_attributes(&attributes);
6573
6574 psa_destroy_key(key);
6575 mbedtls_free(signature);
6576 PSA_DONE();
6577}
6578/* END_CASE */
6579
Gilles Peskine20035e32018-02-03 22:44:14 +01006580/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006581void sign_hash_fail(int key_type_arg, data_t *key_data,
6582 int alg_arg, data_t *input_data,
6583 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006584{
Ronald Cron5425a212020-08-04 14:58:35 +02006585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006586 psa_key_type_t key_type = key_type_arg;
6587 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006588 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006589 psa_status_t actual_status;
6590 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006591 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006592 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6600 psa_set_key_algorithm(&attributes, alg);
6601 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6604 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 actual_status = psa_sign_hash(key, alg,
6607 input_data->x, input_data->len,
6608 signature, signature_size,
6609 &signature_length);
6610 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006611 /* The value of *signature_length is unspecified on error, but
6612 * whatever it is, it should be less than signature_size, so that
6613 * if the caller tries to read *signature_length bytes without
6614 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006616
6617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 psa_reset_key_attributes(&attributes);
6619 psa_destroy_key(key);
6620 mbedtls_free(signature);
6621 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006622}
6623/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006624
Paul Elliott91007972022-12-16 12:21:24 +00006625/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6626void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6627 int alg_arg, data_t *input_data,
6628 int signature_size_arg,
6629 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006630 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006631 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006632{
6633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6634 psa_key_type_t key_type = key_type_arg;
6635 psa_algorithm_t alg = alg_arg;
6636 size_t signature_size = signature_size_arg;
6637 psa_status_t actual_status;
6638 psa_status_t expected_start_status = expected_start_status_arg;
6639 psa_status_t expected_complete_status = expected_complete_status_arg;
6640 unsigned char *signature = NULL;
6641 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006642 uint32_t num_ops = 0;
6643 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006644 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006645 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006646 size_t min_completes = 0;
6647 size_t max_completes = 0;
6648
Paul Elliott91007972022-12-16 12:21:24 +00006649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6650 psa_sign_hash_interruptible_operation_t operation =
6651 psa_sign_hash_interruptible_operation_init();
6652
6653 ASSERT_ALLOC(signature, signature_size);
6654
6655 PSA_ASSERT(psa_crypto_init());
6656
6657 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6658 psa_set_key_algorithm(&attributes, alg);
6659 psa_set_key_type(&attributes, key_type);
6660
6661 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6662 &key));
6663
Paul Elliott0c683352022-12-16 19:16:56 +00006664 psa_interruptible_set_max_ops(max_ops);
6665
Paul Elliott6f600372023-02-06 18:41:05 +00006666 interruptible_signverify_get_minmax_completes(max_ops,
6667 expected_complete_status,
6668 &min_completes,
6669 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006670
Paul Elliott91007972022-12-16 12:21:24 +00006671 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6672 TEST_ASSERT(num_ops_prior == 0);
6673
6674 /* Start performing the signature. */
6675 actual_status = psa_sign_hash_start(&operation, key, alg,
6676 input_data->x, input_data->len);
6677
6678 TEST_EQUAL(actual_status, expected_start_status);
6679
Paul Elliottc9774412023-02-06 15:14:07 +00006680 if (expected_start_status != PSA_SUCCESS) {
6681 actual_status = psa_sign_hash_start(&operation, key, alg,
6682 input_data->x, input_data->len);
6683
6684 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6685 }
6686
Paul Elliott91007972022-12-16 12:21:24 +00006687 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6688 TEST_ASSERT(num_ops_prior == 0);
6689
Paul Elliott91007972022-12-16 12:21:24 +00006690 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006691 do {
Paul Elliott91007972022-12-16 12:21:24 +00006692 actual_status = psa_sign_hash_complete(&operation, signature,
6693 signature_size,
6694 &signature_length);
6695
Paul Elliott0c683352022-12-16 19:16:56 +00006696 num_completes++;
6697
Paul Elliott334d7262023-01-20 17:29:41 +00006698 if (actual_status == PSA_SUCCESS ||
6699 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006700 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00006701 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006702
Paul Elliott91007972022-12-16 12:21:24 +00006703 num_ops_prior = num_ops;
6704 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006705 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006706
Paul Elliottc9774412023-02-06 15:14:07 +00006707 TEST_EQUAL(actual_status, expected_complete_status);
6708
6709 if (expected_complete_status != PSA_SUCCESS) {
6710 actual_status = psa_sign_hash_complete(&operation, signature,
6711 signature_size,
6712 &signature_length);
6713
Paul Elliott334d7262023-01-20 17:29:41 +00006714 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006715 }
6716
Paul Elliott91007972022-12-16 12:21:24 +00006717 PSA_ASSERT(psa_sign_hash_abort(&operation));
6718
Paul Elliott59ad9452022-12-18 15:09:02 +00006719 num_ops = psa_sign_hash_get_num_ops(&operation);
6720 TEST_ASSERT(num_ops == 0);
6721
Paul Elliott91007972022-12-16 12:21:24 +00006722 /* The value of *signature_length is unspecified on error, but
6723 * whatever it is, it should be less than signature_size, so that
6724 * if the caller tries to read *signature_length bytes without
6725 * checking the error code then they don't overflow a buffer. */
6726 TEST_LE_U(signature_length, signature_size);
6727
Paul Elliott0c683352022-12-16 19:16:56 +00006728 TEST_LE_U(min_completes, num_completes);
6729 TEST_LE_U(num_completes, max_completes);
6730
Paul Elliott91007972022-12-16 12:21:24 +00006731exit:
6732 psa_reset_key_attributes(&attributes);
6733 psa_destroy_key(key);
6734 mbedtls_free(signature);
6735 PSA_DONE();
6736}
6737/* END_CASE */
6738
mohammad16038cc1cee2018-03-28 01:21:33 +03006739/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006740void sign_verify_hash(int key_type_arg, data_t *key_data,
6741 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006742{
Ronald Cron5425a212020-08-04 14:58:35 +02006743 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006744 psa_key_type_t key_type = key_type_arg;
6745 psa_algorithm_t alg = alg_arg;
6746 size_t key_bits;
6747 unsigned char *signature = NULL;
6748 size_t signature_size;
6749 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6755 psa_set_key_algorithm(&attributes, alg);
6756 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6759 &key));
6760 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6761 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006762
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006763 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006764 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6766 key_bits, alg);
6767 TEST_ASSERT(signature_size != 0);
6768 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6769 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006770
6771 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 PSA_ASSERT(psa_sign_hash(key, alg,
6773 input_data->x, input_data->len,
6774 signature, signature_size,
6775 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006776 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 TEST_LE_U(signature_length, signature_size);
6778 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006779
6780 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 PSA_ASSERT(psa_verify_hash(key, alg,
6782 input_data->x, input_data->len,
6783 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006784
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006786 /* Flip a bit in the input and verify that the signature is now
6787 * detected as invalid. Flip a bit at the beginning, not at the end,
6788 * because ECDSA may ignore the last few bits of the input. */
6789 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 TEST_EQUAL(psa_verify_hash(key, alg,
6791 input_data->x, input_data->len,
6792 signature, signature_length),
6793 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006794 }
6795
6796exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006797 /*
6798 * Key attributes may have been returned by psa_get_key_attributes()
6799 * thus reset them as required.
6800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006802
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 psa_destroy_key(key);
6804 mbedtls_free(signature);
6805 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006806}
6807/* END_CASE */
6808
Paul Elliott712d5122022-12-07 14:03:10 +00006809/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6810void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006811 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006812 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006813{
6814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6815 psa_key_type_t key_type = key_type_arg;
6816 psa_algorithm_t alg = alg_arg;
6817 size_t key_bits;
6818 unsigned char *signature = NULL;
6819 size_t signature_size;
6820 size_t signature_length = 0xdeadbeef;
6821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6822 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006823 uint32_t max_ops = max_ops_arg;
Paul Elliott0c683352022-12-16 19:16:56 +00006824 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006825 size_t min_completes = 0;
6826 size_t max_completes = 0;
6827
Paul Elliott712d5122022-12-07 14:03:10 +00006828 psa_sign_hash_interruptible_operation_t sign_operation =
6829 psa_sign_hash_interruptible_operation_init();
6830 psa_verify_hash_interruptible_operation_t verify_operation =
6831 psa_verify_hash_interruptible_operation_init();
6832
6833 PSA_ASSERT(psa_crypto_init());
6834
Paul Elliott0c683352022-12-16 19:16:56 +00006835 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6836 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006837 psa_set_key_algorithm(&attributes, alg);
6838 psa_set_key_type(&attributes, key_type);
6839
6840 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6841 &key));
6842 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6843 key_bits = psa_get_key_bits(&attributes);
6844
6845 /* Allocate a buffer which has the size advertised by the
6846 * library. */
6847 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6848 key_bits, alg);
6849 TEST_ASSERT(signature_size != 0);
6850 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6851 ASSERT_ALLOC(signature, signature_size);
6852
Paul Elliott0c683352022-12-16 19:16:56 +00006853 psa_interruptible_set_max_ops(max_ops);
6854
Paul Elliott6f600372023-02-06 18:41:05 +00006855 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6856 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006857
Paul Elliott712d5122022-12-07 14:03:10 +00006858 /* Start performing the signature. */
6859 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6860 input_data->x, input_data->len));
6861
6862 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006863 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006864
Paul Elliott0c683352022-12-16 19:16:56 +00006865 status = psa_sign_hash_complete(&sign_operation, signature,
6866 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006867 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006868
6869 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006870 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006871
6872 TEST_ASSERT(status == PSA_SUCCESS);
6873
Paul Elliott0c683352022-12-16 19:16:56 +00006874 TEST_LE_U(min_completes, num_completes);
6875 TEST_LE_U(num_completes, max_completes);
6876
Paul Elliott712d5122022-12-07 14:03:10 +00006877 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6878
6879 /* Check that the signature length looks sensible. */
6880 TEST_LE_U(signature_length, signature_size);
6881 TEST_ASSERT(signature_length > 0);
6882
Paul Elliott0c683352022-12-16 19:16:56 +00006883 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006884
6885 /* Start verification. */
6886 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6887 input_data->x, input_data->len,
6888 signature, signature_length));
6889
6890 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006891 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006892 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00006893
6894 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006895 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006896
6897 TEST_ASSERT(status == PSA_SUCCESS);
6898
Paul Elliott0c683352022-12-16 19:16:56 +00006899 TEST_LE_U(min_completes, num_completes);
6900 TEST_LE_U(num_completes, max_completes);
6901
Paul Elliott712d5122022-12-07 14:03:10 +00006902 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6903
6904 verify_operation = psa_verify_hash_interruptible_operation_init();
6905
6906 if (input_data->len != 0) {
6907 /* Flip a bit in the input and verify that the signature is now
6908 * detected as invalid. Flip a bit at the beginning, not at the end,
6909 * because ECDSA may ignore the last few bits of the input. */
6910 input_data->x[0] ^= 1;
6911
Paul Elliott712d5122022-12-07 14:03:10 +00006912 /* Start verification. */
6913 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6914 input_data->x, input_data->len,
6915 signature, signature_length));
6916
6917 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006918 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006919 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00006920 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006921
6922 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
6923 }
6924
6925 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6926
6927exit:
6928 /*
6929 * Key attributes may have been returned by psa_get_key_attributes()
6930 * thus reset them as required.
6931 */
6932 psa_reset_key_attributes(&attributes);
6933
6934 psa_destroy_key(key);
6935 mbedtls_free(signature);
6936 PSA_DONE();
6937}
6938/* END_CASE */
6939
Gilles Peskine9911b022018-06-29 17:30:48 +02006940/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006941void verify_hash(int key_type_arg, data_t *key_data,
6942 int alg_arg, data_t *hash_data,
6943 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006944{
Ronald Cron5425a212020-08-04 14:58:35 +02006945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006946 psa_key_type_t key_type = key_type_arg;
6947 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006949
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6955 psa_set_key_algorithm(&attributes, alg);
6956 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006957
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6959 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 PSA_ASSERT(psa_verify_hash(key, alg,
6962 hash_data->x, hash_data->len,
6963 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006964
itayzafrir5c753392018-05-08 11:18:38 +03006965exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 psa_reset_key_attributes(&attributes);
6967 psa_destroy_key(key);
6968 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006969}
6970/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006971
Paul Elliott712d5122022-12-07 14:03:10 +00006972/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6973void verify_hash_interruptible(int key_type_arg, data_t *key_data,
6974 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006975 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006976{
6977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6978 psa_key_type_t key_type = key_type_arg;
6979 psa_algorithm_t alg = alg_arg;
6980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6981 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006982 uint32_t num_ops = 0;
6983 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006984 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006985 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006986 size_t min_completes = 0;
6987 size_t max_completes = 0;
6988
Paul Elliott712d5122022-12-07 14:03:10 +00006989 psa_verify_hash_interruptible_operation_t operation =
6990 psa_verify_hash_interruptible_operation_init();
6991
6992 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
6993
6994 PSA_ASSERT(psa_crypto_init());
6995
6996 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6997 psa_set_key_algorithm(&attributes, alg);
6998 psa_set_key_type(&attributes, key_type);
6999
7000 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7001 &key));
7002
Paul Elliott0c683352022-12-16 19:16:56 +00007003 psa_interruptible_set_max_ops(max_ops);
7004
Paul Elliott6f600372023-02-06 18:41:05 +00007005 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7006 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007007
Paul Elliott712d5122022-12-07 14:03:10 +00007008 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7009
7010 TEST_ASSERT(num_ops_prior == 0);
7011
7012 /* Start verification. */
7013 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7014 hash_data->x, hash_data->len,
7015 signature_data->x, signature_data->len)
7016 );
7017
7018 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7019
7020 TEST_ASSERT(num_ops_prior == 0);
7021
7022 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007023 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007024 status = psa_verify_hash_complete(&operation);
7025
Paul Elliott0c683352022-12-16 19:16:56 +00007026 num_completes++;
7027
Paul Elliott712d5122022-12-07 14:03:10 +00007028 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7029 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00007030 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007031
Paul Elliott712d5122022-12-07 14:03:10 +00007032 num_ops_prior = num_ops;
7033 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007034 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007035
7036 TEST_ASSERT(status == PSA_SUCCESS);
7037
Paul Elliott0c683352022-12-16 19:16:56 +00007038 TEST_LE_U(min_completes, num_completes);
7039 TEST_LE_U(num_completes, max_completes);
7040
Paul Elliott712d5122022-12-07 14:03:10 +00007041 PSA_ASSERT(psa_verify_hash_abort(&operation));
7042
Paul Elliott59ad9452022-12-18 15:09:02 +00007043 num_ops = psa_verify_hash_get_num_ops(&operation);
7044 TEST_ASSERT(num_ops == 0);
7045
Paul Elliott712d5122022-12-07 14:03:10 +00007046exit:
7047 psa_reset_key_attributes(&attributes);
7048 psa_destroy_key(key);
7049 PSA_DONE();
7050}
7051/* END_CASE */
7052
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007053/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007054void verify_hash_fail(int key_type_arg, data_t *key_data,
7055 int alg_arg, data_t *hash_data,
7056 data_t *signature_data,
7057 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007058{
Ronald Cron5425a212020-08-04 14:58:35 +02007059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007060 psa_key_type_t key_type = key_type_arg;
7061 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007062 psa_status_t actual_status;
7063 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007067
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7069 psa_set_key_algorithm(&attributes, alg);
7070 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7073 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 actual_status = psa_verify_hash(key, alg,
7076 hash_data->x, hash_data->len,
7077 signature_data->x, signature_data->len);
7078 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007079
7080exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 psa_reset_key_attributes(&attributes);
7082 psa_destroy_key(key);
7083 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007084}
7085/* END_CASE */
7086
Paul Elliott91007972022-12-16 12:21:24 +00007087/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7088void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7089 int alg_arg, data_t *hash_data,
7090 data_t *signature_data,
7091 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007092 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007093 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007094{
7095 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7096 psa_key_type_t key_type = key_type_arg;
7097 psa_algorithm_t alg = alg_arg;
7098 psa_status_t actual_status;
7099 psa_status_t expected_start_status = expected_start_status_arg;
7100 psa_status_t expected_complete_status = expected_complete_status_arg;
7101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007102 uint32_t num_ops = 0;
7103 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007104 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007105 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007106 size_t min_completes = 0;
7107 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007108 psa_verify_hash_interruptible_operation_t operation =
7109 psa_verify_hash_interruptible_operation_init();
7110
7111 PSA_ASSERT(psa_crypto_init());
7112
7113 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7114 psa_set_key_algorithm(&attributes, alg);
7115 psa_set_key_type(&attributes, key_type);
7116
7117 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7118 &key));
7119
Paul Elliott0c683352022-12-16 19:16:56 +00007120 psa_interruptible_set_max_ops(max_ops);
7121
Paul Elliott6f600372023-02-06 18:41:05 +00007122 interruptible_signverify_get_minmax_completes(max_ops,
7123 expected_complete_status,
7124 &min_completes,
7125 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007126
Paul Elliott91007972022-12-16 12:21:24 +00007127 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7128 TEST_ASSERT(num_ops_prior == 0);
7129
7130 /* Start verification. */
7131 actual_status = psa_verify_hash_start(&operation, key, alg,
7132 hash_data->x, hash_data->len,
7133 signature_data->x,
7134 signature_data->len);
7135
7136 TEST_EQUAL(actual_status, expected_start_status);
7137
Paul Elliottc9774412023-02-06 15:14:07 +00007138 if (expected_start_status != PSA_SUCCESS) {
7139 actual_status = psa_verify_hash_start(&operation, key, alg,
7140 hash_data->x, hash_data->len,
7141 signature_data->x,
7142 signature_data->len);
7143
7144 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7145 }
7146
Paul Elliott91007972022-12-16 12:21:24 +00007147 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7148 TEST_ASSERT(num_ops_prior == 0);
7149
Paul Elliott91007972022-12-16 12:21:24 +00007150 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007151 do {
Paul Elliott91007972022-12-16 12:21:24 +00007152 actual_status = psa_verify_hash_complete(&operation);
7153
Paul Elliott0c683352022-12-16 19:16:56 +00007154 num_completes++;
7155
Paul Elliott334d7262023-01-20 17:29:41 +00007156 if (actual_status == PSA_SUCCESS ||
7157 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007158 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00007159 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007160
Paul Elliott91007972022-12-16 12:21:24 +00007161 num_ops_prior = num_ops;
7162 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007163 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007164
Paul Elliottc9774412023-02-06 15:14:07 +00007165 TEST_EQUAL(actual_status, expected_complete_status);
7166
7167 if (expected_complete_status != PSA_SUCCESS) {
7168 actual_status = psa_verify_hash_complete(&operation);
7169
Paul Elliott334d7262023-01-20 17:29:41 +00007170 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007171 }
7172
Paul Elliott0c683352022-12-16 19:16:56 +00007173 TEST_LE_U(min_completes, num_completes);
7174 TEST_LE_U(num_completes, max_completes);
7175
Paul Elliott91007972022-12-16 12:21:24 +00007176 PSA_ASSERT(psa_verify_hash_abort(&operation));
7177
Paul Elliott59ad9452022-12-18 15:09:02 +00007178 num_ops = psa_verify_hash_get_num_ops(&operation);
7179 TEST_ASSERT(num_ops == 0);
7180
Paul Elliott91007972022-12-16 12:21:24 +00007181exit:
7182 psa_reset_key_attributes(&attributes);
7183 psa_destroy_key(key);
7184 PSA_DONE();
7185}
7186/* END_CASE */
7187
Paul Elliott20a36062022-12-18 13:21:25 +00007188/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007189void interruptible_signverify_hash_state_test(int key_type_arg,
7190 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007191{
7192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7193 psa_key_type_t key_type = key_type_arg;
7194 psa_algorithm_t alg = alg_arg;
7195 size_t key_bits;
7196 unsigned char *signature = NULL;
7197 size_t signature_size;
7198 size_t signature_length = 0xdeadbeef;
7199 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7200 psa_sign_hash_interruptible_operation_t sign_operation =
7201 psa_sign_hash_interruptible_operation_init();
7202 psa_verify_hash_interruptible_operation_t verify_operation =
7203 psa_verify_hash_interruptible_operation_init();
7204
7205 PSA_ASSERT(psa_crypto_init());
7206
7207 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7208 PSA_KEY_USAGE_VERIFY_HASH);
7209 psa_set_key_algorithm(&attributes, alg);
7210 psa_set_key_type(&attributes, key_type);
7211
7212 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7213 &key));
7214 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7215 key_bits = psa_get_key_bits(&attributes);
7216
7217 /* Allocate a buffer which has the size advertised by the
7218 * library. */
7219 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7220 key_bits, alg);
7221 TEST_ASSERT(signature_size != 0);
7222 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7223 ASSERT_ALLOC(signature, signature_size);
7224
7225 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7226
7227 /* --- Attempt completes prior to starts --- */
7228 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7229 signature_size,
7230 &signature_length),
7231 PSA_ERROR_BAD_STATE);
7232
7233 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7234
7235 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7236 PSA_ERROR_BAD_STATE);
7237
7238 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7239
7240 /* --- Aborts in all other places. --- */
7241 psa_sign_hash_abort(&sign_operation);
7242
7243 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7244 input_data->x, input_data->len));
7245
7246 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7247
7248 psa_interruptible_set_max_ops(1);
7249
7250 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7251 input_data->x, input_data->len));
7252
7253 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7254 signature_size,
7255 &signature_length),
7256 PSA_OPERATION_INCOMPLETE);
7257
7258 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7259
7260 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7261
7262 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7263 input_data->x, input_data->len));
7264
7265 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7266 signature_size,
7267 &signature_length));
7268
7269 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7270
7271 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7272
7273 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7274 input_data->x, input_data->len,
7275 signature, signature_length));
7276
7277 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7278
7279 psa_interruptible_set_max_ops(1);
7280
7281 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7282 input_data->x, input_data->len,
7283 signature, signature_length));
7284
7285 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7286 PSA_OPERATION_INCOMPLETE);
7287
7288 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7289
7290 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7291
7292 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7293 input_data->x, input_data->len,
7294 signature, signature_length));
7295
7296 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7297
7298 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7299
7300 /* --- Attempt double starts. --- */
7301
7302 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7303 input_data->x, input_data->len));
7304
7305 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7306 input_data->x, input_data->len),
7307 PSA_ERROR_BAD_STATE);
7308
7309 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7310
7311 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7312 input_data->x, input_data->len,
7313 signature, signature_length));
7314
7315 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7316 input_data->x, input_data->len,
7317 signature, signature_length),
7318 PSA_ERROR_BAD_STATE);
7319
7320 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7321
Paul Elliott76d671a2023-02-07 17:45:18 +00007322exit:
7323 /*
7324 * Key attributes may have been returned by psa_get_key_attributes()
7325 * thus reset them as required.
7326 */
7327 psa_reset_key_attributes(&attributes);
7328
7329 psa_destroy_key(key);
7330 mbedtls_free(signature);
7331 PSA_DONE();
7332}
7333/* END_CASE */
7334
7335/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7336void interruptible_signverify_hash_negative_tests(int key_type_arg,
7337 data_t *key_data, int alg_arg, data_t *input_data)
7338{
7339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7340 psa_key_type_t key_type = key_type_arg;
7341 psa_algorithm_t alg = alg_arg;
7342 size_t key_bits;
7343 unsigned char *signature = NULL;
7344 size_t signature_size;
7345 size_t signature_length = 0xdeadbeef;
7346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7347 uint8_t *input_buffer = NULL;
7348 psa_sign_hash_interruptible_operation_t sign_operation =
7349 psa_sign_hash_interruptible_operation_init();
7350 psa_verify_hash_interruptible_operation_t verify_operation =
7351 psa_verify_hash_interruptible_operation_init();
7352
7353 PSA_ASSERT(psa_crypto_init());
7354
7355 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7356 PSA_KEY_USAGE_VERIFY_HASH);
7357 psa_set_key_algorithm(&attributes, alg);
7358 psa_set_key_type(&attributes, key_type);
7359
7360 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7361 &key));
7362 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7363 key_bits = psa_get_key_bits(&attributes);
7364
7365 /* Allocate a buffer which has the size advertised by the
7366 * library. */
7367 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7368 key_bits, alg);
7369 TEST_ASSERT(signature_size != 0);
7370 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7371 ASSERT_ALLOC(signature, signature_size);
7372
Paul Elliott20a36062022-12-18 13:21:25 +00007373 /* --- Ensure changing the max ops mid operation works (operation should
7374 * complete successfully after setting max ops to unlimited --- */
7375 psa_interruptible_set_max_ops(1);
7376
7377 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7378 input_data->x, input_data->len));
7379
7380 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7381 signature_size,
7382 &signature_length),
7383 PSA_OPERATION_INCOMPLETE);
7384
7385 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7386
7387 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7388 signature_size,
7389 &signature_length));
7390
7391 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7392
7393 psa_interruptible_set_max_ops(1);
7394
7395 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7396 input_data->x, input_data->len,
7397 signature, signature_length));
7398
7399 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7400 PSA_OPERATION_INCOMPLETE);
7401
7402 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7403
7404 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7405
7406 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7407
7408 /* --- Change function inputs mid run, to cause an error (sign only,
7409 * verify passes all inputs to start. --- */
7410
7411 psa_interruptible_set_max_ops(1);
7412
7413 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7414 input_data->x, input_data->len));
7415
7416 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7417 signature_size,
7418 &signature_length),
7419 PSA_OPERATION_INCOMPLETE);
7420
7421 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7422 0,
7423 &signature_length),
7424 PSA_ERROR_BUFFER_TOO_SMALL);
7425
Paul Elliottc9774412023-02-06 15:14:07 +00007426 /* And test that this invalidates the operation. */
7427 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7428 0,
7429 &signature_length),
7430 PSA_ERROR_BAD_STATE);
7431
Paul Elliott20a36062022-12-18 13:21:25 +00007432 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7433
Paul Elliottf9c91a72023-02-05 18:06:38 +00007434 /* Trash the hash buffer in between start and complete, to ensure
7435 * no reliance on external buffers. */
7436 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7437
7438 input_buffer = mbedtls_calloc(1, input_data->len);
7439 TEST_ASSERT(input_buffer != NULL);
7440
7441 memcpy(input_buffer, input_data->x, input_data->len);
7442
7443 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7444 input_buffer, input_data->len));
7445
7446 memset(input_buffer, '!', input_data->len);
7447 mbedtls_free(input_buffer);
7448 input_buffer = NULL;
7449
7450 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7451 signature_size,
7452 &signature_length));
7453
7454 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7455
7456 input_buffer = mbedtls_calloc(1, input_data->len);
7457 TEST_ASSERT(input_buffer != NULL);
7458
7459 memcpy(input_buffer, input_data->x, input_data->len);
7460
7461 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7462 input_buffer, input_data->len,
7463 signature, signature_length));
7464
7465 memset(input_buffer, '!', input_data->len);
7466 mbedtls_free(input_buffer);
7467 input_buffer = NULL;
7468
7469 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7470
7471 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7472
Paul Elliott20a36062022-12-18 13:21:25 +00007473exit:
7474 /*
7475 * Key attributes may have been returned by psa_get_key_attributes()
7476 * thus reset them as required.
7477 */
7478 psa_reset_key_attributes(&attributes);
7479
7480 psa_destroy_key(key);
7481 mbedtls_free(signature);
7482 PSA_DONE();
7483}
7484/* END_CASE */
7485
Paul Elliotta4cb9092023-02-07 18:01:55 +00007486/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7487void interruptible_signverify_hash_maxops_tests(int key_type_arg,
7488 data_t *key_data, int alg_arg, data_t *input_data)
7489{
7490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7491 psa_key_type_t key_type = key_type_arg;
7492 psa_algorithm_t alg = alg_arg;
7493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7494 psa_sign_hash_interruptible_operation_t sign_operation =
7495 psa_sign_hash_interruptible_operation_init();
7496
7497 PSA_ASSERT(psa_crypto_init());
7498
7499 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7500 PSA_KEY_USAGE_VERIFY_HASH);
7501 psa_set_key_algorithm(&attributes, alg);
7502 psa_set_key_type(&attributes, key_type);
7503
7504 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7505 &key));
7506
7507 /* Check that default max ops gets set if we don't set it. */
7508 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7509 input_data->x, input_data->len));
7510
7511 TEST_EQUAL(psa_interruptible_get_max_ops(),
7512 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7513
7514 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7515
7516 /* Check that max ops gets set properly. */
7517
7518 psa_interruptible_set_max_ops(0xbeef);
7519
7520 TEST_EQUAL(psa_interruptible_get_max_ops(),
7521 0xbeef);
7522
7523exit:
7524 /*
7525 * Key attributes may have been returned by psa_get_key_attributes()
7526 * thus reset them as required.
7527 */
7528 psa_reset_key_attributes(&attributes);
7529
7530 psa_destroy_key(key);
7531 PSA_DONE();
7532}
7533/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007534
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007535/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007536void sign_message_deterministic(int key_type_arg,
7537 data_t *key_data,
7538 int alg_arg,
7539 data_t *input_data,
7540 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007541{
7542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7543 psa_key_type_t key_type = key_type_arg;
7544 psa_algorithm_t alg = alg_arg;
7545 size_t key_bits;
7546 unsigned char *signature = NULL;
7547 size_t signature_size;
7548 size_t signature_length = 0xdeadbeef;
7549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7550
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007552
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7554 psa_set_key_algorithm(&attributes, alg);
7555 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007556
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7558 &key));
7559 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7560 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007561
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7563 TEST_ASSERT(signature_size != 0);
7564 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7565 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007566
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 PSA_ASSERT(psa_sign_message(key, alg,
7568 input_data->x, input_data->len,
7569 signature, signature_size,
7570 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007571
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 ASSERT_COMPARE(output_data->x, output_data->len,
7573 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007574
7575exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 psa_destroy_key(key);
7579 mbedtls_free(signature);
7580 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007581
7582}
7583/* END_CASE */
7584
7585/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007586void sign_message_fail(int key_type_arg,
7587 data_t *key_data,
7588 int alg_arg,
7589 data_t *input_data,
7590 int signature_size_arg,
7591 int expected_status_arg)
7592{
7593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7594 psa_key_type_t key_type = key_type_arg;
7595 psa_algorithm_t alg = alg_arg;
7596 size_t signature_size = signature_size_arg;
7597 psa_status_t actual_status;
7598 psa_status_t expected_status = expected_status_arg;
7599 unsigned char *signature = NULL;
7600 size_t signature_length = 0xdeadbeef;
7601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7602
7603 ASSERT_ALLOC(signature, signature_size);
7604
7605 PSA_ASSERT(psa_crypto_init());
7606
7607 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7608 psa_set_key_algorithm(&attributes, alg);
7609 psa_set_key_type(&attributes, key_type);
7610
7611 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7612 &key));
7613
7614 actual_status = psa_sign_message(key, alg,
7615 input_data->x, input_data->len,
7616 signature, signature_size,
7617 &signature_length);
7618 TEST_EQUAL(actual_status, expected_status);
7619 /* The value of *signature_length is unspecified on error, but
7620 * whatever it is, it should be less than signature_size, so that
7621 * if the caller tries to read *signature_length bytes without
7622 * checking the error code then they don't overflow a buffer. */
7623 TEST_LE_U(signature_length, signature_size);
7624
7625exit:
7626 psa_reset_key_attributes(&attributes);
7627 psa_destroy_key(key);
7628 mbedtls_free(signature);
7629 PSA_DONE();
7630}
7631/* END_CASE */
7632
7633/* BEGIN_CASE */
7634void sign_verify_message(int key_type_arg,
7635 data_t *key_data,
7636 int alg_arg,
7637 data_t *input_data)
7638{
7639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7640 psa_key_type_t key_type = key_type_arg;
7641 psa_algorithm_t alg = alg_arg;
7642 size_t key_bits;
7643 unsigned char *signature = NULL;
7644 size_t signature_size;
7645 size_t signature_length = 0xdeadbeef;
7646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7647
7648 PSA_ASSERT(psa_crypto_init());
7649
7650 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7651 PSA_KEY_USAGE_VERIFY_MESSAGE);
7652 psa_set_key_algorithm(&attributes, alg);
7653 psa_set_key_type(&attributes, key_type);
7654
7655 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7656 &key));
7657 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7658 key_bits = psa_get_key_bits(&attributes);
7659
7660 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7661 TEST_ASSERT(signature_size != 0);
7662 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7663 ASSERT_ALLOC(signature, signature_size);
7664
7665 PSA_ASSERT(psa_sign_message(key, alg,
7666 input_data->x, input_data->len,
7667 signature, signature_size,
7668 &signature_length));
7669 TEST_LE_U(signature_length, signature_size);
7670 TEST_ASSERT(signature_length > 0);
7671
7672 PSA_ASSERT(psa_verify_message(key, alg,
7673 input_data->x, input_data->len,
7674 signature, signature_length));
7675
7676 if (input_data->len != 0) {
7677 /* Flip a bit in the input and verify that the signature is now
7678 * detected as invalid. Flip a bit at the beginning, not at the end,
7679 * because ECDSA may ignore the last few bits of the input. */
7680 input_data->x[0] ^= 1;
7681 TEST_EQUAL(psa_verify_message(key, alg,
7682 input_data->x, input_data->len,
7683 signature, signature_length),
7684 PSA_ERROR_INVALID_SIGNATURE);
7685 }
7686
7687exit:
7688 psa_reset_key_attributes(&attributes);
7689
7690 psa_destroy_key(key);
7691 mbedtls_free(signature);
7692 PSA_DONE();
7693}
7694/* END_CASE */
7695
7696/* BEGIN_CASE */
7697void verify_message(int key_type_arg,
7698 data_t *key_data,
7699 int alg_arg,
7700 data_t *input_data,
7701 data_t *signature_data)
7702{
7703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7704 psa_key_type_t key_type = key_type_arg;
7705 psa_algorithm_t alg = alg_arg;
7706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7707
7708 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7709
7710 PSA_ASSERT(psa_crypto_init());
7711
7712 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7713 psa_set_key_algorithm(&attributes, alg);
7714 psa_set_key_type(&attributes, key_type);
7715
7716 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7717 &key));
7718
7719 PSA_ASSERT(psa_verify_message(key, alg,
7720 input_data->x, input_data->len,
7721 signature_data->x, signature_data->len));
7722
7723exit:
7724 psa_reset_key_attributes(&attributes);
7725 psa_destroy_key(key);
7726 PSA_DONE();
7727}
7728/* END_CASE */
7729
7730/* BEGIN_CASE */
7731void verify_message_fail(int key_type_arg,
7732 data_t *key_data,
7733 int alg_arg,
7734 data_t *hash_data,
7735 data_t *signature_data,
7736 int expected_status_arg)
7737{
7738 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7739 psa_key_type_t key_type = key_type_arg;
7740 psa_algorithm_t alg = alg_arg;
7741 psa_status_t actual_status;
7742 psa_status_t expected_status = expected_status_arg;
7743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7744
7745 PSA_ASSERT(psa_crypto_init());
7746
7747 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7748 psa_set_key_algorithm(&attributes, alg);
7749 psa_set_key_type(&attributes, key_type);
7750
7751 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7752 &key));
7753
7754 actual_status = psa_verify_message(key, alg,
7755 hash_data->x, hash_data->len,
7756 signature_data->x,
7757 signature_data->len);
7758 TEST_EQUAL(actual_status, expected_status);
7759
7760exit:
7761 psa_reset_key_attributes(&attributes);
7762 psa_destroy_key(key);
7763 PSA_DONE();
7764}
7765/* END_CASE */
7766
7767/* BEGIN_CASE */
7768void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02007769 data_t *key_data,
7770 int alg_arg,
7771 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 data_t *label,
7773 int expected_output_length_arg,
7774 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02007775{
Ronald Cron5425a212020-08-04 14:58:35 +02007776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007777 psa_key_type_t key_type = key_type_arg;
7778 psa_algorithm_t alg = alg_arg;
7779 size_t expected_output_length = expected_output_length_arg;
7780 size_t key_bits;
7781 unsigned char *output = NULL;
7782 size_t output_size;
7783 size_t output_length = ~0;
7784 psa_status_t actual_status;
7785 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007789
Gilles Peskine656896e2018-06-29 19:12:28 +02007790 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
7792 psa_set_key_algorithm(&attributes, alg);
7793 psa_set_key_type(&attributes, key_type);
7794 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7795 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02007796
7797 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007798 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7799 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007800
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7802 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7803 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02007804
7805 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 actual_status = psa_asymmetric_encrypt(key, alg,
7807 input_data->x, input_data->len,
7808 label->x, label->len,
7809 output, output_size,
7810 &output_length);
7811 TEST_EQUAL(actual_status, expected_status);
7812 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine656896e2018-06-29 19:12:28 +02007813
Gilles Peskine68428122018-06-30 18:42:41 +02007814 /* If the label is empty, the test framework puts a non-null pointer
7815 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007817 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007818 if (output_size != 0) {
7819 memset(output, 0, output_size);
7820 }
7821 actual_status = psa_asymmetric_encrypt(key, alg,
7822 input_data->x, input_data->len,
7823 NULL, label->len,
7824 output, output_size,
7825 &output_length);
7826 TEST_EQUAL(actual_status, expected_status);
7827 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007828 }
7829
Gilles Peskine656896e2018-06-29 19:12:28 +02007830exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007831 /*
7832 * Key attributes may have been returned by psa_get_key_attributes()
7833 * thus reset them as required.
7834 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007836
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 psa_destroy_key(key);
7838 mbedtls_free(output);
7839 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02007840}
7841/* END_CASE */
7842
7843/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007844void asymmetric_encrypt_decrypt(int key_type_arg,
7845 data_t *key_data,
7846 int alg_arg,
7847 data_t *input_data,
7848 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007849{
Ronald Cron5425a212020-08-04 14:58:35 +02007850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007851 psa_key_type_t key_type = key_type_arg;
7852 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007853 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007854 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007855 size_t output_size;
7856 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007857 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007858 size_t output2_size;
7859 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007861
Gilles Peskine449bd832023-01-11 14:50:10 +01007862 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007863
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
7865 psa_set_key_algorithm(&attributes, alg);
7866 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007867
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7869 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007870
7871 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007872 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7873 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007874
Gilles Peskine449bd832023-01-11 14:50:10 +01007875 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7876 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7877 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007878
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007879 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 TEST_LE_U(output2_size,
7881 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
7882 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7883 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007884
Gilles Peskineeebd7382018-06-08 18:11:54 +02007885 /* We test encryption by checking that encrypt-then-decrypt gives back
7886 * the original plaintext because of the non-optional random
7887 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
7889 input_data->x, input_data->len,
7890 label->x, label->len,
7891 output, output_size,
7892 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007893 /* We don't know what ciphertext length to expect, but check that
7894 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007896
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7898 output, output_length,
7899 label->x, label->len,
7900 output2, output2_size,
7901 &output2_length));
7902 ASSERT_COMPARE(input_data->x, input_data->len,
7903 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007904
7905exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007906 /*
7907 * Key attributes may have been returned by psa_get_key_attributes()
7908 * thus reset them as required.
7909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 psa_destroy_key(key);
7913 mbedtls_free(output);
7914 mbedtls_free(output2);
7915 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007916}
7917/* END_CASE */
7918
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007919/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007920void asymmetric_decrypt(int key_type_arg,
7921 data_t *key_data,
7922 int alg_arg,
7923 data_t *input_data,
7924 data_t *label,
7925 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007926{
Ronald Cron5425a212020-08-04 14:58:35 +02007927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007928 psa_key_type_t key_type = key_type_arg;
7929 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007930 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007931 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007932 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007933 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007934 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007935
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007937
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7939 psa_set_key_algorithm(&attributes, alg);
7940 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007941
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7943 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007944
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7946 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007947
7948 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7950 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7951 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007952
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7954 input_data->x, input_data->len,
7955 label->x, label->len,
7956 output,
7957 output_size,
7958 &output_length));
7959 ASSERT_COMPARE(expected_data->x, expected_data->len,
7960 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007961
Gilles Peskine68428122018-06-30 18:42:41 +02007962 /* If the label is empty, the test framework puts a non-null pointer
7963 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007965 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 if (output_size != 0) {
7967 memset(output, 0, output_size);
7968 }
7969 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7970 input_data->x, input_data->len,
7971 NULL, label->len,
7972 output,
7973 output_size,
7974 &output_length));
7975 ASSERT_COMPARE(expected_data->x, expected_data->len,
7976 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007977 }
7978
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007979exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 psa_reset_key_attributes(&attributes);
7981 psa_destroy_key(key);
7982 mbedtls_free(output);
7983 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007984}
7985/* END_CASE */
7986
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007987/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007988void asymmetric_decrypt_fail(int key_type_arg,
7989 data_t *key_data,
7990 int alg_arg,
7991 data_t *input_data,
7992 data_t *label,
7993 int output_size_arg,
7994 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007995{
Ronald Cron5425a212020-08-04 14:58:35 +02007996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007997 psa_key_type_t key_type = key_type_arg;
7998 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007999 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008000 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008001 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008002 psa_status_t actual_status;
8003 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008004 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008005
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008007
Gilles Peskine449bd832023-01-11 14:50:10 +01008008 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008009
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8011 psa_set_key_algorithm(&attributes, alg);
8012 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008013
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8015 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008016
Gilles Peskine449bd832023-01-11 14:50:10 +01008017 actual_status = psa_asymmetric_decrypt(key, alg,
8018 input_data->x, input_data->len,
8019 label->x, label->len,
8020 output, output_size,
8021 &output_length);
8022 TEST_EQUAL(actual_status, expected_status);
8023 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008024
Gilles Peskine68428122018-06-30 18:42:41 +02008025 /* If the label is empty, the test framework puts a non-null pointer
8026 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008028 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008029 if (output_size != 0) {
8030 memset(output, 0, output_size);
8031 }
8032 actual_status = psa_asymmetric_decrypt(key, alg,
8033 input_data->x, input_data->len,
8034 NULL, label->len,
8035 output, output_size,
8036 &output_length);
8037 TEST_EQUAL(actual_status, expected_status);
8038 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008039 }
8040
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 psa_reset_key_attributes(&attributes);
8043 psa_destroy_key(key);
8044 mbedtls_free(output);
8045 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008046}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008047/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008048
8049/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008050void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008051{
8052 /* Test each valid way of initializing the object, except for `= {0}`, as
8053 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8054 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008055 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008056 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008058 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8059 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008060
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008062
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008063 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008064 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8065 PSA_ERROR_BAD_STATE);
8066 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8067 PSA_ERROR_BAD_STATE);
8068 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8069 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008070
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008071 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008072 PSA_ASSERT(psa_key_derivation_abort(&func));
8073 PSA_ASSERT(psa_key_derivation_abort(&init));
8074 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008075}
8076/* END_CASE */
8077
Janos Follath16de4a42019-06-13 16:32:24 +01008078/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008079void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008080{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008081 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008082 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008083 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008084
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008086
Gilles Peskine449bd832023-01-11 14:50:10 +01008087 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8088 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008089
8090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008091 psa_key_derivation_abort(&operation);
8092 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008093}
8094/* END_CASE */
8095
Janos Follathaf3c2a02019-06-12 12:34:34 +01008096/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008097void derive_set_capacity(int alg_arg, int capacity_arg,
8098 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008099{
8100 psa_algorithm_t alg = alg_arg;
8101 size_t capacity = capacity_arg;
8102 psa_status_t expected_status = expected_status_arg;
8103 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8104
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008106
Gilles Peskine449bd832023-01-11 14:50:10 +01008107 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008108
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8110 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008111
8112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 psa_key_derivation_abort(&operation);
8114 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008115}
8116/* END_CASE */
8117
8118/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008119void derive_input(int alg_arg,
8120 int step_arg1, int key_type_arg1, data_t *input1,
8121 int expected_status_arg1,
8122 int step_arg2, int key_type_arg2, data_t *input2,
8123 int expected_status_arg2,
8124 int step_arg3, int key_type_arg3, data_t *input3,
8125 int expected_status_arg3,
8126 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008127{
8128 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8130 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8131 psa_status_t expected_statuses[] = { expected_status_arg1,
8132 expected_status_arg2,
8133 expected_status_arg3 };
8134 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008135 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8136 MBEDTLS_SVC_KEY_ID_INIT,
8137 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008138 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8140 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008141 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008142 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008143 psa_status_t expected_output_status = expected_output_status_arg;
8144 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008145
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008147
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8149 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008150
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008152
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8154 mbedtls_test_set_step(i);
8155 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008156 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8158 psa_set_key_type(&attributes, key_types[i]);
8159 PSA_ASSERT(psa_import_key(&attributes,
8160 inputs[i]->x, inputs[i]->len,
8161 &keys[i]));
8162 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8163 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008164 // When taking a private key as secret input, use key agreement
8165 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008166 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8167 &operation, keys[i]),
8168 expected_statuses[i]);
8169 } else {
8170 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8171 keys[i]),
8172 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008174 } else {
8175 TEST_EQUAL(psa_key_derivation_input_bytes(
8176 &operation, steps[i],
8177 inputs[i]->x, inputs[i]->len),
8178 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008179 }
8180 }
8181
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 if (output_key_type != PSA_KEY_TYPE_NONE) {
8183 psa_reset_key_attributes(&attributes);
8184 psa_set_key_type(&attributes, output_key_type);
8185 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008186 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008187 psa_key_derivation_output_key(&attributes, &operation,
8188 &output_key);
8189 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008190 uint8_t buffer[1];
8191 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008192 psa_key_derivation_output_bytes(&operation,
8193 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008194 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008196
Janos Follathaf3c2a02019-06-12 12:34:34 +01008197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 psa_key_derivation_abort(&operation);
8199 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8200 psa_destroy_key(keys[i]);
8201 }
8202 psa_destroy_key(output_key);
8203 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008204}
8205/* END_CASE */
8206
Janos Follathd958bb72019-07-03 15:02:16 +01008207/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008208void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008209{
Janos Follathd958bb72019-07-03 15:02:16 +01008210 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008212 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008213 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008214 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008216 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008218 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008220 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8221 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008222 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008224
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8228 psa_set_key_algorithm(&attributes, alg);
8229 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008230
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 PSA_ASSERT(psa_import_key(&attributes,
8232 key_data, sizeof(key_data),
8233 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008234
8235 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8237 input1, input1_length,
8238 input2, input2_length,
8239 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008240 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008242
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008243 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8245 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008246
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008248
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8250 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008251
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 psa_key_derivation_abort(&operation);
8254 psa_destroy_key(key);
8255 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008256}
8257/* END_CASE */
8258
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008259/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008260void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008261{
8262 uint8_t output_buffer[16];
8263 size_t buffer_size = 16;
8264 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008265 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008266
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8268 output_buffer, buffer_size)
8269 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008270
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8272 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008273
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008275
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8277 output_buffer, buffer_size)
8278 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008279
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8281 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008282
8283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008285}
8286/* END_CASE */
8287
8288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008289void derive_output(int alg_arg,
8290 int step1_arg, data_t *input1, int expected_status_arg1,
8291 int step2_arg, data_t *input2, int expected_status_arg2,
8292 int step3_arg, data_t *input3, int expected_status_arg3,
8293 int step4_arg, data_t *input4, int expected_status_arg4,
8294 data_t *key_agreement_peer_key,
8295 int requested_capacity_arg,
8296 data_t *expected_output1,
8297 data_t *expected_output2,
8298 int other_key_input_type,
8299 int key_input_type,
8300 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008301{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008302 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8304 data_t *inputs[] = { input1, input2, input3, input4 };
8305 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8306 MBEDTLS_SVC_KEY_ID_INIT,
8307 MBEDTLS_SVC_KEY_ID_INIT,
8308 MBEDTLS_SVC_KEY_ID_INIT };
8309 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8310 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008311 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008312 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008313 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008315 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008317 size_t output_buffer_size = 0;
8318 uint8_t *output_buffer = NULL;
8319 size_t expected_capacity;
8320 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008321 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8322 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8323 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8324 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008325 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008326 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008327 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008328
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8330 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008331 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 }
8333 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008334 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008336 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 ASSERT_ALLOC(output_buffer, output_buffer_size);
8338 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008339
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008340 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8342 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8343 requested_capacity));
8344 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8345 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008346 case 0:
8347 break;
8348 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008350 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 TEST_EQUAL(psa_key_derivation_input_bytes(
8352 &operation, steps[i],
8353 inputs[i]->x, inputs[i]->len),
8354 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008355
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008357 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008359 break;
8360 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8362 psa_set_key_algorithm(&attributes1, alg);
8363 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 PSA_ASSERT(psa_import_key(&attributes1,
8366 inputs[i]->x, inputs[i]->len,
8367 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008368
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8370 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8371 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8372 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008373 }
8374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8376 steps[i],
8377 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008378 break;
8379 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008381 break;
8382 }
8383 break;
8384 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008386 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8388 steps[i],
8389 inputs[i]->x,
8390 inputs[i]->len),
8391 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008392 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008393 case 1: // input key, type DERIVE
8394 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8396 psa_set_key_algorithm(&attributes2, alg);
8397 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008398
8399 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 if (other_key_input_type == 11) {
8401 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8402 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008403
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 PSA_ASSERT(psa_import_key(&attributes2,
8405 inputs[i]->x, inputs[i]->len,
8406 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8409 steps[i],
8410 keys[i]),
8411 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008412 break;
8413 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8415 psa_set_key_algorithm(&attributes3, alg);
8416 psa_set_key_type(&attributes3,
8417 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008418
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 PSA_ASSERT(psa_import_key(&attributes3,
8420 inputs[i]->x, inputs[i]->len,
8421 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008422
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 TEST_EQUAL(psa_key_derivation_key_agreement(
8424 &operation,
8425 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8426 keys[i], key_agreement_peer_key->x,
8427 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008428 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008429 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008431 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008432 }
8433
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008437 break;
8438 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 TEST_EQUAL(psa_key_derivation_input_bytes(
8440 &operation, steps[i],
8441 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008442
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008444 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008446 break;
8447 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008448 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008449
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8451 &current_capacity));
8452 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008453 expected_capacity = requested_capacity;
8454
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008456 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8457
8458 /* For output key derivation secret must be provided using
8459 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008461 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8465 psa_set_key_algorithm(&attributes4, alg);
8466 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8467 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008468
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8470 &derived_key), expected_status);
8471 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008472 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008474 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 status = psa_key_derivation_output_bytes(&operation,
8476 output_buffer, output_sizes[i]);
8477 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008478 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 TEST_ASSERT(status == PSA_SUCCESS ||
8480 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008481 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 } else if (expected_capacity == 0 ||
8483 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008484 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008486 expected_capacity = 0;
8487 continue;
8488 }
8489 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 PSA_ASSERT(status);
8491 if (output_sizes[i] != 0) {
8492 ASSERT_COMPARE(output_buffer, output_sizes[i],
8493 expected_outputs[i], output_sizes[i]);
8494 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008495 /* Check the operation status. */
8496 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8498 &current_capacity));
8499 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008500 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008501 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008503
8504exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 mbedtls_free(output_buffer);
8506 psa_key_derivation_abort(&operation);
8507 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8508 psa_destroy_key(keys[i]);
8509 }
8510 psa_destroy_key(derived_key);
8511 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008512}
8513/* END_CASE */
8514
8515/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008516void derive_full(int alg_arg,
8517 data_t *key_data,
8518 data_t *input1,
8519 data_t *input2,
8520 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008521{
Ronald Cron5425a212020-08-04 14:58:35 +02008522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008523 psa_algorithm_t alg = alg_arg;
8524 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008525 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008526 unsigned char output_buffer[16];
8527 size_t expected_capacity = requested_capacity;
8528 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008530
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008532
Gilles Peskine449bd832023-01-11 14:50:10 +01008533 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8534 psa_set_key_algorithm(&attributes, alg);
8535 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008536
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8538 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8541 input1->x, input1->len,
8542 input2->x, input2->len,
8543 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008544 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008546
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8548 &current_capacity));
8549 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008550
8551 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 while (current_capacity > 0) {
8553 size_t read_size = sizeof(output_buffer);
8554 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008555 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 }
8557 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8558 output_buffer,
8559 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008560 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8562 &current_capacity));
8563 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008564 }
8565
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008566 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8568 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008569
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008571
8572exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 psa_key_derivation_abort(&operation);
8574 psa_destroy_key(key);
8575 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008576}
8577/* END_CASE */
8578
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008579/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008580void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8581 int derivation_step,
8582 int capacity, int expected_capacity_status_arg,
8583 data_t *expected_output,
8584 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008585{
8586 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008588 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008589 uint8_t *output_buffer = NULL;
8590 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008591 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8592 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8593 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008594
Gilles Peskine449bd832023-01-11 14:50:10 +01008595 ASSERT_ALLOC(output_buffer, expected_output->len);
8596 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8599 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8600 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008601
Gilles Peskine449bd832023-01-11 14:50:10 +01008602 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8603 step, input->x, input->len),
8604 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008605
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008609
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8611 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008612
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 TEST_EQUAL(status, expected_output_status);
8614 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8615 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8616 expected_output->len);
8617 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008618
8619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 mbedtls_free(output_buffer);
8621 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008622 PSA_DONE();
8623}
8624/* END_CASE */
8625
Janos Follathe60c9052019-07-03 13:51:30 +01008626/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008627void derive_key_exercise(int alg_arg,
8628 data_t *key_data,
8629 data_t *input1,
8630 data_t *input2,
8631 int derived_type_arg,
8632 int derived_bits_arg,
8633 int derived_usage_arg,
8634 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008635{
Ronald Cron5425a212020-08-04 14:58:35 +02008636 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8637 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008638 psa_algorithm_t alg = alg_arg;
8639 psa_key_type_t derived_type = derived_type_arg;
8640 size_t derived_bits = derived_bits_arg;
8641 psa_key_usage_t derived_usage = derived_usage_arg;
8642 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008644 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008646 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008647
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008649
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8651 psa_set_key_algorithm(&attributes, alg);
8652 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8653 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8654 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008655
8656 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008657 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8658 input1->x, input1->len,
8659 input2->x, input2->len,
8660 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008661 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 }
Janos Follathe60c9052019-07-03 13:51:30 +01008663
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 psa_set_key_usage_flags(&attributes, derived_usage);
8665 psa_set_key_algorithm(&attributes, derived_alg);
8666 psa_set_key_type(&attributes, derived_type);
8667 psa_set_key_bits(&attributes, derived_bits);
8668 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8669 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008670
8671 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8673 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8674 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008675
8676 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008678 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008680
8681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008682 /*
8683 * Key attributes may have been returned by psa_get_key_attributes()
8684 * thus reset them as required.
8685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008687
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 psa_key_derivation_abort(&operation);
8689 psa_destroy_key(base_key);
8690 psa_destroy_key(derived_key);
8691 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008692}
8693/* END_CASE */
8694
Janos Follath42fd8882019-07-03 14:17:09 +01008695/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008696void derive_key_export(int alg_arg,
8697 data_t *key_data,
8698 data_t *input1,
8699 data_t *input2,
8700 int bytes1_arg,
8701 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008702{
Ronald Cron5425a212020-08-04 14:58:35 +02008703 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8704 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008705 psa_algorithm_t alg = alg_arg;
8706 size_t bytes1 = bytes1_arg;
8707 size_t bytes2 = bytes2_arg;
8708 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008709 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008710 uint8_t *output_buffer = NULL;
8711 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008712 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8713 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008714 size_t length;
8715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 ASSERT_ALLOC(output_buffer, capacity);
8717 ASSERT_ALLOC(export_buffer, capacity);
8718 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008719
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8721 psa_set_key_algorithm(&base_attributes, alg);
8722 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8723 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8724 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008725
8726 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8728 input1->x, input1->len,
8729 input2->x, input2->len,
8730 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008731 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 }
Janos Follath42fd8882019-07-03 14:17:09 +01008733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8735 output_buffer,
8736 capacity));
8737 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008738
8739 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008740 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8741 input1->x, input1->len,
8742 input2->x, input2->len,
8743 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008744 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 }
Janos Follath42fd8882019-07-03 14:17:09 +01008746
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8748 psa_set_key_algorithm(&derived_attributes, 0);
8749 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
8750 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
8751 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8752 &derived_key));
8753 PSA_ASSERT(psa_export_key(derived_key,
8754 export_buffer, bytes1,
8755 &length));
8756 TEST_EQUAL(length, bytes1);
8757 PSA_ASSERT(psa_destroy_key(derived_key));
8758 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
8759 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8760 &derived_key));
8761 PSA_ASSERT(psa_export_key(derived_key,
8762 export_buffer + bytes1, bytes2,
8763 &length));
8764 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008765
8766 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
8768 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008769
8770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 mbedtls_free(output_buffer);
8772 mbedtls_free(export_buffer);
8773 psa_key_derivation_abort(&operation);
8774 psa_destroy_key(base_key);
8775 psa_destroy_key(derived_key);
8776 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008777}
8778/* END_CASE */
8779
8780/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008781void derive_key_type(int alg_arg,
8782 data_t *key_data,
8783 data_t *input1,
8784 data_t *input2,
8785 int key_type_arg, int bits_arg,
8786 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008787{
8788 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8789 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8790 const psa_algorithm_t alg = alg_arg;
8791 const psa_key_type_t key_type = key_type_arg;
8792 const size_t bits = bits_arg;
8793 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8794 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008796 uint8_t *export_buffer = NULL;
8797 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8798 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8799 size_t export_length;
8800
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 ASSERT_ALLOC(export_buffer, export_buffer_size);
8802 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008803
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8805 psa_set_key_algorithm(&base_attributes, alg);
8806 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8807 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8808 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008809
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008811 &operation, base_key, alg,
8812 input1->x, input1->len,
8813 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008815 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008817
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8819 psa_set_key_algorithm(&derived_attributes, 0);
8820 psa_set_key_type(&derived_attributes, key_type);
8821 psa_set_key_bits(&derived_attributes, bits);
8822 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8823 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008824
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 PSA_ASSERT(psa_export_key(derived_key,
8826 export_buffer, export_buffer_size,
8827 &export_length));
8828 ASSERT_COMPARE(export_buffer, export_length,
8829 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008830
8831exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 mbedtls_free(export_buffer);
8833 psa_key_derivation_abort(&operation);
8834 psa_destroy_key(base_key);
8835 psa_destroy_key(derived_key);
8836 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008837}
8838/* END_CASE */
8839
8840/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008841void derive_key(int alg_arg,
8842 data_t *key_data, data_t *input1, data_t *input2,
8843 int type_arg, int bits_arg,
8844 int expected_status_arg,
8845 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02008846{
Ronald Cron5425a212020-08-04 14:58:35 +02008847 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8848 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008849 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008850 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008851 size_t bits = bits_arg;
8852 psa_status_t expected_status = expected_status_arg;
8853 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8854 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8855 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02008858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8860 psa_set_key_algorithm(&base_attributes, alg);
8861 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8862 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8863 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02008864
Gilles Peskine449bd832023-01-11 14:50:10 +01008865 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8866 input1->x, input1->len,
8867 input2->x, input2->len,
8868 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02008869 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 }
Gilles Peskinec744d992019-07-30 17:26:54 +02008871
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8873 psa_set_key_algorithm(&derived_attributes, 0);
8874 psa_set_key_type(&derived_attributes, type);
8875 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008876
8877 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 psa_key_derivation_output_key(&derived_attributes,
8879 &operation,
8880 &derived_key);
8881 if (is_large_output > 0) {
8882 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8883 }
8884 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02008885
8886exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 psa_key_derivation_abort(&operation);
8888 psa_destroy_key(base_key);
8889 psa_destroy_key(derived_key);
8890 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02008891}
8892/* END_CASE */
8893
8894/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008895void key_agreement_setup(int alg_arg,
8896 int our_key_type_arg, int our_key_alg_arg,
8897 data_t *our_key_data, data_t *peer_key_data,
8898 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02008899{
Ronald Cron5425a212020-08-04 14:58:35 +02008900 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008901 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008902 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008903 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008904 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008906 psa_status_t expected_status = expected_status_arg;
8907 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008908
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02008910
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8912 psa_set_key_algorithm(&attributes, our_key_alg);
8913 psa_set_key_type(&attributes, our_key_type);
8914 PSA_ASSERT(psa_import_key(&attributes,
8915 our_key_data->x, our_key_data->len,
8916 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008917
Gilles Peskine77f40d82019-04-11 21:27:06 +02008918 /* The tests currently include inputs that should fail at either step.
8919 * Test cases that fail at the setup step should be changed to call
8920 * key_derivation_setup instead, and this function should be renamed
8921 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 status = psa_key_derivation_setup(&operation, alg);
8923 if (status == PSA_SUCCESS) {
8924 TEST_EQUAL(psa_key_derivation_key_agreement(
8925 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8926 our_key,
8927 peer_key_data->x, peer_key_data->len),
8928 expected_status);
8929 } else {
8930 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008931 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008932
8933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 psa_key_derivation_abort(&operation);
8935 psa_destroy_key(our_key);
8936 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008937}
8938/* END_CASE */
8939
8940/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008941void raw_key_agreement(int alg_arg,
8942 int our_key_type_arg, data_t *our_key_data,
8943 data_t *peer_key_data,
8944 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008945{
Ronald Cron5425a212020-08-04 14:58:35 +02008946 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008947 psa_algorithm_t alg = alg_arg;
8948 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008950 unsigned char *output = NULL;
8951 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008952 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02008955
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8957 psa_set_key_algorithm(&attributes, alg);
8958 psa_set_key_type(&attributes, our_key_type);
8959 PSA_ASSERT(psa_import_key(&attributes,
8960 our_key_data->x, our_key_data->len,
8961 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
8964 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008965
Gilles Peskine992bee82022-04-13 23:25:52 +02008966 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01008967 TEST_LE_U(expected_output->len,
8968 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
8969 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
8970 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02008971
8972 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 ASSERT_ALLOC(output, expected_output->len);
8974 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8975 peer_key_data->x, peer_key_data->len,
8976 output, expected_output->len,
8977 &output_length));
8978 ASSERT_COMPARE(output, output_length,
8979 expected_output->x, expected_output->len);
8980 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008981 output = NULL;
8982 output_length = ~0;
8983
8984 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 ASSERT_ALLOC(output, expected_output->len + 1);
8986 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8987 peer_key_data->x, peer_key_data->len,
8988 output, expected_output->len + 1,
8989 &output_length));
8990 ASSERT_COMPARE(output, output_length,
8991 expected_output->x, expected_output->len);
8992 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008993 output = NULL;
8994 output_length = ~0;
8995
8996 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 ASSERT_ALLOC(output, expected_output->len - 1);
8998 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
8999 peer_key_data->x, peer_key_data->len,
9000 output, expected_output->len - 1,
9001 &output_length),
9002 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009003 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 TEST_LE_U(output_length, expected_output->len - 1);
9005 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009006 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009007
9008exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 mbedtls_free(output);
9010 psa_destroy_key(our_key);
9011 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009012}
9013/* END_CASE */
9014
9015/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009016void key_agreement_capacity(int alg_arg,
9017 int our_key_type_arg, data_t *our_key_data,
9018 data_t *peer_key_data,
9019 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009020{
Ronald Cron5425a212020-08-04 14:58:35 +02009021 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009022 psa_algorithm_t alg = alg_arg;
9023 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009024 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009026 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009027 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009028
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9032 psa_set_key_algorithm(&attributes, alg);
9033 psa_set_key_type(&attributes, our_key_type);
9034 PSA_ASSERT(psa_import_key(&attributes,
9035 our_key_data->x, our_key_data->len,
9036 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009037
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9039 PSA_ASSERT(psa_key_derivation_key_agreement(
9040 &operation,
9041 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9042 peer_key_data->x, peer_key_data->len));
9043 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009044 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009045 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9046 PSA_KEY_DERIVATION_INPUT_INFO,
9047 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009048 }
Gilles Peskine59685592018-09-18 12:11:34 +02009049
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009050 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 PSA_ASSERT(psa_key_derivation_get_capacity(
9052 &operation, &actual_capacity));
9053 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009054
Gilles Peskinebf491972018-10-25 22:36:12 +02009055 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 while (actual_capacity > sizeof(output)) {
9057 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9058 output, sizeof(output)));
9059 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009060 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9062 output, actual_capacity));
9063 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9064 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009065
Gilles Peskine59685592018-09-18 12:11:34 +02009066exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 psa_key_derivation_abort(&operation);
9068 psa_destroy_key(our_key);
9069 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009070}
9071/* END_CASE */
9072
9073/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009074void key_agreement_output(int alg_arg,
9075 int our_key_type_arg, data_t *our_key_data,
9076 data_t *peer_key_data,
9077 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009078{
Ronald Cron5425a212020-08-04 14:58:35 +02009079 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009080 psa_algorithm_t alg = alg_arg;
9081 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009082 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009084 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009085
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9087 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009088
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009090
Gilles Peskine449bd832023-01-11 14:50:10 +01009091 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9092 psa_set_key_algorithm(&attributes, alg);
9093 psa_set_key_type(&attributes, our_key_type);
9094 PSA_ASSERT(psa_import_key(&attributes,
9095 our_key_data->x, our_key_data->len,
9096 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009097
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9099 PSA_ASSERT(psa_key_derivation_key_agreement(
9100 &operation,
9101 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9102 peer_key_data->x, peer_key_data->len));
9103 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009104 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009105 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9106 PSA_KEY_DERIVATION_INPUT_INFO,
9107 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009108 }
Gilles Peskine59685592018-09-18 12:11:34 +02009109
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9111 actual_output,
9112 expected_output1->len));
9113 ASSERT_COMPARE(actual_output, expected_output1->len,
9114 expected_output1->x, expected_output1->len);
9115 if (expected_output2->len != 0) {
9116 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9117 actual_output,
9118 expected_output2->len));
9119 ASSERT_COMPARE(actual_output, expected_output2->len,
9120 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009121 }
Gilles Peskine59685592018-09-18 12:11:34 +02009122
9123exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009124 psa_key_derivation_abort(&operation);
9125 psa_destroy_key(our_key);
9126 PSA_DONE();
9127 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009128}
9129/* END_CASE */
9130
9131/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009132void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009133{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009134 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009135 unsigned char *output = NULL;
9136 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009137 size_t i;
9138 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009139
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009141
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 ASSERT_ALLOC(output, bytes);
9143 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009144
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009146
Gilles Peskinea50d7392018-06-21 10:22:13 +02009147 /* Run several times, to ensure that every output byte will be
9148 * nonzero at least once with overwhelming probability
9149 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 for (run = 0; run < 10; run++) {
9151 if (bytes != 0) {
9152 memset(output, 0, bytes);
9153 }
9154 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 for (i = 0; i < bytes; i++) {
9157 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009158 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009160 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009161 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009162
9163 /* Check that every byte was changed to nonzero at least once. This
9164 * validates that psa_generate_random is overwriting every byte of
9165 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 for (i = 0; i < bytes; i++) {
9167 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009168 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009169
9170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009171 PSA_DONE();
9172 mbedtls_free(output);
9173 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009174}
9175/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009176
9177/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009178void generate_key(int type_arg,
9179 int bits_arg,
9180 int usage_arg,
9181 int alg_arg,
9182 int expected_status_arg,
9183 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009184{
Ronald Cron5425a212020-08-04 14:58:35 +02009185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009186 psa_key_type_t type = type_arg;
9187 psa_key_usage_t usage = usage_arg;
9188 size_t bits = bits_arg;
9189 psa_algorithm_t alg = alg_arg;
9190 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009192 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009193
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009195
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 psa_set_key_usage_flags(&attributes, usage);
9197 psa_set_key_algorithm(&attributes, alg);
9198 psa_set_key_type(&attributes, type);
9199 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009200
9201 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009203
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 if (is_large_key > 0) {
9205 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9206 }
9207 TEST_EQUAL(status, expected_status);
9208 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009211
9212 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9214 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9215 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009216
Gilles Peskine818ca122018-06-20 18:16:48 +02009217 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009221
9222exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009223 /*
9224 * Key attributes may have been returned by psa_get_key_attributes()
9225 * thus reset them as required.
9226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 psa_destroy_key(key);
9230 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009231}
9232/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009233
Ronald Cronee414c72021-03-18 18:50:08 +01009234/* 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 +01009235void generate_key_rsa(int bits_arg,
9236 data_t *e_arg,
9237 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009238{
Ronald Cron5425a212020-08-04 14:58:35 +02009239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009240 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009241 size_t bits = bits_arg;
9242 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9243 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9244 psa_status_t expected_status = expected_status_arg;
9245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9246 uint8_t *exported = NULL;
9247 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009249 size_t exported_length = SIZE_MAX;
9250 uint8_t *e_read_buffer = NULL;
9251 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009252 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009253 size_t e_read_length = SIZE_MAX;
9254
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 if (e_arg->len == 0 ||
9256 (e_arg->len == 3 &&
9257 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009258 is_default_public_exponent = 1;
9259 e_read_size = 0;
9260 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 ASSERT_ALLOC(e_read_buffer, e_read_size);
9262 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009263
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009265
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 psa_set_key_usage_flags(&attributes, usage);
9267 psa_set_key_algorithm(&attributes, alg);
9268 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9269 e_arg->x, e_arg->len));
9270 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009271
9272 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9274 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009276 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009277
9278 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9280 TEST_EQUAL(psa_get_key_type(&attributes), type);
9281 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9282 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9283 e_read_buffer, e_read_size,
9284 &e_read_length));
9285 if (is_default_public_exponent) {
9286 TEST_EQUAL(e_read_length, 0);
9287 } else {
9288 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9289 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009290
9291 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009292 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009295
9296 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 PSA_ASSERT(psa_export_public_key(key,
9298 exported, exported_size,
9299 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009300 {
9301 uint8_t *p = exported;
9302 uint8_t *end = exported + exported_length;
9303 size_t len;
9304 /* RSAPublicKey ::= SEQUENCE {
9305 * modulus INTEGER, -- n
9306 * publicExponent INTEGER } -- e
9307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9309 MBEDTLS_ASN1_SEQUENCE |
9310 MBEDTLS_ASN1_CONSTRUCTED));
9311 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9312 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9313 MBEDTLS_ASN1_INTEGER));
9314 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009315 ++p;
9316 --len;
9317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 if (e_arg->len == 0) {
9319 TEST_EQUAL(len, 3);
9320 TEST_EQUAL(p[0], 1);
9321 TEST_EQUAL(p[1], 0);
9322 TEST_EQUAL(p[2], 1);
9323 } else {
9324 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009325 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009326 }
9327
9328exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009329 /*
9330 * Key attributes may have been returned by psa_get_key_attributes() or
9331 * set by psa_set_key_domain_parameters() thus reset them as required.
9332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009333 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009334
Gilles Peskine449bd832023-01-11 14:50:10 +01009335 psa_destroy_key(key);
9336 PSA_DONE();
9337 mbedtls_free(e_read_buffer);
9338 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009339}
9340/* END_CASE */
9341
Darryl Greend49a4992018-06-18 17:27:26 +01009342/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009343void persistent_key_load_key_from_storage(data_t *data,
9344 int type_arg, int bits_arg,
9345 int usage_flags_arg, int alg_arg,
9346 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009347{
Gilles Peskine449bd832023-01-11 14:50:10 +01009348 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9351 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009352 psa_key_type_t type = type_arg;
9353 size_t bits = bits_arg;
9354 psa_key_usage_t usage_flags = usage_flags_arg;
9355 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009356 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009357 unsigned char *first_export = NULL;
9358 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009360 size_t first_exported_length;
9361 size_t second_exported_length;
9362
Gilles Peskine449bd832023-01-11 14:50:10 +01009363 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9364 ASSERT_ALLOC(first_export, export_size);
9365 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009366 }
Darryl Greend49a4992018-06-18 17:27:26 +01009367
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009369
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 psa_set_key_id(&attributes, key_id);
9371 psa_set_key_usage_flags(&attributes, usage_flags);
9372 psa_set_key_algorithm(&attributes, alg);
9373 psa_set_key_type(&attributes, type);
9374 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009375
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009377 case IMPORT_KEY:
9378 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009379 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9380 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009381 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009382
Darryl Green0c6575a2018-11-07 16:05:30 +00009383 case GENERATE_KEY:
9384 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009386 break;
9387
9388 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009389#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 {
9391 /* Create base key */
9392 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9393 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9394 psa_set_key_usage_flags(&base_attributes,
9395 PSA_KEY_USAGE_DERIVE);
9396 psa_set_key_algorithm(&base_attributes, derive_alg);
9397 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9398 PSA_ASSERT(psa_import_key(&base_attributes,
9399 data->x, data->len,
9400 &base_key));
9401 /* Derive a key. */
9402 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9403 PSA_ASSERT(psa_key_derivation_input_key(
9404 &operation,
9405 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9406 PSA_ASSERT(psa_key_derivation_input_bytes(
9407 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9408 NULL, 0));
9409 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9410 &operation,
9411 &key));
9412 PSA_ASSERT(psa_key_derivation_abort(&operation));
9413 PSA_ASSERT(psa_destroy_key(base_key));
9414 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9415 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009416#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009417 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009418#endif
9419 break;
9420
9421 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009423 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009424 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009426
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009427 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9429 PSA_ASSERT(psa_export_key(key,
9430 first_export, export_size,
9431 &first_exported_length));
9432 if (generation_method == IMPORT_KEY) {
9433 ASSERT_COMPARE(data->x, data->len,
9434 first_export, first_exported_length);
9435 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009436 }
Darryl Greend49a4992018-06-18 17:27:26 +01009437
9438 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009440 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009441 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009442
Darryl Greend49a4992018-06-18 17:27:26 +01009443 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009444 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9445 TEST_ASSERT(mbedtls_svc_key_id_equal(
9446 psa_get_key_id(&attributes), key_id));
9447 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9448 PSA_KEY_LIFETIME_PERSISTENT);
9449 TEST_EQUAL(psa_get_key_type(&attributes), type);
9450 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9451 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9452 mbedtls_test_update_key_usage_flags(usage_flags));
9453 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009454
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009455 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9457 PSA_ASSERT(psa_export_key(key,
9458 second_export, export_size,
9459 &second_exported_length));
9460 ASSERT_COMPARE(first_export, first_exported_length,
9461 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009462 }
9463
9464 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009465 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009466 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 }
Darryl Greend49a4992018-06-18 17:27:26 +01009468
9469exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009470 /*
9471 * Key attributes may have been returned by psa_get_key_attributes()
9472 * thus reset them as required.
9473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009475
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 mbedtls_free(first_export);
9477 mbedtls_free(second_export);
9478 psa_key_derivation_abort(&operation);
9479 psa_destroy_key(base_key);
9480 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009481 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009482}
9483/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009484
Neil Armstronga557cb82022-06-10 08:58:32 +02009485/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009486void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9487 int primitive_arg, int hash_arg, int role_arg,
9488 int test_input, data_t *pw_data,
9489 int inj_err_type_arg,
9490 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009491{
9492 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9493 psa_pake_operation_t operation = psa_pake_operation_init();
9494 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009495 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009496 psa_key_type_t key_type_pw = key_type_pw_arg;
9497 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009498 psa_algorithm_t hash_alg = hash_arg;
9499 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009502 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9503 psa_status_t expected_error = expected_error_arg;
9504 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009505 unsigned char *output_buffer = NULL;
9506 size_t output_len = 0;
9507
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009509
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009510 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 PSA_PAKE_STEP_KEY_SHARE);
9512 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009513
Gilles Peskine449bd832023-01-11 14:50:10 +01009514 if (pw_data->len > 0) {
9515 psa_set_key_usage_flags(&attributes, key_usage_pw);
9516 psa_set_key_algorithm(&attributes, alg);
9517 psa_set_key_type(&attributes, key_type_pw);
9518 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9519 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009520 }
9521
Gilles Peskine449bd832023-01-11 14:50:10 +01009522 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9523 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9524 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009527
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9529 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9530 expected_error);
9531 PSA_ASSERT(psa_pake_abort(&operation));
9532 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9533 expected_error);
9534 PSA_ASSERT(psa_pake_abort(&operation));
9535 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9536 expected_error);
9537 PSA_ASSERT(psa_pake_abort(&operation));
9538 TEST_EQUAL(psa_pake_set_role(&operation, role),
9539 expected_error);
9540 PSA_ASSERT(psa_pake_abort(&operation));
9541 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9542 NULL, 0, NULL),
9543 expected_error);
9544 PSA_ASSERT(psa_pake_abort(&operation));
9545 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9546 expected_error);
9547 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009548 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009549 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009550
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 status = psa_pake_setup(&operation, &cipher_suite);
9552 if (status != PSA_SUCCESS) {
9553 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009554 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009555 }
9556
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9558 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9559 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009560 goto exit;
9561 }
9562
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 status = psa_pake_set_role(&operation, role);
9564 if (status != PSA_SUCCESS) {
9565 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009566 goto exit;
9567 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009568
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 if (pw_data->len > 0) {
9570 status = psa_pake_set_password_key(&operation, key);
9571 if (status != PSA_SUCCESS) {
9572 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009573 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009574 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009575 }
9576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9578 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9579 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009580 goto exit;
9581 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009582
Gilles Peskine449bd832023-01-11 14:50:10 +01009583 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9584 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9585 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009586 goto exit;
9587 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009588
Gilles Peskine449bd832023-01-11 14:50:10 +01009589 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009590 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9592 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009593 goto exit;
9594 }
9595
Gilles Peskine449bd832023-01-11 14:50:10 +01009596 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009597 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9599 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009600 goto exit;
9601 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9604 PSA_PAKE_STEP_KEY_SHARE);
9605 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9606 PSA_PAKE_STEP_ZK_PUBLIC);
9607 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9608 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009609
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 if (test_input) {
9611 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9612 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9613 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009614 goto exit;
9615 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009616
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9618 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9619 output_buffer, size_zk_proof),
9620 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009621 goto exit;
9622 }
9623
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9625 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9626 output_buffer, size_zk_proof),
9627 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009628 goto exit;
9629 }
9630
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9632 output_buffer, size_key_share);
9633 if (status != PSA_SUCCESS) {
9634 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009635 goto exit;
9636 }
9637
Gilles Peskine449bd832023-01-11 14:50:10 +01009638 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9639 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9640 output_buffer, size_zk_public + 1),
9641 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009642 goto exit;
9643 }
9644
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009646 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009647 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9648 output_buffer, size_zk_public + 1);
9649 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9650 output_buffer, size_zk_public),
9651 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009652 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009653 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009654 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9656 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9657 NULL, 0, NULL),
9658 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009659 goto exit;
9660 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009661
Gilles Peskine449bd832023-01-11 14:50:10 +01009662 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9663 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9664 output_buffer, buf_size, &output_len),
9665 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009666 goto exit;
9667 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009668
Gilles Peskine449bd832023-01-11 14:50:10 +01009669 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9670 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9671 output_buffer, buf_size, &output_len),
9672 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009673 goto exit;
9674 }
9675
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9677 output_buffer, buf_size, &output_len);
9678 if (status != PSA_SUCCESS) {
9679 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009680 goto exit;
9681 }
9682
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9686 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9687 output_buffer, size_zk_public - 1, &output_len),
9688 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01009689 goto exit;
9690 }
9691
Gilles Peskine449bd832023-01-11 14:50:10 +01009692 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009693 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9695 output_buffer, size_zk_public - 1, &output_len);
9696 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9697 output_buffer, buf_size, &output_len),
9698 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009699 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009700 }
9701 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009702
9703exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009704 PSA_ASSERT(psa_destroy_key(key));
9705 PSA_ASSERT(psa_pake_abort(&operation));
9706 mbedtls_free(output_buffer);
9707 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009708}
9709/* END_CASE */
9710
Neil Armstronga557cb82022-06-10 08:58:32 +02009711/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009712void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
9713 int client_input_first, int inject_error,
9714 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009715{
9716 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9717 psa_pake_operation_t server = psa_pake_operation_init();
9718 psa_pake_operation_t client = psa_pake_operation_init();
9719 psa_algorithm_t alg = alg_arg;
9720 psa_algorithm_t hash_alg = hash_arg;
9721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9723
Gilles Peskine449bd832023-01-11 14:50:10 +01009724 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009725
Gilles Peskine449bd832023-01-11 14:50:10 +01009726 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9727 psa_set_key_algorithm(&attributes, alg);
9728 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9729 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9730 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009731
Gilles Peskine449bd832023-01-11 14:50:10 +01009732 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9733 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9734 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009735
9736
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9738 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009739
Gilles Peskine449bd832023-01-11 14:50:10 +01009740 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9741 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009742
Gilles Peskine449bd832023-01-11 14:50:10 +01009743 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9744 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009745
Gilles Peskine449bd832023-01-11 14:50:10 +01009746 ecjpake_do_round(alg, primitive_arg, &server, &client,
9747 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009748
Gilles Peskine449bd832023-01-11 14:50:10 +01009749 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009751 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009752
Gilles Peskine449bd832023-01-11 14:50:10 +01009753 ecjpake_do_round(alg, primitive_arg, &server, &client,
9754 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009755
9756exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009757 psa_destroy_key(key);
9758 psa_pake_abort(&server);
9759 psa_pake_abort(&client);
9760 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009761}
9762/* END_CASE */
9763
9764/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009765void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
9766 int derive_alg_arg, data_t *pw_data,
9767 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009768{
9769 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9770 psa_pake_operation_t server = psa_pake_operation_init();
9771 psa_pake_operation_t client = psa_pake_operation_init();
9772 psa_algorithm_t alg = alg_arg;
9773 psa_algorithm_t hash_alg = hash_arg;
9774 psa_algorithm_t derive_alg = derive_alg_arg;
9775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9777 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009779 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009781 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009782
Gilles Peskine449bd832023-01-11 14:50:10 +01009783 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009784
Gilles Peskine449bd832023-01-11 14:50:10 +01009785 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9786 psa_set_key_algorithm(&attributes, alg);
9787 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9788 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9789 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009790
Gilles Peskine449bd832023-01-11 14:50:10 +01009791 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9792 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9793 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009794
Neil Armstrong1e855602022-06-15 11:32:11 +02009795 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009796 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
9797 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02009798
Gilles Peskine449bd832023-01-11 14:50:10 +01009799 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
9800 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
9801 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
9802 PSA_KEY_DERIVATION_INPUT_SEED,
9803 (const uint8_t *) "", 0));
9804 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
9805 PSA_KEY_DERIVATION_INPUT_SEED,
9806 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02009807 }
9808
Gilles Peskine449bd832023-01-11 14:50:10 +01009809 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9810 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009811
Gilles Peskine449bd832023-01-11 14:50:10 +01009812 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9813 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009814
Gilles Peskine449bd832023-01-11 14:50:10 +01009815 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9816 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009817
Gilles Peskine449bd832023-01-11 14:50:10 +01009818 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
9819 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9820 PSA_ERROR_BAD_STATE);
9821 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9822 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009823 goto exit;
9824 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009825
Neil Armstrongf983caf2022-06-15 15:27:48 +02009826 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009827 ecjpake_do_round(alg, primitive_arg, &server, &client,
9828 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009829
Gilles Peskine449bd832023-01-11 14:50:10 +01009830 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
9831 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9832 PSA_ERROR_BAD_STATE);
9833 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9834 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009835 goto exit;
9836 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009837
Neil Armstrongf983caf2022-06-15 15:27:48 +02009838 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009839 ecjpake_do_round(alg, primitive_arg, &server, &client,
9840 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009841
Gilles Peskine449bd832023-01-11 14:50:10 +01009842 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
9843 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009844
9845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009846 psa_key_derivation_abort(&server_derive);
9847 psa_key_derivation_abort(&client_derive);
9848 psa_destroy_key(key);
9849 psa_pake_abort(&server);
9850 psa_pake_abort(&client);
9851 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009852}
9853/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009854
9855/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009856void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009857{
9858 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9859 const size_t bits = 256;
9860 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01009861 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009862 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01009863 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009864
9865 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9866 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009867 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9868 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
9869 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9870 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009871 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01009872 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9873 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009874
9875 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01009876 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9877 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
9878 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9879 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
9880 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9881 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009882
9883 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9885 PSA_PAKE_OUTPUT_MAX_SIZE);
9886 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9887 PSA_PAKE_OUTPUT_MAX_SIZE);
9888 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9889 PSA_PAKE_OUTPUT_MAX_SIZE);
9890 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9891 PSA_PAKE_INPUT_MAX_SIZE);
9892 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9893 PSA_PAKE_INPUT_MAX_SIZE);
9894 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9895 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009896}
9897/* END_CASE */