blob: a1b3c902b69b755a0a4a329dfd2c66a683ee4432 [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 Elliott6f600372023-02-06 18:41:05 +00001223static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1224 psa_status_t expected_status,
1225 size_t *min_completes,
1226 size_t *max_completes)
1227{
1228
1229 /* This is slightly contrived, but we only really know that with a minimum
1230 value of max_ops that a successful operation should take more than one op
1231 to complete, and likewise that with a max_ops of
1232 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1233 if (max_ops == 0 || max_ops == 1) {
1234 /* Failure test cases will fail on the first op. */
1235 if (expected_status == PSA_SUCCESS) {
1236 *min_completes = 2;
1237 } else {
1238 *min_completes = 1;
1239 }
1240
1241 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1242 } else {
1243 *min_completes = 1;
1244 *max_completes = 1;
1245 }
1246}
1247
Gilles Peskinee59236f2018-01-27 23:32:46 +01001248/* END_HEADER */
1249
1250/* BEGIN_DEPENDENCIES
1251 * depends_on:MBEDTLS_PSA_CRYPTO_C
1252 * END_DEPENDENCIES
1253 */
1254
1255/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001257{
1258 size_t max_truncated_mac_size =
1259 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1260
1261 /* Check that the length for a truncated MAC always fits in the algorithm
1262 * encoding. The shifted mask is the maximum truncated value. The
1263 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001265}
1266/* END_CASE */
1267
1268/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269void import_with_policy(int type_arg,
1270 int usage_arg, int alg_arg,
1271 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001272{
1273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1274 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001276 psa_key_type_t type = type_arg;
1277 psa_key_usage_t usage = usage_arg;
1278 psa_algorithm_t alg = alg_arg;
1279 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001281 psa_status_t status;
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 psa_set_key_type(&attributes, type);
1286 psa_set_key_usage_flags(&attributes, usage);
1287 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 status = psa_import_key(&attributes,
1290 key_material, sizeof(key_material),
1291 &key);
1292 TEST_EQUAL(status, expected_status);
1293 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001294 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1298 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1299 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1300 mbedtls_test_update_key_usage_flags(usage));
1301 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1302 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 PSA_ASSERT(psa_destroy_key(key));
1305 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001306
1307exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001308 /*
1309 * Key attributes may have been returned by psa_get_key_attributes()
1310 * thus reset them as required.
1311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 psa_destroy_key(key);
1315 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001316}
1317/* END_CASE */
1318
1319/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320void import_with_data(data_t *data, int type_arg,
1321 int attr_bits_arg,
1322 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001323{
1324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1325 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001326 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001327 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001328 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001329 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 psa_set_key_type(&attributes, type);
1335 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 status = psa_import_key(&attributes, data->x, data->len, &key);
1338 TEST_EQUAL(status, expected_status);
1339 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001340 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1344 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1345 if (attr_bits != 0) {
1346 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1347 }
1348 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 PSA_ASSERT(psa_destroy_key(key));
1351 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352
1353exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001354 /*
1355 * Key attributes may have been returned by psa_get_key_attributes()
1356 * thus reset them as required.
1357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 psa_destroy_key(key);
1361 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362}
1363/* END_CASE */
1364
1365/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001366/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367void import_large_key(int type_arg, int byte_size_arg,
1368 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001369{
1370 psa_key_type_t type = type_arg;
1371 size_t byte_size = byte_size_arg;
1372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1373 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001374 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001375 psa_status_t status;
1376 uint8_t *buffer = NULL;
1377 size_t buffer_size = byte_size + 1;
1378 size_t n;
1379
Steven Cooreman69967ce2021-01-18 18:01:08 +01001380 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001381 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1383 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001386
1387 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1389 psa_set_key_type(&attributes, type);
1390 status = psa_import_key(&attributes, buffer, byte_size, &key);
1391 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1392 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (status == PSA_SUCCESS) {
1395 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1396 TEST_EQUAL(psa_get_key_type(&attributes), type);
1397 TEST_EQUAL(psa_get_key_bits(&attributes),
1398 PSA_BYTES_TO_BITS(byte_size));
1399 ASSERT_NO_SLOT_NUMBER(&attributes);
1400 memset(buffer, 0, byte_size + 1);
1401 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1402 for (n = 0; n < byte_size; n++) {
1403 TEST_EQUAL(buffer[n], 'K');
1404 }
1405 for (n = byte_size; n < buffer_size; n++) {
1406 TEST_EQUAL(buffer[n], 0);
1407 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001408 }
1409
1410exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001411 /*
1412 * Key attributes may have been returned by psa_get_key_attributes()
1413 * thus reset them as required.
1414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 psa_destroy_key(key);
1418 PSA_DONE();
1419 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001420}
1421/* END_CASE */
1422
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001423/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001424/* Import an RSA key with a valid structure (but not valid numbers
1425 * inside, beyond having sensible size and parity). This is expected to
1426 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001428{
Ronald Cron5425a212020-08-04 14:58:35 +02001429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001430 size_t bits = bits_arg;
1431 psa_status_t expected_status = expected_status_arg;
1432 psa_status_t status;
1433 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001434 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001435 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001437 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001438 unsigned char *p;
1439 int ret;
1440 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 PSA_ASSERT(psa_crypto_init());
1444 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1447 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001448 length = ret;
1449
1450 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 psa_set_key_type(&attributes, type);
1452 status = psa_import_key(&attributes, p, length, &key);
1453 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (status == PSA_SUCCESS) {
1456 PSA_ASSERT(psa_destroy_key(key));
1457 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001458
1459exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_free(buffer);
1461 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001462}
1463/* END_CASE */
1464
1465/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466void import_export(data_t *data,
1467 int type_arg,
1468 int usage_arg, int alg_arg,
1469 int lifetime_arg,
1470 int expected_bits,
1471 int export_size_delta,
1472 int expected_export_status_arg,
1473 /*whether reexport must give the original input exactly*/
1474 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475{
Ronald Cron5425a212020-08-04 14:58:35 +02001476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001478 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001479 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301481 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482 unsigned char *exported = NULL;
1483 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001485 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001488 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489
Moran Pekercb088e72018-07-17 17:36:59 +03001490 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 ASSERT_ALLOC(exported, export_size);
1492 if (!canonical_input) {
1493 ASSERT_ALLOC(reexported, export_size);
1494 }
1495 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 psa_set_key_lifetime(&attributes, lifetime);
1498 psa_set_key_usage_flags(&attributes, usage_arg);
1499 psa_set_key_algorithm(&attributes, alg);
1500 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001501
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504
1505 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1507 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1508 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1509 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510
1511 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 status = psa_export_key(key, exported, export_size, &exported_length);
1513 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001514
1515 /* The exported length must be set by psa_export_key() to a value between 0
1516 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1518 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1519 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1522 export_size - exported_length));
1523 if (status != PSA_SUCCESS) {
1524 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001526 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527
Gilles Peskineea38a922021-02-13 00:05:16 +01001528 /* Run sanity checks on the exported key. For non-canonical inputs,
1529 * this validates the canonical representations. For canonical inputs,
1530 * this doesn't directly validate the implementation, but it still helps
1531 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if (!psa_key_lifetime_is_external(lifetime)) {
1533 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301534 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 }
Archana4d7ae1d2021-07-07 02:50:22 +05301536 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (canonical_input) {
1539 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1540 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001541 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1543 &key2));
1544 PSA_ASSERT(psa_export_key(key2,
1545 reexported,
1546 export_size,
1547 &reexported_length));
1548 ASSERT_COMPARE(exported, exported_length,
1549 reexported, reexported_length);
1550 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001551 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 TEST_LE_U(exported_length,
1553 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1554 psa_get_key_bits(&got_attributes)));
1555 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556
1557destroy:
1558 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 PSA_ASSERT(psa_destroy_key(key));
1560 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561
1562exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001563 /*
1564 * Key attributes may have been returned by psa_get_key_attributes()
1565 * thus reset them as required.
1566 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 psa_reset_key_attributes(&got_attributes);
1568 psa_destroy_key(key);
1569 mbedtls_free(exported);
1570 mbedtls_free(reexported);
1571 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572}
1573/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001574
Moran Pekerf709f4a2018-06-06 17:26:04 +03001575/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576void import_export_public_key(data_t *data,
1577 int type_arg, // key pair or public key
1578 int alg_arg,
1579 int lifetime_arg,
1580 int export_size_delta,
1581 int expected_export_status_arg,
1582 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001583{
Ronald Cron5425a212020-08-04 14:58:35 +02001584 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001585 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001586 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001587 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001588 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301589 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001590 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001591 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001592 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 psa_set_key_lifetime(&attributes, lifetime);
1598 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1599 psa_set_key_algorithm(&attributes, alg);
1600 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001601
1602 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001604
Gilles Peskine49c25912018-10-29 15:15:31 +01001605 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 ASSERT_ALLOC(exported, export_size);
1607 status = psa_export_public_key(key,
1608 exported, export_size,
1609 &exported_length);
1610 TEST_EQUAL(status, expected_export_status);
1611 if (status == PSA_SUCCESS) {
1612 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001613 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1615 bits = psa_get_key_bits(&attributes);
1616 TEST_LE_U(expected_public_key->len,
1617 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1618 TEST_LE_U(expected_public_key->len,
1619 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1620 TEST_LE_U(expected_public_key->len,
1621 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1622 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1623 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001624 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001625exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001626 /*
1627 * Key attributes may have been returned by psa_get_key_attributes()
1628 * thus reset them as required.
1629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 mbedtls_free(exported);
1633 psa_destroy_key(key);
1634 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001635}
1636/* END_CASE */
1637
Gilles Peskine20035e32018-02-03 22:44:14 +01001638/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639void import_and_exercise_key(data_t *data,
1640 int type_arg,
1641 int bits_arg,
1642 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001643{
Ronald Cron5425a212020-08-04 14:58:35 +02001644 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001645 psa_key_type_t type = type_arg;
1646 size_t bits = bits_arg;
1647 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001650 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 psa_set_key_usage_flags(&attributes, usage);
1655 psa_set_key_algorithm(&attributes, alg);
1656 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001657
1658 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001660
1661 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1663 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1664 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001665
1666 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001668 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 PSA_ASSERT(psa_destroy_key(key));
1672 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001673
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001674exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001675 /*
1676 * Key attributes may have been returned by psa_get_key_attributes()
1677 * thus reset them as required.
1678 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 psa_reset_key_attributes(&attributes);
1682 psa_destroy_key(key);
1683 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001684}
1685/* END_CASE */
1686
1687/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001688void effective_key_attributes(int type_arg, int expected_type_arg,
1689 int bits_arg, int expected_bits_arg,
1690 int usage_arg, int expected_usage_arg,
1691 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001692{
Ronald Cron5425a212020-08-04 14:58:35 +02001693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001694 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001695 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001696 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001697 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001698 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001699 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001700 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001701 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 psa_set_key_usage_flags(&attributes, usage);
1707 psa_set_key_algorithm(&attributes, alg);
1708 psa_set_key_type(&attributes, key_type);
1709 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 PSA_ASSERT(psa_generate_key(&attributes, &key));
1712 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1715 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1716 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1717 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1718 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001719
1720exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001721 /*
1722 * Key attributes may have been returned by psa_get_key_attributes()
1723 * thus reset them as required.
1724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 psa_destroy_key(key);
1728 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733void check_key_policy(int type_arg, int bits_arg,
1734 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001735{
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1737 usage_arg,
1738 mbedtls_test_update_key_usage_flags(usage_arg),
1739 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001740 goto exit;
1741}
1742/* END_CASE */
1743
1744/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001746{
1747 /* Test each valid way of initializing the object, except for `= {0}`, as
1748 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1749 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001750 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001752 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1753 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1758 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1759 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 TEST_EQUAL(psa_get_key_type(&func), 0);
1762 TEST_EQUAL(psa_get_key_type(&init), 0);
1763 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 TEST_EQUAL(psa_get_key_bits(&func), 0);
1766 TEST_EQUAL(psa_get_key_bits(&init), 0);
1767 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1770 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1771 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1774 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1775 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001776}
1777/* END_CASE */
1778
1779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001780void mac_key_policy(int policy_usage_arg,
1781 int policy_alg_arg,
1782 int key_type_arg,
1783 data_t *key_data,
1784 int exercise_alg_arg,
1785 int expected_status_sign_arg,
1786 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001787{
Ronald Cron5425a212020-08-04 14:58:35 +02001788 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001790 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001791 psa_key_type_t key_type = key_type_arg;
1792 psa_algorithm_t policy_alg = policy_alg_arg;
1793 psa_algorithm_t exercise_alg = exercise_alg_arg;
1794 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001795 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001796 psa_status_t expected_status_sign = expected_status_sign_arg;
1797 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001798 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 psa_set_key_usage_flags(&attributes, policy_usage);
1803 psa_set_key_algorithm(&attributes, policy_alg);
1804 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1807 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1810 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1813 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001814
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001815 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001817 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1819 input, 128,
1820 mac, PSA_MAC_MAX_SIZE, &mac_len),
1821 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001822
Neil Armstrong3af9b972022-02-07 12:20:21 +01001823 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 PSA_ASSERT(psa_mac_abort(&operation));
1825 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1826 if (status == PSA_SUCCESS) {
1827 status = psa_mac_update(&operation, input, 128);
1828 if (status == PSA_SUCCESS) {
1829 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1830 &mac_len),
1831 expected_status_sign);
1832 } else {
1833 TEST_EQUAL(status, expected_status_sign);
1834 }
1835 } else {
1836 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001837 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001839
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001840 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 status = psa_mac_verify(key, exercise_alg, input, 128,
1842 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1845 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1846 } else {
1847 TEST_EQUAL(status, expected_status_verify);
1848 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001849
Neil Armstrong3af9b972022-02-07 12:20:21 +01001850 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1852 if (status == PSA_SUCCESS) {
1853 status = psa_mac_update(&operation, input, 128);
1854 if (status == PSA_SUCCESS) {
1855 status = psa_mac_verify_finish(&operation, mac, mac_len);
1856 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1857 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1858 } else {
1859 TEST_EQUAL(status, expected_status_verify);
1860 }
1861 } else {
1862 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001863 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 } else {
1865 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001866 }
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 memset(mac, 0, sizeof(mac));
1871 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1872 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001873
1874exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 psa_mac_abort(&operation);
1876 psa_destroy_key(key);
1877 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001878}
1879/* END_CASE */
1880
1881/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882void cipher_key_policy(int policy_usage_arg,
1883 int policy_alg,
1884 int key_type,
1885 data_t *key_data,
1886 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001887{
Ronald Cron5425a212020-08-04 14:58:35 +02001888 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001890 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001891 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001892 size_t output_buffer_size = 0;
1893 size_t input_buffer_size = 0;
1894 size_t output_length = 0;
1895 uint8_t *output = NULL;
1896 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001897 psa_status_t status;
1898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1900 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1901 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 ASSERT_ALLOC(input, input_buffer_size);
1904 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 psa_set_key_usage_flags(&attributes, policy_usage);
1909 psa_set_key_algorithm(&attributes, policy_alg);
1910 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1913 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001915 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 TEST_EQUAL(policy_usage,
1917 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001918
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001919 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1921 output, output_buffer_size,
1922 &output_length);
1923 if (policy_alg == exercise_alg &&
1924 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1925 PSA_ASSERT(status);
1926 } else {
1927 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1928 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001929
1930 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1932 if (policy_alg == exercise_alg &&
1933 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1934 PSA_ASSERT(status);
1935 } else {
1936 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1937 }
1938 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001940 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1942 input, input_buffer_size,
1943 &output_length);
1944 if (policy_alg == exercise_alg &&
1945 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1946 PSA_ASSERT(status);
1947 } else {
1948 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1949 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001950
1951 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1953 if (policy_alg == exercise_alg &&
1954 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1955 PSA_ASSERT(status);
1956 } else {
1957 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1958 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959
1960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 psa_cipher_abort(&operation);
1962 mbedtls_free(input);
1963 mbedtls_free(output);
1964 psa_destroy_key(key);
1965 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966}
1967/* END_CASE */
1968
1969/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970void aead_key_policy(int policy_usage_arg,
1971 int policy_alg,
1972 int key_type,
1973 data_t *key_data,
1974 int nonce_length_arg,
1975 int tag_length_arg,
1976 int exercise_alg,
1977 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001978{
Ronald Cron5425a212020-08-04 14:58:35 +02001979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001981 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001982 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001984 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 size_t nonce_length = nonce_length_arg;
1987 unsigned char tag[16];
1988 size_t tag_length = tag_length_arg;
1989 size_t output_length;
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 TEST_LE_U(nonce_length, sizeof(nonce));
1992 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 psa_set_key_usage_flags(&attributes, policy_usage);
1997 psa_set_key_algorithm(&attributes, policy_alg);
1998 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2001 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002003 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 TEST_EQUAL(policy_usage,
2005 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002006
Neil Armstrong752d8112022-02-07 14:51:11 +01002007 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 status = psa_aead_encrypt(key, exercise_alg,
2009 nonce, nonce_length,
2010 NULL, 0,
2011 NULL, 0,
2012 tag, tag_length,
2013 &output_length);
2014 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2015 TEST_EQUAL(status, expected_status);
2016 } else {
2017 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2018 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
Neil Armstrong752d8112022-02-07 14:51:11 +01002020 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2022 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2023 TEST_EQUAL(status, expected_status);
2024 } else {
2025 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2026 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002027
2028 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 memset(tag, 0, sizeof(tag));
2030 status = psa_aead_decrypt(key, exercise_alg,
2031 nonce, nonce_length,
2032 NULL, 0,
2033 tag, tag_length,
2034 NULL, 0,
2035 &output_length);
2036 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2037 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2038 } else if (expected_status == PSA_SUCCESS) {
2039 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2040 } else {
2041 TEST_EQUAL(status, expected_status);
2042 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Neil Armstrong752d8112022-02-07 14:51:11 +01002044 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 PSA_ASSERT(psa_aead_abort(&operation));
2046 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2047 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2048 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2049 } else {
2050 TEST_EQUAL(status, expected_status);
2051 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002052
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 PSA_ASSERT(psa_aead_abort(&operation));
2055 psa_destroy_key(key);
2056 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061void asymmetric_encryption_key_policy(int policy_usage_arg,
2062 int policy_alg,
2063 int key_type,
2064 data_t *key_data,
2065 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066{
Ronald Cron5425a212020-08-04 14:58:35 +02002067 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002069 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070 psa_status_t status;
2071 size_t key_bits;
2072 size_t buffer_length;
2073 unsigned char *buffer = NULL;
2074 size_t output_length;
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 psa_set_key_usage_flags(&attributes, policy_usage);
2079 psa_set_key_algorithm(&attributes, policy_alg);
2080 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2083 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002085 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 TEST_EQUAL(policy_usage,
2087 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2090 key_bits = psa_get_key_bits(&attributes);
2091 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2092 exercise_alg);
2093 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 status = psa_asymmetric_encrypt(key, exercise_alg,
2096 NULL, 0,
2097 NULL, 0,
2098 buffer, buffer_length,
2099 &output_length);
2100 if (policy_alg == exercise_alg &&
2101 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2102 PSA_ASSERT(status);
2103 } else {
2104 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2105 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (buffer_length != 0) {
2108 memset(buffer, 0, buffer_length);
2109 }
2110 status = psa_asymmetric_decrypt(key, exercise_alg,
2111 buffer, buffer_length,
2112 NULL, 0,
2113 buffer, buffer_length,
2114 &output_length);
2115 if (policy_alg == exercise_alg &&
2116 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2117 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2118 } else {
2119 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2120 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
2122exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002123 /*
2124 * Key attributes may have been returned by psa_get_key_attributes()
2125 * thus reset them as required.
2126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 psa_destroy_key(key);
2130 PSA_DONE();
2131 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132}
2133/* END_CASE */
2134
2135/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136void asymmetric_signature_key_policy(int policy_usage_arg,
2137 int policy_alg,
2138 int key_type,
2139 data_t *key_data,
2140 int exercise_alg,
2141 int payload_length_arg,
2142 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143{
Ronald Cron5425a212020-08-04 14:58:35 +02002144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002146 psa_key_usage_t policy_usage = policy_usage_arg;
2147 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002150 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2151 * compatible with the policy and `payload_length_arg` is supposed to be
2152 * a valid input length to sign. If `payload_length_arg <= 0`,
2153 * `exercise_alg` is supposed to be forbidden by the policy. */
2154 int compatible_alg = payload_length_arg > 0;
2155 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002157 size_t signature_length;
2158
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002159 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002160 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 TEST_EQUAL(expected_usage,
2162 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 psa_set_key_usage_flags(&attributes, policy_usage);
2167 psa_set_key_algorithm(&attributes, policy_alg);
2168 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2171 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 status = psa_sign_hash(key, exercise_alg,
2176 payload, payload_length,
2177 signature, sizeof(signature),
2178 &signature_length);
2179 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2180 PSA_ASSERT(status);
2181 } else {
2182 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2183 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 memset(signature, 0, sizeof(signature));
2186 status = psa_verify_hash(key, exercise_alg,
2187 payload, payload_length,
2188 signature, sizeof(signature));
2189 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2190 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2191 } else {
2192 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2193 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2196 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2197 status = psa_sign_message(key, exercise_alg,
2198 payload, payload_length,
2199 signature, sizeof(signature),
2200 &signature_length);
2201 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2202 PSA_ASSERT(status);
2203 } else {
2204 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2205 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 memset(signature, 0, sizeof(signature));
2208 status = psa_verify_message(key, exercise_alg,
2209 payload, payload_length,
2210 signature, sizeof(signature));
2211 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2212 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2213 } else {
2214 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2215 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002216 }
2217
Gilles Peskined5b33222018-06-18 22:20:03 +02002218exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 psa_destroy_key(key);
2220 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002221}
2222/* END_CASE */
2223
Janos Follathba3fab92019-06-11 14:50:16 +01002224/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225void derive_key_policy(int policy_usage,
2226 int policy_alg,
2227 int key_type,
2228 data_t *key_data,
2229 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002230{
Ronald Cron5425a212020-08-04 14:58:35 +02002231 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002233 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002234 psa_status_t status;
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 psa_set_key_usage_flags(&attributes, policy_usage);
2239 psa_set_key_algorithm(&attributes, policy_alg);
2240 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2243 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2248 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2249 PSA_ASSERT(psa_key_derivation_input_bytes(
2250 &operation,
2251 PSA_KEY_DERIVATION_INPUT_SEED,
2252 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002253 }
Janos Follathba3fab92019-06-11 14:50:16 +01002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 status = psa_key_derivation_input_key(&operation,
2256 PSA_KEY_DERIVATION_INPUT_SECRET,
2257 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (policy_alg == exercise_alg &&
2260 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2261 PSA_ASSERT(status);
2262 } else {
2263 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2264 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002265
2266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 psa_key_derivation_abort(&operation);
2268 psa_destroy_key(key);
2269 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002270}
2271/* END_CASE */
2272
2273/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274void agreement_key_policy(int policy_usage,
2275 int policy_alg,
2276 int key_type_arg,
2277 data_t *key_data,
2278 int exercise_alg,
2279 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002280{
Ronald Cron5425a212020-08-04 14:58:35 +02002281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002283 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002284 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002285 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002286 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 psa_set_key_usage_flags(&attributes, policy_usage);
2291 psa_set_key_algorithm(&attributes, policy_alg);
2292 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2295 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2298 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002301
2302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 psa_key_derivation_abort(&operation);
2304 psa_destroy_key(key);
2305 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002306}
2307/* END_CASE */
2308
2309/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310void key_policy_alg2(int key_type_arg, data_t *key_data,
2311 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002312{
Ronald Cron5425a212020-08-04 14:58:35 +02002313 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002314 psa_key_type_t key_type = key_type_arg;
2315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2316 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2317 psa_key_usage_t usage = usage_arg;
2318 psa_algorithm_t alg = alg_arg;
2319 psa_algorithm_t alg2 = alg2_arg;
2320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 psa_set_key_usage_flags(&attributes, usage);
2324 psa_set_key_algorithm(&attributes, alg);
2325 psa_set_key_enrollment_algorithm(&attributes, alg2);
2326 psa_set_key_type(&attributes, key_type);
2327 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2328 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002329
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002330 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 usage = mbedtls_test_update_key_usage_flags(usage);
2332 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2333 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2334 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2335 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002338 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 }
2340 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002341 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002343
2344exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002345 /*
2346 * Key attributes may have been returned by psa_get_key_attributes()
2347 * thus reset them as required.
2348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 psa_destroy_key(key);
2352 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002353}
2354/* END_CASE */
2355
2356/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002357void raw_agreement_key_policy(int policy_usage,
2358 int policy_alg,
2359 int key_type_arg,
2360 data_t *key_data,
2361 int exercise_alg,
2362 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363{
Ronald Cron5425a212020-08-04 14:58:35 +02002364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002367 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002368 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002369 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 psa_set_key_usage_flags(&attributes, policy_usage);
2374 psa_set_key_algorithm(&attributes, policy_alg);
2375 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2378 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383
2384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 psa_key_derivation_abort(&operation);
2386 psa_destroy_key(key);
2387 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388}
2389/* END_CASE */
2390
2391/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392void copy_success(int source_usage_arg,
2393 int source_alg_arg, int source_alg2_arg,
2394 unsigned int source_lifetime_arg,
2395 int type_arg, data_t *material,
2396 int copy_attributes,
2397 int target_usage_arg,
2398 int target_alg_arg, int target_alg2_arg,
2399 unsigned int target_lifetime_arg,
2400 int expected_usage_arg,
2401 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002402{
Gilles Peskineca25db92019-04-19 11:43:08 +02002403 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2404 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002405 psa_key_usage_t expected_usage = expected_usage_arg;
2406 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002407 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302408 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2409 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002410 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2411 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002412 uint8_t *export_buffer = NULL;
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002415
Gilles Peskineca25db92019-04-19 11:43:08 +02002416 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2418 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2419 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2420 psa_set_key_type(&source_attributes, type_arg);
2421 psa_set_key_lifetime(&source_attributes, source_lifetime);
2422 PSA_ASSERT(psa_import_key(&source_attributes,
2423 material->x, material->len,
2424 &source_key));
2425 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002426
Gilles Peskineca25db92019-04-19 11:43:08 +02002427 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002429 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002430 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (target_usage_arg != -1) {
2434 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2435 }
2436 if (target_alg_arg != -1) {
2437 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2438 }
2439 if (target_alg2_arg != -1) {
2440 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2441 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002442
Archana8a180362021-07-05 02:18:48 +05302443
Gilles Peskine57ab7212019-01-28 13:03:09 +01002444 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 PSA_ASSERT(psa_copy_key(source_key,
2446 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447
2448 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450
2451 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2453 TEST_EQUAL(psa_get_key_type(&source_attributes),
2454 psa_get_key_type(&target_attributes));
2455 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2456 psa_get_key_bits(&target_attributes));
2457 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2458 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2459 TEST_EQUAL(expected_alg2,
2460 psa_get_key_enrollment_algorithm(&target_attributes));
2461 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002462 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 ASSERT_ALLOC(export_buffer, material->len);
2464 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2465 material->len, &length));
2466 ASSERT_COMPARE(material->x, material->len,
2467 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (!psa_key_lifetime_is_external(target_lifetime)) {
2471 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 }
2474 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302475 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 }
Archana8a180362021-07-05 02:18:48 +05302477 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480
2481exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002482 /*
2483 * Source and target key attributes may have been returned by
2484 * psa_get_key_attributes() thus reset them as required.
2485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 psa_reset_key_attributes(&source_attributes);
2487 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 PSA_DONE();
2490 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002491}
2492/* END_CASE */
2493
2494/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495void copy_fail(int source_usage_arg,
2496 int source_alg_arg, int source_alg2_arg,
2497 int source_lifetime_arg,
2498 int type_arg, data_t *material,
2499 int target_type_arg, int target_bits_arg,
2500 int target_usage_arg,
2501 int target_alg_arg, int target_alg2_arg,
2502 int target_id_arg, int target_lifetime_arg,
2503 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002504{
2505 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2506 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002507 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2508 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002512
2513 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2515 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2516 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2517 psa_set_key_type(&source_attributes, type_arg);
2518 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2519 PSA_ASSERT(psa_import_key(&source_attributes,
2520 material->x, material->len,
2521 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002522
2523 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 psa_set_key_id(&target_attributes, key_id);
2525 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2526 psa_set_key_type(&target_attributes, target_type_arg);
2527 psa_set_key_bits(&target_attributes, target_bits_arg);
2528 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2529 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2530 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002531
2532 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 TEST_EQUAL(psa_copy_key(source_key,
2534 &target_attributes, &target_key),
2535 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002538
Gilles Peskine4a644642019-05-03 17:14:08 +02002539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 psa_reset_key_attributes(&source_attributes);
2541 psa_reset_key_attributes(&target_attributes);
2542 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002547void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002548{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002549 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002550 /* Test each valid way of initializing the object, except for `= {0}`, as
2551 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2552 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002553 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002555 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2556 psa_hash_operation_t zero;
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002559
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002560 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2562 PSA_ERROR_BAD_STATE);
2563 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2564 PSA_ERROR_BAD_STATE);
2565 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2566 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002567
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002568 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 PSA_ASSERT(psa_hash_abort(&func));
2570 PSA_ASSERT(psa_hash_abort(&init));
2571 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002572}
2573/* END_CASE */
2574
2575/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002576void hash_setup(int alg_arg,
2577 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002578{
2579 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002580 uint8_t *output = NULL;
2581 size_t output_size = 0;
2582 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002583 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002584 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585 psa_status_t status;
2586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588
Neil Armstrongedb20862022-02-07 15:47:44 +01002589 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 output_size = PSA_HASH_LENGTH(alg);
2591 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 status = psa_hash_compute(alg, NULL, 0,
2594 output, output_size, &output_length);
2595 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002596
2597 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 status = psa_hash_setup(&operation, alg);
2599 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002600
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002601 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002603
2604 /* If setup failed, reproduce the failure, so as to
2605 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (status != PSA_SUCCESS) {
2607 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2608 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002609
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002610 /* Now the operation object should be reusable. */
2611#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2613 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002614#endif
2615
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002616exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 mbedtls_free(output);
2618 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619}
2620/* END_CASE */
2621
2622/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623void hash_compute_fail(int alg_arg, data_t *input,
2624 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002625{
2626 psa_algorithm_t alg = alg_arg;
2627 uint8_t *output = NULL;
2628 size_t output_size = output_size_arg;
2629 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002630 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002631 psa_status_t expected_status = expected_status_arg;
2632 psa_status_t status;
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002637
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002638 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 status = psa_hash_compute(alg, input->x, input->len,
2640 output, output_size, &output_length);
2641 TEST_EQUAL(status, expected_status);
2642 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002643
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002644 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 status = psa_hash_setup(&operation, alg);
2646 if (status == PSA_SUCCESS) {
2647 status = psa_hash_update(&operation, input->x, input->len);
2648 if (status == PSA_SUCCESS) {
2649 status = psa_hash_finish(&operation, output, output_size,
2650 &output_length);
2651 if (status == PSA_SUCCESS) {
2652 TEST_LE_U(output_length, output_size);
2653 } else {
2654 TEST_EQUAL(status, expected_status);
2655 }
2656 } else {
2657 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002658 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 } else {
2660 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002661 }
2662
Gilles Peskine0a749c82019-11-28 19:33:58 +01002663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 PSA_ASSERT(psa_hash_abort(&operation));
2665 mbedtls_free(output);
2666 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002667}
2668/* END_CASE */
2669
2670/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002671void hash_compare_fail(int alg_arg, data_t *input,
2672 data_t *reference_hash,
2673 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002674{
2675 psa_algorithm_t alg = alg_arg;
2676 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002677 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002678 psa_status_t status;
2679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002681
Neil Armstrong55a1be12022-02-07 11:23:20 +01002682 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 status = psa_hash_compare(alg, input->x, input->len,
2684 reference_hash->x, reference_hash->len);
2685 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002686
Neil Armstrong55a1be12022-02-07 11:23:20 +01002687 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 status = psa_hash_setup(&operation, alg);
2689 if (status == PSA_SUCCESS) {
2690 status = psa_hash_update(&operation, input->x, input->len);
2691 if (status == PSA_SUCCESS) {
2692 status = psa_hash_verify(&operation, reference_hash->x,
2693 reference_hash->len);
2694 TEST_EQUAL(status, expected_status);
2695 } else {
2696 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002697 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 } else {
2699 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002700 }
2701
Gilles Peskine88e08462020-01-28 20:43:00 +01002702exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 PSA_ASSERT(psa_hash_abort(&operation));
2704 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002705}
2706/* END_CASE */
2707
2708/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002709void hash_compute_compare(int alg_arg, data_t *input,
2710 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002711{
2712 psa_algorithm_t alg = alg_arg;
2713 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2714 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002716 size_t i;
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002719
Neil Armstrongca30a002022-02-07 11:40:23 +01002720 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2722 output, PSA_HASH_LENGTH(alg),
2723 &output_length));
2724 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2725 ASSERT_COMPARE(output, output_length,
2726 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002727
Neil Armstrongca30a002022-02-07 11:40:23 +01002728 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 PSA_ASSERT(psa_hash_setup(&operation, alg));
2730 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2731 PSA_ASSERT(psa_hash_finish(&operation, output,
2732 PSA_HASH_LENGTH(alg),
2733 &output_length));
2734 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2735 ASSERT_COMPARE(output, output_length,
2736 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002737
2738 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2740 output, sizeof(output),
2741 &output_length));
2742 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2743 ASSERT_COMPARE(output, output_length,
2744 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002745
Neil Armstrongca30a002022-02-07 11:40:23 +01002746 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 PSA_ASSERT(psa_hash_setup(&operation, alg));
2748 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2749 PSA_ASSERT(psa_hash_finish(&operation, output,
2750 sizeof(output), &output_length));
2751 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2752 ASSERT_COMPARE(output, output_length,
2753 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002754
2755 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2757 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002758
Neil Armstrongca30a002022-02-07 11:40:23 +01002759 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 PSA_ASSERT(psa_hash_setup(&operation, alg));
2761 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2762 PSA_ASSERT(psa_hash_verify(&operation, output,
2763 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002764
2765 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2767 output, output_length + 1),
2768 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002769
Neil Armstrongca30a002022-02-07 11:40:23 +01002770 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 PSA_ASSERT(psa_hash_setup(&operation, alg));
2772 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2773 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2774 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002775
2776 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2778 output, output_length - 1),
2779 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002780
Neil Armstrongca30a002022-02-07 11:40:23 +01002781 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 PSA_ASSERT(psa_hash_setup(&operation, alg));
2783 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2784 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2785 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002786
Gilles Peskine0a749c82019-11-28 19:33:58 +01002787 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 for (i = 0; i < output_length; i++) {
2789 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002790 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002791
2792 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2794 output, output_length),
2795 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002796
2797 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 PSA_ASSERT(psa_hash_setup(&operation, alg));
2799 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2800 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2801 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002802
Gilles Peskine0a749c82019-11-28 19:33:58 +01002803 output[i] ^= 1;
2804 }
2805
2806exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 PSA_ASSERT(psa_hash_abort(&operation));
2808 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002809}
2810/* END_CASE */
2811
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002812/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002814{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002815 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002816 unsigned char input[] = "";
2817 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002818 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002819 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2820 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2822 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002823 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002824 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002825 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002828
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002829 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 PSA_ASSERT(psa_hash_setup(&operation, alg));
2831 ASSERT_OPERATION_IS_ACTIVE(operation);
2832 TEST_EQUAL(psa_hash_setup(&operation, alg),
2833 PSA_ERROR_BAD_STATE);
2834 ASSERT_OPERATION_IS_INACTIVE(operation);
2835 PSA_ASSERT(psa_hash_abort(&operation));
2836 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002837
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002838 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2840 PSA_ERROR_BAD_STATE);
2841 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002842
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002843 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002845 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 ASSERT_OPERATION_IS_ACTIVE(operation);
2847 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2848 PSA_ERROR_BAD_STATE);
2849 ASSERT_OPERATION_IS_INACTIVE(operation);
2850 PSA_ASSERT(psa_hash_abort(&operation));
2851 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002852
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002853 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 PSA_ASSERT(psa_hash_setup(&operation, alg));
2855 PSA_ASSERT(psa_hash_finish(&operation,
2856 hash, sizeof(hash), &hash_len));
2857 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2858 PSA_ERROR_BAD_STATE);
2859 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002860
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002861 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 TEST_EQUAL(psa_hash_verify(&operation,
2863 valid_hash, sizeof(valid_hash)),
2864 PSA_ERROR_BAD_STATE);
2865 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002866
2867 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 PSA_ASSERT(psa_hash_setup(&operation, alg));
2869 PSA_ASSERT(psa_hash_finish(&operation,
2870 hash, sizeof(hash), &hash_len));
2871 TEST_EQUAL(psa_hash_verify(&operation,
2872 valid_hash, sizeof(valid_hash)),
2873 PSA_ERROR_BAD_STATE);
2874 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002875
2876 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 PSA_ASSERT(psa_hash_setup(&operation, alg));
2878 ASSERT_OPERATION_IS_ACTIVE(operation);
2879 PSA_ASSERT(psa_hash_verify(&operation,
2880 valid_hash, sizeof(valid_hash)));
2881 ASSERT_OPERATION_IS_INACTIVE(operation);
2882 TEST_EQUAL(psa_hash_verify(&operation,
2883 valid_hash, sizeof(valid_hash)),
2884 PSA_ERROR_BAD_STATE);
2885 ASSERT_OPERATION_IS_INACTIVE(operation);
2886 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002887
2888 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 TEST_EQUAL(psa_hash_finish(&operation,
2890 hash, sizeof(hash), &hash_len),
2891 PSA_ERROR_BAD_STATE);
2892 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002893
2894 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 PSA_ASSERT(psa_hash_setup(&operation, alg));
2896 PSA_ASSERT(psa_hash_finish(&operation,
2897 hash, sizeof(hash), &hash_len));
2898 TEST_EQUAL(psa_hash_finish(&operation,
2899 hash, sizeof(hash), &hash_len),
2900 PSA_ERROR_BAD_STATE);
2901 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002902
2903 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 PSA_ASSERT(psa_hash_setup(&operation, alg));
2905 PSA_ASSERT(psa_hash_verify(&operation,
2906 valid_hash, sizeof(valid_hash)));
2907 TEST_EQUAL(psa_hash_finish(&operation,
2908 hash, sizeof(hash), &hash_len),
2909 PSA_ERROR_BAD_STATE);
2910 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002911
2912exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002914}
2915/* END_CASE */
2916
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002917/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002919{
2920 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002921 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2922 * appended to it */
2923 unsigned char hash[] = {
2924 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2925 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2927 };
2928 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002929 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002932
itayzafrir27e69452018-11-01 14:26:34 +02002933 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 PSA_ASSERT(psa_hash_setup(&operation, alg));
2935 ASSERT_OPERATION_IS_ACTIVE(operation);
2936 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2937 PSA_ERROR_INVALID_SIGNATURE);
2938 ASSERT_OPERATION_IS_INACTIVE(operation);
2939 PSA_ASSERT(psa_hash_abort(&operation));
2940 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002941
itayzafrir27e69452018-11-01 14:26:34 +02002942 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 PSA_ASSERT(psa_hash_setup(&operation, alg));
2944 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2945 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002946
itayzafrir27e69452018-11-01 14:26:34 +02002947 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 PSA_ASSERT(psa_hash_setup(&operation, alg));
2949 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2950 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002951
itayzafrirec93d302018-10-18 18:01:10 +03002952exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002954}
2955/* END_CASE */
2956
Ronald Cronee414c72021-03-18 18:50:08 +01002957/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002958void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002959{
2960 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002961 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002963 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002964 size_t hash_len;
2965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002967
itayzafrir58028322018-10-25 10:22:01 +03002968 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_hash_setup(&operation, alg));
2970 TEST_EQUAL(psa_hash_finish(&operation,
2971 hash, expected_size - 1, &hash_len),
2972 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002973
2974exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002976}
2977/* END_CASE */
2978
Ronald Cronee414c72021-03-18 18:50:08 +01002979/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002980void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002981{
2982 psa_algorithm_t alg = PSA_ALG_SHA_256;
2983 unsigned char hash[PSA_HASH_MAX_SIZE];
2984 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2985 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2986 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2987 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2988 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2989 size_t hash_len;
2990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 PSA_ASSERT(psa_crypto_init());
2992 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
2995 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
2996 PSA_ASSERT(psa_hash_finish(&op_finished,
2997 hash, sizeof(hash), &hash_len));
2998 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
2999 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3002 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3005 PSA_ASSERT(psa_hash_finish(&op_init,
3006 hash, sizeof(hash), &hash_len));
3007 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3008 PSA_ASSERT(psa_hash_finish(&op_finished,
3009 hash, sizeof(hash), &hash_len));
3010 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3011 PSA_ASSERT(psa_hash_finish(&op_aborted,
3012 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003013
3014exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 psa_hash_abort(&op_source);
3016 psa_hash_abort(&op_init);
3017 psa_hash_abort(&op_setup);
3018 psa_hash_abort(&op_finished);
3019 psa_hash_abort(&op_aborted);
3020 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003021}
3022/* END_CASE */
3023
Ronald Cronee414c72021-03-18 18:50:08 +01003024/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003026{
3027 psa_algorithm_t alg = PSA_ALG_SHA_256;
3028 unsigned char hash[PSA_HASH_MAX_SIZE];
3029 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3030 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3031 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3032 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3033 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3034 size_t hash_len;
3035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3039 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3040 PSA_ASSERT(psa_hash_finish(&op_finished,
3041 hash, sizeof(hash), &hash_len));
3042 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3043 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3046 PSA_ASSERT(psa_hash_finish(&op_target,
3047 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3050 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3051 PSA_ERROR_BAD_STATE);
3052 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3053 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003054
3055exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 psa_hash_abort(&op_target);
3057 psa_hash_abort(&op_init);
3058 psa_hash_abort(&op_setup);
3059 psa_hash_abort(&op_finished);
3060 psa_hash_abort(&op_aborted);
3061 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003062}
3063/* END_CASE */
3064
itayzafrir58028322018-10-25 10:22:01 +03003065/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003067{
Jaeden Amero252ef282019-02-15 14:05:35 +00003068 const uint8_t input[1] = { 0 };
3069
Jaeden Amero769ce272019-01-04 11:48:03 +00003070 /* Test each valid way of initializing the object, except for `= {0}`, as
3071 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3072 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003073 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003075 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3076 psa_mac_operation_t zero;
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003079
Jaeden Amero252ef282019-02-15 14:05:35 +00003080 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 TEST_EQUAL(psa_mac_update(&func,
3082 input, sizeof(input)),
3083 PSA_ERROR_BAD_STATE);
3084 TEST_EQUAL(psa_mac_update(&init,
3085 input, sizeof(input)),
3086 PSA_ERROR_BAD_STATE);
3087 TEST_EQUAL(psa_mac_update(&zero,
3088 input, sizeof(input)),
3089 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003090
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003091 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 PSA_ASSERT(psa_mac_abort(&func));
3093 PSA_ASSERT(psa_mac_abort(&init));
3094 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003095}
3096/* END_CASE */
3097
3098/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099void mac_setup(int key_type_arg,
3100 data_t *key,
3101 int alg_arg,
3102 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003103{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003104 psa_key_type_t key_type = key_type_arg;
3105 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003106 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003107 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003108 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3109#if defined(KNOWN_SUPPORTED_MAC_ALG)
3110 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3111#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3116 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 }
3119 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003121 /* The operation object should be reusable. */
3122#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3124 smoke_test_key_data,
3125 sizeof(smoke_test_key_data),
3126 KNOWN_SUPPORTED_MAC_ALG,
3127 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 }
3130 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003131#endif
3132
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003133exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003135}
3136/* END_CASE */
3137
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003138/* 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 +01003139void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003140{
Ronald Cron5425a212020-08-04 14:58:35 +02003141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003142 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3143 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003144 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003145 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3146 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3148 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003150 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3151 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3152 size_t sign_mac_length = 0;
3153 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3154 const uint8_t verify_mac[] = {
3155 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3156 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3158 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 PSA_ASSERT(psa_crypto_init());
3161 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3162 psa_set_key_algorithm(&attributes, alg);
3163 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3166 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167
Jaeden Amero252ef282019-02-15 14:05:35 +00003168 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3170 PSA_ERROR_BAD_STATE);
3171 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003172
3173 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3175 &sign_mac_length),
3176 PSA_ERROR_BAD_STATE);
3177 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003178
3179 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 TEST_EQUAL(psa_mac_verify_finish(&operation,
3181 verify_mac, sizeof(verify_mac)),
3182 PSA_ERROR_BAD_STATE);
3183 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003184
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003185 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3187 ASSERT_OPERATION_IS_ACTIVE(operation);
3188 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3189 PSA_ERROR_BAD_STATE);
3190 ASSERT_OPERATION_IS_INACTIVE(operation);
3191 PSA_ASSERT(psa_mac_abort(&operation));
3192 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003193
Jaeden Amero252ef282019-02-15 14:05:35 +00003194 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3196 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3197 PSA_ASSERT(psa_mac_sign_finish(&operation,
3198 sign_mac, sizeof(sign_mac),
3199 &sign_mac_length));
3200 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3201 PSA_ERROR_BAD_STATE);
3202 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003203
3204 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3206 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3207 PSA_ASSERT(psa_mac_verify_finish(&operation,
3208 verify_mac, sizeof(verify_mac)));
3209 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3210 PSA_ERROR_BAD_STATE);
3211 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003212
3213 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3215 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3216 PSA_ASSERT(psa_mac_sign_finish(&operation,
3217 sign_mac, sizeof(sign_mac),
3218 &sign_mac_length));
3219 TEST_EQUAL(psa_mac_sign_finish(&operation,
3220 sign_mac, sizeof(sign_mac),
3221 &sign_mac_length),
3222 PSA_ERROR_BAD_STATE);
3223 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003224
3225 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3227 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3228 PSA_ASSERT(psa_mac_verify_finish(&operation,
3229 verify_mac, sizeof(verify_mac)));
3230 TEST_EQUAL(psa_mac_verify_finish(&operation,
3231 verify_mac, sizeof(verify_mac)),
3232 PSA_ERROR_BAD_STATE);
3233 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003234
3235 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3237 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3238 ASSERT_OPERATION_IS_ACTIVE(operation);
3239 TEST_EQUAL(psa_mac_verify_finish(&operation,
3240 verify_mac, sizeof(verify_mac)),
3241 PSA_ERROR_BAD_STATE);
3242 ASSERT_OPERATION_IS_INACTIVE(operation);
3243 PSA_ASSERT(psa_mac_abort(&operation));
3244 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003245
3246 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3248 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3249 ASSERT_OPERATION_IS_ACTIVE(operation);
3250 TEST_EQUAL(psa_mac_sign_finish(&operation,
3251 sign_mac, sizeof(sign_mac),
3252 &sign_mac_length),
3253 PSA_ERROR_BAD_STATE);
3254 ASSERT_OPERATION_IS_INACTIVE(operation);
3255 PSA_ASSERT(psa_mac_abort(&operation));
3256 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003257
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003259
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003262}
3263/* END_CASE */
3264
3265/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003266void mac_sign_verify_multi(int key_type_arg,
3267 data_t *key_data,
3268 int alg_arg,
3269 data_t *input,
3270 int is_verify,
3271 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003272{
3273 size_t data_part_len = 0;
3274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003276 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003278
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (mac_multipart_internal_func(key_type_arg, key_data,
3280 alg_arg,
3281 input, data_part_len,
3282 expected_mac,
3283 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003284 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003286
3287 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003289
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 if (mac_multipart_internal_func(key_type_arg, key_data,
3291 alg_arg,
3292 input, data_part_len,
3293 expected_mac,
3294 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003295 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003297 }
3298
3299 /* Goto is required to silence warnings about unused labels, as we
3300 * don't actually do any test assertions in this function. */
3301 goto exit;
3302}
3303/* END_CASE */
3304
3305/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306void mac_sign(int key_type_arg,
3307 data_t *key_data,
3308 int alg_arg,
3309 data_t *input,
3310 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003311{
Ronald Cron5425a212020-08-04 14:58:35 +02003312 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003313 psa_key_type_t key_type = key_type_arg;
3314 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003315 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003317 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003318 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003320 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003321 const size_t output_sizes_to_test[] = {
3322 0,
3323 1,
3324 expected_mac->len - 1,
3325 expected_mac->len,
3326 expected_mac->len + 1,
3327 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003330 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003332
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3336 psa_set_key_algorithm(&attributes, alg);
3337 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3340 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003343 const size_t output_size = output_sizes_to_test[i];
3344 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 (output_size >= expected_mac->len ? PSA_SUCCESS :
3346 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003347
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 mbedtls_test_set_step(output_size);
3349 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003350
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003351 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 TEST_EQUAL(psa_mac_compute(key, alg,
3353 input->x, input->len,
3354 actual_mac, output_size, &mac_length),
3355 expected_status);
3356 if (expected_status == PSA_SUCCESS) {
3357 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3358 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003359 }
3360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 if (output_size > 0) {
3362 memset(actual_mac, 0, output_size);
3363 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003364
3365 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3367 PSA_ASSERT(psa_mac_update(&operation,
3368 input->x, input->len));
3369 TEST_EQUAL(psa_mac_sign_finish(&operation,
3370 actual_mac, output_size,
3371 &mac_length),
3372 expected_status);
3373 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 if (expected_status == PSA_SUCCESS) {
3376 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3377 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003378 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003380 actual_mac = NULL;
3381 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003382
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003383exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 psa_mac_abort(&operation);
3385 psa_destroy_key(key);
3386 PSA_DONE();
3387 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388}
3389/* END_CASE */
3390
3391/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003392void mac_verify(int key_type_arg,
3393 data_t *key_data,
3394 int alg_arg,
3395 data_t *input,
3396 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003397{
Ronald Cron5425a212020-08-04 14:58:35 +02003398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003399 psa_key_type_t key_type = key_type_arg;
3400 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003401 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003403 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3410 psa_set_key_algorithm(&attributes, alg);
3411 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3414 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003415
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003416 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3418 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003419
3420 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3422 PSA_ASSERT(psa_mac_update(&operation,
3423 input->x, input->len));
3424 PSA_ASSERT(psa_mac_verify_finish(&operation,
3425 expected_mac->x,
3426 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003427
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003428 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 TEST_EQUAL(psa_mac_verify(key, alg,
3430 input->x, input->len,
3431 expected_mac->x,
3432 expected_mac->len - 1),
3433 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003434
3435 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3437 PSA_ASSERT(psa_mac_update(&operation,
3438 input->x, input->len));
3439 TEST_EQUAL(psa_mac_verify_finish(&operation,
3440 expected_mac->x,
3441 expected_mac->len - 1),
3442 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003443
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003444 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3446 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3447 TEST_EQUAL(psa_mac_verify(key, alg,
3448 input->x, input->len,
3449 perturbed_mac, expected_mac->len + 1),
3450 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003451
3452 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3454 PSA_ASSERT(psa_mac_update(&operation,
3455 input->x, input->len));
3456 TEST_EQUAL(psa_mac_verify_finish(&operation,
3457 perturbed_mac,
3458 expected_mac->len + 1),
3459 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003460
3461 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 for (size_t i = 0; i < expected_mac->len; i++) {
3463 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003464 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 TEST_EQUAL(psa_mac_verify(key, alg,
3467 input->x, input->len,
3468 perturbed_mac, expected_mac->len),
3469 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003470
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3472 PSA_ASSERT(psa_mac_update(&operation,
3473 input->x, input->len));
3474 TEST_EQUAL(psa_mac_verify_finish(&operation,
3475 perturbed_mac,
3476 expected_mac->len),
3477 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003478 perturbed_mac[i] ^= 1;
3479 }
3480
Gilles Peskine8c9def32018-02-08 10:02:12 +01003481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 psa_mac_abort(&operation);
3483 psa_destroy_key(key);
3484 PSA_DONE();
3485 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003486}
3487/* END_CASE */
3488
3489/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003490void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003491{
Jaeden Ameroab439972019-02-15 14:12:05 +00003492 const uint8_t input[1] = { 0 };
3493 unsigned char output[1] = { 0 };
3494 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003495 /* Test each valid way of initializing the object, except for `= {0}`, as
3496 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3497 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003498 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003500 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3501 psa_cipher_operation_t zero;
3502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003504
Jaeden Ameroab439972019-02-15 14:12:05 +00003505 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 TEST_EQUAL(psa_cipher_update(&func,
3507 input, sizeof(input),
3508 output, sizeof(output),
3509 &output_length),
3510 PSA_ERROR_BAD_STATE);
3511 TEST_EQUAL(psa_cipher_update(&init,
3512 input, sizeof(input),
3513 output, sizeof(output),
3514 &output_length),
3515 PSA_ERROR_BAD_STATE);
3516 TEST_EQUAL(psa_cipher_update(&zero,
3517 input, sizeof(input),
3518 output, sizeof(output),
3519 &output_length),
3520 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003521
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003522 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 PSA_ASSERT(psa_cipher_abort(&func));
3524 PSA_ASSERT(psa_cipher_abort(&init));
3525 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003526}
3527/* END_CASE */
3528
3529/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003530void cipher_setup(int key_type_arg,
3531 data_t *key,
3532 int alg_arg,
3533 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003534{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003535 psa_key_type_t key_type = key_type_arg;
3536 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003537 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003538 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003539 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003540#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003541 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3542#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003545
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3547 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003548 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 }
3550 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003551
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003552 /* The operation object should be reusable. */
3553#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3555 smoke_test_key_data,
3556 sizeof(smoke_test_key_data),
3557 KNOWN_SUPPORTED_CIPHER_ALG,
3558 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003559 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 }
3561 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003562#endif
3563
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 psa_cipher_abort(&operation);
3566 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003567}
3568/* END_CASE */
3569
Ronald Cronee414c72021-03-18 18:50:08 +01003570/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003571void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003572{
Ronald Cron5425a212020-08-04 14:58:35 +02003573 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003574 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3575 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003577 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003578 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003579 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003580 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003581 0xaa, 0xaa, 0xaa, 0xaa
3582 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003583 const uint8_t text[] = {
3584 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 0xbb, 0xbb, 0xbb, 0xbb
3586 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003587 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003588 size_t length = 0;
3589
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 PSA_ASSERT(psa_crypto_init());
3591 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3592 psa_set_key_algorithm(&attributes, alg);
3593 psa_set_key_type(&attributes, key_type);
3594 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3595 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003596
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003597 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3599 ASSERT_OPERATION_IS_ACTIVE(operation);
3600 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3601 PSA_ERROR_BAD_STATE);
3602 ASSERT_OPERATION_IS_INACTIVE(operation);
3603 PSA_ASSERT(psa_cipher_abort(&operation));
3604 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003605
3606 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3608 ASSERT_OPERATION_IS_ACTIVE(operation);
3609 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3610 PSA_ERROR_BAD_STATE);
3611 ASSERT_OPERATION_IS_INACTIVE(operation);
3612 PSA_ASSERT(psa_cipher_abort(&operation));
3613 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003614
Jaeden Ameroab439972019-02-15 14:12:05 +00003615 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3617 buffer, sizeof(buffer),
3618 &length),
3619 PSA_ERROR_BAD_STATE);
3620 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003621
3622 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003623 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3624 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3625 buffer, sizeof(buffer),
3626 &length));
3627 ASSERT_OPERATION_IS_ACTIVE(operation);
3628 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3629 buffer, sizeof(buffer),
3630 &length),
3631 PSA_ERROR_BAD_STATE);
3632 ASSERT_OPERATION_IS_INACTIVE(operation);
3633 PSA_ASSERT(psa_cipher_abort(&operation));
3634 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003635
3636 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003637 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3638 PSA_ASSERT(psa_cipher_set_iv(&operation,
3639 iv, sizeof(iv)));
3640 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3641 buffer, sizeof(buffer),
3642 &length),
3643 PSA_ERROR_BAD_STATE);
3644 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003645
3646 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 TEST_EQUAL(psa_cipher_set_iv(&operation,
3648 iv, sizeof(iv)),
3649 PSA_ERROR_BAD_STATE);
3650 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003651
3652 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003653 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3654 PSA_ASSERT(psa_cipher_set_iv(&operation,
3655 iv, sizeof(iv)));
3656 ASSERT_OPERATION_IS_ACTIVE(operation);
3657 TEST_EQUAL(psa_cipher_set_iv(&operation,
3658 iv, sizeof(iv)),
3659 PSA_ERROR_BAD_STATE);
3660 ASSERT_OPERATION_IS_INACTIVE(operation);
3661 PSA_ASSERT(psa_cipher_abort(&operation));
3662 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003663
3664 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3666 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3667 buffer, sizeof(buffer),
3668 &length));
3669 TEST_EQUAL(psa_cipher_set_iv(&operation,
3670 iv, sizeof(iv)),
3671 PSA_ERROR_BAD_STATE);
3672 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003673
3674 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 TEST_EQUAL(psa_cipher_update(&operation,
3676 text, sizeof(text),
3677 buffer, sizeof(buffer),
3678 &length),
3679 PSA_ERROR_BAD_STATE);
3680 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003681
3682 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3684 ASSERT_OPERATION_IS_ACTIVE(operation);
3685 TEST_EQUAL(psa_cipher_update(&operation,
3686 text, sizeof(text),
3687 buffer, sizeof(buffer),
3688 &length),
3689 PSA_ERROR_BAD_STATE);
3690 ASSERT_OPERATION_IS_INACTIVE(operation);
3691 PSA_ASSERT(psa_cipher_abort(&operation));
3692 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003693
3694 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003695 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3696 PSA_ASSERT(psa_cipher_set_iv(&operation,
3697 iv, sizeof(iv)));
3698 PSA_ASSERT(psa_cipher_finish(&operation,
3699 buffer, sizeof(buffer), &length));
3700 TEST_EQUAL(psa_cipher_update(&operation,
3701 text, sizeof(text),
3702 buffer, sizeof(buffer),
3703 &length),
3704 PSA_ERROR_BAD_STATE);
3705 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003706
3707 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003708 TEST_EQUAL(psa_cipher_finish(&operation,
3709 buffer, sizeof(buffer), &length),
3710 PSA_ERROR_BAD_STATE);
3711 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003712
3713 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003715 /* Not calling update means we are encrypting an empty buffer, which is OK
3716 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 ASSERT_OPERATION_IS_ACTIVE(operation);
3718 TEST_EQUAL(psa_cipher_finish(&operation,
3719 buffer, sizeof(buffer), &length),
3720 PSA_ERROR_BAD_STATE);
3721 ASSERT_OPERATION_IS_INACTIVE(operation);
3722 PSA_ASSERT(psa_cipher_abort(&operation));
3723 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003724
3725 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3727 PSA_ASSERT(psa_cipher_set_iv(&operation,
3728 iv, sizeof(iv)));
3729 PSA_ASSERT(psa_cipher_finish(&operation,
3730 buffer, sizeof(buffer), &length));
3731 TEST_EQUAL(psa_cipher_finish(&operation,
3732 buffer, sizeof(buffer), &length),
3733 PSA_ERROR_BAD_STATE);
3734 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003735
Gilles Peskine449bd832023-01-11 14:50:10 +01003736 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003737
Jaeden Ameroab439972019-02-15 14:12:05 +00003738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 psa_cipher_abort(&operation);
3740 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003741}
3742/* END_CASE */
3743
3744/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003745void cipher_encrypt_fail(int alg_arg,
3746 int key_type_arg,
3747 data_t *key_data,
3748 data_t *input,
3749 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003750{
Ronald Cron5425a212020-08-04 14:58:35 +02003751 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003752 psa_status_t status;
3753 psa_key_type_t key_type = key_type_arg;
3754 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003755 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003756 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003757 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3758 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003759 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003760 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003761 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003762 size_t function_output_length;
3763 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3765
Gilles Peskine449bd832023-01-11 14:50:10 +01003766 if (PSA_ERROR_BAD_STATE != expected_status) {
3767 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003768
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3770 psa_set_key_algorithm(&attributes, alg);
3771 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003772
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3774 input->len);
3775 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003776
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3778 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003779 }
3780
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003781 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3783 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003784
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003786
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003787 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003788 status = psa_cipher_encrypt_setup(&operation, key, alg);
3789 if (status == PSA_SUCCESS) {
3790 if (alg != PSA_ALG_ECB_NO_PADDING) {
3791 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3792 iv, iv_size,
3793 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003794 }
3795
Gilles Peskine449bd832023-01-11 14:50:10 +01003796 status = psa_cipher_update(&operation, input->x, input->len,
3797 output, output_buffer_size,
3798 &function_output_length);
3799 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003800 output_length += function_output_length;
3801
Gilles Peskine449bd832023-01-11 14:50:10 +01003802 status = psa_cipher_finish(&operation, output + output_length,
3803 output_buffer_size - output_length,
3804 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003805
Gilles Peskine449bd832023-01-11 14:50:10 +01003806 TEST_EQUAL(status, expected_status);
3807 } else {
3808 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003809 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 } else {
3811 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003812 }
3813
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003814exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003815 psa_cipher_abort(&operation);
3816 mbedtls_free(output);
3817 psa_destroy_key(key);
3818 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003819}
3820/* END_CASE */
3821
3822/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003823void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3824 data_t *input, int iv_length,
3825 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003826{
3827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3828 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3829 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3830 size_t output_buffer_size = 0;
3831 unsigned char *output = NULL;
3832
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3834 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003837
Gilles Peskine449bd832023-01-11 14:50:10 +01003838 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3839 psa_set_key_algorithm(&attributes, alg);
3840 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003841
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3843 &key));
3844 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3845 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3846 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003847
3848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 psa_cipher_abort(&operation);
3850 mbedtls_free(output);
3851 psa_destroy_key(key);
3852 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003853}
3854/* END_CASE */
3855
3856/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003857void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3858 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003859{
3860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3861 psa_key_type_t key_type = key_type_arg;
3862 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003863 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3864 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003865 unsigned char *output = NULL;
3866 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003867 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003868 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3869
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003871
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003872 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 TEST_LE_U(ciphertext->len,
3874 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3875 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3876 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3877 TEST_LE_U(plaintext->len,
3878 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3879 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3880 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003881
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003882
3883 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003884 psa_set_key_usage_flags(&attributes,
3885 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3886 psa_set_key_algorithm(&attributes, alg);
3887 psa_set_key_type(&attributes, key_type);
3888 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3889 &key));
3890 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3891 plaintext->len);
3892 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003893
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003894 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3896 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3897 PSA_ERROR_BAD_STATE);
3898 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3899 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3900 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003901
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003902 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3904 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3905 &length),
3906 PSA_ERROR_BAD_STATE);
3907 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3908 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3909 &length),
3910 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003911
Gilles Peskine286c3142022-04-20 17:09:38 +02003912 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003914 output_length = 0;
3915 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003916 PSA_ASSERT(psa_cipher_update(&operation,
3917 plaintext->x, plaintext->len,
3918 output, output_buffer_size,
3919 &length));
3920 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003921 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 PSA_ASSERT(psa_cipher_finish(&operation,
3923 mbedtls_buffer_offset(output, output_length),
3924 output_buffer_size - output_length,
3925 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003926 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3928 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003929
Gilles Peskine286c3142022-04-20 17:09:38 +02003930 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003932 output_length = 0;
3933 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 PSA_ASSERT(psa_cipher_update(&operation,
3935 ciphertext->x, ciphertext->len,
3936 output, output_buffer_size,
3937 &length));
3938 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003939 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 PSA_ASSERT(psa_cipher_finish(&operation,
3941 mbedtls_buffer_offset(output, output_length),
3942 output_buffer_size - output_length,
3943 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003944 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 ASSERT_COMPARE(plaintext->x, plaintext->len,
3946 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003947
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003948 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003949 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3951 output, output_buffer_size,
3952 &output_length));
3953 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3954 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003955
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003956 /* One-shot decryption */
3957 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3959 output, output_buffer_size,
3960 &output_length));
3961 ASSERT_COMPARE(plaintext->x, plaintext->len,
3962 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003963
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003964exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 PSA_ASSERT(psa_cipher_abort(&operation));
3966 mbedtls_free(output);
3967 psa_cipher_abort(&operation);
3968 psa_destroy_key(key);
3969 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003970}
3971/* END_CASE */
3972
3973/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003974void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003975{
3976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3977 psa_algorithm_t alg = alg_arg;
3978 psa_key_type_t key_type = key_type_arg;
3979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3980 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3981 psa_status_t status;
3982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01003984
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3986 psa_set_key_algorithm(&attributes, alg);
3987 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01003988
3989 /* Usage of either of these two size macros would cause divide by zero
3990 * with incorrect key types previously. Input length should be irrelevant
3991 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
3993 0);
3994 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01003995
3996
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3998 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01003999
4000 /* Should fail due to invalid alg type (to support invalid key type).
4001 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004003
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004005
4006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 psa_cipher_abort(&operation);
4008 psa_destroy_key(key);
4009 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004010}
4011/* END_CASE */
4012
4013/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004014void cipher_encrypt_validation(int alg_arg,
4015 int key_type_arg,
4016 data_t *key_data,
4017 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004018{
4019 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4020 psa_key_type_t key_type = key_type_arg;
4021 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004023 unsigned char *output1 = NULL;
4024 size_t output1_buffer_size = 0;
4025 size_t output1_length = 0;
4026 unsigned char *output2 = NULL;
4027 size_t output2_buffer_size = 0;
4028 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004029 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004030 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4036 psa_set_key_algorithm(&attributes, alg);
4037 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4040 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4041 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4042 ASSERT_ALLOC(output1, output1_buffer_size);
4043 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4046 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004047
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004048 /* The one-shot cipher encryption uses generated iv so validating
4049 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4051 output1_buffer_size, &output1_length));
4052 TEST_LE_U(output1_length,
4053 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4054 TEST_LE_U(output1_length,
4055 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4058 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 PSA_ASSERT(psa_cipher_update(&operation,
4061 input->x, input->len,
4062 output2, output2_buffer_size,
4063 &function_output_length));
4064 TEST_LE_U(function_output_length,
4065 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4066 TEST_LE_U(function_output_length,
4067 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004068 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 PSA_ASSERT(psa_cipher_finish(&operation,
4071 output2 + output2_length,
4072 output2_buffer_size - output2_length,
4073 &function_output_length));
4074 TEST_LE_U(function_output_length,
4075 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4076 TEST_LE_U(function_output_length,
4077 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004078 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 PSA_ASSERT(psa_cipher_abort(&operation));
4081 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4082 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004083
Gilles Peskine50e586b2018-06-08 14:28:46 +02004084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 psa_cipher_abort(&operation);
4086 mbedtls_free(output1);
4087 mbedtls_free(output2);
4088 psa_destroy_key(key);
4089 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004090}
4091/* END_CASE */
4092
4093/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004094void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4095 data_t *key_data, data_t *iv,
4096 data_t *input,
4097 int first_part_size_arg,
4098 int output1_length_arg, int output2_length_arg,
4099 data_t *expected_output,
4100 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004101{
Ronald Cron5425a212020-08-04 14:58:35 +02004102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004103 psa_key_type_t key_type = key_type_arg;
4104 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004105 psa_status_t status;
4106 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004107 size_t first_part_size = first_part_size_arg;
4108 size_t output1_length = output1_length_arg;
4109 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004110 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004111 size_t output_buffer_size = 0;
4112 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004113 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004114 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004118
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4120 psa_set_key_algorithm(&attributes, alg);
4121 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4124 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (iv->len > 0) {
4129 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004130 }
4131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4133 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4134 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 TEST_LE_U(first_part_size, input->len);
4137 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4138 output, output_buffer_size,
4139 &function_output_length));
4140 TEST_ASSERT(function_output_length == output1_length);
4141 TEST_LE_U(function_output_length,
4142 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4143 TEST_LE_U(function_output_length,
4144 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004145 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (first_part_size < input->len) {
4148 PSA_ASSERT(psa_cipher_update(&operation,
4149 input->x + first_part_size,
4150 input->len - first_part_size,
4151 (output_buffer_size == 0 ? NULL :
4152 output + total_output_length),
4153 output_buffer_size - total_output_length,
4154 &function_output_length));
4155 TEST_ASSERT(function_output_length == output2_length);
4156 TEST_LE_U(function_output_length,
4157 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4158 alg,
4159 input->len - first_part_size));
4160 TEST_LE_U(function_output_length,
4161 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004162 total_output_length += function_output_length;
4163 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 status = psa_cipher_finish(&operation,
4166 (output_buffer_size == 0 ? NULL :
4167 output + total_output_length),
4168 output_buffer_size - total_output_length,
4169 &function_output_length);
4170 TEST_LE_U(function_output_length,
4171 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4172 TEST_LE_U(function_output_length,
4173 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004174 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (expected_status == PSA_SUCCESS) {
4178 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 ASSERT_COMPARE(expected_output->x, expected_output->len,
4181 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004182 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004183
4184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 psa_cipher_abort(&operation);
4186 mbedtls_free(output);
4187 psa_destroy_key(key);
4188 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004189}
4190/* END_CASE */
4191
4192/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004193void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4194 data_t *key_data, data_t *iv,
4195 data_t *input,
4196 int first_part_size_arg,
4197 int output1_length_arg, int output2_length_arg,
4198 data_t *expected_output,
4199 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004200{
Ronald Cron5425a212020-08-04 14:58:35 +02004201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004202 psa_key_type_t key_type = key_type_arg;
4203 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004204 psa_status_t status;
4205 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004206 size_t first_part_size = first_part_size_arg;
4207 size_t output1_length = output1_length_arg;
4208 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004209 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004210 size_t output_buffer_size = 0;
4211 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004212 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004213 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4219 psa_set_key_algorithm(&attributes, alg);
4220 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4223 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004226
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 if (iv->len > 0) {
4228 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004229 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4232 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4233 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 TEST_LE_U(first_part_size, input->len);
4236 PSA_ASSERT(psa_cipher_update(&operation,
4237 input->x, first_part_size,
4238 output, output_buffer_size,
4239 &function_output_length));
4240 TEST_ASSERT(function_output_length == output1_length);
4241 TEST_LE_U(function_output_length,
4242 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4243 TEST_LE_U(function_output_length,
4244 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004245 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 if (first_part_size < input->len) {
4248 PSA_ASSERT(psa_cipher_update(&operation,
4249 input->x + first_part_size,
4250 input->len - first_part_size,
4251 (output_buffer_size == 0 ? NULL :
4252 output + total_output_length),
4253 output_buffer_size - total_output_length,
4254 &function_output_length));
4255 TEST_ASSERT(function_output_length == output2_length);
4256 TEST_LE_U(function_output_length,
4257 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4258 alg,
4259 input->len - first_part_size));
4260 TEST_LE_U(function_output_length,
4261 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004262 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004263 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 status = psa_cipher_finish(&operation,
4266 (output_buffer_size == 0 ? NULL :
4267 output + total_output_length),
4268 output_buffer_size - total_output_length,
4269 &function_output_length);
4270 TEST_LE_U(function_output_length,
4271 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4272 TEST_LE_U(function_output_length,
4273 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004274 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 if (expected_status == PSA_SUCCESS) {
4278 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 ASSERT_COMPARE(expected_output->x, expected_output->len,
4281 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004282 }
4283
Gilles Peskine50e586b2018-06-08 14:28:46 +02004284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 psa_cipher_abort(&operation);
4286 mbedtls_free(output);
4287 psa_destroy_key(key);
4288 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004289}
4290/* END_CASE */
4291
Gilles Peskine50e586b2018-06-08 14:28:46 +02004292/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004293void cipher_decrypt_fail(int alg_arg,
4294 int key_type_arg,
4295 data_t *key_data,
4296 data_t *iv,
4297 data_t *input_arg,
4298 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004299{
4300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4301 psa_status_t status;
4302 psa_key_type_t key_type = key_type_arg;
4303 psa_algorithm_t alg = alg_arg;
4304 psa_status_t expected_status = expected_status_arg;
4305 unsigned char *input = NULL;
4306 size_t input_buffer_size = 0;
4307 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004308 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004309 size_t output_buffer_size = 0;
4310 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004311 size_t function_output_length;
4312 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 if (PSA_ERROR_BAD_STATE != expected_status) {
4316 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4319 psa_set_key_algorithm(&attributes, alg);
4320 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4323 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004324 }
4325
4326 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4328 if (input_buffer_size > 0) {
4329 ASSERT_ALLOC(input, input_buffer_size);
4330 memcpy(input, iv->x, iv->len);
4331 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004332 }
4333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4335 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004336
Neil Armstrong66a479f2022-02-07 15:41:19 +01004337 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4339 output_buffer_size, &output_length);
4340 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004341
Neil Armstrong66a479f2022-02-07 15:41:19 +01004342 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 status = psa_cipher_decrypt_setup(&operation, key, alg);
4344 if (status == PSA_SUCCESS) {
4345 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4346 input_arg->len) +
4347 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4348 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (iv->len > 0) {
4351 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (status != PSA_SUCCESS) {
4354 TEST_EQUAL(status, expected_status);
4355 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004356 }
4357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 if (status == PSA_SUCCESS) {
4359 status = psa_cipher_update(&operation,
4360 input_arg->x, input_arg->len,
4361 output_multi, output_buffer_size,
4362 &function_output_length);
4363 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004364 output_length = function_output_length;
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 status = psa_cipher_finish(&operation,
4367 output_multi + output_length,
4368 output_buffer_size - output_length,
4369 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 TEST_EQUAL(status, expected_status);
4372 } else {
4373 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004374 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 } else {
4376 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004377 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 } else {
4379 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004380 }
4381
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 psa_cipher_abort(&operation);
4384 mbedtls_free(input);
4385 mbedtls_free(output);
4386 mbedtls_free(output_multi);
4387 psa_destroy_key(key);
4388 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004389}
4390/* END_CASE */
4391
4392/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004393void cipher_decrypt(int alg_arg,
4394 int key_type_arg,
4395 data_t *key_data,
4396 data_t *iv,
4397 data_t *input_arg,
4398 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004399{
4400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4401 psa_key_type_t key_type = key_type_arg;
4402 psa_algorithm_t alg = alg_arg;
4403 unsigned char *input = NULL;
4404 size_t input_buffer_size = 0;
4405 unsigned char *output = NULL;
4406 size_t output_buffer_size = 0;
4407 size_t output_length = 0;
4408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4413 psa_set_key_algorithm(&attributes, alg);
4414 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004415
4416 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4418 if (input_buffer_size > 0) {
4419 ASSERT_ALLOC(input, input_buffer_size);
4420 memcpy(input, iv->x, iv->len);
4421 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004422 }
4423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4425 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4428 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4431 output_buffer_size, &output_length));
4432 TEST_LE_U(output_length,
4433 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4434 TEST_LE_U(output_length,
4435 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 ASSERT_COMPARE(expected_output->x, expected_output->len,
4438 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004439exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 mbedtls_free(input);
4441 mbedtls_free(output);
4442 psa_destroy_key(key);
4443 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004444}
4445/* END_CASE */
4446
4447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004448void cipher_verify_output(int alg_arg,
4449 int key_type_arg,
4450 data_t *key_data,
4451 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004452{
Ronald Cron5425a212020-08-04 14:58:35 +02004453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004454 psa_key_type_t key_type = key_type_arg;
4455 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004456 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004457 size_t output1_size = 0;
4458 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004459 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004460 size_t output2_size = 0;
4461 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4467 psa_set_key_algorithm(&attributes, alg);
4468 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4471 &key));
4472 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4473 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4476 output1, output1_size,
4477 &output1_length));
4478 TEST_LE_U(output1_length,
4479 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4480 TEST_LE_U(output1_length,
4481 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004482
4483 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004485
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4487 output2, output2_size,
4488 &output2_length));
4489 TEST_LE_U(output2_length,
4490 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4491 TEST_LE_U(output2_length,
4492 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004495
4496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 mbedtls_free(output1);
4498 mbedtls_free(output2);
4499 psa_destroy_key(key);
4500 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004501}
4502/* END_CASE */
4503
4504/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004505void cipher_verify_output_multipart(int alg_arg,
4506 int key_type_arg,
4507 data_t *key_data,
4508 data_t *input,
4509 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004510{
Ronald Cron5425a212020-08-04 14:58:35 +02004511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004512 psa_key_type_t key_type = key_type_arg;
4513 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004514 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004516 size_t iv_size = 16;
4517 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004518 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004519 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004520 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004521 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004522 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004523 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004524 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004525 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4526 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4532 psa_set_key_algorithm(&attributes, alg);
4533 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4536 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4539 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 if (alg != PSA_ALG_ECB_NO_PADDING) {
4542 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4543 iv, iv_size,
4544 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004545 }
4546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4548 TEST_LE_U(output1_buffer_size,
4549 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4550 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004553
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4555 output1, output1_buffer_size,
4556 &function_output_length));
4557 TEST_LE_U(function_output_length,
4558 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4559 TEST_LE_U(function_output_length,
4560 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004561 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 PSA_ASSERT(psa_cipher_update(&operation1,
4564 input->x + first_part_size,
4565 input->len - first_part_size,
4566 output1, output1_buffer_size,
4567 &function_output_length));
4568 TEST_LE_U(function_output_length,
4569 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4570 alg,
4571 input->len - first_part_size));
4572 TEST_LE_U(function_output_length,
4573 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004574 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 PSA_ASSERT(psa_cipher_finish(&operation1,
4577 output1 + output1_length,
4578 output1_buffer_size - output1_length,
4579 &function_output_length));
4580 TEST_LE_U(function_output_length,
4581 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4582 TEST_LE_U(function_output_length,
4583 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004584 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004587
Gilles Peskine048b7f02018-06-08 14:20:49 +02004588 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 TEST_LE_U(output2_buffer_size,
4590 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4591 TEST_LE_U(output2_buffer_size,
4592 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4593 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 if (iv_length > 0) {
4596 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4597 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004598 }
Moran Pekerded84402018-06-06 16:36:50 +03004599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4601 output2, output2_buffer_size,
4602 &function_output_length));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4605 TEST_LE_U(function_output_length,
4606 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004607 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 PSA_ASSERT(psa_cipher_update(&operation2,
4610 output1 + first_part_size,
4611 output1_length - first_part_size,
4612 output2, output2_buffer_size,
4613 &function_output_length));
4614 TEST_LE_U(function_output_length,
4615 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4616 alg,
4617 output1_length - first_part_size));
4618 TEST_LE_U(function_output_length,
4619 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004620 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 PSA_ASSERT(psa_cipher_finish(&operation2,
4623 output2 + output2_length,
4624 output2_buffer_size - output2_length,
4625 &function_output_length));
4626 TEST_LE_U(function_output_length,
4627 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4628 TEST_LE_U(function_output_length,
4629 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004630 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004635
4636exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 psa_cipher_abort(&operation1);
4638 psa_cipher_abort(&operation2);
4639 mbedtls_free(output1);
4640 mbedtls_free(output2);
4641 psa_destroy_key(key);
4642 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004643}
4644/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004645
Gilles Peskine20035e32018-02-03 22:44:14 +01004646/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004647void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4648 int alg_arg,
4649 data_t *nonce,
4650 data_t *additional_data,
4651 data_t *input_data,
4652 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004653{
Ronald Cron5425a212020-08-04 14:58:35 +02004654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004655 psa_key_type_t key_type = key_type_arg;
4656 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004657 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004658 unsigned char *output_data = NULL;
4659 size_t output_size = 0;
4660 size_t output_length = 0;
4661 unsigned char *output_data2 = NULL;
4662 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004663 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004664 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4670 psa_set_key_algorithm(&attributes, alg);
4671 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4674 &key));
4675 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4676 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4679 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004680 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4681 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4683 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4684 TEST_EQUAL(output_size,
4685 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4686 TEST_LE_U(output_size,
4687 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004688 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 status = psa_aead_encrypt(key, alg,
4692 nonce->x, nonce->len,
4693 additional_data->x,
4694 additional_data->len,
4695 input_data->x, input_data->len,
4696 output_data, output_size,
4697 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004698
4699 /* If the operation is not supported, just skip and not fail in case the
4700 * encryption involves a common limitation of cryptography hardwares and
4701 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (status == PSA_ERROR_NOT_SUPPORTED) {
4703 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4704 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004705 }
4706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if (PSA_SUCCESS == expected_result) {
4710 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004711
Gilles Peskine003a4a92019-05-14 16:09:40 +02004712 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4713 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 TEST_EQUAL(input_data->len,
4715 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 TEST_LE_U(input_data->len,
4718 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 TEST_EQUAL(psa_aead_decrypt(key, alg,
4721 nonce->x, nonce->len,
4722 additional_data->x,
4723 additional_data->len,
4724 output_data, output_length,
4725 output_data2, output_length,
4726 &output_length2),
4727 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 ASSERT_COMPARE(input_data->x, input_data->len,
4730 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004731 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004732
Gilles Peskinea1cac842018-06-11 19:33:02 +02004733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 psa_destroy_key(key);
4735 mbedtls_free(output_data);
4736 mbedtls_free(output_data2);
4737 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004738}
4739/* END_CASE */
4740
4741/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004742void aead_encrypt(int key_type_arg, data_t *key_data,
4743 int alg_arg,
4744 data_t *nonce,
4745 data_t *additional_data,
4746 data_t *input_data,
4747 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004748{
Ronald Cron5425a212020-08-04 14:58:35 +02004749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004750 psa_key_type_t key_type = key_type_arg;
4751 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004752 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004753 unsigned char *output_data = NULL;
4754 size_t output_size = 0;
4755 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004757 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4762 psa_set_key_algorithm(&attributes, alg);
4763 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4766 &key));
4767 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4768 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4771 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004772 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4773 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 TEST_EQUAL(output_size,
4775 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4776 TEST_LE_U(output_size,
4777 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4778 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 status = psa_aead_encrypt(key, alg,
4781 nonce->x, nonce->len,
4782 additional_data->x, additional_data->len,
4783 input_data->x, input_data->len,
4784 output_data, output_size,
4785 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786
Ronald Cron28a45ed2021-02-09 20:35:42 +01004787 /* If the operation is not supported, just skip and not fail in case the
4788 * encryption involves a common limitation of cryptography hardwares and
4789 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (status == PSA_ERROR_NOT_SUPPORTED) {
4791 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4792 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004793 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 PSA_ASSERT(status);
4796 ASSERT_COMPARE(expected_result->x, expected_result->len,
4797 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004798
Gilles Peskinea1cac842018-06-11 19:33:02 +02004799exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 psa_destroy_key(key);
4801 mbedtls_free(output_data);
4802 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803}
4804/* END_CASE */
4805
4806/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004807void aead_decrypt(int key_type_arg, data_t *key_data,
4808 int alg_arg,
4809 data_t *nonce,
4810 data_t *additional_data,
4811 data_t *input_data,
4812 data_t *expected_data,
4813 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004814{
Ronald Cron5425a212020-08-04 14:58:35 +02004815 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004816 psa_key_type_t key_type = key_type_arg;
4817 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004818 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004819 unsigned char *output_data = NULL;
4820 size_t output_size = 0;
4821 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004823 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004824 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4829 psa_set_key_algorithm(&attributes, alg);
4830 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4833 &key));
4834 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4835 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4838 alg);
4839 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4840 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004841 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4842 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 TEST_EQUAL(output_size,
4844 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4845 TEST_LE_U(output_size,
4846 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004847 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 status = psa_aead_decrypt(key, alg,
4851 nonce->x, nonce->len,
4852 additional_data->x,
4853 additional_data->len,
4854 input_data->x, input_data->len,
4855 output_data, output_size,
4856 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004857
Ronald Cron28a45ed2021-02-09 20:35:42 +01004858 /* If the operation is not supported, just skip and not fail in case the
4859 * decryption involves a common limitation of cryptography hardwares and
4860 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 if (status == PSA_ERROR_NOT_SUPPORTED) {
4862 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4863 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004864 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (expected_result == PSA_SUCCESS) {
4869 ASSERT_COMPARE(expected_data->x, expected_data->len,
4870 output_data, output_length);
4871 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872
Gilles Peskinea1cac842018-06-11 19:33:02 +02004873exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 psa_destroy_key(key);
4875 mbedtls_free(output_data);
4876 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004877}
4878/* END_CASE */
4879
4880/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004881void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4882 int alg_arg,
4883 data_t *nonce,
4884 data_t *additional_data,
4885 data_t *input_data,
4886 int do_set_lengths,
4887 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004888{
Paul Elliottd3f82412021-06-16 16:52:21 +01004889 size_t ad_part_len = 0;
4890 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004891 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004892
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4894 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004895
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 if (do_set_lengths) {
4897 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004898 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004900 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004902 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004903
4904 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (!aead_multipart_internal_func(key_type_arg, key_data,
4906 alg_arg, nonce,
4907 additional_data,
4908 ad_part_len,
4909 input_data, -1,
4910 set_lengths_method,
4911 expected_output,
4912 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004913 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004914 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 /* length(0) part, length(ad_part_len) part, length(0) part... */
4917 mbedtls_test_set_step(1000 + ad_part_len);
4918
4919 if (!aead_multipart_internal_func(key_type_arg, key_data,
4920 alg_arg, nonce,
4921 additional_data,
4922 ad_part_len,
4923 input_data, -1,
4924 set_lengths_method,
4925 expected_output,
4926 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004927 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 }
4929 }
4930
4931 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4932 /* Split data into length(data_part_len) parts. */
4933 mbedtls_test_set_step(2000 + data_part_len);
4934
4935 if (do_set_lengths) {
4936 if (data_part_len & 0x01) {
4937 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4938 } else {
4939 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4940 }
4941 }
4942
4943 if (!aead_multipart_internal_func(key_type_arg, key_data,
4944 alg_arg, nonce,
4945 additional_data, -1,
4946 input_data, data_part_len,
4947 set_lengths_method,
4948 expected_output,
4949 1, 0)) {
4950 break;
4951 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004952
4953 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 if (!aead_multipart_internal_func(key_type_arg, key_data,
4957 alg_arg, nonce,
4958 additional_data, -1,
4959 input_data, data_part_len,
4960 set_lengths_method,
4961 expected_output,
4962 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004963 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004965 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004966
Paul Elliott8fc45162021-06-23 16:06:01 +01004967 /* Goto is required to silence warnings about unused labels, as we
4968 * don't actually do any test assertions in this function. */
4969 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004970}
4971/* END_CASE */
4972
4973/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004974void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4975 int alg_arg,
4976 data_t *nonce,
4977 data_t *additional_data,
4978 data_t *input_data,
4979 int do_set_lengths,
4980 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004981{
Paul Elliottd3f82412021-06-16 16:52:21 +01004982 size_t ad_part_len = 0;
4983 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004984 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004987 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (do_set_lengths) {
4991 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004992 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004994 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004996 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 if (!aead_multipart_internal_func(key_type_arg, key_data,
4999 alg_arg, nonce,
5000 additional_data,
5001 ad_part_len,
5002 input_data, -1,
5003 set_lengths_method,
5004 expected_output,
5005 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005006 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005008
5009 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (!aead_multipart_internal_func(key_type_arg, key_data,
5013 alg_arg, nonce,
5014 additional_data,
5015 ad_part_len,
5016 input_data, -1,
5017 set_lengths_method,
5018 expected_output,
5019 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005022 }
5023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 if (do_set_lengths) {
5029 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005030 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005034 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (!aead_multipart_internal_func(key_type_arg, key_data,
5037 alg_arg, nonce,
5038 additional_data, -1,
5039 input_data, data_part_len,
5040 set_lengths_method,
5041 expected_output,
5042 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005043 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005045
5046 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005048
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 if (!aead_multipart_internal_func(key_type_arg, key_data,
5050 alg_arg, nonce,
5051 additional_data, -1,
5052 input_data, data_part_len,
5053 set_lengths_method,
5054 expected_output,
5055 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005058 }
5059
Paul Elliott8fc45162021-06-23 16:06:01 +01005060 /* Goto is required to silence warnings about unused labels, as we
5061 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005062 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005063}
5064/* END_CASE */
5065
5066/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005067void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5068 int alg_arg,
5069 int nonce_length,
5070 int expected_nonce_length_arg,
5071 data_t *additional_data,
5072 data_t *input_data,
5073 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005074{
5075
5076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5077 psa_key_type_t key_type = key_type_arg;
5078 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005079 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005080 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5081 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5082 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005083 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005084 size_t actual_nonce_length = 0;
5085 size_t expected_nonce_length = expected_nonce_length_arg;
5086 unsigned char *output = NULL;
5087 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005088 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005089 size_t ciphertext_size = 0;
5090 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005091 size_t tag_length = 0;
5092 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5097 psa_set_key_algorithm(&attributes, alg);
5098 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005099
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5101 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005112
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005116
5117 /* If the operation is not supported, just skip and not fail in case the
5118 * encryption involves a common limitation of cryptography hardwares and
5119 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (status == PSA_ERROR_NOT_SUPPORTED) {
5121 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5122 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005123 }
5124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5128 nonce_length,
5129 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (expected_status == PSA_SUCCESS) {
5136 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5137 alg));
5138 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005143 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5145 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5148 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5151 output, output_size,
5152 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5155 &ciphertext_length, tag_buffer,
5156 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005157 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005158
5159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 psa_destroy_key(key);
5161 mbedtls_free(output);
5162 mbedtls_free(ciphertext);
5163 psa_aead_abort(&operation);
5164 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005165}
5166/* END_CASE */
5167
5168/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005169void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5170 int alg_arg,
5171 int nonce_length_arg,
5172 int set_lengths_method_arg,
5173 data_t *additional_data,
5174 data_t *input_data,
5175 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005176{
5177
5178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5179 psa_key_type_t key_type = key_type_arg;
5180 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005181 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005182 uint8_t *nonce_buffer = NULL;
5183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5184 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5185 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005186 unsigned char *output = NULL;
5187 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005188 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005189 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005190 size_t ciphertext_size = 0;
5191 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005192 size_t tag_length = 0;
5193 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005194 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005195 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5200 psa_set_key_algorithm(&attributes, alg);
5201 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5204 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005207
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005211
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005213
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005219
5220 /* If the operation is not supported, just skip and not fail in case the
5221 * encryption involves a common limitation of cryptography hardwares and
5222 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 if (status == PSA_ERROR_NOT_SUPPORTED) {
5224 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5225 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005226 }
5227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005229
Paul Elliott4023ffd2021-09-10 16:21:22 +01005230 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 if (nonce_length_arg == -1) {
5232 /* Arbitrary size buffer, to test zero length valid buffer. */
5233 ASSERT_ALLOC(nonce_buffer, 4);
5234 nonce_length = 0;
5235 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005236 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 nonce_length = (size_t) nonce_length_arg;
5238 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005239
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 if (nonce_buffer) {
5241 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005242 nonce_buffer[index] = 'a' + index;
5243 }
Paul Elliott66696b52021-08-16 18:42:41 +01005244 }
Paul Elliott863864a2021-07-23 17:28:31 +01005245 }
5246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5248 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5249 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005250 }
5251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 if (expected_status == PSA_SUCCESS) {
5257 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5258 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5259 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005260 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005262 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 }
Paul Elliott863864a2021-07-23 17:28:31 +01005264
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005265 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5267 additional_data->len),
5268 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5271 output, output_size,
5272 &ciphertext_length),
5273 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005274
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5276 &ciphertext_length, tag_buffer,
5277 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5278 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005279 }
5280
5281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 psa_destroy_key(key);
5283 mbedtls_free(output);
5284 mbedtls_free(ciphertext);
5285 mbedtls_free(nonce_buffer);
5286 psa_aead_abort(&operation);
5287 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005288}
5289/* END_CASE */
5290
5291/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005292void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005293 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005294 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005295 data_t *nonce,
5296 data_t *additional_data,
5297 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005299{
5300
5301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5302 psa_key_type_t key_type = key_type_arg;
5303 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005304 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5306 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5307 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005308 unsigned char *output = NULL;
5309 unsigned char *ciphertext = NULL;
5310 size_t output_size = output_size_arg;
5311 size_t ciphertext_size = 0;
5312 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005313 size_t tag_length = 0;
5314 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5319 psa_set_key_algorithm(&attributes, alg);
5320 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5323 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005332
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005334
5335 /* If the operation is not supported, just skip and not fail in case the
5336 * encryption involves a common limitation of cryptography hardwares and
5337 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 if (status == PSA_ERROR_NOT_SUPPORTED) {
5339 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5340 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005341 }
5342
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5346 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5351 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 status = psa_aead_update(&operation, input_data->x, input_data->len,
5354 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005359 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5361 &ciphertext_length, tag_buffer,
5362 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005363 }
5364
5365exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 psa_destroy_key(key);
5367 mbedtls_free(output);
5368 mbedtls_free(ciphertext);
5369 psa_aead_abort(&operation);
5370 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005371}
5372/* END_CASE */
5373
Paul Elliott91b021e2021-07-23 18:52:31 +01005374/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005375void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5376 int alg_arg,
5377 int finish_ciphertext_size_arg,
5378 int tag_size_arg,
5379 data_t *nonce,
5380 data_t *additional_data,
5381 data_t *input_data,
5382 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005383{
5384
5385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5386 psa_key_type_t key_type = key_type_arg;
5387 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005388 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5390 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5391 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005392 unsigned char *ciphertext = NULL;
5393 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005394 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005395 size_t ciphertext_size = 0;
5396 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5398 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005399 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005402
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5404 psa_set_key_algorithm(&attributes, alg);
5405 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5408 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005415
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005419
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005421
5422 /* If the operation is not supported, just skip and not fail in case the
5423 * encryption involves a common limitation of cryptography hardwares and
5424 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 if (status == PSA_ERROR_NOT_SUPPORTED) {
5426 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5427 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005428 }
5429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5435 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5438 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5441 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005442
5443 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 status = psa_aead_finish(&operation, finish_ciphertext,
5445 finish_ciphertext_size,
5446 &ciphertext_length, tag_buffer,
5447 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005450
5451exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 psa_destroy_key(key);
5453 mbedtls_free(ciphertext);
5454 mbedtls_free(finish_ciphertext);
5455 mbedtls_free(tag_buffer);
5456 psa_aead_abort(&operation);
5457 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005458}
5459/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005460
5461/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005462void aead_multipart_verify(int key_type_arg, data_t *key_data,
5463 int alg_arg,
5464 data_t *nonce,
5465 data_t *additional_data,
5466 data_t *input_data,
5467 data_t *tag,
5468 int tag_usage_arg,
5469 int expected_setup_status_arg,
5470 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005471{
5472 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5473 psa_key_type_t key_type = key_type_arg;
5474 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005475 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5477 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5478 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005479 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005480 unsigned char *plaintext = NULL;
5481 unsigned char *finish_plaintext = NULL;
5482 size_t plaintext_size = 0;
5483 size_t plaintext_length = 0;
5484 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005485 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005486 unsigned char *tag_buffer = NULL;
5487 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005488
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005490
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5492 psa_set_key_algorithm(&attributes, alg);
5493 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5496 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005497
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005499
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5501 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005504
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005508
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005510
5511 /* If the operation is not supported, just skip and not fail in case the
5512 * encryption involves a common limitation of cryptography hardwares and
5513 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 if (status == PSA_ERROR_NOT_SUPPORTED) {
5515 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5516 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005517 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005521 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 }
Paul Elliott9961a662021-09-17 19:19:02 +01005523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 status = psa_aead_set_lengths(&operation, additional_data->len,
5529 input_data->len);
5530 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005531
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5533 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5536 input_data->len,
5537 plaintext, plaintext_size,
5538 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005541 tag_buffer = tag->x;
5542 tag_size = tag->len;
5543 }
5544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 status = psa_aead_verify(&operation, finish_plaintext,
5546 verify_plaintext_size,
5547 &plaintext_length,
5548 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005551
5552exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 psa_destroy_key(key);
5554 mbedtls_free(plaintext);
5555 mbedtls_free(finish_plaintext);
5556 psa_aead_abort(&operation);
5557 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005558}
5559/* END_CASE */
5560
Paul Elliott9961a662021-09-17 19:19:02 +01005561/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005562void aead_multipart_setup(int key_type_arg, data_t *key_data,
5563 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005564{
5565 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5566 psa_key_type_t key_type = key_type_arg;
5567 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005568 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5570 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5571 psa_status_t expected_status = expected_status_arg;
5572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 psa_set_key_usage_flags(&attributes,
5576 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5577 psa_set_key_algorithm(&attributes, alg);
5578 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5581 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005592
5593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 psa_destroy_key(key);
5595 psa_aead_abort(&operation);
5596 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005597}
5598/* END_CASE */
5599
5600/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005601void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5602 int alg_arg,
5603 data_t *nonce,
5604 data_t *additional_data,
5605 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005606{
5607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5608 psa_key_type_t key_type = key_type_arg;
5609 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005610 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005611 unsigned char *output_data = NULL;
5612 unsigned char *final_data = NULL;
5613 size_t output_size = 0;
5614 size_t finish_output_size = 0;
5615 size_t output_length = 0;
5616 size_t key_bits = 0;
5617 size_t tag_length = 0;
5618 size_t tag_size = 0;
5619 size_t nonce_length = 0;
5620 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5621 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5622 size_t output_part_length = 0;
5623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 psa_set_key_usage_flags(&attributes,
5628 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5629 psa_set_key_algorithm(&attributes, alg);
5630 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5633 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5636 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005637
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005647
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005651
5652 /* Test all operations error without calling setup first. */
5653
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5655 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5660 PSA_AEAD_NONCE_MAX_SIZE,
5661 &nonce_length),
5662 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Paul Elliott481be342021-07-16 17:38:47 +01005666 /* ------------------------------------------------------- */
5667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5669 input_data->len),
5670 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673
Paul Elliott481be342021-07-16 17:38:47 +01005674 /* ------------------------------------------------------- */
5675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5677 additional_data->len),
5678 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Paul Elliott481be342021-07-16 17:38:47 +01005682 /* ------------------------------------------------------- */
5683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5685 input_data->len, output_data,
5686 output_size, &output_length),
5687 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
Paul Elliott481be342021-07-16 17:38:47 +01005691 /* ------------------------------------------------------- */
5692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5694 finish_output_size,
5695 &output_part_length,
5696 tag_buffer, tag_length,
5697 &tag_size),
5698 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Paul Elliott481be342021-07-16 17:38:47 +01005702 /* ------------------------------------------------------- */
5703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5705 finish_output_size,
5706 &output_part_length,
5707 tag_buffer,
5708 tag_length),
5709 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712
5713 /* Test for double setups. */
5714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5718 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005721
Paul Elliott481be342021-07-16 17:38:47 +01005722 /* ------------------------------------------------------- */
5723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5727 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005730
Paul Elliott374a2be2021-07-16 17:53:40 +01005731 /* ------------------------------------------------------- */
5732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5736 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005739
5740 /* ------------------------------------------------------- */
5741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5745 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005748
Paul Elliottc23a9a02021-06-21 18:32:46 +01005749 /* Test for not setting a nonce. */
5750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5754 additional_data->len),
5755 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005758
Paul Elliott7f628422021-09-01 12:08:29 +01005759 /* ------------------------------------------------------- */
5760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005762
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5764 input_data->len, output_data,
5765 output_size, &output_length),
5766 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005769
Paul Elliottbdc2c682021-09-21 18:37:10 +01005770 /* ------------------------------------------------------- */
5771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5775 finish_output_size,
5776 &output_part_length,
5777 tag_buffer, tag_length,
5778 &tag_size),
5779 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005782
5783 /* ------------------------------------------------------- */
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5788 finish_output_size,
5789 &output_part_length,
5790 tag_buffer,
5791 tag_length),
5792 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005795
Paul Elliottc23a9a02021-06-21 18:32:46 +01005796 /* Test for double setting nonce. */
5797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5803 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005806
Paul Elliott374a2be2021-07-16 17:53:40 +01005807 /* Test for double generating nonce. */
5808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5812 PSA_AEAD_NONCE_MAX_SIZE,
5813 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5816 PSA_AEAD_NONCE_MAX_SIZE,
5817 &nonce_length),
5818 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005819
5820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005822
5823 /* Test for generate nonce then set and vice versa */
5824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5828 PSA_AEAD_NONCE_MAX_SIZE,
5829 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5832 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005835
Andrzej Kurekad837522021-12-15 15:28:49 +01005836 /* Test for generating nonce after calling set lengths */
5837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5841 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5844 PSA_AEAD_NONCE_MAX_SIZE,
5845 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005848
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005849 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (operation.alg == PSA_ALG_CCM) {
5854 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5855 input_data->len),
5856 PSA_ERROR_INVALID_ARGUMENT);
5857 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5858 PSA_AEAD_NONCE_MAX_SIZE,
5859 &nonce_length),
5860 PSA_ERROR_BAD_STATE);
5861 } else {
5862 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5863 input_data->len));
5864 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5865 PSA_AEAD_NONCE_MAX_SIZE,
5866 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005867 }
5868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005870
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005871 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005872#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5876 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5877 input_data->len),
5878 PSA_ERROR_INVALID_ARGUMENT);
5879 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5880 PSA_AEAD_NONCE_MAX_SIZE,
5881 &nonce_length),
5882 PSA_ERROR_BAD_STATE);
5883 } else {
5884 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5885 input_data->len));
5886 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5887 PSA_AEAD_NONCE_MAX_SIZE,
5888 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005889 }
5890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005892#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005893
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005894 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5899 PSA_AEAD_NONCE_MAX_SIZE,
5900 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if (operation.alg == PSA_ALG_CCM) {
5903 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5904 input_data->len),
5905 PSA_ERROR_INVALID_ARGUMENT);
5906 } else {
5907 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5908 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005909 }
5910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005912
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005913 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005914 /* Test for setting nonce after calling set lengths */
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5919 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005920
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005922
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005924
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005925 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (operation.alg == PSA_ALG_CCM) {
5930 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5931 input_data->len),
5932 PSA_ERROR_INVALID_ARGUMENT);
5933 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5934 PSA_ERROR_BAD_STATE);
5935 } else {
5936 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5937 input_data->len));
5938 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005939 }
5940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005942
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005943 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005944#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5948 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5949 input_data->len),
5950 PSA_ERROR_INVALID_ARGUMENT);
5951 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5952 PSA_ERROR_BAD_STATE);
5953 } else {
5954 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5955 input_data->len));
5956 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005957 }
5958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005960#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005961
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005962 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005965
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 if (operation.alg == PSA_ALG_CCM) {
5969 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5970 input_data->len),
5971 PSA_ERROR_INVALID_ARGUMENT);
5972 } else {
5973 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5974 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005975 }
5976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005978
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005979 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00005980#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (operation.alg == PSA_ALG_GCM) {
5984 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5985 SIZE_MAX),
5986 PSA_ERROR_INVALID_ARGUMENT);
5987 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5988 PSA_ERROR_BAD_STATE);
5989 } else if (operation.alg != PSA_ALG_CCM) {
5990 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5991 SIZE_MAX));
5992 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005993 }
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00005996#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005997
Tom Cosgrove1797b052022-12-04 17:19:59 +00005998 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00005999#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (operation.alg == PSA_ALG_GCM) {
6005 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6006 SIZE_MAX),
6007 PSA_ERROR_INVALID_ARGUMENT);
6008 } else if (operation.alg != PSA_ALG_CCM) {
6009 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6010 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006011 }
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006014#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006015
6016 /* ------------------------------------------------------- */
6017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6023 PSA_AEAD_NONCE_MAX_SIZE,
6024 &nonce_length),
6025 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006028
Paul Elliott7220cae2021-06-22 17:25:57 +01006029 /* Test for generating nonce in decrypt setup. */
6030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6034 PSA_AEAD_NONCE_MAX_SIZE,
6035 &nonce_length),
6036 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006039
Paul Elliottc23a9a02021-06-21 18:32:46 +01006040 /* Test for setting lengths twice. */
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6047 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6050 input_data->len),
6051 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006054
Andrzej Kurekad837522021-12-15 15:28:49 +01006055 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006060
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6064 additional_data->len),
6065 PSA_ERROR_BAD_STATE);
6066 } else {
6067 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6068 additional_data->len));
6069
6070 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6071 input_data->len),
6072 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006073 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006075
6076 /* ------------------------------------------------------- */
6077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (operation.alg == PSA_ALG_CCM) {
6083 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6084 input_data->len, output_data,
6085 output_size, &output_length),
6086 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 } else {
6089 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6090 input_data->len, output_data,
6091 output_size, &output_length));
6092
6093 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6094 input_data->len),
6095 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006096 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006098
6099 /* ------------------------------------------------------- */
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (operation.alg == PSA_ALG_CCM) {
6106 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6107 finish_output_size,
6108 &output_part_length,
6109 tag_buffer, tag_length,
6110 &tag_size));
6111 } else {
6112 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6113 finish_output_size,
6114 &output_part_length,
6115 tag_buffer, tag_length,
6116 &tag_size));
6117
6118 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6119 input_data->len),
6120 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006121 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006123
6124 /* Test for setting lengths after generating nonce + already starting data. */
6125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006127
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6129 PSA_AEAD_NONCE_MAX_SIZE,
6130 &nonce_length));
6131 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6134 additional_data->len),
6135 PSA_ERROR_BAD_STATE);
6136 } else {
6137 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6138 additional_data->len));
6139
6140 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6141 input_data->len),
6142 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006143 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006145
6146 /* ------------------------------------------------------- */
6147
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6151 PSA_AEAD_NONCE_MAX_SIZE,
6152 &nonce_length));
6153 if (operation.alg == PSA_ALG_CCM) {
6154 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6155 input_data->len, output_data,
6156 output_size, &output_length),
6157 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 } else {
6160 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6161 input_data->len, output_data,
6162 output_size, &output_length));
6163
6164 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6165 input_data->len),
6166 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006167 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006169
6170 /* ------------------------------------------------------- */
6171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6175 PSA_AEAD_NONCE_MAX_SIZE,
6176 &nonce_length));
6177 if (operation.alg == PSA_ALG_CCM) {
6178 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6179 finish_output_size,
6180 &output_part_length,
6181 tag_buffer, tag_length,
6182 &tag_size));
6183 } else {
6184 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6185 finish_output_size,
6186 &output_part_length,
6187 tag_buffer, tag_length,
6188 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6191 input_data->len),
6192 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006193 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006195
Paul Elliott243080c2021-07-21 19:01:17 +01006196 /* Test for not sending any additional data or data after setting non zero
6197 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6204 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6207 finish_output_size,
6208 &output_part_length,
6209 tag_buffer, tag_length,
6210 &tag_size),
6211 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006214
Paul Elliott243080c2021-07-21 19:01:17 +01006215 /* Test for not sending any additional data or data after setting non-zero
6216 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6223 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6226 finish_output_size,
6227 &output_part_length,
6228 tag_buffer,
6229 tag_length),
6230 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006233
Paul Elliott243080c2021-07-21 19:01:17 +01006234 /* Test for not sending any additional data after setting a non-zero length
6235 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6242 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6245 input_data->len, output_data,
6246 output_size, &output_length),
6247 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Paul Elliottf94bd992021-09-19 18:15:59 +01006251 /* Test for not sending any data after setting a non-zero length for it.*/
6252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6258 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6261 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6264 finish_output_size,
6265 &output_part_length,
6266 tag_buffer, tag_length,
6267 &tag_size),
6268 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006271
Paul Elliottb0450fe2021-09-01 15:06:26 +01006272 /* Test for sending too much additional data after setting lengths. */
6273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006279
6280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6282 additional_data->len),
6283 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006286
Paul Elliotta2a09b02021-09-22 14:56:40 +01006287 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6294 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6297 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6300 1),
6301 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006304
Paul Elliottb0450fe2021-09-01 15:06:26 +01006305 /* Test for sending too much data after setting lengths. */
6306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6314 input_data->len, output_data,
6315 output_size, &output_length),
6316 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006319
Paul Elliotta2a09b02021-09-22 14:56:40 +01006320 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6327 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6330 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6333 input_data->len, output_data,
6334 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6337 1, output_data,
6338 output_size, &output_length),
6339 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Paul Elliottc23a9a02021-06-21 18:32:46 +01006343 /* Test sending additional data after data. */
6344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 if (operation.alg != PSA_ALG_CCM) {
6350 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6351 input_data->len, output_data,
6352 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6355 additional_data->len),
6356 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006357 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006359
Paul Elliott534d0b42021-06-22 19:15:20 +01006360 /* Test calling finish on decryption. */
6361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6367 finish_output_size,
6368 &output_part_length,
6369 tag_buffer, tag_length,
6370 &tag_size),
6371 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006374
6375 /* Test calling verify on encryption. */
6376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6382 finish_output_size,
6383 &output_part_length,
6384 tag_buffer,
6385 tag_length),
6386 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006389
6390
Paul Elliottc23a9a02021-06-21 18:32:46 +01006391exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 psa_destroy_key(key);
6393 psa_aead_abort(&operation);
6394 mbedtls_free(output_data);
6395 mbedtls_free(final_data);
6396 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006397}
6398/* END_CASE */
6399
6400/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006401void signature_size(int type_arg,
6402 int bits,
6403 int alg_arg,
6404 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006405{
6406 psa_key_type_t type = type_arg;
6407 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006411
Gilles Peskinee59236f2018-01-27 23:32:46 +01006412exit:
6413 ;
6414}
6415/* END_CASE */
6416
6417/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006418void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6419 int alg_arg, data_t *input_data,
6420 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006421{
Ronald Cron5425a212020-08-04 14:58:35 +02006422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006423 psa_key_type_t key_type = key_type_arg;
6424 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006425 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006426 unsigned char *signature = NULL;
6427 size_t signature_size;
6428 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6434 psa_set_key_algorithm(&attributes, alg);
6435 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6438 &key));
6439 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6440 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006441
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006442 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006443 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6445 key_bits, alg);
6446 TEST_ASSERT(signature_size != 0);
6447 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6448 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006449
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006450 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 PSA_ASSERT(psa_sign_hash(key, alg,
6452 input_data->x, input_data->len,
6453 signature, signature_size,
6454 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006455 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 ASSERT_COMPARE(output_data->x, output_data->len,
6457 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006458
6459exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006460 /*
6461 * Key attributes may have been returned by psa_get_key_attributes()
6462 * thus reset them as required.
6463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 psa_destroy_key(key);
6467 mbedtls_free(signature);
6468 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006469}
6470/* END_CASE */
6471
Paul Elliott712d5122022-12-07 14:03:10 +00006472/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6473void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6474 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006475 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006476{
6477 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6478 psa_key_type_t key_type = key_type_arg;
6479 psa_algorithm_t alg = alg_arg;
6480 size_t key_bits;
6481 unsigned char *signature = NULL;
6482 size_t signature_size;
6483 size_t signature_length = 0xdeadbeef;
6484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6485 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006486 uint32_t num_ops = 0;
6487 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006488 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006489 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006490 size_t min_completes = 0;
6491 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006492
Paul Elliott712d5122022-12-07 14:03:10 +00006493 psa_sign_hash_interruptible_operation_t operation =
6494 psa_sign_hash_interruptible_operation_init();
6495
6496 PSA_ASSERT(psa_crypto_init());
6497
6498 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6499 psa_set_key_algorithm(&attributes, alg);
6500 psa_set_key_type(&attributes, key_type);
6501
6502 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6503 &key));
6504 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6505 key_bits = psa_get_key_bits(&attributes);
6506
6507 /* Allocate a buffer which has the size advertised by the
6508 * library. */
6509 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6510 key_bits, alg);
6511 TEST_ASSERT(signature_size != 0);
6512 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6513 ASSERT_ALLOC(signature, signature_size);
6514
Paul Elliott0c683352022-12-16 19:16:56 +00006515 psa_interruptible_set_max_ops(max_ops);
6516
Paul Elliott6f600372023-02-06 18:41:05 +00006517 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6518 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006519
Paul Elliott712d5122022-12-07 14:03:10 +00006520 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6521 TEST_ASSERT(num_ops_prior == 0);
6522
6523 /* Start performing the signature. */
6524 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6525 input_data->x, input_data->len));
6526
6527 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6528 TEST_ASSERT(num_ops_prior == 0);
6529
6530 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006531 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006532 status = psa_sign_hash_complete(&operation, signature, signature_size,
6533 &signature_length);
6534
Paul Elliott0c683352022-12-16 19:16:56 +00006535 num_completes++;
6536
Paul Elliott712d5122022-12-07 14:03:10 +00006537 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6538 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00006539 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006540
Paul Elliott712d5122022-12-07 14:03:10 +00006541 num_ops_prior = num_ops;
6542 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006543 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006544
6545 TEST_ASSERT(status == PSA_SUCCESS);
6546
Paul Elliott0c683352022-12-16 19:16:56 +00006547 TEST_LE_U(min_completes, num_completes);
6548 TEST_LE_U(num_completes, max_completes);
6549
Paul Elliott712d5122022-12-07 14:03:10 +00006550 /* Verify that the signature is what is expected. */
6551 ASSERT_COMPARE(output_data->x, output_data->len,
6552 signature, signature_length);
6553
6554 PSA_ASSERT(psa_sign_hash_abort(&operation));
6555
Paul Elliott59ad9452022-12-18 15:09:02 +00006556 num_ops = psa_sign_hash_get_num_ops(&operation);
6557 TEST_ASSERT(num_ops == 0);
6558
Paul Elliott712d5122022-12-07 14:03:10 +00006559exit:
6560
6561 /*
6562 * Key attributes may have been returned by psa_get_key_attributes()
6563 * thus reset them as required.
6564 */
6565 psa_reset_key_attributes(&attributes);
6566
6567 psa_destroy_key(key);
6568 mbedtls_free(signature);
6569 PSA_DONE();
6570}
6571/* END_CASE */
6572
Gilles Peskine20035e32018-02-03 22:44:14 +01006573/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006574void sign_hash_fail(int key_type_arg, data_t *key_data,
6575 int alg_arg, data_t *input_data,
6576 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006577{
Ronald Cron5425a212020-08-04 14:58:35 +02006578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006579 psa_key_type_t key_type = key_type_arg;
6580 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006581 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006582 psa_status_t actual_status;
6583 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006584 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006585 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006591
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6593 psa_set_key_algorithm(&attributes, alg);
6594 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006595
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6597 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 actual_status = psa_sign_hash(key, alg,
6600 input_data->x, input_data->len,
6601 signature, signature_size,
6602 &signature_length);
6603 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006604 /* The value of *signature_length is unspecified on error, but
6605 * whatever it is, it should be less than signature_size, so that
6606 * if the caller tries to read *signature_length bytes without
6607 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006609
6610exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 psa_reset_key_attributes(&attributes);
6612 psa_destroy_key(key);
6613 mbedtls_free(signature);
6614 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006615}
6616/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006617
Paul Elliott91007972022-12-16 12:21:24 +00006618/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6619void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6620 int alg_arg, data_t *input_data,
6621 int signature_size_arg,
6622 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006623 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006624 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006625{
6626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6627 psa_key_type_t key_type = key_type_arg;
6628 psa_algorithm_t alg = alg_arg;
6629 size_t signature_size = signature_size_arg;
6630 psa_status_t actual_status;
6631 psa_status_t expected_start_status = expected_start_status_arg;
6632 psa_status_t expected_complete_status = expected_complete_status_arg;
6633 unsigned char *signature = NULL;
6634 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006635 uint32_t num_ops = 0;
6636 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006637 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006638 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006639 size_t min_completes = 0;
6640 size_t max_completes = 0;
6641
Paul Elliott91007972022-12-16 12:21:24 +00006642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6643 psa_sign_hash_interruptible_operation_t operation =
6644 psa_sign_hash_interruptible_operation_init();
6645
6646 ASSERT_ALLOC(signature, signature_size);
6647
6648 PSA_ASSERT(psa_crypto_init());
6649
6650 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6651 psa_set_key_algorithm(&attributes, alg);
6652 psa_set_key_type(&attributes, key_type);
6653
6654 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6655 &key));
6656
Paul Elliott0c683352022-12-16 19:16:56 +00006657 psa_interruptible_set_max_ops(max_ops);
6658
Paul Elliott6f600372023-02-06 18:41:05 +00006659 interruptible_signverify_get_minmax_completes(max_ops,
6660 expected_complete_status,
6661 &min_completes,
6662 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006663
Paul Elliott91007972022-12-16 12:21:24 +00006664 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6665 TEST_ASSERT(num_ops_prior == 0);
6666
6667 /* Start performing the signature. */
6668 actual_status = psa_sign_hash_start(&operation, key, alg,
6669 input_data->x, input_data->len);
6670
6671 TEST_EQUAL(actual_status, expected_start_status);
6672
Paul Elliottc9774412023-02-06 15:14:07 +00006673 if (expected_start_status != PSA_SUCCESS) {
6674 actual_status = psa_sign_hash_start(&operation, key, alg,
6675 input_data->x, input_data->len);
6676
6677 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6678 }
6679
Paul Elliott91007972022-12-16 12:21:24 +00006680 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6681 TEST_ASSERT(num_ops_prior == 0);
6682
Paul Elliott91007972022-12-16 12:21:24 +00006683 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006684 do {
Paul Elliott91007972022-12-16 12:21:24 +00006685 actual_status = psa_sign_hash_complete(&operation, signature,
6686 signature_size,
6687 &signature_length);
6688
Paul Elliott0c683352022-12-16 19:16:56 +00006689 num_completes++;
6690
Paul Elliott334d7262023-01-20 17:29:41 +00006691 if (actual_status == PSA_SUCCESS ||
6692 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006693 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00006694 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006695
Paul Elliott91007972022-12-16 12:21:24 +00006696 num_ops_prior = num_ops;
6697 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006698 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006699
Paul Elliottc9774412023-02-06 15:14:07 +00006700 TEST_EQUAL(actual_status, expected_complete_status);
6701
6702 if (expected_complete_status != PSA_SUCCESS) {
6703 actual_status = psa_sign_hash_complete(&operation, signature,
6704 signature_size,
6705 &signature_length);
6706
Paul Elliott334d7262023-01-20 17:29:41 +00006707 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006708 }
6709
Paul Elliott91007972022-12-16 12:21:24 +00006710 PSA_ASSERT(psa_sign_hash_abort(&operation));
6711
Paul Elliott59ad9452022-12-18 15:09:02 +00006712 num_ops = psa_sign_hash_get_num_ops(&operation);
6713 TEST_ASSERT(num_ops == 0);
6714
Paul Elliott91007972022-12-16 12:21:24 +00006715 /* The value of *signature_length is unspecified on error, but
6716 * whatever it is, it should be less than signature_size, so that
6717 * if the caller tries to read *signature_length bytes without
6718 * checking the error code then they don't overflow a buffer. */
6719 TEST_LE_U(signature_length, signature_size);
6720
Paul Elliott0c683352022-12-16 19:16:56 +00006721 TEST_LE_U(min_completes, num_completes);
6722 TEST_LE_U(num_completes, max_completes);
6723
Paul Elliott91007972022-12-16 12:21:24 +00006724exit:
6725 psa_reset_key_attributes(&attributes);
6726 psa_destroy_key(key);
6727 mbedtls_free(signature);
6728 PSA_DONE();
6729}
6730/* END_CASE */
6731
mohammad16038cc1cee2018-03-28 01:21:33 +03006732/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006733void sign_verify_hash(int key_type_arg, data_t *key_data,
6734 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006735{
Ronald Cron5425a212020-08-04 14:58:35 +02006736 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006737 psa_key_type_t key_type = key_type_arg;
6738 psa_algorithm_t alg = alg_arg;
6739 size_t key_bits;
6740 unsigned char *signature = NULL;
6741 size_t signature_size;
6742 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006744
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006746
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6748 psa_set_key_algorithm(&attributes, alg);
6749 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6752 &key));
6753 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6754 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006755
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006756 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006757 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6759 key_bits, alg);
6760 TEST_ASSERT(signature_size != 0);
6761 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6762 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006763
6764 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 PSA_ASSERT(psa_sign_hash(key, alg,
6766 input_data->x, input_data->len,
6767 signature, signature_size,
6768 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006769 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 TEST_LE_U(signature_length, signature_size);
6771 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006772
6773 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 PSA_ASSERT(psa_verify_hash(key, alg,
6775 input_data->x, input_data->len,
6776 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006779 /* Flip a bit in the input and verify that the signature is now
6780 * detected as invalid. Flip a bit at the beginning, not at the end,
6781 * because ECDSA may ignore the last few bits of the input. */
6782 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 TEST_EQUAL(psa_verify_hash(key, alg,
6784 input_data->x, input_data->len,
6785 signature, signature_length),
6786 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006787 }
6788
6789exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006790 /*
6791 * Key attributes may have been returned by psa_get_key_attributes()
6792 * thus reset them as required.
6793 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006795
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 psa_destroy_key(key);
6797 mbedtls_free(signature);
6798 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006799}
6800/* END_CASE */
6801
Paul Elliott712d5122022-12-07 14:03:10 +00006802/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6803void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006804 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006805 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006806{
6807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6808 psa_key_type_t key_type = key_type_arg;
6809 psa_algorithm_t alg = alg_arg;
6810 size_t key_bits;
6811 unsigned char *signature = NULL;
6812 size_t signature_size;
6813 size_t signature_length = 0xdeadbeef;
6814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6815 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006816 uint32_t max_ops = max_ops_arg;
Paul Elliott0c683352022-12-16 19:16:56 +00006817 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006818 size_t min_completes = 0;
6819 size_t max_completes = 0;
6820
Paul Elliott712d5122022-12-07 14:03:10 +00006821 psa_sign_hash_interruptible_operation_t sign_operation =
6822 psa_sign_hash_interruptible_operation_init();
6823 psa_verify_hash_interruptible_operation_t verify_operation =
6824 psa_verify_hash_interruptible_operation_init();
6825
6826 PSA_ASSERT(psa_crypto_init());
6827
Paul Elliott0c683352022-12-16 19:16:56 +00006828 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6829 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006830 psa_set_key_algorithm(&attributes, alg);
6831 psa_set_key_type(&attributes, key_type);
6832
6833 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6834 &key));
6835 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6836 key_bits = psa_get_key_bits(&attributes);
6837
6838 /* Allocate a buffer which has the size advertised by the
6839 * library. */
6840 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6841 key_bits, alg);
6842 TEST_ASSERT(signature_size != 0);
6843 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6844 ASSERT_ALLOC(signature, signature_size);
6845
Paul Elliott0c683352022-12-16 19:16:56 +00006846 psa_interruptible_set_max_ops(max_ops);
6847
Paul Elliott6f600372023-02-06 18:41:05 +00006848 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6849 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006850
Paul Elliott712d5122022-12-07 14:03:10 +00006851 /* Start performing the signature. */
6852 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6853 input_data->x, input_data->len));
6854
6855 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006856 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006857
Paul Elliott0c683352022-12-16 19:16:56 +00006858 status = psa_sign_hash_complete(&sign_operation, signature,
6859 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006860 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006861
6862 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006863 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006864
6865 TEST_ASSERT(status == PSA_SUCCESS);
6866
Paul Elliott0c683352022-12-16 19:16:56 +00006867 TEST_LE_U(min_completes, num_completes);
6868 TEST_LE_U(num_completes, max_completes);
6869
Paul Elliott712d5122022-12-07 14:03:10 +00006870 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6871
6872 /* Check that the signature length looks sensible. */
6873 TEST_LE_U(signature_length, signature_size);
6874 TEST_ASSERT(signature_length > 0);
6875
Paul Elliott0c683352022-12-16 19:16:56 +00006876 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006877
6878 /* Start verification. */
6879 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6880 input_data->x, input_data->len,
6881 signature, signature_length));
6882
6883 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006884 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006885 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00006886
6887 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00006888 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006889
6890 TEST_ASSERT(status == PSA_SUCCESS);
6891
Paul Elliott0c683352022-12-16 19:16:56 +00006892 TEST_LE_U(min_completes, num_completes);
6893 TEST_LE_U(num_completes, max_completes);
6894
Paul Elliott712d5122022-12-07 14:03:10 +00006895 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6896
6897 verify_operation = psa_verify_hash_interruptible_operation_init();
6898
6899 if (input_data->len != 0) {
6900 /* Flip a bit in the input and verify that the signature is now
6901 * detected as invalid. Flip a bit at the beginning, not at the end,
6902 * because ECDSA may ignore the last few bits of the input. */
6903 input_data->x[0] ^= 1;
6904
Paul Elliott712d5122022-12-07 14:03:10 +00006905 /* Start verification. */
6906 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
6907 input_data->x, input_data->len,
6908 signature, signature_length));
6909
6910 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006911 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006912 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00006913 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006914
6915 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
6916 }
6917
6918 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
6919
6920exit:
6921 /*
6922 * Key attributes may have been returned by psa_get_key_attributes()
6923 * thus reset them as required.
6924 */
6925 psa_reset_key_attributes(&attributes);
6926
6927 psa_destroy_key(key);
6928 mbedtls_free(signature);
6929 PSA_DONE();
6930}
6931/* END_CASE */
6932
Gilles Peskine9911b022018-06-29 17:30:48 +02006933/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006934void verify_hash(int key_type_arg, data_t *key_data,
6935 int alg_arg, data_t *hash_data,
6936 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006937{
Ronald Cron5425a212020-08-04 14:58:35 +02006938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006939 psa_key_type_t key_type = key_type_arg;
6940 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6948 psa_set_key_algorithm(&attributes, alg);
6949 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006950
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6952 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 PSA_ASSERT(psa_verify_hash(key, alg,
6955 hash_data->x, hash_data->len,
6956 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006957
itayzafrir5c753392018-05-08 11:18:38 +03006958exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 psa_reset_key_attributes(&attributes);
6960 psa_destroy_key(key);
6961 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006962}
6963/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006964
Paul Elliott712d5122022-12-07 14:03:10 +00006965/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
6966void verify_hash_interruptible(int key_type_arg, data_t *key_data,
6967 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006968 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006969{
6970 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6971 psa_key_type_t key_type = key_type_arg;
6972 psa_algorithm_t alg = alg_arg;
6973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6974 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006975 uint32_t num_ops = 0;
6976 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006977 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006978 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006979 size_t min_completes = 0;
6980 size_t max_completes = 0;
6981
Paul Elliott712d5122022-12-07 14:03:10 +00006982 psa_verify_hash_interruptible_operation_t operation =
6983 psa_verify_hash_interruptible_operation_init();
6984
6985 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
6986
6987 PSA_ASSERT(psa_crypto_init());
6988
6989 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6990 psa_set_key_algorithm(&attributes, alg);
6991 psa_set_key_type(&attributes, key_type);
6992
6993 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6994 &key));
6995
Paul Elliott0c683352022-12-16 19:16:56 +00006996 psa_interruptible_set_max_ops(max_ops);
6997
Paul Elliott6f600372023-02-06 18:41:05 +00006998 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6999 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007000
Paul Elliott712d5122022-12-07 14:03:10 +00007001 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7002
7003 TEST_ASSERT(num_ops_prior == 0);
7004
7005 /* Start verification. */
7006 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7007 hash_data->x, hash_data->len,
7008 signature_data->x, signature_data->len)
7009 );
7010
7011 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7012
7013 TEST_ASSERT(num_ops_prior == 0);
7014
7015 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007016 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007017 status = psa_verify_hash_complete(&operation);
7018
Paul Elliott0c683352022-12-16 19:16:56 +00007019 num_completes++;
7020
Paul Elliott712d5122022-12-07 14:03:10 +00007021 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7022 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott712d5122022-12-07 14:03:10 +00007023 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007024
Paul Elliott712d5122022-12-07 14:03:10 +00007025 num_ops_prior = num_ops;
7026 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007027 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007028
7029 TEST_ASSERT(status == PSA_SUCCESS);
7030
Paul Elliott0c683352022-12-16 19:16:56 +00007031 TEST_LE_U(min_completes, num_completes);
7032 TEST_LE_U(num_completes, max_completes);
7033
Paul Elliott712d5122022-12-07 14:03:10 +00007034 PSA_ASSERT(psa_verify_hash_abort(&operation));
7035
Paul Elliott59ad9452022-12-18 15:09:02 +00007036 num_ops = psa_verify_hash_get_num_ops(&operation);
7037 TEST_ASSERT(num_ops == 0);
7038
Paul Elliott712d5122022-12-07 14:03:10 +00007039exit:
7040 psa_reset_key_attributes(&attributes);
7041 psa_destroy_key(key);
7042 PSA_DONE();
7043}
7044/* END_CASE */
7045
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007046/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007047void verify_hash_fail(int key_type_arg, data_t *key_data,
7048 int alg_arg, data_t *hash_data,
7049 data_t *signature_data,
7050 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007051{
Ronald Cron5425a212020-08-04 14:58:35 +02007052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007053 psa_key_type_t key_type = key_type_arg;
7054 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007055 psa_status_t actual_status;
7056 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007058
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7062 psa_set_key_algorithm(&attributes, alg);
7063 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7066 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007067
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 actual_status = psa_verify_hash(key, alg,
7069 hash_data->x, hash_data->len,
7070 signature_data->x, signature_data->len);
7071 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007072
7073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 psa_reset_key_attributes(&attributes);
7075 psa_destroy_key(key);
7076 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007077}
7078/* END_CASE */
7079
Paul Elliott91007972022-12-16 12:21:24 +00007080/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7081void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7082 int alg_arg, data_t *hash_data,
7083 data_t *signature_data,
7084 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007085 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007086 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007087{
7088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7089 psa_key_type_t key_type = key_type_arg;
7090 psa_algorithm_t alg = alg_arg;
7091 psa_status_t actual_status;
7092 psa_status_t expected_start_status = expected_start_status_arg;
7093 psa_status_t expected_complete_status = expected_complete_status_arg;
7094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007095 uint32_t num_ops = 0;
7096 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007097 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007098 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007099 size_t min_completes = 0;
7100 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007101 psa_verify_hash_interruptible_operation_t operation =
7102 psa_verify_hash_interruptible_operation_init();
7103
7104 PSA_ASSERT(psa_crypto_init());
7105
7106 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7107 psa_set_key_algorithm(&attributes, alg);
7108 psa_set_key_type(&attributes, key_type);
7109
7110 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7111 &key));
7112
Paul Elliott0c683352022-12-16 19:16:56 +00007113 psa_interruptible_set_max_ops(max_ops);
7114
Paul Elliott6f600372023-02-06 18:41:05 +00007115 interruptible_signverify_get_minmax_completes(max_ops,
7116 expected_complete_status,
7117 &min_completes,
7118 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007119
Paul Elliott91007972022-12-16 12:21:24 +00007120 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7121 TEST_ASSERT(num_ops_prior == 0);
7122
7123 /* Start verification. */
7124 actual_status = psa_verify_hash_start(&operation, key, alg,
7125 hash_data->x, hash_data->len,
7126 signature_data->x,
7127 signature_data->len);
7128
7129 TEST_EQUAL(actual_status, expected_start_status);
7130
Paul Elliottc9774412023-02-06 15:14:07 +00007131 if (expected_start_status != PSA_SUCCESS) {
7132 actual_status = psa_verify_hash_start(&operation, key, alg,
7133 hash_data->x, hash_data->len,
7134 signature_data->x,
7135 signature_data->len);
7136
7137 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7138 }
7139
Paul Elliott91007972022-12-16 12:21:24 +00007140 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7141 TEST_ASSERT(num_ops_prior == 0);
7142
Paul Elliott91007972022-12-16 12:21:24 +00007143 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007144 do {
Paul Elliott91007972022-12-16 12:21:24 +00007145 actual_status = psa_verify_hash_complete(&operation);
7146
Paul Elliott0c683352022-12-16 19:16:56 +00007147 num_completes++;
7148
Paul Elliott334d7262023-01-20 17:29:41 +00007149 if (actual_status == PSA_SUCCESS ||
7150 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007151 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott91007972022-12-16 12:21:24 +00007152 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007153
Paul Elliott91007972022-12-16 12:21:24 +00007154 num_ops_prior = num_ops;
7155 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007156 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007157
Paul Elliottc9774412023-02-06 15:14:07 +00007158 TEST_EQUAL(actual_status, expected_complete_status);
7159
7160 if (expected_complete_status != PSA_SUCCESS) {
7161 actual_status = psa_verify_hash_complete(&operation);
7162
Paul Elliott334d7262023-01-20 17:29:41 +00007163 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007164 }
7165
Paul Elliott0c683352022-12-16 19:16:56 +00007166 TEST_LE_U(min_completes, num_completes);
7167 TEST_LE_U(num_completes, max_completes);
7168
Paul Elliott91007972022-12-16 12:21:24 +00007169 PSA_ASSERT(psa_verify_hash_abort(&operation));
7170
Paul Elliott59ad9452022-12-18 15:09:02 +00007171 num_ops = psa_verify_hash_get_num_ops(&operation);
7172 TEST_ASSERT(num_ops == 0);
7173
Paul Elliott91007972022-12-16 12:21:24 +00007174exit:
7175 psa_reset_key_attributes(&attributes);
7176 psa_destroy_key(key);
7177 PSA_DONE();
7178}
7179/* END_CASE */
7180
Paul Elliott20a36062022-12-18 13:21:25 +00007181/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
7182void hash_interruptible_state_test(int key_type_arg, data_t *key_data,
7183 int alg_arg, data_t *input_data)
7184{
7185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7186 psa_key_type_t key_type = key_type_arg;
7187 psa_algorithm_t alg = alg_arg;
7188 size_t key_bits;
7189 unsigned char *signature = NULL;
7190 size_t signature_size;
7191 size_t signature_length = 0xdeadbeef;
7192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf9c91a72023-02-05 18:06:38 +00007193 uint8_t *input_buffer = NULL;
Paul Elliott20a36062022-12-18 13:21:25 +00007194 psa_sign_hash_interruptible_operation_t sign_operation =
7195 psa_sign_hash_interruptible_operation_init();
7196 psa_verify_hash_interruptible_operation_t verify_operation =
7197 psa_verify_hash_interruptible_operation_init();
7198
7199 PSA_ASSERT(psa_crypto_init());
7200
7201 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7202 PSA_KEY_USAGE_VERIFY_HASH);
7203 psa_set_key_algorithm(&attributes, alg);
7204 psa_set_key_type(&attributes, key_type);
7205
7206 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7207 &key));
7208 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7209 key_bits = psa_get_key_bits(&attributes);
7210
7211 /* Allocate a buffer which has the size advertised by the
7212 * library. */
7213 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7214 key_bits, alg);
7215 TEST_ASSERT(signature_size != 0);
7216 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7217 ASSERT_ALLOC(signature, signature_size);
7218
7219 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7220
7221 /* --- Attempt completes prior to starts --- */
7222 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7223 signature_size,
7224 &signature_length),
7225 PSA_ERROR_BAD_STATE);
7226
7227 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7228
7229 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7230 PSA_ERROR_BAD_STATE);
7231
7232 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7233
7234 /* --- Aborts in all other places. --- */
7235 psa_sign_hash_abort(&sign_operation);
7236
7237 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7238 input_data->x, input_data->len));
7239
7240 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7241
7242 psa_interruptible_set_max_ops(1);
7243
7244 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7245 input_data->x, input_data->len));
7246
7247 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7248 signature_size,
7249 &signature_length),
7250 PSA_OPERATION_INCOMPLETE);
7251
7252 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7253
7254 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7255
7256 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7257 input_data->x, input_data->len));
7258
7259 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7260 signature_size,
7261 &signature_length));
7262
7263 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7264
7265 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7266
7267 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7268 input_data->x, input_data->len,
7269 signature, signature_length));
7270
7271 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7272
7273 psa_interruptible_set_max_ops(1);
7274
7275 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7276 input_data->x, input_data->len,
7277 signature, signature_length));
7278
7279 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7280 PSA_OPERATION_INCOMPLETE);
7281
7282 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7283
7284 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7285
7286 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7287 input_data->x, input_data->len,
7288 signature, signature_length));
7289
7290 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7291
7292 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7293
7294 /* --- Attempt double starts. --- */
7295
7296 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7297 input_data->x, input_data->len));
7298
7299 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7300 input_data->x, input_data->len),
7301 PSA_ERROR_BAD_STATE);
7302
7303 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7304
7305 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7306 input_data->x, input_data->len,
7307 signature, signature_length));
7308
7309 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7310 input_data->x, input_data->len,
7311 signature, signature_length),
7312 PSA_ERROR_BAD_STATE);
7313
7314 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7315
7316 /* --- Ensure changing the max ops mid operation works (operation should
7317 * complete successfully after setting max ops to unlimited --- */
7318 psa_interruptible_set_max_ops(1);
7319
7320 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7321 input_data->x, input_data->len));
7322
7323 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7324 signature_size,
7325 &signature_length),
7326 PSA_OPERATION_INCOMPLETE);
7327
7328 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7329
7330 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7331 signature_size,
7332 &signature_length));
7333
7334 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7335
7336 psa_interruptible_set_max_ops(1);
7337
7338 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7339 input_data->x, input_data->len,
7340 signature, signature_length));
7341
7342 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7343 PSA_OPERATION_INCOMPLETE);
7344
7345 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7346
7347 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7348
7349 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7350
7351 /* --- Change function inputs mid run, to cause an error (sign only,
7352 * verify passes all inputs to start. --- */
7353
7354 psa_interruptible_set_max_ops(1);
7355
7356 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7357 input_data->x, input_data->len));
7358
7359 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7360 signature_size,
7361 &signature_length),
7362 PSA_OPERATION_INCOMPLETE);
7363
7364 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7365 0,
7366 &signature_length),
7367 PSA_ERROR_BUFFER_TOO_SMALL);
7368
Paul Elliottc9774412023-02-06 15:14:07 +00007369 /* And test that this invalidates the operation. */
7370 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7371 0,
7372 &signature_length),
7373 PSA_ERROR_BAD_STATE);
7374
Paul Elliott20a36062022-12-18 13:21:25 +00007375 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7376
Paul Elliottf9c91a72023-02-05 18:06:38 +00007377 /* Trash the hash buffer in between start and complete, to ensure
7378 * no reliance on external buffers. */
7379 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7380
7381 input_buffer = mbedtls_calloc(1, input_data->len);
7382 TEST_ASSERT(input_buffer != NULL);
7383
7384 memcpy(input_buffer, input_data->x, input_data->len);
7385
7386 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7387 input_buffer, input_data->len));
7388
7389 memset(input_buffer, '!', input_data->len);
7390 mbedtls_free(input_buffer);
7391 input_buffer = NULL;
7392
7393 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7394 signature_size,
7395 &signature_length));
7396
7397 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7398
7399 input_buffer = mbedtls_calloc(1, input_data->len);
7400 TEST_ASSERT(input_buffer != NULL);
7401
7402 memcpy(input_buffer, input_data->x, input_data->len);
7403
7404 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7405 input_buffer, input_data->len,
7406 signature, signature_length));
7407
7408 memset(input_buffer, '!', input_data->len);
7409 mbedtls_free(input_buffer);
7410 input_buffer = NULL;
7411
7412 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7413
7414 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7415
Paul Elliott20a36062022-12-18 13:21:25 +00007416exit:
7417 /*
7418 * Key attributes may have been returned by psa_get_key_attributes()
7419 * thus reset them as required.
7420 */
7421 psa_reset_key_attributes(&attributes);
7422
7423 psa_destroy_key(key);
7424 mbedtls_free(signature);
7425 PSA_DONE();
7426}
7427/* END_CASE */
7428
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007429/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007430void sign_message_deterministic(int key_type_arg,
7431 data_t *key_data,
7432 int alg_arg,
7433 data_t *input_data,
7434 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007435{
7436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7437 psa_key_type_t key_type = key_type_arg;
7438 psa_algorithm_t alg = alg_arg;
7439 size_t key_bits;
7440 unsigned char *signature = NULL;
7441 size_t signature_size;
7442 size_t signature_length = 0xdeadbeef;
7443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007446
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7448 psa_set_key_algorithm(&attributes, alg);
7449 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7452 &key));
7453 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7454 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007455
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7457 TEST_ASSERT(signature_size != 0);
7458 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7459 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007460
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 PSA_ASSERT(psa_sign_message(key, alg,
7462 input_data->x, input_data->len,
7463 signature, signature_size,
7464 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007465
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 ASSERT_COMPARE(output_data->x, output_data->len,
7467 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007468
7469exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007471
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 psa_destroy_key(key);
7473 mbedtls_free(signature);
7474 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007475
7476}
7477/* END_CASE */
7478
7479/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007480void sign_message_fail(int key_type_arg,
7481 data_t *key_data,
7482 int alg_arg,
7483 data_t *input_data,
7484 int signature_size_arg,
7485 int expected_status_arg)
7486{
7487 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7488 psa_key_type_t key_type = key_type_arg;
7489 psa_algorithm_t alg = alg_arg;
7490 size_t signature_size = signature_size_arg;
7491 psa_status_t actual_status;
7492 psa_status_t expected_status = expected_status_arg;
7493 unsigned char *signature = NULL;
7494 size_t signature_length = 0xdeadbeef;
7495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7496
7497 ASSERT_ALLOC(signature, signature_size);
7498
7499 PSA_ASSERT(psa_crypto_init());
7500
7501 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7502 psa_set_key_algorithm(&attributes, alg);
7503 psa_set_key_type(&attributes, key_type);
7504
7505 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7506 &key));
7507
7508 actual_status = psa_sign_message(key, alg,
7509 input_data->x, input_data->len,
7510 signature, signature_size,
7511 &signature_length);
7512 TEST_EQUAL(actual_status, expected_status);
7513 /* The value of *signature_length is unspecified on error, but
7514 * whatever it is, it should be less than signature_size, so that
7515 * if the caller tries to read *signature_length bytes without
7516 * checking the error code then they don't overflow a buffer. */
7517 TEST_LE_U(signature_length, signature_size);
7518
7519exit:
7520 psa_reset_key_attributes(&attributes);
7521 psa_destroy_key(key);
7522 mbedtls_free(signature);
7523 PSA_DONE();
7524}
7525/* END_CASE */
7526
7527/* BEGIN_CASE */
7528void sign_verify_message(int key_type_arg,
7529 data_t *key_data,
7530 int alg_arg,
7531 data_t *input_data)
7532{
7533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7534 psa_key_type_t key_type = key_type_arg;
7535 psa_algorithm_t alg = alg_arg;
7536 size_t key_bits;
7537 unsigned char *signature = NULL;
7538 size_t signature_size;
7539 size_t signature_length = 0xdeadbeef;
7540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7541
7542 PSA_ASSERT(psa_crypto_init());
7543
7544 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7545 PSA_KEY_USAGE_VERIFY_MESSAGE);
7546 psa_set_key_algorithm(&attributes, alg);
7547 psa_set_key_type(&attributes, key_type);
7548
7549 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7550 &key));
7551 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7552 key_bits = psa_get_key_bits(&attributes);
7553
7554 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7555 TEST_ASSERT(signature_size != 0);
7556 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7557 ASSERT_ALLOC(signature, signature_size);
7558
7559 PSA_ASSERT(psa_sign_message(key, alg,
7560 input_data->x, input_data->len,
7561 signature, signature_size,
7562 &signature_length));
7563 TEST_LE_U(signature_length, signature_size);
7564 TEST_ASSERT(signature_length > 0);
7565
7566 PSA_ASSERT(psa_verify_message(key, alg,
7567 input_data->x, input_data->len,
7568 signature, signature_length));
7569
7570 if (input_data->len != 0) {
7571 /* Flip a bit in the input and verify that the signature is now
7572 * detected as invalid. Flip a bit at the beginning, not at the end,
7573 * because ECDSA may ignore the last few bits of the input. */
7574 input_data->x[0] ^= 1;
7575 TEST_EQUAL(psa_verify_message(key, alg,
7576 input_data->x, input_data->len,
7577 signature, signature_length),
7578 PSA_ERROR_INVALID_SIGNATURE);
7579 }
7580
7581exit:
7582 psa_reset_key_attributes(&attributes);
7583
7584 psa_destroy_key(key);
7585 mbedtls_free(signature);
7586 PSA_DONE();
7587}
7588/* END_CASE */
7589
7590/* BEGIN_CASE */
7591void verify_message(int key_type_arg,
7592 data_t *key_data,
7593 int alg_arg,
7594 data_t *input_data,
7595 data_t *signature_data)
7596{
7597 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7598 psa_key_type_t key_type = key_type_arg;
7599 psa_algorithm_t alg = alg_arg;
7600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7601
7602 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7603
7604 PSA_ASSERT(psa_crypto_init());
7605
7606 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7607 psa_set_key_algorithm(&attributes, alg);
7608 psa_set_key_type(&attributes, key_type);
7609
7610 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7611 &key));
7612
7613 PSA_ASSERT(psa_verify_message(key, alg,
7614 input_data->x, input_data->len,
7615 signature_data->x, signature_data->len));
7616
7617exit:
7618 psa_reset_key_attributes(&attributes);
7619 psa_destroy_key(key);
7620 PSA_DONE();
7621}
7622/* END_CASE */
7623
7624/* BEGIN_CASE */
7625void verify_message_fail(int key_type_arg,
7626 data_t *key_data,
7627 int alg_arg,
7628 data_t *hash_data,
7629 data_t *signature_data,
7630 int expected_status_arg)
7631{
7632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7633 psa_key_type_t key_type = key_type_arg;
7634 psa_algorithm_t alg = alg_arg;
7635 psa_status_t actual_status;
7636 psa_status_t expected_status = expected_status_arg;
7637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7638
7639 PSA_ASSERT(psa_crypto_init());
7640
7641 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
7642 psa_set_key_algorithm(&attributes, alg);
7643 psa_set_key_type(&attributes, key_type);
7644
7645 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7646 &key));
7647
7648 actual_status = psa_verify_message(key, alg,
7649 hash_data->x, hash_data->len,
7650 signature_data->x,
7651 signature_data->len);
7652 TEST_EQUAL(actual_status, expected_status);
7653
7654exit:
7655 psa_reset_key_attributes(&attributes);
7656 psa_destroy_key(key);
7657 PSA_DONE();
7658}
7659/* END_CASE */
7660
7661/* BEGIN_CASE */
7662void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02007663 data_t *key_data,
7664 int alg_arg,
7665 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 data_t *label,
7667 int expected_output_length_arg,
7668 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02007669{
Ronald Cron5425a212020-08-04 14:58:35 +02007670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007671 psa_key_type_t key_type = key_type_arg;
7672 psa_algorithm_t alg = alg_arg;
7673 size_t expected_output_length = expected_output_length_arg;
7674 size_t key_bits;
7675 unsigned char *output = NULL;
7676 size_t output_size;
7677 size_t output_length = ~0;
7678 psa_status_t actual_status;
7679 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007681
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007683
Gilles Peskine656896e2018-06-29 19:12:28 +02007684 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
7686 psa_set_key_algorithm(&attributes, alg);
7687 psa_set_key_type(&attributes, key_type);
7688 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7689 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02007690
7691 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7693 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7696 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7697 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02007698
7699 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 actual_status = psa_asymmetric_encrypt(key, alg,
7701 input_data->x, input_data->len,
7702 label->x, label->len,
7703 output, output_size,
7704 &output_length);
7705 TEST_EQUAL(actual_status, expected_status);
7706 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine656896e2018-06-29 19:12:28 +02007707
Gilles Peskine68428122018-06-30 18:42:41 +02007708 /* If the label is empty, the test framework puts a non-null pointer
7709 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007711 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 if (output_size != 0) {
7713 memset(output, 0, output_size);
7714 }
7715 actual_status = psa_asymmetric_encrypt(key, alg,
7716 input_data->x, input_data->len,
7717 NULL, label->len,
7718 output, output_size,
7719 &output_length);
7720 TEST_EQUAL(actual_status, expected_status);
7721 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007722 }
7723
Gilles Peskine656896e2018-06-29 19:12:28 +02007724exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007725 /*
7726 * Key attributes may have been returned by psa_get_key_attributes()
7727 * thus reset them as required.
7728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007729 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007730
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 psa_destroy_key(key);
7732 mbedtls_free(output);
7733 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02007734}
7735/* END_CASE */
7736
7737/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007738void asymmetric_encrypt_decrypt(int key_type_arg,
7739 data_t *key_data,
7740 int alg_arg,
7741 data_t *input_data,
7742 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007743{
Ronald Cron5425a212020-08-04 14:58:35 +02007744 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007745 psa_key_type_t key_type = key_type_arg;
7746 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007747 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007748 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007749 size_t output_size;
7750 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007751 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007752 size_t output2_size;
7753 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007754 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007755
Gilles Peskine449bd832023-01-11 14:50:10 +01007756 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007757
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
7759 psa_set_key_algorithm(&attributes, alg);
7760 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007761
Gilles Peskine449bd832023-01-11 14:50:10 +01007762 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7763 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007764
7765 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7767 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007768
Gilles Peskine449bd832023-01-11 14:50:10 +01007769 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7770 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
7771 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007772
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007773 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 TEST_LE_U(output2_size,
7775 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
7776 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7777 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007778
Gilles Peskineeebd7382018-06-08 18:11:54 +02007779 /* We test encryption by checking that encrypt-then-decrypt gives back
7780 * the original plaintext because of the non-optional random
7781 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
7783 input_data->x, input_data->len,
7784 label->x, label->len,
7785 output, output_size,
7786 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007787 /* We don't know what ciphertext length to expect, but check that
7788 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007789 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007790
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7792 output, output_length,
7793 label->x, label->len,
7794 output2, output2_size,
7795 &output2_length));
7796 ASSERT_COMPARE(input_data->x, input_data->len,
7797 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007798
7799exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007800 /*
7801 * Key attributes may have been returned by psa_get_key_attributes()
7802 * thus reset them as required.
7803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007805
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 psa_destroy_key(key);
7807 mbedtls_free(output);
7808 mbedtls_free(output2);
7809 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007810}
7811/* END_CASE */
7812
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007813/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007814void asymmetric_decrypt(int key_type_arg,
7815 data_t *key_data,
7816 int alg_arg,
7817 data_t *input_data,
7818 data_t *label,
7819 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007820{
Ronald Cron5425a212020-08-04 14:58:35 +02007821 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007822 psa_key_type_t key_type = key_type_arg;
7823 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007824 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007825 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007826 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007827 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007829
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007831
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7833 psa_set_key_algorithm(&attributes, alg);
7834 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007835
Gilles Peskine449bd832023-01-11 14:50:10 +01007836 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7837 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007838
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7840 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007841
7842 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7844 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7845 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007846
Gilles Peskine449bd832023-01-11 14:50:10 +01007847 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7848 input_data->x, input_data->len,
7849 label->x, label->len,
7850 output,
7851 output_size,
7852 &output_length));
7853 ASSERT_COMPARE(expected_data->x, expected_data->len,
7854 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007855
Gilles Peskine68428122018-06-30 18:42:41 +02007856 /* If the label is empty, the test framework puts a non-null pointer
7857 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007859 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 if (output_size != 0) {
7861 memset(output, 0, output_size);
7862 }
7863 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7864 input_data->x, input_data->len,
7865 NULL, label->len,
7866 output,
7867 output_size,
7868 &output_length));
7869 ASSERT_COMPARE(expected_data->x, expected_data->len,
7870 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007871 }
7872
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007873exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007874 psa_reset_key_attributes(&attributes);
7875 psa_destroy_key(key);
7876 mbedtls_free(output);
7877 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007878}
7879/* END_CASE */
7880
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007881/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007882void asymmetric_decrypt_fail(int key_type_arg,
7883 data_t *key_data,
7884 int alg_arg,
7885 data_t *input_data,
7886 data_t *label,
7887 int output_size_arg,
7888 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007889{
Ronald Cron5425a212020-08-04 14:58:35 +02007890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007891 psa_key_type_t key_type = key_type_arg;
7892 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007893 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007894 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007895 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007896 psa_status_t actual_status;
7897 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7905 psa_set_key_algorithm(&attributes, alg);
7906 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7909 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007910
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 actual_status = psa_asymmetric_decrypt(key, alg,
7912 input_data->x, input_data->len,
7913 label->x, label->len,
7914 output, output_size,
7915 &output_length);
7916 TEST_EQUAL(actual_status, expected_status);
7917 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007918
Gilles Peskine68428122018-06-30 18:42:41 +02007919 /* If the label is empty, the test framework puts a non-null pointer
7920 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007922 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 if (output_size != 0) {
7924 memset(output, 0, output_size);
7925 }
7926 actual_status = psa_asymmetric_decrypt(key, alg,
7927 input_data->x, input_data->len,
7928 NULL, label->len,
7929 output, output_size,
7930 &output_length);
7931 TEST_EQUAL(actual_status, expected_status);
7932 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02007933 }
7934
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007935exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 psa_reset_key_attributes(&attributes);
7937 psa_destroy_key(key);
7938 mbedtls_free(output);
7939 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007940}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007941/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007942
7943/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007944void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00007945{
7946 /* Test each valid way of initializing the object, except for `= {0}`, as
7947 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7948 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007949 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007950 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01007951 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007952 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7953 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007954
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007956
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007957 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007958 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
7959 PSA_ERROR_BAD_STATE);
7960 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
7961 PSA_ERROR_BAD_STATE);
7962 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
7963 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007964
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007965 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 PSA_ASSERT(psa_key_derivation_abort(&func));
7967 PSA_ASSERT(psa_key_derivation_abort(&init));
7968 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007969}
7970/* END_CASE */
7971
Janos Follath16de4a42019-06-13 16:32:24 +01007972/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007973void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02007974{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007975 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007976 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007977 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007978
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02007980
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7982 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02007983
7984exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 psa_key_derivation_abort(&operation);
7986 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02007987}
7988/* END_CASE */
7989
Janos Follathaf3c2a02019-06-12 12:34:34 +01007990/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007991void derive_set_capacity(int alg_arg, int capacity_arg,
7992 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01007993{
7994 psa_algorithm_t alg = alg_arg;
7995 size_t capacity = capacity_arg;
7996 psa_status_t expected_status = expected_status_arg;
7997 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7998
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008000
Gilles Peskine449bd832023-01-11 14:50:10 +01008001 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008002
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8004 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008005
8006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008007 psa_key_derivation_abort(&operation);
8008 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008009}
8010/* END_CASE */
8011
8012/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008013void derive_input(int alg_arg,
8014 int step_arg1, int key_type_arg1, data_t *input1,
8015 int expected_status_arg1,
8016 int step_arg2, int key_type_arg2, data_t *input2,
8017 int expected_status_arg2,
8018 int step_arg3, int key_type_arg3, data_t *input3,
8019 int expected_status_arg3,
8020 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008021{
8022 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8024 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8025 psa_status_t expected_statuses[] = { expected_status_arg1,
8026 expected_status_arg2,
8027 expected_status_arg3 };
8028 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008029 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8030 MBEDTLS_SVC_KEY_ID_INIT,
8031 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008032 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8034 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008035 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008036 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008037 psa_status_t expected_output_status = expected_output_status_arg;
8038 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008039
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008041
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8043 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008044
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008046
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8048 mbedtls_test_set_step(i);
8049 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008050 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008051 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8052 psa_set_key_type(&attributes, key_types[i]);
8053 PSA_ASSERT(psa_import_key(&attributes,
8054 inputs[i]->x, inputs[i]->len,
8055 &keys[i]));
8056 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8057 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008058 // When taking a private key as secret input, use key agreement
8059 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008060 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8061 &operation, keys[i]),
8062 expected_statuses[i]);
8063 } else {
8064 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8065 keys[i]),
8066 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 } else {
8069 TEST_EQUAL(psa_key_derivation_input_bytes(
8070 &operation, steps[i],
8071 inputs[i]->x, inputs[i]->len),
8072 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008073 }
8074 }
8075
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 if (output_key_type != PSA_KEY_TYPE_NONE) {
8077 psa_reset_key_attributes(&attributes);
8078 psa_set_key_type(&attributes, output_key_type);
8079 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008080 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008081 psa_key_derivation_output_key(&attributes, &operation,
8082 &output_key);
8083 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008084 uint8_t buffer[1];
8085 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008086 psa_key_derivation_output_bytes(&operation,
8087 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008089 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008090
Janos Follathaf3c2a02019-06-12 12:34:34 +01008091exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 psa_key_derivation_abort(&operation);
8093 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8094 psa_destroy_key(keys[i]);
8095 }
8096 psa_destroy_key(output_key);
8097 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008098}
8099/* END_CASE */
8100
Janos Follathd958bb72019-07-03 15:02:16 +01008101/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008102void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008103{
Janos Follathd958bb72019-07-03 15:02:16 +01008104 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008106 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008107 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008108 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008110 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008112 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008114 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8115 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008116 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008118
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008120
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8122 psa_set_key_algorithm(&attributes, alg);
8123 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008124
Gilles Peskine449bd832023-01-11 14:50:10 +01008125 PSA_ASSERT(psa_import_key(&attributes,
8126 key_data, sizeof(key_data),
8127 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008128
8129 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8131 input1, input1_length,
8132 input2, input2_length,
8133 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008136
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008137 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8139 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008140
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008142
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8144 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008145
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 psa_key_derivation_abort(&operation);
8148 psa_destroy_key(key);
8149 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008150}
8151/* END_CASE */
8152
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008153/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008154void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008155{
8156 uint8_t output_buffer[16];
8157 size_t buffer_size = 16;
8158 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008159 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008160
Gilles Peskine449bd832023-01-11 14:50:10 +01008161 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8162 output_buffer, buffer_size)
8163 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008164
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8166 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008167
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008169
Gilles Peskine449bd832023-01-11 14:50:10 +01008170 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8171 output_buffer, buffer_size)
8172 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008173
Gilles Peskine449bd832023-01-11 14:50:10 +01008174 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8175 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008176
8177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008179}
8180/* END_CASE */
8181
8182/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008183void derive_output(int alg_arg,
8184 int step1_arg, data_t *input1, int expected_status_arg1,
8185 int step2_arg, data_t *input2, int expected_status_arg2,
8186 int step3_arg, data_t *input3, int expected_status_arg3,
8187 int step4_arg, data_t *input4, int expected_status_arg4,
8188 data_t *key_agreement_peer_key,
8189 int requested_capacity_arg,
8190 data_t *expected_output1,
8191 data_t *expected_output2,
8192 int other_key_input_type,
8193 int key_input_type,
8194 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008195{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008196 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008197 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8198 data_t *inputs[] = { input1, input2, input3, input4 };
8199 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8200 MBEDTLS_SVC_KEY_ID_INIT,
8201 MBEDTLS_SVC_KEY_ID_INIT,
8202 MBEDTLS_SVC_KEY_ID_INIT };
8203 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8204 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008205 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008206 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008207 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008208 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008209 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008210 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008211 size_t output_buffer_size = 0;
8212 uint8_t *output_buffer = NULL;
8213 size_t expected_capacity;
8214 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008215 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8216 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8217 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8218 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008219 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008220 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008221 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8224 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008225 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 }
8227 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008228 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 ASSERT_ALLOC(output_buffer, output_buffer_size);
8232 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008233
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008234 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8236 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8237 requested_capacity));
8238 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8239 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008240 case 0:
8241 break;
8242 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008244 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 TEST_EQUAL(psa_key_derivation_input_bytes(
8246 &operation, steps[i],
8247 inputs[i]->x, inputs[i]->len),
8248 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008249
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008253 break;
8254 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8256 psa_set_key_algorithm(&attributes1, alg);
8257 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008258
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 PSA_ASSERT(psa_import_key(&attributes1,
8260 inputs[i]->x, inputs[i]->len,
8261 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008262
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8264 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8265 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8266 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008267 }
8268
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8270 steps[i],
8271 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008272 break;
8273 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008275 break;
8276 }
8277 break;
8278 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008280 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8282 steps[i],
8283 inputs[i]->x,
8284 inputs[i]->len),
8285 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008286 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008287 case 1: // input key, type DERIVE
8288 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8290 psa_set_key_algorithm(&attributes2, alg);
8291 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008292
8293 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008294 if (other_key_input_type == 11) {
8295 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8296 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008297
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 PSA_ASSERT(psa_import_key(&attributes2,
8299 inputs[i]->x, inputs[i]->len,
8300 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8303 steps[i],
8304 keys[i]),
8305 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008306 break;
8307 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8309 psa_set_key_algorithm(&attributes3, alg);
8310 psa_set_key_type(&attributes3,
8311 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008312
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 PSA_ASSERT(psa_import_key(&attributes3,
8314 inputs[i]->x, inputs[i]->len,
8315 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008316
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 TEST_EQUAL(psa_key_derivation_key_agreement(
8318 &operation,
8319 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8320 keys[i], key_agreement_peer_key->x,
8321 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008322 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008323 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008325 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008326 }
8327
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008329 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008331 break;
8332 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 TEST_EQUAL(psa_key_derivation_input_bytes(
8334 &operation, steps[i],
8335 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008336
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008338 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008340 break;
8341 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008342 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008343
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8345 &current_capacity));
8346 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008347 expected_capacity = requested_capacity;
8348
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008350 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8351
8352 /* For output key derivation secret must be provided using
8353 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008355 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008357
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8359 psa_set_key_algorithm(&attributes4, alg);
8360 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8361 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008362
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8364 &derived_key), expected_status);
8365 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008366 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008368 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 status = psa_key_derivation_output_bytes(&operation,
8370 output_buffer, output_sizes[i]);
8371 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008372 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 TEST_ASSERT(status == PSA_SUCCESS ||
8374 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008375 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 } else if (expected_capacity == 0 ||
8377 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008378 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008380 expected_capacity = 0;
8381 continue;
8382 }
8383 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 PSA_ASSERT(status);
8385 if (output_sizes[i] != 0) {
8386 ASSERT_COMPARE(output_buffer, output_sizes[i],
8387 expected_outputs[i], output_sizes[i]);
8388 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008389 /* Check the operation status. */
8390 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8392 &current_capacity));
8393 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008394 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008395 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008397
8398exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 mbedtls_free(output_buffer);
8400 psa_key_derivation_abort(&operation);
8401 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8402 psa_destroy_key(keys[i]);
8403 }
8404 psa_destroy_key(derived_key);
8405 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008406}
8407/* END_CASE */
8408
8409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008410void derive_full(int alg_arg,
8411 data_t *key_data,
8412 data_t *input1,
8413 data_t *input2,
8414 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008415{
Ronald Cron5425a212020-08-04 14:58:35 +02008416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008417 psa_algorithm_t alg = alg_arg;
8418 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008419 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008420 unsigned char output_buffer[16];
8421 size_t expected_capacity = requested_capacity;
8422 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008424
Gilles Peskine449bd832023-01-11 14:50:10 +01008425 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008426
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8428 psa_set_key_algorithm(&attributes, alg);
8429 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008430
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8432 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008433
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8435 input1->x, input1->len,
8436 input2->x, input2->len,
8437 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008438 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008440
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8442 &current_capacity));
8443 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008444
8445 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 while (current_capacity > 0) {
8447 size_t read_size = sizeof(output_buffer);
8448 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008449 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 }
8451 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8452 output_buffer,
8453 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008454 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8456 &current_capacity));
8457 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008458 }
8459
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008460 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8462 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008465
8466exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008467 psa_key_derivation_abort(&operation);
8468 psa_destroy_key(key);
8469 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008470}
8471/* END_CASE */
8472
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008473/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008474void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8475 int derivation_step,
8476 int capacity, int expected_capacity_status_arg,
8477 data_t *expected_output,
8478 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008479{
8480 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8481 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008482 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008483 uint8_t *output_buffer = NULL;
8484 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008485 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8486 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8487 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008488
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 ASSERT_ALLOC(output_buffer, expected_output->len);
8490 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008491
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8493 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8494 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008495
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8497 step, input->x, input->len),
8498 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008499
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008501 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008503
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8505 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008506
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 TEST_EQUAL(status, expected_output_status);
8508 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8509 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8510 expected_output->len);
8511 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008512
8513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 mbedtls_free(output_buffer);
8515 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008516 PSA_DONE();
8517}
8518/* END_CASE */
8519
Janos Follathe60c9052019-07-03 13:51:30 +01008520/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008521void derive_key_exercise(int alg_arg,
8522 data_t *key_data,
8523 data_t *input1,
8524 data_t *input2,
8525 int derived_type_arg,
8526 int derived_bits_arg,
8527 int derived_usage_arg,
8528 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008529{
Ronald Cron5425a212020-08-04 14:58:35 +02008530 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8531 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008532 psa_algorithm_t alg = alg_arg;
8533 psa_key_type_t derived_type = derived_type_arg;
8534 size_t derived_bits = derived_bits_arg;
8535 psa_key_usage_t derived_usage = derived_usage_arg;
8536 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008538 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008540 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008541
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008543
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8545 psa_set_key_algorithm(&attributes, alg);
8546 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8547 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8548 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008549
8550 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008551 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8552 input1->x, input1->len,
8553 input2->x, input2->len,
8554 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 }
Janos Follathe60c9052019-07-03 13:51:30 +01008557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 psa_set_key_usage_flags(&attributes, derived_usage);
8559 psa_set_key_algorithm(&attributes, derived_alg);
8560 psa_set_key_type(&attributes, derived_type);
8561 psa_set_key_bits(&attributes, derived_bits);
8562 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8563 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008564
8565 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
8567 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
8568 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008569
8570 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02008572 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02008574
8575exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008576 /*
8577 * Key attributes may have been returned by psa_get_key_attributes()
8578 * thus reset them as required.
8579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008581
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 psa_key_derivation_abort(&operation);
8583 psa_destroy_key(base_key);
8584 psa_destroy_key(derived_key);
8585 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008586}
8587/* END_CASE */
8588
Janos Follath42fd8882019-07-03 14:17:09 +01008589/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008590void derive_key_export(int alg_arg,
8591 data_t *key_data,
8592 data_t *input1,
8593 data_t *input2,
8594 int bytes1_arg,
8595 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008596{
Ronald Cron5425a212020-08-04 14:58:35 +02008597 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8598 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008599 psa_algorithm_t alg = alg_arg;
8600 size_t bytes1 = bytes1_arg;
8601 size_t bytes2 = bytes2_arg;
8602 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008603 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008604 uint8_t *output_buffer = NULL;
8605 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008606 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8607 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008608 size_t length;
8609
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 ASSERT_ALLOC(output_buffer, capacity);
8611 ASSERT_ALLOC(export_buffer, capacity);
8612 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008613
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8615 psa_set_key_algorithm(&base_attributes, alg);
8616 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8617 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8618 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008619
8620 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8622 input1->x, input1->len,
8623 input2->x, input2->len,
8624 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 }
Janos Follath42fd8882019-07-03 14:17:09 +01008627
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8629 output_buffer,
8630 capacity));
8631 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008632
8633 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8635 input1->x, input1->len,
8636 input2->x, input2->len,
8637 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01008638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 }
Janos Follath42fd8882019-07-03 14:17:09 +01008640
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8642 psa_set_key_algorithm(&derived_attributes, 0);
8643 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
8644 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
8645 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8646 &derived_key));
8647 PSA_ASSERT(psa_export_key(derived_key,
8648 export_buffer, bytes1,
8649 &length));
8650 TEST_EQUAL(length, bytes1);
8651 PSA_ASSERT(psa_destroy_key(derived_key));
8652 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
8653 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8654 &derived_key));
8655 PSA_ASSERT(psa_export_key(derived_key,
8656 export_buffer + bytes1, bytes2,
8657 &length));
8658 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008659
8660 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
8662 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02008663
8664exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 mbedtls_free(output_buffer);
8666 mbedtls_free(export_buffer);
8667 psa_key_derivation_abort(&operation);
8668 psa_destroy_key(base_key);
8669 psa_destroy_key(derived_key);
8670 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02008671}
8672/* END_CASE */
8673
8674/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008675void derive_key_type(int alg_arg,
8676 data_t *key_data,
8677 data_t *input1,
8678 data_t *input2,
8679 int key_type_arg, int bits_arg,
8680 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008681{
8682 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8683 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8684 const psa_algorithm_t alg = alg_arg;
8685 const psa_key_type_t key_type = key_type_arg;
8686 const size_t bits = bits_arg;
8687 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8688 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008690 uint8_t *export_buffer = NULL;
8691 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8692 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8693 size_t export_length;
8694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 ASSERT_ALLOC(export_buffer, export_buffer_size);
8696 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008697
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8699 psa_set_key_algorithm(&base_attributes, alg);
8700 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8701 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8702 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008703
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008705 &operation, base_key, alg,
8706 input1->x, input1->len,
8707 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008709 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8713 psa_set_key_algorithm(&derived_attributes, 0);
8714 psa_set_key_type(&derived_attributes, key_type);
8715 psa_set_key_bits(&derived_attributes, bits);
8716 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
8717 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008718
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 PSA_ASSERT(psa_export_key(derived_key,
8720 export_buffer, export_buffer_size,
8721 &export_length));
8722 ASSERT_COMPARE(export_buffer, export_length,
8723 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008724
8725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 mbedtls_free(export_buffer);
8727 psa_key_derivation_abort(&operation);
8728 psa_destroy_key(base_key);
8729 psa_destroy_key(derived_key);
8730 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008731}
8732/* END_CASE */
8733
8734/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008735void derive_key(int alg_arg,
8736 data_t *key_data, data_t *input1, data_t *input2,
8737 int type_arg, int bits_arg,
8738 int expected_status_arg,
8739 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02008740{
Ronald Cron5425a212020-08-04 14:58:35 +02008741 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8742 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008743 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008744 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008745 size_t bits = bits_arg;
8746 psa_status_t expected_status = expected_status_arg;
8747 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8748 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8749 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8750
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02008752
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
8754 psa_set_key_algorithm(&base_attributes, alg);
8755 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8756 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
8757 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02008758
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8760 input1->x, input1->len,
8761 input2->x, input2->len,
8762 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02008763 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 }
Gilles Peskinec744d992019-07-30 17:26:54 +02008765
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
8767 psa_set_key_algorithm(&derived_attributes, 0);
8768 psa_set_key_type(&derived_attributes, type);
8769 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008770
8771 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 psa_key_derivation_output_key(&derived_attributes,
8773 &operation,
8774 &derived_key);
8775 if (is_large_output > 0) {
8776 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8777 }
8778 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02008779
8780exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 psa_key_derivation_abort(&operation);
8782 psa_destroy_key(base_key);
8783 psa_destroy_key(derived_key);
8784 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02008785}
8786/* END_CASE */
8787
8788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008789void key_agreement_setup(int alg_arg,
8790 int our_key_type_arg, int our_key_alg_arg,
8791 data_t *our_key_data, data_t *peer_key_data,
8792 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02008793{
Ronald Cron5425a212020-08-04 14:58:35 +02008794 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008795 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008796 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008797 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008798 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008800 psa_status_t expected_status = expected_status_arg;
8801 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008802
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02008804
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8806 psa_set_key_algorithm(&attributes, our_key_alg);
8807 psa_set_key_type(&attributes, our_key_type);
8808 PSA_ASSERT(psa_import_key(&attributes,
8809 our_key_data->x, our_key_data->len,
8810 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008811
Gilles Peskine77f40d82019-04-11 21:27:06 +02008812 /* The tests currently include inputs that should fail at either step.
8813 * Test cases that fail at the setup step should be changed to call
8814 * key_derivation_setup instead, and this function should be renamed
8815 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 status = psa_key_derivation_setup(&operation, alg);
8817 if (status == PSA_SUCCESS) {
8818 TEST_EQUAL(psa_key_derivation_key_agreement(
8819 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8820 our_key,
8821 peer_key_data->x, peer_key_data->len),
8822 expected_status);
8823 } else {
8824 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008825 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008826
8827exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 psa_key_derivation_abort(&operation);
8829 psa_destroy_key(our_key);
8830 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008831}
8832/* END_CASE */
8833
8834/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008835void raw_key_agreement(int alg_arg,
8836 int our_key_type_arg, data_t *our_key_data,
8837 data_t *peer_key_data,
8838 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008839{
Ronald Cron5425a212020-08-04 14:58:35 +02008840 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008841 psa_algorithm_t alg = alg_arg;
8842 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008844 unsigned char *output = NULL;
8845 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008846 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008847
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02008849
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8851 psa_set_key_algorithm(&attributes, alg);
8852 psa_set_key_type(&attributes, our_key_type);
8853 PSA_ASSERT(psa_import_key(&attributes,
8854 our_key_data->x, our_key_data->len,
8855 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02008856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
8858 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008859
Gilles Peskine992bee82022-04-13 23:25:52 +02008860 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 TEST_LE_U(expected_output->len,
8862 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
8863 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
8864 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02008865
8866 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 ASSERT_ALLOC(output, expected_output->len);
8868 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8869 peer_key_data->x, peer_key_data->len,
8870 output, expected_output->len,
8871 &output_length));
8872 ASSERT_COMPARE(output, output_length,
8873 expected_output->x, expected_output->len);
8874 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008875 output = NULL;
8876 output_length = ~0;
8877
8878 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 ASSERT_ALLOC(output, expected_output->len + 1);
8880 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8881 peer_key_data->x, peer_key_data->len,
8882 output, expected_output->len + 1,
8883 &output_length));
8884 ASSERT_COMPARE(output, output_length,
8885 expected_output->x, expected_output->len);
8886 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008887 output = NULL;
8888 output_length = ~0;
8889
8890 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 ASSERT_ALLOC(output, expected_output->len - 1);
8892 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
8893 peer_key_data->x, peer_key_data->len,
8894 output, expected_output->len - 1,
8895 &output_length),
8896 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02008897 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 TEST_LE_U(output_length, expected_output->len - 1);
8899 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008900 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008901
8902exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 mbedtls_free(output);
8904 psa_destroy_key(our_key);
8905 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02008906}
8907/* END_CASE */
8908
8909/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008910void key_agreement_capacity(int alg_arg,
8911 int our_key_type_arg, data_t *our_key_data,
8912 data_t *peer_key_data,
8913 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02008914{
Ronald Cron5425a212020-08-04 14:58:35 +02008915 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008916 psa_algorithm_t alg = alg_arg;
8917 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008918 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008920 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008921 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008922
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008924
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8926 psa_set_key_algorithm(&attributes, alg);
8927 psa_set_key_type(&attributes, our_key_type);
8928 PSA_ASSERT(psa_import_key(&attributes,
8929 our_key_data->x, our_key_data->len,
8930 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8933 PSA_ASSERT(psa_key_derivation_key_agreement(
8934 &operation,
8935 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8936 peer_key_data->x, peer_key_data->len));
8937 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008938 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8940 PSA_KEY_DERIVATION_INPUT_INFO,
8941 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008942 }
Gilles Peskine59685592018-09-18 12:11:34 +02008943
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008944 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 PSA_ASSERT(psa_key_derivation_get_capacity(
8946 &operation, &actual_capacity));
8947 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02008948
Gilles Peskinebf491972018-10-25 22:36:12 +02008949 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 while (actual_capacity > sizeof(output)) {
8951 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8952 output, sizeof(output)));
8953 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02008954 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8956 output, actual_capacity));
8957 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
8958 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02008959
Gilles Peskine59685592018-09-18 12:11:34 +02008960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 psa_key_derivation_abort(&operation);
8962 psa_destroy_key(our_key);
8963 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02008964}
8965/* END_CASE */
8966
8967/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008968void key_agreement_output(int alg_arg,
8969 int our_key_type_arg, data_t *our_key_data,
8970 data_t *peer_key_data,
8971 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02008972{
Ronald Cron5425a212020-08-04 14:58:35 +02008973 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008974 psa_algorithm_t alg = alg_arg;
8975 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008976 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008978 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008979
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
8981 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02008982
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008984
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8986 psa_set_key_algorithm(&attributes, alg);
8987 psa_set_key_type(&attributes, our_key_type);
8988 PSA_ASSERT(psa_import_key(&attributes,
8989 our_key_data->x, our_key_data->len,
8990 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008991
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8993 PSA_ASSERT(psa_key_derivation_key_agreement(
8994 &operation,
8995 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8996 peer_key_data->x, peer_key_data->len));
8997 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008998 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008999 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9000 PSA_KEY_DERIVATION_INPUT_INFO,
9001 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009002 }
Gilles Peskine59685592018-09-18 12:11:34 +02009003
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9005 actual_output,
9006 expected_output1->len));
9007 ASSERT_COMPARE(actual_output, expected_output1->len,
9008 expected_output1->x, expected_output1->len);
9009 if (expected_output2->len != 0) {
9010 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9011 actual_output,
9012 expected_output2->len));
9013 ASSERT_COMPARE(actual_output, expected_output2->len,
9014 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009015 }
Gilles Peskine59685592018-09-18 12:11:34 +02009016
9017exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 psa_key_derivation_abort(&operation);
9019 psa_destroy_key(our_key);
9020 PSA_DONE();
9021 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009022}
9023/* END_CASE */
9024
9025/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009026void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009027{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009028 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009029 unsigned char *output = NULL;
9030 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009031 size_t i;
9032 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009035
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 ASSERT_ALLOC(output, bytes);
9037 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009038
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009040
Gilles Peskinea50d7392018-06-21 10:22:13 +02009041 /* Run several times, to ensure that every output byte will be
9042 * nonzero at least once with overwhelming probability
9043 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 for (run = 0; run < 10; run++) {
9045 if (bytes != 0) {
9046 memset(output, 0, bytes);
9047 }
9048 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009049
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 for (i = 0; i < bytes; i++) {
9051 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009052 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009054 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009055 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009056
9057 /* Check that every byte was changed to nonzero at least once. This
9058 * validates that psa_generate_random is overwriting every byte of
9059 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 for (i = 0; i < bytes; i++) {
9061 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009062 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009063
9064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 PSA_DONE();
9066 mbedtls_free(output);
9067 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009068}
9069/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009070
9071/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009072void generate_key(int type_arg,
9073 int bits_arg,
9074 int usage_arg,
9075 int alg_arg,
9076 int expected_status_arg,
9077 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009078{
Ronald Cron5425a212020-08-04 14:58:35 +02009079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009080 psa_key_type_t type = type_arg;
9081 psa_key_usage_t usage = usage_arg;
9082 size_t bits = bits_arg;
9083 psa_algorithm_t alg = alg_arg;
9084 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009086 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009087
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009089
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 psa_set_key_usage_flags(&attributes, usage);
9091 psa_set_key_algorithm(&attributes, alg);
9092 psa_set_key_type(&attributes, type);
9093 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009094
9095 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009097
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 if (is_large_key > 0) {
9099 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9100 }
9101 TEST_EQUAL(status, expected_status);
9102 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009105
9106 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9108 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9109 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009110
Gilles Peskine818ca122018-06-20 18:16:48 +02009111 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009114 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009115
9116exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009117 /*
9118 * Key attributes may have been returned by psa_get_key_attributes()
9119 * thus reset them as required.
9120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009121 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009122
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 psa_destroy_key(key);
9124 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009125}
9126/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009127
Ronald Cronee414c72021-03-18 18:50:08 +01009128/* 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 +01009129void generate_key_rsa(int bits_arg,
9130 data_t *e_arg,
9131 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009132{
Ronald Cron5425a212020-08-04 14:58:35 +02009133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009134 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009135 size_t bits = bits_arg;
9136 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9137 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9138 psa_status_t expected_status = expected_status_arg;
9139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9140 uint8_t *exported = NULL;
9141 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009143 size_t exported_length = SIZE_MAX;
9144 uint8_t *e_read_buffer = NULL;
9145 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009147 size_t e_read_length = SIZE_MAX;
9148
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 if (e_arg->len == 0 ||
9150 (e_arg->len == 3 &&
9151 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009152 is_default_public_exponent = 1;
9153 e_read_size = 0;
9154 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 ASSERT_ALLOC(e_read_buffer, e_read_size);
9156 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 psa_set_key_usage_flags(&attributes, usage);
9161 psa_set_key_algorithm(&attributes, alg);
9162 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9163 e_arg->x, e_arg->len));
9164 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009165
9166 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9168 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009171
9172 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009173 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9174 TEST_EQUAL(psa_get_key_type(&attributes), type);
9175 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9176 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9177 e_read_buffer, e_read_size,
9178 &e_read_length));
9179 if (is_default_public_exponent) {
9180 TEST_EQUAL(e_read_length, 0);
9181 } else {
9182 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9183 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009184
9185 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009186 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009189
9190 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009191 PSA_ASSERT(psa_export_public_key(key,
9192 exported, exported_size,
9193 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009194 {
9195 uint8_t *p = exported;
9196 uint8_t *end = exported + exported_length;
9197 size_t len;
9198 /* RSAPublicKey ::= SEQUENCE {
9199 * modulus INTEGER, -- n
9200 * publicExponent INTEGER } -- e
9201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9203 MBEDTLS_ASN1_SEQUENCE |
9204 MBEDTLS_ASN1_CONSTRUCTED));
9205 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9206 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9207 MBEDTLS_ASN1_INTEGER));
9208 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009209 ++p;
9210 --len;
9211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if (e_arg->len == 0) {
9213 TEST_EQUAL(len, 3);
9214 TEST_EQUAL(p[0], 1);
9215 TEST_EQUAL(p[1], 0);
9216 TEST_EQUAL(p[2], 1);
9217 } else {
9218 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009219 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009220 }
9221
9222exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009223 /*
9224 * Key attributes may have been returned by psa_get_key_attributes() or
9225 * set by psa_set_key_domain_parameters() thus reset them as required.
9226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 psa_destroy_key(key);
9230 PSA_DONE();
9231 mbedtls_free(e_read_buffer);
9232 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009233}
9234/* END_CASE */
9235
Darryl Greend49a4992018-06-18 17:27:26 +01009236/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009237void persistent_key_load_key_from_storage(data_t *data,
9238 int type_arg, int bits_arg,
9239 int usage_flags_arg, int alg_arg,
9240 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009241{
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009244 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9245 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009246 psa_key_type_t type = type_arg;
9247 size_t bits = bits_arg;
9248 psa_key_usage_t usage_flags = usage_flags_arg;
9249 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009250 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009251 unsigned char *first_export = NULL;
9252 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009254 size_t first_exported_length;
9255 size_t second_exported_length;
9256
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9258 ASSERT_ALLOC(first_export, export_size);
9259 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009260 }
Darryl Greend49a4992018-06-18 17:27:26 +01009261
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009263
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 psa_set_key_id(&attributes, key_id);
9265 psa_set_key_usage_flags(&attributes, usage_flags);
9266 psa_set_key_algorithm(&attributes, alg);
9267 psa_set_key_type(&attributes, type);
9268 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009271 case IMPORT_KEY:
9272 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9274 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009275 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009276
Darryl Green0c6575a2018-11-07 16:05:30 +00009277 case GENERATE_KEY:
9278 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009280 break;
9281
9282 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009283#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 {
9285 /* Create base key */
9286 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9287 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9288 psa_set_key_usage_flags(&base_attributes,
9289 PSA_KEY_USAGE_DERIVE);
9290 psa_set_key_algorithm(&base_attributes, derive_alg);
9291 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9292 PSA_ASSERT(psa_import_key(&base_attributes,
9293 data->x, data->len,
9294 &base_key));
9295 /* Derive a key. */
9296 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9297 PSA_ASSERT(psa_key_derivation_input_key(
9298 &operation,
9299 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9300 PSA_ASSERT(psa_key_derivation_input_bytes(
9301 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9302 NULL, 0));
9303 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9304 &operation,
9305 &key));
9306 PSA_ASSERT(psa_key_derivation_abort(&operation));
9307 PSA_ASSERT(psa_destroy_key(base_key));
9308 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9309 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009310#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009312#endif
9313 break;
9314
9315 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009317 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009320
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009321 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9323 PSA_ASSERT(psa_export_key(key,
9324 first_export, export_size,
9325 &first_exported_length));
9326 if (generation_method == IMPORT_KEY) {
9327 ASSERT_COMPARE(data->x, data->len,
9328 first_export, first_exported_length);
9329 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009330 }
Darryl Greend49a4992018-06-18 17:27:26 +01009331
9332 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009333 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009334 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009335 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009336
Darryl Greend49a4992018-06-18 17:27:26 +01009337 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009338 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9339 TEST_ASSERT(mbedtls_svc_key_id_equal(
9340 psa_get_key_id(&attributes), key_id));
9341 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9342 PSA_KEY_LIFETIME_PERSISTENT);
9343 TEST_EQUAL(psa_get_key_type(&attributes), type);
9344 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9345 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9346 mbedtls_test_update_key_usage_flags(usage_flags));
9347 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009348
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009349 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009350 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9351 PSA_ASSERT(psa_export_key(key,
9352 second_export, export_size,
9353 &second_exported_length));
9354 ASSERT_COMPARE(first_export, first_exported_length,
9355 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009356 }
9357
9358 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009360 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 }
Darryl Greend49a4992018-06-18 17:27:26 +01009362
9363exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009364 /*
9365 * Key attributes may have been returned by psa_get_key_attributes()
9366 * thus reset them as required.
9367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009369
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 mbedtls_free(first_export);
9371 mbedtls_free(second_export);
9372 psa_key_derivation_abort(&operation);
9373 psa_destroy_key(base_key);
9374 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009375 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009376}
9377/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009378
Neil Armstronga557cb82022-06-10 08:58:32 +02009379/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009380void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9381 int primitive_arg, int hash_arg, int role_arg,
9382 int test_input, data_t *pw_data,
9383 int inj_err_type_arg,
9384 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009385{
9386 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9387 psa_pake_operation_t operation = psa_pake_operation_init();
9388 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009389 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009390 psa_key_type_t key_type_pw = key_type_pw_arg;
9391 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009392 psa_algorithm_t hash_alg = hash_arg;
9393 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009396 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9397 psa_status_t expected_error = expected_error_arg;
9398 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009399 unsigned char *output_buffer = NULL;
9400 size_t output_len = 0;
9401
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009403
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009404 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 PSA_PAKE_STEP_KEY_SHARE);
9406 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009407
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 if (pw_data->len > 0) {
9409 psa_set_key_usage_flags(&attributes, key_usage_pw);
9410 psa_set_key_algorithm(&attributes, alg);
9411 psa_set_key_type(&attributes, key_type_pw);
9412 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9413 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009414 }
9415
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9417 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9418 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009419
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009421
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9423 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9424 expected_error);
9425 PSA_ASSERT(psa_pake_abort(&operation));
9426 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9427 expected_error);
9428 PSA_ASSERT(psa_pake_abort(&operation));
9429 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9430 expected_error);
9431 PSA_ASSERT(psa_pake_abort(&operation));
9432 TEST_EQUAL(psa_pake_set_role(&operation, role),
9433 expected_error);
9434 PSA_ASSERT(psa_pake_abort(&operation));
9435 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9436 NULL, 0, NULL),
9437 expected_error);
9438 PSA_ASSERT(psa_pake_abort(&operation));
9439 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9440 expected_error);
9441 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009442 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009443 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009444
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 status = psa_pake_setup(&operation, &cipher_suite);
9446 if (status != PSA_SUCCESS) {
9447 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009448 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009449 }
9450
Gilles Peskine449bd832023-01-11 14:50:10 +01009451 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9452 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9453 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009454 goto exit;
9455 }
9456
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 status = psa_pake_set_role(&operation, role);
9458 if (status != PSA_SUCCESS) {
9459 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009460 goto exit;
9461 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009462
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 if (pw_data->len > 0) {
9464 status = psa_pake_set_password_key(&operation, key);
9465 if (status != PSA_SUCCESS) {
9466 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009467 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009468 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009469 }
9470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9472 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9473 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009474 goto exit;
9475 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009476
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9478 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9479 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009480 goto exit;
9481 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009482
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009484 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9486 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009487 goto exit;
9488 }
9489
Gilles Peskine449bd832023-01-11 14:50:10 +01009490 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009491 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9493 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009494 goto exit;
9495 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009496
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9498 PSA_PAKE_STEP_KEY_SHARE);
9499 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9500 PSA_PAKE_STEP_ZK_PUBLIC);
9501 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9502 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009503
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 if (test_input) {
9505 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9506 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9507 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009508 goto exit;
9509 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009510
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9512 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9513 output_buffer, size_zk_proof),
9514 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009515 goto exit;
9516 }
9517
Gilles Peskine449bd832023-01-11 14:50:10 +01009518 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9519 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9520 output_buffer, size_zk_proof),
9521 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009522 goto exit;
9523 }
9524
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9526 output_buffer, size_key_share);
9527 if (status != PSA_SUCCESS) {
9528 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009529 goto exit;
9530 }
9531
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9533 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9534 output_buffer, size_zk_public + 1),
9535 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009536 goto exit;
9537 }
9538
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009540 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9542 output_buffer, size_zk_public + 1);
9543 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9544 output_buffer, size_zk_public),
9545 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009546 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009547 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009548 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009549 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9550 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9551 NULL, 0, NULL),
9552 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009553 goto exit;
9554 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009555
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9557 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9558 output_buffer, buf_size, &output_len),
9559 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009560 goto exit;
9561 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009562
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9564 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9565 output_buffer, buf_size, &output_len),
9566 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009567 goto exit;
9568 }
9569
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9571 output_buffer, buf_size, &output_len);
9572 if (status != PSA_SUCCESS) {
9573 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009574 goto exit;
9575 }
9576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01009578
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9580 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9581 output_buffer, size_zk_public - 1, &output_len),
9582 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01009583 goto exit;
9584 }
9585
Gilles Peskine449bd832023-01-11 14:50:10 +01009586 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009587 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009588 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9589 output_buffer, size_zk_public - 1, &output_len);
9590 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9591 output_buffer, buf_size, &output_len),
9592 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009593 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009594 }
9595 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009596
9597exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 PSA_ASSERT(psa_destroy_key(key));
9599 PSA_ASSERT(psa_pake_abort(&operation));
9600 mbedtls_free(output_buffer);
9601 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009602}
9603/* END_CASE */
9604
Neil Armstronga557cb82022-06-10 08:58:32 +02009605/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009606void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
9607 int client_input_first, int inject_error,
9608 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009609{
9610 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9611 psa_pake_operation_t server = psa_pake_operation_init();
9612 psa_pake_operation_t client = psa_pake_operation_init();
9613 psa_algorithm_t alg = alg_arg;
9614 psa_algorithm_t hash_alg = hash_arg;
9615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9617
Gilles Peskine449bd832023-01-11 14:50:10 +01009618 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009619
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9621 psa_set_key_algorithm(&attributes, alg);
9622 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9623 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9624 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009625
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9627 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9628 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009629
9630
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9632 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009633
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9635 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009636
Gilles Peskine449bd832023-01-11 14:50:10 +01009637 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9638 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009639
Gilles Peskine449bd832023-01-11 14:50:10 +01009640 ecjpake_do_round(alg, primitive_arg, &server, &client,
9641 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009642
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009644 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009646
Gilles Peskine449bd832023-01-11 14:50:10 +01009647 ecjpake_do_round(alg, primitive_arg, &server, &client,
9648 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009649
9650exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009651 psa_destroy_key(key);
9652 psa_pake_abort(&server);
9653 psa_pake_abort(&client);
9654 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009655}
9656/* END_CASE */
9657
9658/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009659void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
9660 int derive_alg_arg, data_t *pw_data,
9661 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009662{
9663 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9664 psa_pake_operation_t server = psa_pake_operation_init();
9665 psa_pake_operation_t client = psa_pake_operation_init();
9666 psa_algorithm_t alg = alg_arg;
9667 psa_algorithm_t hash_alg = hash_arg;
9668 psa_algorithm_t derive_alg = derive_alg_arg;
9669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9671 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009673 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01009674 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009675 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009676
Gilles Peskine449bd832023-01-11 14:50:10 +01009677 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009678
Gilles Peskine449bd832023-01-11 14:50:10 +01009679 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9680 psa_set_key_algorithm(&attributes, alg);
9681 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
9682 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9683 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9686 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
9687 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009688
Neil Armstrong1e855602022-06-15 11:32:11 +02009689 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009690 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
9691 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02009692
Gilles Peskine449bd832023-01-11 14:50:10 +01009693 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
9694 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
9695 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
9696 PSA_KEY_DERIVATION_INPUT_SEED,
9697 (const uint8_t *) "", 0));
9698 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
9699 PSA_KEY_DERIVATION_INPUT_SEED,
9700 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02009701 }
9702
Gilles Peskine449bd832023-01-11 14:50:10 +01009703 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
9704 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009705
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
9707 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009708
Gilles Peskine449bd832023-01-11 14:50:10 +01009709 PSA_ASSERT(psa_pake_set_password_key(&server, key));
9710 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009711
Gilles Peskine449bd832023-01-11 14:50:10 +01009712 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
9713 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9714 PSA_ERROR_BAD_STATE);
9715 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9716 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009717 goto exit;
9718 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009719
Neil Armstrongf983caf2022-06-15 15:27:48 +02009720 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009721 ecjpake_do_round(alg, primitive_arg, &server, &client,
9722 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009723
Gilles Peskine449bd832023-01-11 14:50:10 +01009724 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
9725 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
9726 PSA_ERROR_BAD_STATE);
9727 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
9728 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009729 goto exit;
9730 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009731
Neil Armstrongf983caf2022-06-15 15:27:48 +02009732 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01009733 ecjpake_do_round(alg, primitive_arg, &server, &client,
9734 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009735
Gilles Peskine449bd832023-01-11 14:50:10 +01009736 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
9737 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009738
9739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009740 psa_key_derivation_abort(&server_derive);
9741 psa_key_derivation_abort(&client_derive);
9742 psa_destroy_key(key);
9743 psa_pake_abort(&server);
9744 psa_pake_abort(&client);
9745 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009746}
9747/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009748
9749/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009750void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009751{
9752 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9753 const size_t bits = 256;
9754 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01009755 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009756 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01009757 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009758
9759 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9760 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009761 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9762 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
9763 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9764 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009765 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01009766 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9767 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009768
9769 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01009770 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9771 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
9772 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9773 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
9774 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9775 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009776
9777 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9779 PSA_PAKE_OUTPUT_MAX_SIZE);
9780 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9781 PSA_PAKE_OUTPUT_MAX_SIZE);
9782 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9783 PSA_PAKE_OUTPUT_MAX_SIZE);
9784 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9785 PSA_PAKE_INPUT_MAX_SIZE);
9786 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9787 PSA_PAKE_INPUT_MAX_SIZE);
9788 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9789 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009790}
9791/* END_CASE */