blob: 34bb5e206efcfa9609819b67dedee50520a46861 [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
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010016/* For psa_can_do_hash() */
17#include "psa_crypto_core.h"
18
Gilles Peskine8e94efe2021-02-13 00:25:53 +010019#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010020#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010021#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053022#if defined(PSA_CRYPTO_DRIVER_TEST)
23#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053024#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
25#else
26#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053027#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010028
Gilles Peskine4023c012021-05-27 13:21:20 +020029/* If this comes up, it's a bug in the test code or in the test data. */
30#define UNUSED 0xdeadbeef
31
Dave Rodgman647791d2021-06-23 12:49:59 +010032/* Assert that an operation is (not) active.
33 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010034#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
35#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010036
Przemek Stekiel7c795482022-11-15 22:26:12 +010037#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010038int ecjpake_operation_setup(psa_pake_operation_t *operation,
39 psa_pake_cipher_suite_t *cipher_suite,
40 psa_pake_role_t role,
41 mbedtls_svc_key_id_t key,
42 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010043{
Gilles Peskine449bd832023-01-11 14:50:10 +010044 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if (key_available) {
51 PSA_ASSERT(psa_pake_set_password_key(operation, key));
52 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010053 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010054exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010055 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010056}
57#endif
58
Jaeden Amerof24c7f82018-06-27 17:20:43 +010059/** An invalid export length that will never be set by psa_export_key(). */
60static const size_t INVALID_EXPORT_LENGTH = ~0U;
61
Gilles Peskinea7aa4422018-08-14 15:17:54 +020062/** Test if a buffer contains a constant byte value.
63 *
64 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020065 *
66 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020067 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020068 * \param size Size of the buffer in bytes.
69 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020070 * \return 1 if the buffer is all-bits-zero.
71 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074{
75 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 for (i = 0; i < size; i++) {
77 if (((unsigned char *) buffer)[i] != c) {
78 return 0;
79 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080 }
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020082}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010083#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020084/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int asn1_write_10x(unsigned char **p,
86 unsigned char *start,
87 size_t bits,
88 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020089{
90 int ret;
91 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (bits == 0) {
93 return MBEDTLS_ERR_ASN1_INVALID_DATA;
94 }
95 if (bits <= 8 && x >= 1 << (bits - 1)) {
96 return MBEDTLS_ERR_ASN1_INVALID_DATA;
97 }
98 if (*p < start || *p - start < (ptrdiff_t) len) {
99 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
100 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200101 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 (*p)[len-1] = x;
103 if (bits % 8 == 0) {
104 (*p)[1] |= 1;
105 } else {
106 (*p)[0] |= 1 << (bits % 8);
107 }
108 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
109 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
110 MBEDTLS_ASN1_INTEGER));
111 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200112}
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114static int construct_fake_rsa_key(unsigned char *buffer,
115 size_t buffer_size,
116 unsigned char **p,
117 size_t bits,
118 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200119{
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200121 int ret;
122 int len = 0;
123 /* Construct something that looks like a DER encoding of
124 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
125 * RSAPrivateKey ::= SEQUENCE {
126 * version Version,
127 * modulus INTEGER, -- n
128 * publicExponent INTEGER, -- e
129 * privateExponent INTEGER, -- d
130 * prime1 INTEGER, -- p
131 * prime2 INTEGER, -- q
132 * exponent1 INTEGER, -- d mod (p-1)
133 * exponent2 INTEGER, -- d mod (q-1)
134 * coefficient INTEGER, -- (inverse of q) mod p
135 * otherPrimeInfos OtherPrimeInfos OPTIONAL
136 * }
137 * Or, for a public key, the same structure with only
138 * version, modulus and publicExponent.
139 */
140 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (keypair) {
142 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
143 asn1_write_10x(p, buffer, half_bits, 1));
144 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* q */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
151 asn1_write_10x(p, buffer, half_bits, 3));
152 MBEDTLS_ASN1_CHK_ADD(len, /* d */
153 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200154 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
156 asn1_write_10x(p, buffer, 17, 1));
157 MBEDTLS_ASN1_CHK_ADD(len, /* n */
158 asn1_write_10x(p, buffer, bits, 1));
159 if (keypair) {
160 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
161 mbedtls_asn1_write_int(p, buffer, 0));
162 }
163 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200164 {
165 const unsigned char tag =
166 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200168 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100171#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173int exercise_mac_setup(psa_key_type_t key_type,
174 const unsigned char *key_bytes,
175 size_t key_length,
176 psa_algorithm_t alg,
177 psa_mac_operation_t *operation,
178 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100179{
Ronald Cron5425a212020-08-04 14:58:35 +0200180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
184 psa_set_key_algorithm(&attributes, alg);
185 psa_set_key_type(&attributes, key_type);
186 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100189 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* If setup failed, reproduce the failure, so that the caller can
192 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (*status != PSA_SUCCESS) {
194 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 }
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 psa_destroy_key(key);
198 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100199
200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 psa_destroy_key(key);
202 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203}
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205int exercise_cipher_setup(psa_key_type_t key_type,
206 const unsigned char *key_bytes,
207 size_t key_length,
208 psa_algorithm_t alg,
209 psa_cipher_operation_t *operation,
210 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100211{
Ronald Cron5425a212020-08-04 14:58:35 +0200212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
216 psa_set_key_algorithm(&attributes, alg);
217 psa_set_key_type(&attributes, key_type);
218 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 /* If setup failed, reproduce the failure, so that the caller can
224 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (*status != PSA_SUCCESS) {
226 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
227 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100228 }
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 psa_destroy_key(key);
231 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100232
233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 psa_destroy_key(key);
235 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100236}
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239{
240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 uint8_t buffer[1];
243 size_t length;
244 int ok = 0;
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 psa_set_key_id(&attributes, key_id);
247 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
248 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
249 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
250 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
251 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200254 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
256 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
257 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
258 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
259 TEST_EQUAL(psa_get_key_type(&attributes), 0);
260 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
263 PSA_ERROR_INVALID_HANDLE);
264 TEST_EQUAL(psa_export_public_key(key,
265 buffer, sizeof(buffer), &length),
266 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200267
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200268 ok = 1;
269
270exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100271 /*
272 * Key attributes may have been returned by psa_get_key_attributes()
273 * thus reset them as required.
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200278}
279
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200280/* Assert that a key isn't reported as having a slot number. */
281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100282#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200283 do \
284 { \
285 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 TEST_EQUAL(psa_get_key_slot_number( \
287 attributes, \
288 &ASSERT_NO_SLOT_NUMBER_slot_number), \
289 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200290 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200292#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293#define ASSERT_NO_SLOT_NUMBER(attributes) \
294 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200295#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
296
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100297/* An overapproximation of the amount of storage needed for a key of the
298 * given type and with the given content. The API doesn't make it easy
299 * to find a good value for the size. The current implementation doesn't
300 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301#define KEY_BITS_FROM_DATA(type, data) \
302 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100303
Darryl Green0c6575a2018-11-07 16:05:30 +0000304typedef enum {
305 IMPORT_KEY = 0,
306 GENERATE_KEY = 1,
307 DERIVE_KEY = 2
308} generate_method;
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100311 DO_NOT_SET_LENGTHS = 0,
312 SET_LENGTHS_BEFORE_NONCE = 1,
313 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100314} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100317 USE_NULL_TAG = 0,
318 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100319} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100320
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321/*!
322 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 * \param key_type_arg Type of key passed in
324 * \param key_data The encryption / decryption key data
325 * \param alg_arg The type of algorithm used
326 * \param nonce Nonce data
327 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100328 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100329 * feed additional data in to be encrypted /
330 * decrypted. If -1, no chunking.
331 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100332 * \param data_part_len_arg If not -1, the length of chunks to feed
333 * the data in to be encrypted / decrypted. If
334 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100335 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100336 * expected here, this controls whether or not
337 * to set lengths, and in what order with
338 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100341 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100342 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100343 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
346 int alg_arg,
347 data_t *nonce,
348 data_t *additional_data,
349 int ad_part_len_arg,
350 data_t *input_data,
351 int data_part_len_arg,
352 set_lengths_method_t set_lengths_method,
353 data_t *expected_output,
354 int is_encrypt,
355 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100356{
357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
358 psa_key_type_t key_type = key_type_arg;
359 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100360 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100361 unsigned char *output_data = NULL;
362 unsigned char *part_data = NULL;
363 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100364 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100366 size_t output_size = 0;
367 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100368 size_t output_length = 0;
369 size_t key_bits = 0;
370 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100371 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100372 size_t part_length = 0;
373 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100374 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100375 size_t ad_part_len = 0;
376 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
379 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100382 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (is_encrypt) {
387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
388 } else {
389 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100391
392 psa_set_key_algorithm(&attributes, alg);
393 psa_set_key_type(&attributes, key_type);
394
395 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
396 &key));
397
398 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
399 key_bits = psa_get_key_bits(&attributes);
400
401 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
402
403 if (is_encrypt) {
404 /* Tag gets written at end of buffer. */
405 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
406 (input_data->len +
407 tag_length));
408 data_true_size = input_data->len;
409 } else {
410 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
411 (input_data->len -
412 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100414 /* Do not want to attempt to decrypt tag. */
415 data_true_size = input_data->len - tag_length;
416 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (is_encrypt) {
421 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
422 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
423 } else {
424 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
425 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100426 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (is_encrypt) {
431 status = psa_aead_encrypt_setup(&operation, key, alg);
432 } else {
433 status = psa_aead_decrypt_setup(&operation, key, alg);
434 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100435
436 /* If the operation is not supported, just skip and not fail in case the
437 * encryption involves a common limitation of cryptography hardwares and
438 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (status == PSA_ERROR_NOT_SUPPORTED) {
440 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
441 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 }
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
447 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
448 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
449 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
450 data_true_size));
451 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
452 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
453 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
456 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100461 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100464 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 part_offset += part_length, part_count++) {
466 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100467 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100469 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100471 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100472 }
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 PSA_ASSERT(psa_aead_update_ad(&operation,
475 additional_data->x + part_offset,
476 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100477
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
482 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100486 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 data_part_len = (size_t) data_part_len_arg;
488 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
489 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100494 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 part_offset += part_length, part_count++) {
496 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100497 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else if ((data_true_size - part_offset) < data_part_len) {
499 part_length = (data_true_size - part_offset);
500 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100501 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 }
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 PSA_ASSERT(psa_aead_update(&operation,
505 (input_data->x + part_offset),
506 part_length, part_data,
507 part_data_size,
508 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (output_data && output_part_length) {
511 memcpy((output_data + output_length), part_data,
512 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 }
514
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 output_length += output_part_length;
516 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
520 data_true_size, output_data,
521 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (is_encrypt) {
525 PSA_ASSERT(psa_aead_finish(&operation, final_data,
526 final_output_size,
527 &output_part_length,
528 tag_buffer, tag_length,
529 &tag_size));
530 } else {
531 PSA_ASSERT(psa_aead_verify(&operation, final_data,
532 final_output_size,
533 &output_part_length,
534 (input_data->x + data_true_size),
535 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100536 }
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (output_data && output_part_length) {
539 memcpy((output_data + output_length), final_data,
540 output_part_length);
541 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100542
543 output_length += output_part_length;
544
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100545
546 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
547 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (is_encrypt) {
549 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (output_data && tag_length) {
552 memcpy((output_data + output_length), tag_buffer,
553 tag_length);
554 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
556 output_length += tag_length;
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 TEST_EQUAL(output_length,
559 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
560 input_data->len));
561 TEST_LE_U(output_length,
562 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
563 } else {
564 TEST_EQUAL(output_length,
565 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
566 input_data->len));
567 TEST_LE_U(output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100569 }
570
Paul Elliottd3f82412021-06-16 16:52:21 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 ASSERT_COMPARE(expected_output->x, expected_output->len,
573 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100577
578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 psa_destroy_key(key);
580 psa_aead_abort(&operation);
581 mbedtls_free(output_data);
582 mbedtls_free(part_data);
583 mbedtls_free(final_data);
584 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100587}
588
Neil Armstrong4766f992022-02-28 16:23:59 +0100589/*!
590 * \brief Internal Function for MAC multipart tests.
591 * \param key_type_arg Type of key passed in
592 * \param key_data The encryption / decryption key data
593 * \param alg_arg The type of algorithm used
594 * \param input_data Data to encrypt / decrypt
595 * \param data_part_len_arg If not -1, the length of chunks to feed
596 * the data in to be encrypted / decrypted. If
597 * -1, no chunking
598 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000599 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100600 * \param do_zero_parts If non-zero, interleave zero length chunks
601 * with normal length chunks.
602 * \return int Zero on failure, non-zero on success.
603 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
605 int alg_arg,
606 data_t *input_data,
607 int data_part_len_arg,
608 data_t *expected_output,
609 int is_verify,
610 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100611{
612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
613 psa_key_type_t key_type = key_type_arg;
614 psa_algorithm_t alg = alg_arg;
615 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
616 unsigned char mac[PSA_MAC_MAX_SIZE];
617 size_t part_offset = 0;
618 size_t part_length = 0;
619 size_t data_part_len = 0;
620 size_t mac_len = 0;
621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
622 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
623
624 int test_ok = 0;
625 size_t part_count = 0;
626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (is_verify) {
630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
631 } else {
632 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
633 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 psa_set_key_algorithm(&attributes, alg);
636 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
639 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (is_verify) {
642 status = psa_mac_verify_setup(&operation, key, alg);
643 } else {
644 status = psa_mac_sign_setup(&operation, key, alg);
645 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100650 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100654 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 part_offset += part_length, part_count++) {
656 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100657 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 } else if ((input_data->len - part_offset) < data_part_len) {
659 part_length = (input_data->len - part_offset);
660 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100661 part_length = data_part_len;
662 }
663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 PSA_ASSERT(psa_mac_update(&operation,
665 (input_data->x + part_offset),
666 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100667 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100669 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
671 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (is_verify) {
675 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
676 expected_output->len));
677 } else {
678 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
679 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 ASSERT_COMPARE(expected_output->x, expected_output->len,
682 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100683 }
684
685 test_ok = 1;
686
687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 psa_destroy_key(key);
689 psa_mac_abort(&operation);
690 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100693}
694
Neil Armstrong75673ab2022-06-15 17:39:01 +0200695#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
697 psa_pake_operation_t *server,
698 psa_pake_operation_t *client,
699 int client_input_first,
700 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200701{
702 unsigned char *buffer0 = NULL, *buffer1 = NULL;
703 size_t buffer_length = (
704 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
705 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
706 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200707 /* The output should be exactly this size according to the spec */
708 const size_t expected_size_key_share =
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
710 /* The output should be exactly this size according to the spec */
711 const size_t expected_size_zk_public =
712 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
713 /* The output can be smaller: the spec allows stripping leading zeroes */
714 const size_t max_expected_size_zk_proof =
715 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200716 size_t buffer0_off = 0;
717 size_t buffer1_off = 0;
718 size_t s_g1_len, s_g2_len, s_a_len;
719 size_t s_g1_off, s_g2_off, s_a_off;
720 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
721 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
722 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
723 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
724 size_t c_g1_len, c_g2_len, c_a_len;
725 size_t c_g1_off, c_g2_off, c_a_off;
726 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
727 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
728 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
729 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
730 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200731 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 ASSERT_ALLOC(buffer0, buffer_length);
734 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737 case 1:
738 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
740 buffer0 + buffer0_off,
741 512 - buffer0_off, &s_g1_len));
742 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200743 s_g1_off = buffer0_off;
744 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
746 buffer0 + buffer0_off,
747 512 - buffer0_off, &s_x1_pk_len));
748 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200749 s_x1_pk_off = buffer0_off;
750 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
752 buffer0 + buffer0_off,
753 512 - buffer0_off, &s_x1_pr_len));
754 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200755 s_x1_pr_off = buffer0_off;
756 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
758 buffer0 + buffer0_off,
759 512 - buffer0_off, &s_g2_len));
760 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200761 s_g2_off = buffer0_off;
762 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
764 buffer0 + buffer0_off,
765 512 - buffer0_off, &s_x2_pk_len));
766 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200767 s_x2_pk_off = buffer0_off;
768 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
770 buffer0 + buffer0_off,
771 512 - buffer0_off, &s_x2_pr_len));
772 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200773 s_x2_pr_off = buffer0_off;
774 buffer0_off += s_x2_pr_len;
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500777 buffer0[s_x1_pr_off + 8] ^= 1;
778 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200779 expected_status = PSA_ERROR_DATA_INVALID;
780 }
781
Neil Armstrong51009d72022-09-05 17:59:54 +0200782 /*
783 * When injecting errors in inputs, the implementation is
784 * free to detect it right away of with a delay.
785 * This permits delaying the error until the end of the input
786 * sequence, if no error appears then, this will be treated
787 * as an error.
788 */
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200791 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
793 buffer0 + s_g1_off, s_g1_len);
794 if (inject_error == 1 && status != PSA_SUCCESS) {
795 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200796 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 } else {
798 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200799 }
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
802 buffer0 + s_x1_pk_off,
803 s_x1_pk_len);
804 if (inject_error == 1 && status != PSA_SUCCESS) {
805 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200806 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } else {
808 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200809 }
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
812 buffer0 + s_x1_pr_off,
813 s_x1_pr_len);
814 if (inject_error == 1 && status != PSA_SUCCESS) {
815 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200816 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 } else {
818 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200819 }
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
822 buffer0 + s_g2_off,
823 s_g2_len);
824 if (inject_error == 1 && status != PSA_SUCCESS) {
825 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200826 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 } else {
828 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200829 }
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
832 buffer0 + s_x2_pk_off,
833 s_x2_pk_len);
834 if (inject_error == 1 && status != PSA_SUCCESS) {
835 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200836 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 } else {
838 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200839 }
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
842 buffer0 + s_x2_pr_off,
843 s_x2_pr_len);
844 if (inject_error == 1 && status != PSA_SUCCESS) {
845 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200846 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 } else {
848 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200849 }
850
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200851 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (inject_error == 1) {
853 TEST_ASSERT(
854 !"One of the last psa_pake_input() calls should have returned the expected error.");
855 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200856 }
857
858 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
860 buffer1 + buffer1_off,
861 512 - buffer1_off, &c_g1_len));
862 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200863 c_g1_off = buffer1_off;
864 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
866 buffer1 + buffer1_off,
867 512 - buffer1_off, &c_x1_pk_len));
868 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200869 c_x1_pk_off = buffer1_off;
870 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
872 buffer1 + buffer1_off,
873 512 - buffer1_off, &c_x1_pr_len));
874 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200875 c_x1_pr_off = buffer1_off;
876 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
878 buffer1 + buffer1_off,
879 512 - buffer1_off, &c_g2_len));
880 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200881 c_g2_off = buffer1_off;
882 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
884 buffer1 + buffer1_off,
885 512 - buffer1_off, &c_x2_pk_len));
886 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200887 c_x2_pk_off = buffer1_off;
888 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
890 buffer1 + buffer1_off,
891 512 - buffer1_off, &c_x2_pr_len));
892 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200893 c_x2_pr_off = buffer1_off;
894 buffer1_off += c_x2_pr_len;
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200897 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
899 buffer0 + s_g1_off, s_g1_len);
900 if (inject_error == 1 && status != PSA_SUCCESS) {
901 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 } else {
904 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200905 }
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
908 buffer0 + s_x1_pk_off,
909 s_x1_pk_len);
910 if (inject_error == 1 && status != PSA_SUCCESS) {
911 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200912 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 } else {
914 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200915 }
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
918 buffer0 + s_x1_pr_off,
919 s_x1_pr_len);
920 if (inject_error == 1 && status != PSA_SUCCESS) {
921 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200922 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 } else {
924 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200925 }
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
928 buffer0 + s_g2_off,
929 s_g2_len);
930 if (inject_error == 1 && status != PSA_SUCCESS) {
931 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200932 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 } else {
934 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200935 }
936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
938 buffer0 + s_x2_pk_off,
939 s_x2_pk_len);
940 if (inject_error == 1 && status != PSA_SUCCESS) {
941 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200942 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 } else {
944 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200945 }
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
948 buffer0 + s_x2_pr_off,
949 s_x2_pr_len);
950 if (inject_error == 1 && status != PSA_SUCCESS) {
951 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200952 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 } else {
954 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200955 }
956
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200957 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (inject_error == 1) {
959 TEST_ASSERT(
960 !"One of the last psa_pake_input() calls should have returned the expected error.");
961 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200962 }
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500965 buffer1[c_x1_pr_off + 12] ^= 1;
966 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200967 expected_status = PSA_ERROR_DATA_INVALID;
968 }
969
970 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
972 buffer1 + c_g1_off, c_g1_len);
973 if (inject_error == 2 && status != PSA_SUCCESS) {
974 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200975 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 } else {
977 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200978 }
979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
981 buffer1 + c_x1_pk_off, c_x1_pk_len);
982 if (inject_error == 2 && status != PSA_SUCCESS) {
983 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200984 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 } else {
986 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200987 }
988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
990 buffer1 + c_x1_pr_off, c_x1_pr_len);
991 if (inject_error == 2 && status != PSA_SUCCESS) {
992 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200993 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 } else {
995 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200996 }
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
999 buffer1 + c_g2_off, c_g2_len);
1000 if (inject_error == 2 && status != PSA_SUCCESS) {
1001 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001002 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 } else {
1004 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001005 }
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1008 buffer1 + c_x2_pk_off, c_x2_pk_len);
1009 if (inject_error == 2 && status != PSA_SUCCESS) {
1010 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001011 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 } else {
1013 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001014 }
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1017 buffer1 + c_x2_pr_off, c_x2_pr_len);
1018 if (inject_error == 2 && status != PSA_SUCCESS) {
1019 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001020 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 } else {
1022 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001023 }
1024
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001025 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (inject_error == 2) {
1027 TEST_ASSERT(
1028 !"One of the last psa_pake_input() calls should have returned the expected error.");
1029 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001030
1031 break;
1032
1033 case 2:
1034 /* Server second round Output */
1035 buffer0_off = 0;
1036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1038 buffer0 + buffer0_off,
1039 512 - buffer0_off, &s_a_len));
1040 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001041 s_a_off = buffer0_off;
1042 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1044 buffer0 + buffer0_off,
1045 512 - buffer0_off, &s_x2s_pk_len));
1046 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001047 s_x2s_pk_off = buffer0_off;
1048 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1050 buffer0 + buffer0_off,
1051 512 - buffer0_off, &s_x2s_pr_len));
1052 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001053 s_x2s_pr_off = buffer0_off;
1054 buffer0_off += s_x2s_pr_len;
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001057 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001058 expected_status = PSA_ERROR_DATA_INVALID;
1059 }
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001062 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1064 buffer0 + s_a_off, s_a_len);
1065 if (inject_error == 3 && status != PSA_SUCCESS) {
1066 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 } else {
1069 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1073 buffer0 + s_x2s_pk_off,
1074 s_x2s_pk_len);
1075 if (inject_error == 3 && status != PSA_SUCCESS) {
1076 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001077 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 } else {
1079 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001080 }
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1083 buffer0 + s_x2s_pr_off,
1084 s_x2s_pr_len);
1085 if (inject_error == 3 && status != PSA_SUCCESS) {
1086 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001087 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 } else {
1089 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001090 }
1091
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001092 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (inject_error == 3) {
1094 TEST_ASSERT(
1095 !"One of the last psa_pake_input() calls should have returned the expected error.");
1096 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001097 }
1098
1099 /* Client second round Output */
1100 buffer1_off = 0;
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1103 buffer1 + buffer1_off,
1104 512 - buffer1_off, &c_a_len));
1105 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001106 c_a_off = buffer1_off;
1107 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1109 buffer1 + buffer1_off,
1110 512 - buffer1_off, &c_x2s_pk_len));
1111 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001112 c_x2s_pk_off = buffer1_off;
1113 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1115 buffer1 + buffer1_off,
1116 512 - buffer1_off, &c_x2s_pr_len));
1117 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001118 c_x2s_pr_off = buffer1_off;
1119 buffer1_off += c_x2s_pr_len;
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001122 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1124 buffer0 + s_a_off, s_a_len);
1125 if (inject_error == 3 && status != PSA_SUCCESS) {
1126 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001127 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 } else {
1129 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001130 }
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1133 buffer0 + s_x2s_pk_off,
1134 s_x2s_pk_len);
1135 if (inject_error == 3 && status != PSA_SUCCESS) {
1136 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001137 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 } else {
1139 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001140 }
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1143 buffer0 + s_x2s_pr_off,
1144 s_x2s_pr_len);
1145 if (inject_error == 3 && status != PSA_SUCCESS) {
1146 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001147 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 } else {
1149 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001150 }
1151
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001152 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (inject_error == 3) {
1154 TEST_ASSERT(
1155 !"One of the last psa_pake_input() calls should have returned the expected error.");
1156 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001157 }
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001160 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001161 expected_status = PSA_ERROR_DATA_INVALID;
1162 }
1163
1164 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1166 buffer1 + c_a_off, c_a_len);
1167 if (inject_error == 4 && status != PSA_SUCCESS) {
1168 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001169 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 } else {
1171 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1175 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1176 if (inject_error == 4 && status != PSA_SUCCESS) {
1177 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001178 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 } else {
1180 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001181 }
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1184 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1185 if (inject_error == 4 && status != PSA_SUCCESS) {
1186 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001187 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 } else {
1189 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001190 }
1191
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001192 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (inject_error == 4) {
1194 TEST_ASSERT(
1195 !"One of the last psa_pake_input() calls should have returned the expected error.");
1196 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001197
1198 break;
1199
1200 }
1201
Neil Armstrongf983caf2022-06-15 15:27:48 +02001202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_free(buffer0);
1204 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001206#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001209 INJECT_ERR_NONE = 0,
1210 INJECT_ERR_UNINITIALIZED_ACCESS,
1211 INJECT_ERR_DUPLICATE_SETUP,
1212 INJECT_ERR_INVALID_USER,
1213 INJECT_ERR_INVALID_PEER,
1214 INJECT_ERR_SET_USER,
1215 INJECT_ERR_SET_PEER,
1216 INJECT_EMPTY_IO_BUFFER,
1217 INJECT_UNKNOWN_STEP,
1218 INJECT_INVALID_FIRST_STEP,
1219 INJECT_WRONG_BUFFER_SIZE,
1220 INJECT_VALID_OPERATION_AFTER_FAILURE,
1221 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1222 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1223} ecjpake_injected_failure_t;
1224
Paul Elliott01885fa2023-02-09 12:07:30 +00001225#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001226
Paul Elliott6f600372023-02-06 18:41:05 +00001227static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1228 psa_status_t expected_status,
1229 size_t *min_completes,
1230 size_t *max_completes)
1231{
1232
1233 /* This is slightly contrived, but we only really know that with a minimum
1234 value of max_ops that a successful operation should take more than one op
1235 to complete, and likewise that with a max_ops of
1236 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1237 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001238
Paul Elliott6f600372023-02-06 18:41:05 +00001239 if (expected_status == PSA_SUCCESS) {
1240 *min_completes = 2;
1241 } else {
1242 *min_completes = 1;
1243 }
1244
1245 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1246 } else {
1247 *min_completes = 1;
1248 *max_completes = 1;
1249 }
1250}
Paul Elliott01885fa2023-02-09 12:07:30 +00001251#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001252
Gilles Peskinee59236f2018-01-27 23:32:46 +01001253/* END_HEADER */
1254
1255/* BEGIN_DEPENDENCIES
1256 * depends_on:MBEDTLS_PSA_CRYPTO_C
1257 * END_DEPENDENCIES
1258 */
1259
1260/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001261void psa_can_do_hash()
1262{
1263 /* We can't test that this is specific to drivers until partial init has
1264 * been implemented, but we can at least test before/after full init. */
1265 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1266 PSA_INIT();
1267 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1268 PSA_DONE();
1269}
1270/* END_CASE */
1271
1272/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001274{
1275 size_t max_truncated_mac_size =
1276 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1277
1278 /* Check that the length for a truncated MAC always fits in the algorithm
1279 * encoding. The shifted mask is the maximum truncated value. The
1280 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001282}
1283/* END_CASE */
1284
1285/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286void import_with_policy(int type_arg,
1287 int usage_arg, int alg_arg,
1288 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001289{
1290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1291 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001293 psa_key_type_t type = type_arg;
1294 psa_key_usage_t usage = usage_arg;
1295 psa_algorithm_t alg = alg_arg;
1296 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001298 psa_status_t status;
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 psa_set_key_type(&attributes, type);
1303 psa_set_key_usage_flags(&attributes, usage);
1304 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 status = psa_import_key(&attributes,
1307 key_material, sizeof(key_material),
1308 &key);
1309 TEST_EQUAL(status, expected_status);
1310 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001311 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1315 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1316 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1317 mbedtls_test_update_key_usage_flags(usage));
1318 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1319 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 PSA_ASSERT(psa_destroy_key(key));
1322 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001323
1324exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001325 /*
1326 * Key attributes may have been returned by psa_get_key_attributes()
1327 * thus reset them as required.
1328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 psa_destroy_key(key);
1332 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001333}
1334/* END_CASE */
1335
1336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337void import_with_data(data_t *data, int type_arg,
1338 int attr_bits_arg,
1339 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001340{
1341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1342 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001344 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001345 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001346 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 psa_set_key_type(&attributes, type);
1352 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 status = psa_import_key(&attributes, data->x, data->len, &key);
1355 TEST_EQUAL(status, expected_status);
1356 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001357 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1361 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1362 if (attr_bits != 0) {
1363 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1364 }
1365 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 PSA_ASSERT(psa_destroy_key(key));
1368 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369
1370exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001371 /*
1372 * Key attributes may have been returned by psa_get_key_attributes()
1373 * thus reset them as required.
1374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 psa_destroy_key(key);
1378 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379}
1380/* END_CASE */
1381
1382/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001383/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384void import_large_key(int type_arg, int byte_size_arg,
1385 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001386{
1387 psa_key_type_t type = type_arg;
1388 size_t byte_size = byte_size_arg;
1389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1390 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001392 psa_status_t status;
1393 uint8_t *buffer = NULL;
1394 size_t buffer_size = byte_size + 1;
1395 size_t n;
1396
Steven Cooreman69967ce2021-01-18 18:01:08 +01001397 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001398 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1400 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001403
1404 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1406 psa_set_key_type(&attributes, type);
1407 status = psa_import_key(&attributes, buffer, byte_size, &key);
1408 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1409 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (status == PSA_SUCCESS) {
1412 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1413 TEST_EQUAL(psa_get_key_type(&attributes), type);
1414 TEST_EQUAL(psa_get_key_bits(&attributes),
1415 PSA_BYTES_TO_BITS(byte_size));
1416 ASSERT_NO_SLOT_NUMBER(&attributes);
1417 memset(buffer, 0, byte_size + 1);
1418 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1419 for (n = 0; n < byte_size; n++) {
1420 TEST_EQUAL(buffer[n], 'K');
1421 }
1422 for (n = byte_size; n < buffer_size; n++) {
1423 TEST_EQUAL(buffer[n], 0);
1424 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001425 }
1426
1427exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001428 /*
1429 * Key attributes may have been returned by psa_get_key_attributes()
1430 * thus reset them as required.
1431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 psa_destroy_key(key);
1435 PSA_DONE();
1436 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001437}
1438/* END_CASE */
1439
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001440/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001441/* Import an RSA key with a valid structure (but not valid numbers
1442 * inside, beyond having sensible size and parity). This is expected to
1443 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001444void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001445{
Ronald Cron5425a212020-08-04 14:58:35 +02001446 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001447 size_t bits = bits_arg;
1448 psa_status_t expected_status = expected_status_arg;
1449 psa_status_t status;
1450 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001451 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001452 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001454 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001455 unsigned char *p;
1456 int ret;
1457 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 PSA_ASSERT(psa_crypto_init());
1461 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1464 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001465 length = ret;
1466
1467 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 psa_set_key_type(&attributes, type);
1469 status = psa_import_key(&attributes, p, length, &key);
1470 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (status == PSA_SUCCESS) {
1473 PSA_ASSERT(psa_destroy_key(key));
1474 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001475
1476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 mbedtls_free(buffer);
1478 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001479}
1480/* END_CASE */
1481
1482/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483void import_export(data_t *data,
1484 int type_arg,
1485 int usage_arg, int alg_arg,
1486 int lifetime_arg,
1487 int expected_bits,
1488 int export_size_delta,
1489 int expected_export_status_arg,
1490 /*whether reexport must give the original input exactly*/
1491 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492{
Ronald Cron5425a212020-08-04 14:58:35 +02001493 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001494 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001495 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001496 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001497 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301498 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499 unsigned char *exported = NULL;
1500 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001501 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001502 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001505 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001506 psa_status_t expected_import_result = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001507
Moran Pekercb088e72018-07-17 17:36:59 +03001508 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 ASSERT_ALLOC(exported, export_size);
1510 if (!canonical_input) {
1511 ASSERT_ALLOC(reexported, export_size);
1512 }
1513 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 psa_set_key_lifetime(&attributes, lifetime);
1516 psa_set_key_usage_flags(&attributes, usage_arg);
1517 psa_set_key_algorithm(&attributes, alg);
1518 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001519
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001520 if (PSA_KEY_TYPE_IS_DH(type) &&
1521 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
1522 export_size -= 8;
1523 }
1524
1525 if (PSA_KEY_TYPE_IS_DH(type) &&
1526 (data->len != 256 && data->len != 384 &&
1527 data->len != 512 && data->len != 768 && data->len != 1024)) {
1528 expected_import_result = PSA_ERROR_INVALID_ARGUMENT;
1529 }
1530
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001532 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
1533 expected_import_result);
1534
1535 if (expected_import_result != PSA_SUCCESS) {
1536 goto exit;
1537 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538
1539 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1541 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1542 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1543 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544
1545 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 status = psa_export_key(key, exported, export_size, &exported_length);
1547 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001548
1549 /* The exported length must be set by psa_export_key() to a value between 0
1550 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1552 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1553 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1556 export_size - exported_length));
1557 if (status != PSA_SUCCESS) {
1558 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001559 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001560 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561
Gilles Peskineea38a922021-02-13 00:05:16 +01001562 /* Run sanity checks on the exported key. For non-canonical inputs,
1563 * this validates the canonical representations. For canonical inputs,
1564 * this doesn't directly validate the implementation, but it still helps
1565 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (!psa_key_lifetime_is_external(lifetime)) {
1567 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 }
Archana4d7ae1d2021-07-07 02:50:22 +05301570 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (canonical_input) {
1573 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1574 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001575 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1577 &key2));
1578 PSA_ASSERT(psa_export_key(key2,
1579 reexported,
1580 export_size,
1581 &reexported_length));
1582 ASSERT_COMPARE(exported, exported_length,
1583 reexported, reexported_length);
1584 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 TEST_LE_U(exported_length,
1587 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1588 psa_get_key_bits(&got_attributes)));
1589 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590
1591destroy:
1592 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 PSA_ASSERT(psa_destroy_key(key));
1594 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595
1596exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001597 /*
1598 * Key attributes may have been returned by psa_get_key_attributes()
1599 * thus reset them as required.
1600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 psa_reset_key_attributes(&got_attributes);
1602 psa_destroy_key(key);
1603 mbedtls_free(exported);
1604 mbedtls_free(reexported);
1605 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606}
1607/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001608
Moran Pekerf709f4a2018-06-06 17:26:04 +03001609/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610void import_export_public_key(data_t *data,
1611 int type_arg, // key pair or public key
1612 int alg_arg,
1613 int lifetime_arg,
1614 int export_size_delta,
1615 int expected_export_status_arg,
1616 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001617{
Ronald Cron5425a212020-08-04 14:58:35 +02001618 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001619 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001620 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001621 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001622 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301623 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001624 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001625 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001626 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 psa_set_key_lifetime(&attributes, lifetime);
1632 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1633 psa_set_key_algorithm(&attributes, alg);
1634 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001635
1636 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001638
Gilles Peskine49c25912018-10-29 15:15:31 +01001639 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 ASSERT_ALLOC(exported, export_size);
1641 status = psa_export_public_key(key,
1642 exported, export_size,
1643 &exported_length);
1644 TEST_EQUAL(status, expected_export_status);
1645 if (status == PSA_SUCCESS) {
1646 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001647 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1649 bits = psa_get_key_bits(&attributes);
1650 TEST_LE_U(expected_public_key->len,
1651 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1652 TEST_LE_U(expected_public_key->len,
1653 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1654 TEST_LE_U(expected_public_key->len,
1655 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1656 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1657 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001658 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001659exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001660 /*
1661 * Key attributes may have been returned by psa_get_key_attributes()
1662 * thus reset them as required.
1663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 mbedtls_free(exported);
1667 psa_destroy_key(key);
1668 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001669}
1670/* END_CASE */
1671
Gilles Peskine20035e32018-02-03 22:44:14 +01001672/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001673void import_and_exercise_key(data_t *data,
1674 int type_arg,
1675 int bits_arg,
1676 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677{
Ronald Cron5425a212020-08-04 14:58:35 +02001678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001679 psa_key_type_t type = type_arg;
1680 size_t bits = bits_arg;
1681 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001684 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 psa_set_key_usage_flags(&attributes, usage);
1689 psa_set_key_algorithm(&attributes, alg);
1690 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691
1692 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001694
1695 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1697 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1698 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001699
1700 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001702 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 PSA_ASSERT(psa_destroy_key(key));
1706 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001707
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001708exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001709 /*
1710 * Key attributes may have been returned by psa_get_key_attributes()
1711 * thus reset them as required.
1712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 psa_reset_key_attributes(&attributes);
1716 psa_destroy_key(key);
1717 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001718}
1719/* END_CASE */
1720
1721/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722void effective_key_attributes(int type_arg, int expected_type_arg,
1723 int bits_arg, int expected_bits_arg,
1724 int usage_arg, int expected_usage_arg,
1725 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001726{
Ronald Cron5425a212020-08-04 14:58:35 +02001727 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001728 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001729 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001730 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001731 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001732 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001733 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001734 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001735 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 psa_set_key_usage_flags(&attributes, usage);
1741 psa_set_key_algorithm(&attributes, alg);
1742 psa_set_key_type(&attributes, key_type);
1743 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 PSA_ASSERT(psa_generate_key(&attributes, &key));
1746 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1749 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1750 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1751 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1752 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001753
1754exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001755 /*
1756 * Key attributes may have been returned by psa_get_key_attributes()
1757 * thus reset them as required.
1758 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 psa_destroy_key(key);
1762 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001763}
1764/* END_CASE */
1765
1766/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001767void check_key_policy(int type_arg, int bits_arg,
1768 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001769{
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1771 usage_arg,
1772 mbedtls_test_update_key_usage_flags(usage_arg),
1773 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001774 goto exit;
1775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001779void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001780{
1781 /* Test each valid way of initializing the object, except for `= {0}`, as
1782 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1783 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001784 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001786 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1787 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1792 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1793 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 TEST_EQUAL(psa_get_key_type(&func), 0);
1796 TEST_EQUAL(psa_get_key_type(&init), 0);
1797 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 TEST_EQUAL(psa_get_key_bits(&func), 0);
1800 TEST_EQUAL(psa_get_key_bits(&init), 0);
1801 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1804 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1805 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1808 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1809 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001810}
1811/* END_CASE */
1812
1813/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814void mac_key_policy(int policy_usage_arg,
1815 int policy_alg_arg,
1816 int key_type_arg,
1817 data_t *key_data,
1818 int exercise_alg_arg,
1819 int expected_status_sign_arg,
1820 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001821{
Ronald Cron5425a212020-08-04 14:58:35 +02001822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001824 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001825 psa_key_type_t key_type = key_type_arg;
1826 psa_algorithm_t policy_alg = policy_alg_arg;
1827 psa_algorithm_t exercise_alg = exercise_alg_arg;
1828 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001829 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001830 psa_status_t expected_status_sign = expected_status_sign_arg;
1831 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001832 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 psa_set_key_usage_flags(&attributes, policy_usage);
1837 psa_set_key_algorithm(&attributes, policy_alg);
1838 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1841 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1844 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1847 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001848
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001849 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001851 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1853 input, 128,
1854 mac, PSA_MAC_MAX_SIZE, &mac_len),
1855 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001856
Neil Armstrong3af9b972022-02-07 12:20:21 +01001857 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 PSA_ASSERT(psa_mac_abort(&operation));
1859 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1860 if (status == PSA_SUCCESS) {
1861 status = psa_mac_update(&operation, input, 128);
1862 if (status == PSA_SUCCESS) {
1863 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1864 &mac_len),
1865 expected_status_sign);
1866 } else {
1867 TEST_EQUAL(status, expected_status_sign);
1868 }
1869 } else {
1870 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001871 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001873
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001874 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 status = psa_mac_verify(key, exercise_alg, input, 128,
1876 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1879 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1880 } else {
1881 TEST_EQUAL(status, expected_status_verify);
1882 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001883
Neil Armstrong3af9b972022-02-07 12:20:21 +01001884 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1886 if (status == PSA_SUCCESS) {
1887 status = psa_mac_update(&operation, input, 128);
1888 if (status == PSA_SUCCESS) {
1889 status = psa_mac_verify_finish(&operation, mac, mac_len);
1890 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1891 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1892 } else {
1893 TEST_EQUAL(status, expected_status_verify);
1894 }
1895 } else {
1896 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001897 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 } else {
1899 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001900 }
1901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 memset(mac, 0, sizeof(mac));
1905 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1906 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001907
1908exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 psa_mac_abort(&operation);
1910 psa_destroy_key(key);
1911 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001912}
1913/* END_CASE */
1914
1915/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916void cipher_key_policy(int policy_usage_arg,
1917 int policy_alg,
1918 int key_type,
1919 data_t *key_data,
1920 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921{
Ronald Cron5425a212020-08-04 14:58:35 +02001922 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001924 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001925 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001926 size_t output_buffer_size = 0;
1927 size_t input_buffer_size = 0;
1928 size_t output_length = 0;
1929 uint8_t *output = NULL;
1930 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931 psa_status_t status;
1932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1934 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1935 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 ASSERT_ALLOC(input, input_buffer_size);
1938 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 psa_set_key_usage_flags(&attributes, policy_usage);
1943 psa_set_key_algorithm(&attributes, policy_alg);
1944 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1947 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001948
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001949 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 TEST_EQUAL(policy_usage,
1951 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001952
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001953 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1955 output, output_buffer_size,
1956 &output_length);
1957 if (policy_alg == exercise_alg &&
1958 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1959 PSA_ASSERT(status);
1960 } else {
1961 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1962 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001963
1964 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1966 if (policy_alg == exercise_alg &&
1967 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1968 PSA_ASSERT(status);
1969 } else {
1970 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1971 }
1972 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001974 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1976 input, input_buffer_size,
1977 &output_length);
1978 if (policy_alg == exercise_alg &&
1979 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1980 PSA_ASSERT(status);
1981 } else {
1982 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1983 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001984
1985 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1987 if (policy_alg == exercise_alg &&
1988 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1989 PSA_ASSERT(status);
1990 } else {
1991 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1992 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993
1994exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 psa_cipher_abort(&operation);
1996 mbedtls_free(input);
1997 mbedtls_free(output);
1998 psa_destroy_key(key);
1999 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000}
2001/* END_CASE */
2002
2003/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004void aead_key_policy(int policy_usage_arg,
2005 int policy_alg,
2006 int key_type,
2007 data_t *key_data,
2008 int nonce_length_arg,
2009 int tag_length_arg,
2010 int exercise_alg,
2011 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012{
Ronald Cron5425a212020-08-04 14:58:35 +02002013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002015 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002016 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002018 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020 size_t nonce_length = nonce_length_arg;
2021 unsigned char tag[16];
2022 size_t tag_length = tag_length_arg;
2023 size_t output_length;
2024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 TEST_LE_U(nonce_length, sizeof(nonce));
2026 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 psa_set_key_usage_flags(&attributes, policy_usage);
2031 psa_set_key_algorithm(&attributes, policy_alg);
2032 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2035 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002036
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002037 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 TEST_EQUAL(policy_usage,
2039 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002040
Neil Armstrong752d8112022-02-07 14:51:11 +01002041 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_aead_encrypt(key, exercise_alg,
2043 nonce, nonce_length,
2044 NULL, 0,
2045 NULL, 0,
2046 tag, tag_length,
2047 &output_length);
2048 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2049 TEST_EQUAL(status, expected_status);
2050 } else {
2051 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2052 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053
Neil Armstrong752d8112022-02-07 14:51:11 +01002054 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2056 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2057 TEST_EQUAL(status, expected_status);
2058 } else {
2059 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2060 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002061
2062 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 memset(tag, 0, sizeof(tag));
2064 status = psa_aead_decrypt(key, exercise_alg,
2065 nonce, nonce_length,
2066 NULL, 0,
2067 tag, tag_length,
2068 NULL, 0,
2069 &output_length);
2070 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2071 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2072 } else if (expected_status == PSA_SUCCESS) {
2073 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2074 } else {
2075 TEST_EQUAL(status, expected_status);
2076 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077
Neil Armstrong752d8112022-02-07 14:51:11 +01002078 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 PSA_ASSERT(psa_aead_abort(&operation));
2080 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2081 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2082 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2083 } else {
2084 TEST_EQUAL(status, expected_status);
2085 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002086
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 PSA_ASSERT(psa_aead_abort(&operation));
2089 psa_destroy_key(key);
2090 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091}
2092/* END_CASE */
2093
2094/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095void asymmetric_encryption_key_policy(int policy_usage_arg,
2096 int policy_alg,
2097 int key_type,
2098 data_t *key_data,
2099 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100{
Ronald Cron5425a212020-08-04 14:58:35 +02002101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002103 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104 psa_status_t status;
2105 size_t key_bits;
2106 size_t buffer_length;
2107 unsigned char *buffer = NULL;
2108 size_t output_length;
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 psa_set_key_usage_flags(&attributes, policy_usage);
2113 psa_set_key_algorithm(&attributes, policy_alg);
2114 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2117 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002119 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 TEST_EQUAL(policy_usage,
2121 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2124 key_bits = psa_get_key_bits(&attributes);
2125 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2126 exercise_alg);
2127 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 status = psa_asymmetric_encrypt(key, exercise_alg,
2130 NULL, 0,
2131 NULL, 0,
2132 buffer, buffer_length,
2133 &output_length);
2134 if (policy_alg == exercise_alg &&
2135 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2136 PSA_ASSERT(status);
2137 } else {
2138 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2139 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (buffer_length != 0) {
2142 memset(buffer, 0, buffer_length);
2143 }
2144 status = psa_asymmetric_decrypt(key, exercise_alg,
2145 buffer, buffer_length,
2146 NULL, 0,
2147 buffer, buffer_length,
2148 &output_length);
2149 if (policy_alg == exercise_alg &&
2150 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2151 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2152 } else {
2153 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2154 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002155
2156exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002157 /*
2158 * Key attributes may have been returned by psa_get_key_attributes()
2159 * thus reset them as required.
2160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 psa_destroy_key(key);
2164 PSA_DONE();
2165 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002166}
2167/* END_CASE */
2168
2169/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170void asymmetric_signature_key_policy(int policy_usage_arg,
2171 int policy_alg,
2172 int key_type,
2173 data_t *key_data,
2174 int exercise_alg,
2175 int payload_length_arg,
2176 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177{
Ronald Cron5425a212020-08-04 14:58:35 +02002178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002180 psa_key_usage_t policy_usage = policy_usage_arg;
2181 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002184 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2185 * compatible with the policy and `payload_length_arg` is supposed to be
2186 * a valid input length to sign. If `payload_length_arg <= 0`,
2187 * `exercise_alg` is supposed to be forbidden by the policy. */
2188 int compatible_alg = payload_length_arg > 0;
2189 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191 size_t signature_length;
2192
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002193 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002194 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 TEST_EQUAL(expected_usage,
2196 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 psa_set_key_usage_flags(&attributes, policy_usage);
2201 psa_set_key_algorithm(&attributes, policy_alg);
2202 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2205 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 status = psa_sign_hash(key, exercise_alg,
2210 payload, payload_length,
2211 signature, sizeof(signature),
2212 &signature_length);
2213 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2214 PSA_ASSERT(status);
2215 } else {
2216 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2217 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 memset(signature, 0, sizeof(signature));
2220 status = psa_verify_hash(key, exercise_alg,
2221 payload, payload_length,
2222 signature, sizeof(signature));
2223 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2224 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2225 } else {
2226 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2227 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2230 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2231 status = psa_sign_message(key, exercise_alg,
2232 payload, payload_length,
2233 signature, sizeof(signature),
2234 &signature_length);
2235 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2236 PSA_ASSERT(status);
2237 } else {
2238 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2239 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 memset(signature, 0, sizeof(signature));
2242 status = psa_verify_message(key, exercise_alg,
2243 payload, payload_length,
2244 signature, sizeof(signature));
2245 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2246 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2247 } else {
2248 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2249 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002250 }
2251
Gilles Peskined5b33222018-06-18 22:20:03 +02002252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 psa_destroy_key(key);
2254 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002255}
2256/* END_CASE */
2257
Janos Follathba3fab92019-06-11 14:50:16 +01002258/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259void derive_key_policy(int policy_usage,
2260 int policy_alg,
2261 int key_type,
2262 data_t *key_data,
2263 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002264{
Ronald Cron5425a212020-08-04 14:58:35 +02002265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002267 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268 psa_status_t status;
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 psa_set_key_usage_flags(&attributes, policy_usage);
2273 psa_set_key_algorithm(&attributes, policy_alg);
2274 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2277 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2282 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2283 PSA_ASSERT(psa_key_derivation_input_bytes(
2284 &operation,
2285 PSA_KEY_DERIVATION_INPUT_SEED,
2286 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002287 }
Janos Follathba3fab92019-06-11 14:50:16 +01002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 status = psa_key_derivation_input_key(&operation,
2290 PSA_KEY_DERIVATION_INPUT_SECRET,
2291 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (policy_alg == exercise_alg &&
2294 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2295 PSA_ASSERT(status);
2296 } else {
2297 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2298 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002299
2300exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 psa_key_derivation_abort(&operation);
2302 psa_destroy_key(key);
2303 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002304}
2305/* END_CASE */
2306
2307/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308void agreement_key_policy(int policy_usage,
2309 int policy_alg,
2310 int key_type_arg,
2311 data_t *key_data,
2312 int exercise_alg,
2313 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314{
Ronald Cron5425a212020-08-04 14:58:35 +02002315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002317 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002318 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002319 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002320 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_set_key_usage_flags(&attributes, policy_usage);
2325 psa_set_key_algorithm(&attributes, policy_alg);
2326 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2329 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2332 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002335
2336exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 psa_key_derivation_abort(&operation);
2338 psa_destroy_key(key);
2339 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340}
2341/* END_CASE */
2342
2343/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002344void key_policy_alg2(int key_type_arg, data_t *key_data,
2345 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002346{
Ronald Cron5425a212020-08-04 14:58:35 +02002347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002348 psa_key_type_t key_type = key_type_arg;
2349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2350 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2351 psa_key_usage_t usage = usage_arg;
2352 psa_algorithm_t alg = alg_arg;
2353 psa_algorithm_t alg2 = alg2_arg;
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 psa_set_key_usage_flags(&attributes, usage);
2358 psa_set_key_algorithm(&attributes, alg);
2359 psa_set_key_enrollment_algorithm(&attributes, alg2);
2360 psa_set_key_type(&attributes, key_type);
2361 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2362 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002363
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002364 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 usage = mbedtls_test_update_key_usage_flags(usage);
2366 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2367 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2368 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2369 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 }
2374 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002375 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002377
2378exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002379 /*
2380 * Key attributes may have been returned by psa_get_key_attributes()
2381 * thus reset them as required.
2382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 psa_destroy_key(key);
2386 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002387}
2388/* END_CASE */
2389
2390/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002391void raw_agreement_key_policy(int policy_usage,
2392 int policy_alg,
2393 int key_type_arg,
2394 data_t *key_data,
2395 int exercise_alg,
2396 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002397{
Ronald Cron5425a212020-08-04 14:58:35 +02002398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002400 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002401 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002402 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002403 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 psa_set_key_usage_flags(&attributes, policy_usage);
2408 psa_set_key_algorithm(&attributes, policy_alg);
2409 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2412 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002417
2418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 psa_key_derivation_abort(&operation);
2420 psa_destroy_key(key);
2421 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002422}
2423/* END_CASE */
2424
2425/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426void copy_success(int source_usage_arg,
2427 int source_alg_arg, int source_alg2_arg,
2428 unsigned int source_lifetime_arg,
2429 int type_arg, data_t *material,
2430 int copy_attributes,
2431 int target_usage_arg,
2432 int target_alg_arg, int target_alg2_arg,
2433 unsigned int target_lifetime_arg,
2434 int expected_usage_arg,
2435 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002436{
Gilles Peskineca25db92019-04-19 11:43:08 +02002437 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2438 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002439 psa_key_usage_t expected_usage = expected_usage_arg;
2440 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002441 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302442 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2443 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002444 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2445 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002446 uint8_t *export_buffer = NULL;
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449
Gilles Peskineca25db92019-04-19 11:43:08 +02002450 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2452 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2453 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2454 psa_set_key_type(&source_attributes, type_arg);
2455 psa_set_key_lifetime(&source_attributes, source_lifetime);
2456 PSA_ASSERT(psa_import_key(&source_attributes,
2457 material->x, material->len,
2458 &source_key));
2459 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002460
Gilles Peskineca25db92019-04-19 11:43:08 +02002461 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002463 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002464 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (target_usage_arg != -1) {
2468 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2469 }
2470 if (target_alg_arg != -1) {
2471 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2472 }
2473 if (target_alg2_arg != -1) {
2474 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2475 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002476
Archana8a180362021-07-05 02:18:48 +05302477
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 PSA_ASSERT(psa_copy_key(source_key,
2480 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002481
2482 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002484
2485 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2487 TEST_EQUAL(psa_get_key_type(&source_attributes),
2488 psa_get_key_type(&target_attributes));
2489 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2490 psa_get_key_bits(&target_attributes));
2491 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2492 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2493 TEST_EQUAL(expected_alg2,
2494 psa_get_key_enrollment_algorithm(&target_attributes));
2495 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002496 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 ASSERT_ALLOC(export_buffer, material->len);
2498 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2499 material->len, &length));
2500 ASSERT_COMPARE(material->x, material->len,
2501 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002502 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if (!psa_key_lifetime_is_external(target_lifetime)) {
2505 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302506 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 }
2508 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 }
Archana8a180362021-07-05 02:18:48 +05302511 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002514
2515exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002516 /*
2517 * Source and target key attributes may have been returned by
2518 * psa_get_key_attributes() thus reset them as required.
2519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 psa_reset_key_attributes(&source_attributes);
2521 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 PSA_DONE();
2524 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002525}
2526/* END_CASE */
2527
2528/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529void copy_fail(int source_usage_arg,
2530 int source_alg_arg, int source_alg2_arg,
2531 int source_lifetime_arg,
2532 int type_arg, data_t *material,
2533 int target_type_arg, int target_bits_arg,
2534 int target_usage_arg,
2535 int target_alg_arg, int target_alg2_arg,
2536 int target_id_arg, int target_lifetime_arg,
2537 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002538{
2539 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2540 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002541 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2542 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002546
2547 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2549 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2550 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2551 psa_set_key_type(&source_attributes, type_arg);
2552 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2553 PSA_ASSERT(psa_import_key(&source_attributes,
2554 material->x, material->len,
2555 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002556
2557 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 psa_set_key_id(&target_attributes, key_id);
2559 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2560 psa_set_key_type(&target_attributes, target_type_arg);
2561 psa_set_key_bits(&target_attributes, target_bits_arg);
2562 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2563 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2564 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002565
2566 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 TEST_EQUAL(psa_copy_key(source_key,
2568 &target_attributes, &target_key),
2569 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002572
Gilles Peskine4a644642019-05-03 17:14:08 +02002573exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 psa_reset_key_attributes(&source_attributes);
2575 psa_reset_key_attributes(&target_attributes);
2576 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002577}
2578/* END_CASE */
2579
2580/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002582{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002583 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002584 /* Test each valid way of initializing the object, except for `= {0}`, as
2585 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2586 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002587 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002589 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2590 psa_hash_operation_t zero;
2591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002593
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002594 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2596 PSA_ERROR_BAD_STATE);
2597 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2598 PSA_ERROR_BAD_STATE);
2599 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2600 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002601
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002602 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 PSA_ASSERT(psa_hash_abort(&func));
2604 PSA_ASSERT(psa_hash_abort(&init));
2605 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002606}
2607/* END_CASE */
2608
2609/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002610void hash_setup(int alg_arg,
2611 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002612{
2613 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002614 uint8_t *output = NULL;
2615 size_t output_size = 0;
2616 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002617 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002618 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619 psa_status_t status;
2620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002622
Neil Armstrongedb20862022-02-07 15:47:44 +01002623 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 output_size = PSA_HASH_LENGTH(alg);
2625 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 status = psa_hash_compute(alg, NULL, 0,
2628 output, output_size, &output_length);
2629 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002630
2631 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 status = psa_hash_setup(&operation, alg);
2633 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002634
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002635 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002637
2638 /* If setup failed, reproduce the failure, so as to
2639 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (status != PSA_SUCCESS) {
2641 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2642 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002643
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002644 /* Now the operation object should be reusable. */
2645#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2647 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002648#endif
2649
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002650exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 mbedtls_free(output);
2652 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002653}
2654/* END_CASE */
2655
2656/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002657void hash_compute_fail(int alg_arg, data_t *input,
2658 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002659{
2660 psa_algorithm_t alg = alg_arg;
2661 uint8_t *output = NULL;
2662 size_t output_size = output_size_arg;
2663 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002664 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002665 psa_status_t expected_status = expected_status_arg;
2666 psa_status_t status;
2667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002671
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002672 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 status = psa_hash_compute(alg, input->x, input->len,
2674 output, output_size, &output_length);
2675 TEST_EQUAL(status, expected_status);
2676 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002677
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002678 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 status = psa_hash_setup(&operation, alg);
2680 if (status == PSA_SUCCESS) {
2681 status = psa_hash_update(&operation, input->x, input->len);
2682 if (status == PSA_SUCCESS) {
2683 status = psa_hash_finish(&operation, output, output_size,
2684 &output_length);
2685 if (status == PSA_SUCCESS) {
2686 TEST_LE_U(output_length, output_size);
2687 } else {
2688 TEST_EQUAL(status, expected_status);
2689 }
2690 } else {
2691 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002692 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 } else {
2694 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002695 }
2696
Gilles Peskine0a749c82019-11-28 19:33:58 +01002697exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 PSA_ASSERT(psa_hash_abort(&operation));
2699 mbedtls_free(output);
2700 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002701}
2702/* END_CASE */
2703
2704/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002705void hash_compare_fail(int alg_arg, data_t *input,
2706 data_t *reference_hash,
2707 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002708{
2709 psa_algorithm_t alg = alg_arg;
2710 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002711 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002712 psa_status_t status;
2713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002715
Neil Armstrong55a1be12022-02-07 11:23:20 +01002716 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 status = psa_hash_compare(alg, input->x, input->len,
2718 reference_hash->x, reference_hash->len);
2719 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002720
Neil Armstrong55a1be12022-02-07 11:23:20 +01002721 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 status = psa_hash_setup(&operation, alg);
2723 if (status == PSA_SUCCESS) {
2724 status = psa_hash_update(&operation, input->x, input->len);
2725 if (status == PSA_SUCCESS) {
2726 status = psa_hash_verify(&operation, reference_hash->x,
2727 reference_hash->len);
2728 TEST_EQUAL(status, expected_status);
2729 } else {
2730 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002731 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 } else {
2733 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002734 }
2735
Gilles Peskine88e08462020-01-28 20:43:00 +01002736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 PSA_ASSERT(psa_hash_abort(&operation));
2738 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002739}
2740/* END_CASE */
2741
2742/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002743void hash_compute_compare(int alg_arg, data_t *input,
2744 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002745{
2746 psa_algorithm_t alg = alg_arg;
2747 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2748 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002749 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002750 size_t i;
2751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002753
Neil Armstrongca30a002022-02-07 11:40:23 +01002754 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2756 output, PSA_HASH_LENGTH(alg),
2757 &output_length));
2758 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2759 ASSERT_COMPARE(output, output_length,
2760 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002761
Neil Armstrongca30a002022-02-07 11:40:23 +01002762 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 PSA_ASSERT(psa_hash_setup(&operation, alg));
2764 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2765 PSA_ASSERT(psa_hash_finish(&operation, output,
2766 PSA_HASH_LENGTH(alg),
2767 &output_length));
2768 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2769 ASSERT_COMPARE(output, output_length,
2770 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002771
2772 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2774 output, sizeof(output),
2775 &output_length));
2776 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2777 ASSERT_COMPARE(output, output_length,
2778 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002779
Neil Armstrongca30a002022-02-07 11:40:23 +01002780 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 PSA_ASSERT(psa_hash_setup(&operation, alg));
2782 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2783 PSA_ASSERT(psa_hash_finish(&operation, output,
2784 sizeof(output), &output_length));
2785 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2786 ASSERT_COMPARE(output, output_length,
2787 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002788
2789 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2791 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002792
Neil Armstrongca30a002022-02-07 11:40:23 +01002793 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 PSA_ASSERT(psa_hash_setup(&operation, alg));
2795 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2796 PSA_ASSERT(psa_hash_verify(&operation, output,
2797 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002798
2799 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2801 output, output_length + 1),
2802 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002803
Neil Armstrongca30a002022-02-07 11:40:23 +01002804 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 PSA_ASSERT(psa_hash_setup(&operation, alg));
2806 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2807 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2808 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002809
2810 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2812 output, output_length - 1),
2813 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002814
Neil Armstrongca30a002022-02-07 11:40:23 +01002815 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 PSA_ASSERT(psa_hash_setup(&operation, alg));
2817 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2818 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2819 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002820
Gilles Peskine0a749c82019-11-28 19:33:58 +01002821 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 for (i = 0; i < output_length; i++) {
2823 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002824 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002825
2826 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2828 output, output_length),
2829 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002830
2831 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 PSA_ASSERT(psa_hash_setup(&operation, alg));
2833 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2834 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2835 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002836
Gilles Peskine0a749c82019-11-28 19:33:58 +01002837 output[i] ^= 1;
2838 }
2839
2840exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 PSA_ASSERT(psa_hash_abort(&operation));
2842 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002843}
2844/* END_CASE */
2845
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002846/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002847void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002848{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002849 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002850 unsigned char input[] = "";
2851 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002852 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002853 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2854 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2856 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002857 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002858 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002859 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002862
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002863 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 PSA_ASSERT(psa_hash_setup(&operation, alg));
2865 ASSERT_OPERATION_IS_ACTIVE(operation);
2866 TEST_EQUAL(psa_hash_setup(&operation, alg),
2867 PSA_ERROR_BAD_STATE);
2868 ASSERT_OPERATION_IS_INACTIVE(operation);
2869 PSA_ASSERT(psa_hash_abort(&operation));
2870 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002871
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002872 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2874 PSA_ERROR_BAD_STATE);
2875 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002876
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002877 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002879 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 ASSERT_OPERATION_IS_ACTIVE(operation);
2881 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2882 PSA_ERROR_BAD_STATE);
2883 ASSERT_OPERATION_IS_INACTIVE(operation);
2884 PSA_ASSERT(psa_hash_abort(&operation));
2885 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002886
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002887 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_ASSERT(psa_hash_setup(&operation, alg));
2889 PSA_ASSERT(psa_hash_finish(&operation,
2890 hash, sizeof(hash), &hash_len));
2891 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2892 PSA_ERROR_BAD_STATE);
2893 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002894
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002895 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 TEST_EQUAL(psa_hash_verify(&operation,
2897 valid_hash, sizeof(valid_hash)),
2898 PSA_ERROR_BAD_STATE);
2899 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002900
2901 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 PSA_ASSERT(psa_hash_setup(&operation, alg));
2903 PSA_ASSERT(psa_hash_finish(&operation,
2904 hash, sizeof(hash), &hash_len));
2905 TEST_EQUAL(psa_hash_verify(&operation,
2906 valid_hash, sizeof(valid_hash)),
2907 PSA_ERROR_BAD_STATE);
2908 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002909
2910 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 PSA_ASSERT(psa_hash_setup(&operation, alg));
2912 ASSERT_OPERATION_IS_ACTIVE(operation);
2913 PSA_ASSERT(psa_hash_verify(&operation,
2914 valid_hash, sizeof(valid_hash)));
2915 ASSERT_OPERATION_IS_INACTIVE(operation);
2916 TEST_EQUAL(psa_hash_verify(&operation,
2917 valid_hash, sizeof(valid_hash)),
2918 PSA_ERROR_BAD_STATE);
2919 ASSERT_OPERATION_IS_INACTIVE(operation);
2920 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002921
2922 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 TEST_EQUAL(psa_hash_finish(&operation,
2924 hash, sizeof(hash), &hash_len),
2925 PSA_ERROR_BAD_STATE);
2926 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002927
2928 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 PSA_ASSERT(psa_hash_setup(&operation, alg));
2930 PSA_ASSERT(psa_hash_finish(&operation,
2931 hash, sizeof(hash), &hash_len));
2932 TEST_EQUAL(psa_hash_finish(&operation,
2933 hash, sizeof(hash), &hash_len),
2934 PSA_ERROR_BAD_STATE);
2935 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002936
2937 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 PSA_ASSERT(psa_hash_setup(&operation, alg));
2939 PSA_ASSERT(psa_hash_verify(&operation,
2940 valid_hash, sizeof(valid_hash)));
2941 TEST_EQUAL(psa_hash_finish(&operation,
2942 hash, sizeof(hash), &hash_len),
2943 PSA_ERROR_BAD_STATE);
2944 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002945
2946exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002948}
2949/* END_CASE */
2950
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002951/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002953{
2954 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002955 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2956 * appended to it */
2957 unsigned char hash[] = {
2958 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2959 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2961 };
2962 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;
itayzafrirec93d302018-10-18 18:01:10 +03002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002966
itayzafrir27e69452018-11-01 14:26:34 +02002967 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 PSA_ASSERT(psa_hash_setup(&operation, alg));
2969 ASSERT_OPERATION_IS_ACTIVE(operation);
2970 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2971 PSA_ERROR_INVALID_SIGNATURE);
2972 ASSERT_OPERATION_IS_INACTIVE(operation);
2973 PSA_ASSERT(psa_hash_abort(&operation));
2974 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002975
itayzafrir27e69452018-11-01 14:26:34 +02002976 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 PSA_ASSERT(psa_hash_setup(&operation, alg));
2978 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2979 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002980
itayzafrir27e69452018-11-01 14:26:34 +02002981 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 PSA_ASSERT(psa_hash_setup(&operation, alg));
2983 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2984 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002985
itayzafrirec93d302018-10-18 18:01:10 +03002986exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002988}
2989/* END_CASE */
2990
Ronald Cronee414c72021-03-18 18:50:08 +01002991/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002992void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002993{
2994 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002995 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002997 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002998 size_t hash_len;
2999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003001
itayzafrir58028322018-10-25 10:22:01 +03003002 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 PSA_ASSERT(psa_hash_setup(&operation, alg));
3004 TEST_EQUAL(psa_hash_finish(&operation,
3005 hash, expected_size - 1, &hash_len),
3006 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003007
3008exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003010}
3011/* END_CASE */
3012
Ronald Cronee414c72021-03-18 18:50:08 +01003013/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003015{
3016 psa_algorithm_t alg = PSA_ALG_SHA_256;
3017 unsigned char hash[PSA_HASH_MAX_SIZE];
3018 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3019 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3020 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3021 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3022 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3023 size_t hash_len;
3024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 PSA_ASSERT(psa_crypto_init());
3026 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3029 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3030 PSA_ASSERT(psa_hash_finish(&op_finished,
3031 hash, sizeof(hash), &hash_len));
3032 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3033 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3036 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3039 PSA_ASSERT(psa_hash_finish(&op_init,
3040 hash, sizeof(hash), &hash_len));
3041 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3042 PSA_ASSERT(psa_hash_finish(&op_finished,
3043 hash, sizeof(hash), &hash_len));
3044 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3045 PSA_ASSERT(psa_hash_finish(&op_aborted,
3046 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003047
3048exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 psa_hash_abort(&op_source);
3050 psa_hash_abort(&op_init);
3051 psa_hash_abort(&op_setup);
3052 psa_hash_abort(&op_finished);
3053 psa_hash_abort(&op_aborted);
3054 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003055}
3056/* END_CASE */
3057
Ronald Cronee414c72021-03-18 18:50:08 +01003058/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003059void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003060{
3061 psa_algorithm_t alg = PSA_ALG_SHA_256;
3062 unsigned char hash[PSA_HASH_MAX_SIZE];
3063 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3064 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3065 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3066 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3067 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3068 size_t hash_len;
3069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3073 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3074 PSA_ASSERT(psa_hash_finish(&op_finished,
3075 hash, sizeof(hash), &hash_len));
3076 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3077 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3080 PSA_ASSERT(psa_hash_finish(&op_target,
3081 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3084 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3085 PSA_ERROR_BAD_STATE);
3086 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3087 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003088
3089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 psa_hash_abort(&op_target);
3091 psa_hash_abort(&op_init);
3092 psa_hash_abort(&op_setup);
3093 psa_hash_abort(&op_finished);
3094 psa_hash_abort(&op_aborted);
3095 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003096}
3097/* END_CASE */
3098
itayzafrir58028322018-10-25 10:22:01 +03003099/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003100void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003101{
Jaeden Amero252ef282019-02-15 14:05:35 +00003102 const uint8_t input[1] = { 0 };
3103
Jaeden Amero769ce272019-01-04 11:48:03 +00003104 /* Test each valid way of initializing the object, except for `= {0}`, as
3105 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3106 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003107 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003109 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3110 psa_mac_operation_t zero;
3111
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003113
Jaeden Amero252ef282019-02-15 14:05:35 +00003114 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 TEST_EQUAL(psa_mac_update(&func,
3116 input, sizeof(input)),
3117 PSA_ERROR_BAD_STATE);
3118 TEST_EQUAL(psa_mac_update(&init,
3119 input, sizeof(input)),
3120 PSA_ERROR_BAD_STATE);
3121 TEST_EQUAL(psa_mac_update(&zero,
3122 input, sizeof(input)),
3123 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003124
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003125 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 PSA_ASSERT(psa_mac_abort(&func));
3127 PSA_ASSERT(psa_mac_abort(&init));
3128 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003129}
3130/* END_CASE */
3131
3132/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003133void mac_setup(int key_type_arg,
3134 data_t *key,
3135 int alg_arg,
3136 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003137{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138 psa_key_type_t key_type = key_type_arg;
3139 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003140 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003141 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003142 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3143#if defined(KNOWN_SUPPORTED_MAC_ALG)
3144 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3145#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3150 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 }
3153 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003154
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003155 /* The operation object should be reusable. */
3156#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3158 smoke_test_key_data,
3159 sizeof(smoke_test_key_data),
3160 KNOWN_SUPPORTED_MAC_ALG,
3161 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 }
3164 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003165#endif
3166
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003169}
3170/* END_CASE */
3171
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003172/* 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 +01003173void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003174{
Ronald Cron5425a212020-08-04 14:58:35 +02003175 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003176 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3177 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003178 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003179 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3180 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3182 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003184 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3185 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3186 size_t sign_mac_length = 0;
3187 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3188 const uint8_t verify_mac[] = {
3189 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3190 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3192 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 PSA_ASSERT(psa_crypto_init());
3195 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3196 psa_set_key_algorithm(&attributes, alg);
3197 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3200 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003201
Jaeden Amero252ef282019-02-15 14:05:35 +00003202 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3204 PSA_ERROR_BAD_STATE);
3205 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003206
3207 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3209 &sign_mac_length),
3210 PSA_ERROR_BAD_STATE);
3211 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003212
3213 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 TEST_EQUAL(psa_mac_verify_finish(&operation,
3215 verify_mac, sizeof(verify_mac)),
3216 PSA_ERROR_BAD_STATE);
3217 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003218
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003219 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3221 ASSERT_OPERATION_IS_ACTIVE(operation);
3222 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3223 PSA_ERROR_BAD_STATE);
3224 ASSERT_OPERATION_IS_INACTIVE(operation);
3225 PSA_ASSERT(psa_mac_abort(&operation));
3226 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003227
Jaeden Amero252ef282019-02-15 14:05:35 +00003228 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3230 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3231 PSA_ASSERT(psa_mac_sign_finish(&operation,
3232 sign_mac, sizeof(sign_mac),
3233 &sign_mac_length));
3234 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3235 PSA_ERROR_BAD_STATE);
3236 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003237
3238 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3240 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3241 PSA_ASSERT(psa_mac_verify_finish(&operation,
3242 verify_mac, sizeof(verify_mac)));
3243 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3244 PSA_ERROR_BAD_STATE);
3245 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003246
3247 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3249 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3250 PSA_ASSERT(psa_mac_sign_finish(&operation,
3251 sign_mac, sizeof(sign_mac),
3252 &sign_mac_length));
3253 TEST_EQUAL(psa_mac_sign_finish(&operation,
3254 sign_mac, sizeof(sign_mac),
3255 &sign_mac_length),
3256 PSA_ERROR_BAD_STATE);
3257 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003258
3259 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3261 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3262 PSA_ASSERT(psa_mac_verify_finish(&operation,
3263 verify_mac, sizeof(verify_mac)));
3264 TEST_EQUAL(psa_mac_verify_finish(&operation,
3265 verify_mac, sizeof(verify_mac)),
3266 PSA_ERROR_BAD_STATE);
3267 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003268
3269 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3271 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3272 ASSERT_OPERATION_IS_ACTIVE(operation);
3273 TEST_EQUAL(psa_mac_verify_finish(&operation,
3274 verify_mac, sizeof(verify_mac)),
3275 PSA_ERROR_BAD_STATE);
3276 ASSERT_OPERATION_IS_INACTIVE(operation);
3277 PSA_ASSERT(psa_mac_abort(&operation));
3278 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003279
3280 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3282 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3283 ASSERT_OPERATION_IS_ACTIVE(operation);
3284 TEST_EQUAL(psa_mac_sign_finish(&operation,
3285 sign_mac, sizeof(sign_mac),
3286 &sign_mac_length),
3287 PSA_ERROR_BAD_STATE);
3288 ASSERT_OPERATION_IS_INACTIVE(operation);
3289 PSA_ASSERT(psa_mac_abort(&operation));
3290 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003293
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003296}
3297/* END_CASE */
3298
3299/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003300void mac_sign_verify_multi(int key_type_arg,
3301 data_t *key_data,
3302 int alg_arg,
3303 data_t *input,
3304 int is_verify,
3305 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003306{
3307 size_t data_part_len = 0;
3308
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003310 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003312
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 if (mac_multipart_internal_func(key_type_arg, key_data,
3314 alg_arg,
3315 input, data_part_len,
3316 expected_mac,
3317 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003318 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003320
3321 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (mac_multipart_internal_func(key_type_arg, key_data,
3325 alg_arg,
3326 input, data_part_len,
3327 expected_mac,
3328 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003329 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003331 }
3332
3333 /* Goto is required to silence warnings about unused labels, as we
3334 * don't actually do any test assertions in this function. */
3335 goto exit;
3336}
3337/* END_CASE */
3338
3339/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003340void mac_sign(int key_type_arg,
3341 data_t *key_data,
3342 int alg_arg,
3343 data_t *input,
3344 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003345{
Ronald Cron5425a212020-08-04 14:58:35 +02003346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003347 psa_key_type_t key_type = key_type_arg;
3348 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003349 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003351 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003352 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003354 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003355 const size_t output_sizes_to_test[] = {
3356 0,
3357 1,
3358 expected_mac->len - 1,
3359 expected_mac->len,
3360 expected_mac->len + 1,
3361 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003364 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3370 psa_set_key_algorithm(&attributes, alg);
3371 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3374 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003375
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003377 const size_t output_size = output_sizes_to_test[i];
3378 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 (output_size >= expected_mac->len ? PSA_SUCCESS :
3380 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 mbedtls_test_set_step(output_size);
3383 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003384
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003385 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 TEST_EQUAL(psa_mac_compute(key, alg,
3387 input->x, input->len,
3388 actual_mac, output_size, &mac_length),
3389 expected_status);
3390 if (expected_status == PSA_SUCCESS) {
3391 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3392 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003393 }
3394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 if (output_size > 0) {
3396 memset(actual_mac, 0, output_size);
3397 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003398
3399 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3401 PSA_ASSERT(psa_mac_update(&operation,
3402 input->x, input->len));
3403 TEST_EQUAL(psa_mac_sign_finish(&operation,
3404 actual_mac, output_size,
3405 &mac_length),
3406 expected_status);
3407 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 if (expected_status == PSA_SUCCESS) {
3410 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3411 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003414 actual_mac = NULL;
3415 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003416
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003417exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 psa_mac_abort(&operation);
3419 psa_destroy_key(key);
3420 PSA_DONE();
3421 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003422}
3423/* END_CASE */
3424
3425/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003426void mac_verify(int key_type_arg,
3427 data_t *key_data,
3428 int alg_arg,
3429 data_t *input,
3430 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003431{
Ronald Cron5425a212020-08-04 14:58:35 +02003432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003433 psa_key_type_t key_type = key_type_arg;
3434 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003435 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003437 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3444 psa_set_key_algorithm(&attributes, alg);
3445 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3448 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003449
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003450 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3452 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003453
3454 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3456 PSA_ASSERT(psa_mac_update(&operation,
3457 input->x, input->len));
3458 PSA_ASSERT(psa_mac_verify_finish(&operation,
3459 expected_mac->x,
3460 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003461
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003462 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 TEST_EQUAL(psa_mac_verify(key, alg,
3464 input->x, input->len,
3465 expected_mac->x,
3466 expected_mac->len - 1),
3467 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003468
3469 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3471 PSA_ASSERT(psa_mac_update(&operation,
3472 input->x, input->len));
3473 TEST_EQUAL(psa_mac_verify_finish(&operation,
3474 expected_mac->x,
3475 expected_mac->len - 1),
3476 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003477
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003478 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3480 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3481 TEST_EQUAL(psa_mac_verify(key, alg,
3482 input->x, input->len,
3483 perturbed_mac, expected_mac->len + 1),
3484 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003485
3486 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3488 PSA_ASSERT(psa_mac_update(&operation,
3489 input->x, input->len));
3490 TEST_EQUAL(psa_mac_verify_finish(&operation,
3491 perturbed_mac,
3492 expected_mac->len + 1),
3493 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003494
3495 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 for (size_t i = 0; i < expected_mac->len; i++) {
3497 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003498 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003499
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 TEST_EQUAL(psa_mac_verify(key, alg,
3501 input->x, input->len,
3502 perturbed_mac, expected_mac->len),
3503 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003504
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3506 PSA_ASSERT(psa_mac_update(&operation,
3507 input->x, input->len));
3508 TEST_EQUAL(psa_mac_verify_finish(&operation,
3509 perturbed_mac,
3510 expected_mac->len),
3511 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003512 perturbed_mac[i] ^= 1;
3513 }
3514
Gilles Peskine8c9def32018-02-08 10:02:12 +01003515exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 psa_mac_abort(&operation);
3517 psa_destroy_key(key);
3518 PSA_DONE();
3519 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003520}
3521/* END_CASE */
3522
3523/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003524void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003525{
Jaeden Ameroab439972019-02-15 14:12:05 +00003526 const uint8_t input[1] = { 0 };
3527 unsigned char output[1] = { 0 };
3528 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003529 /* Test each valid way of initializing the object, except for `= {0}`, as
3530 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3531 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003532 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003534 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3535 psa_cipher_operation_t zero;
3536
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003538
Jaeden Ameroab439972019-02-15 14:12:05 +00003539 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 TEST_EQUAL(psa_cipher_update(&func,
3541 input, sizeof(input),
3542 output, sizeof(output),
3543 &output_length),
3544 PSA_ERROR_BAD_STATE);
3545 TEST_EQUAL(psa_cipher_update(&init,
3546 input, sizeof(input),
3547 output, sizeof(output),
3548 &output_length),
3549 PSA_ERROR_BAD_STATE);
3550 TEST_EQUAL(psa_cipher_update(&zero,
3551 input, sizeof(input),
3552 output, sizeof(output),
3553 &output_length),
3554 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003555
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003556 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 PSA_ASSERT(psa_cipher_abort(&func));
3558 PSA_ASSERT(psa_cipher_abort(&init));
3559 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003560}
3561/* END_CASE */
3562
3563/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003564void cipher_setup(int key_type_arg,
3565 data_t *key,
3566 int alg_arg,
3567 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003568{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003569 psa_key_type_t key_type = key_type_arg;
3570 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003571 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003572 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003573 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003574#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003575 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3576#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003577
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3581 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003582 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 }
3584 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003585
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003586 /* The operation object should be reusable. */
3587#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3589 smoke_test_key_data,
3590 sizeof(smoke_test_key_data),
3591 KNOWN_SUPPORTED_CIPHER_ALG,
3592 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003593 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 }
3595 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003596#endif
3597
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003598exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 psa_cipher_abort(&operation);
3600 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003601}
3602/* END_CASE */
3603
Ronald Cronee414c72021-03-18 18:50:08 +01003604/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003605void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003606{
Ronald Cron5425a212020-08-04 14:58:35 +02003607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003608 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3609 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003611 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003612 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003613 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003614 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003615 0xaa, 0xaa, 0xaa, 0xaa
3616 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003617 const uint8_t text[] = {
3618 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003619 0xbb, 0xbb, 0xbb, 0xbb
3620 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003621 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003622 size_t length = 0;
3623
Gilles Peskine449bd832023-01-11 14:50:10 +01003624 PSA_ASSERT(psa_crypto_init());
3625 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3626 psa_set_key_algorithm(&attributes, alg);
3627 psa_set_key_type(&attributes, key_type);
3628 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3629 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003630
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003631 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003632 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3633 ASSERT_OPERATION_IS_ACTIVE(operation);
3634 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3635 PSA_ERROR_BAD_STATE);
3636 ASSERT_OPERATION_IS_INACTIVE(operation);
3637 PSA_ASSERT(psa_cipher_abort(&operation));
3638 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003639
3640 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3642 ASSERT_OPERATION_IS_ACTIVE(operation);
3643 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3644 PSA_ERROR_BAD_STATE);
3645 ASSERT_OPERATION_IS_INACTIVE(operation);
3646 PSA_ASSERT(psa_cipher_abort(&operation));
3647 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003648
Jaeden Ameroab439972019-02-15 14:12:05 +00003649 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3651 buffer, sizeof(buffer),
3652 &length),
3653 PSA_ERROR_BAD_STATE);
3654 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003655
3656 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3658 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3659 buffer, sizeof(buffer),
3660 &length));
3661 ASSERT_OPERATION_IS_ACTIVE(operation);
3662 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3663 buffer, sizeof(buffer),
3664 &length),
3665 PSA_ERROR_BAD_STATE);
3666 ASSERT_OPERATION_IS_INACTIVE(operation);
3667 PSA_ASSERT(psa_cipher_abort(&operation));
3668 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003669
3670 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003671 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3672 PSA_ASSERT(psa_cipher_set_iv(&operation,
3673 iv, sizeof(iv)));
3674 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3675 buffer, sizeof(buffer),
3676 &length),
3677 PSA_ERROR_BAD_STATE);
3678 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003679
3680 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 TEST_EQUAL(psa_cipher_set_iv(&operation,
3682 iv, sizeof(iv)),
3683 PSA_ERROR_BAD_STATE);
3684 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003685
3686 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003687 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3688 PSA_ASSERT(psa_cipher_set_iv(&operation,
3689 iv, sizeof(iv)));
3690 ASSERT_OPERATION_IS_ACTIVE(operation);
3691 TEST_EQUAL(psa_cipher_set_iv(&operation,
3692 iv, sizeof(iv)),
3693 PSA_ERROR_BAD_STATE);
3694 ASSERT_OPERATION_IS_INACTIVE(operation);
3695 PSA_ASSERT(psa_cipher_abort(&operation));
3696 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003697
3698 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3700 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3701 buffer, sizeof(buffer),
3702 &length));
3703 TEST_EQUAL(psa_cipher_set_iv(&operation,
3704 iv, sizeof(iv)),
3705 PSA_ERROR_BAD_STATE);
3706 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003707
3708 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003709 TEST_EQUAL(psa_cipher_update(&operation,
3710 text, sizeof(text),
3711 buffer, sizeof(buffer),
3712 &length),
3713 PSA_ERROR_BAD_STATE);
3714 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003715
3716 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3718 ASSERT_OPERATION_IS_ACTIVE(operation);
3719 TEST_EQUAL(psa_cipher_update(&operation,
3720 text, sizeof(text),
3721 buffer, sizeof(buffer),
3722 &length),
3723 PSA_ERROR_BAD_STATE);
3724 ASSERT_OPERATION_IS_INACTIVE(operation);
3725 PSA_ASSERT(psa_cipher_abort(&operation));
3726 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003727
3728 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3730 PSA_ASSERT(psa_cipher_set_iv(&operation,
3731 iv, sizeof(iv)));
3732 PSA_ASSERT(psa_cipher_finish(&operation,
3733 buffer, sizeof(buffer), &length));
3734 TEST_EQUAL(psa_cipher_update(&operation,
3735 text, sizeof(text),
3736 buffer, sizeof(buffer),
3737 &length),
3738 PSA_ERROR_BAD_STATE);
3739 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003740
3741 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 TEST_EQUAL(psa_cipher_finish(&operation,
3743 buffer, sizeof(buffer), &length),
3744 PSA_ERROR_BAD_STATE);
3745 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003746
3747 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003749 /* Not calling update means we are encrypting an empty buffer, which is OK
3750 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 ASSERT_OPERATION_IS_ACTIVE(operation);
3752 TEST_EQUAL(psa_cipher_finish(&operation,
3753 buffer, sizeof(buffer), &length),
3754 PSA_ERROR_BAD_STATE);
3755 ASSERT_OPERATION_IS_INACTIVE(operation);
3756 PSA_ASSERT(psa_cipher_abort(&operation));
3757 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003758
3759 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3761 PSA_ASSERT(psa_cipher_set_iv(&operation,
3762 iv, sizeof(iv)));
3763 PSA_ASSERT(psa_cipher_finish(&operation,
3764 buffer, sizeof(buffer), &length));
3765 TEST_EQUAL(psa_cipher_finish(&operation,
3766 buffer, sizeof(buffer), &length),
3767 PSA_ERROR_BAD_STATE);
3768 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003769
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003771
Jaeden Ameroab439972019-02-15 14:12:05 +00003772exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 psa_cipher_abort(&operation);
3774 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003775}
3776/* END_CASE */
3777
3778/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003779void cipher_encrypt_fail(int alg_arg,
3780 int key_type_arg,
3781 data_t *key_data,
3782 data_t *input,
3783 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003784{
Ronald Cron5425a212020-08-04 14:58:35 +02003785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003786 psa_status_t status;
3787 psa_key_type_t key_type = key_type_arg;
3788 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003789 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003791 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3792 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003793 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003794 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003795 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003796 size_t function_output_length;
3797 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3799
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 if (PSA_ERROR_BAD_STATE != expected_status) {
3801 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3804 psa_set_key_algorithm(&attributes, alg);
3805 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003806
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3808 input->len);
3809 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3812 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003813 }
3814
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003815 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3817 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003818
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003821 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 status = psa_cipher_encrypt_setup(&operation, key, alg);
3823 if (status == PSA_SUCCESS) {
3824 if (alg != PSA_ALG_ECB_NO_PADDING) {
3825 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3826 iv, iv_size,
3827 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003828 }
3829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 status = psa_cipher_update(&operation, input->x, input->len,
3831 output, output_buffer_size,
3832 &function_output_length);
3833 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003834 output_length += function_output_length;
3835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 status = psa_cipher_finish(&operation, output + output_length,
3837 output_buffer_size - output_length,
3838 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 TEST_EQUAL(status, expected_status);
3841 } else {
3842 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003843 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 } else {
3845 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003846 }
3847
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 psa_cipher_abort(&operation);
3850 mbedtls_free(output);
3851 psa_destroy_key(key);
3852 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003853}
3854/* END_CASE */
3855
3856/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003857void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3858 data_t *input, int iv_length,
3859 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003860{
3861 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3862 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3864 size_t output_buffer_size = 0;
3865 unsigned char *output = NULL;
3866
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3868 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003869
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003871
Gilles Peskine449bd832023-01-11 14:50:10 +01003872 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3873 psa_set_key_algorithm(&attributes, alg);
3874 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003875
Gilles Peskine449bd832023-01-11 14:50:10 +01003876 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3877 &key));
3878 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3879 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3880 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003881
3882exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003883 psa_cipher_abort(&operation);
3884 mbedtls_free(output);
3885 psa_destroy_key(key);
3886 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003887}
3888/* END_CASE */
3889
3890/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003891void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3892 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003893{
3894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3895 psa_key_type_t key_type = key_type_arg;
3896 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003897 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3898 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003899 unsigned char *output = NULL;
3900 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003901 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3903
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003905
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003906 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 TEST_LE_U(ciphertext->len,
3908 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3909 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3910 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3911 TEST_LE_U(plaintext->len,
3912 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3913 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3914 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003915
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003916
3917 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 psa_set_key_usage_flags(&attributes,
3919 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3920 psa_set_key_algorithm(&attributes, alg);
3921 psa_set_key_type(&attributes, key_type);
3922 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3923 &key));
3924 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3925 plaintext->len);
3926 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003927
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003928 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3930 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3931 PSA_ERROR_BAD_STATE);
3932 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3933 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3934 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003935
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003936 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3938 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3939 &length),
3940 PSA_ERROR_BAD_STATE);
3941 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3942 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3943 &length),
3944 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003945
Gilles Peskine286c3142022-04-20 17:09:38 +02003946 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003948 output_length = 0;
3949 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 PSA_ASSERT(psa_cipher_update(&operation,
3951 plaintext->x, plaintext->len,
3952 output, output_buffer_size,
3953 &length));
3954 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003955 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 PSA_ASSERT(psa_cipher_finish(&operation,
3957 mbedtls_buffer_offset(output, output_length),
3958 output_buffer_size - output_length,
3959 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003960 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3962 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003963
Gilles Peskine286c3142022-04-20 17:09:38 +02003964 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003966 output_length = 0;
3967 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 PSA_ASSERT(psa_cipher_update(&operation,
3969 ciphertext->x, ciphertext->len,
3970 output, output_buffer_size,
3971 &length));
3972 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003973 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 PSA_ASSERT(psa_cipher_finish(&operation,
3975 mbedtls_buffer_offset(output, output_length),
3976 output_buffer_size - output_length,
3977 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003978 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 ASSERT_COMPARE(plaintext->x, plaintext->len,
3980 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003981
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003982 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003983 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3985 output, output_buffer_size,
3986 &output_length));
3987 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3988 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003989
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003990 /* One-shot decryption */
3991 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3993 output, output_buffer_size,
3994 &output_length));
3995 ASSERT_COMPARE(plaintext->x, plaintext->len,
3996 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003997
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003998exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 PSA_ASSERT(psa_cipher_abort(&operation));
4000 mbedtls_free(output);
4001 psa_cipher_abort(&operation);
4002 psa_destroy_key(key);
4003 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004004}
4005/* END_CASE */
4006
4007/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004008void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004009{
4010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4011 psa_algorithm_t alg = alg_arg;
4012 psa_key_type_t key_type = key_type_arg;
4013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4014 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4015 psa_status_t status;
4016
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4020 psa_set_key_algorithm(&attributes, alg);
4021 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004022
4023 /* Usage of either of these two size macros would cause divide by zero
4024 * with incorrect key types previously. Input length should be irrelevant
4025 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4027 0);
4028 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004029
4030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4032 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004033
4034 /* Should fail due to invalid alg type (to support invalid key type).
4035 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004037
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004039
4040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 psa_cipher_abort(&operation);
4042 psa_destroy_key(key);
4043 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004044}
4045/* END_CASE */
4046
4047/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004048void cipher_encrypt_validation(int alg_arg,
4049 int key_type_arg,
4050 data_t *key_data,
4051 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004052{
4053 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4054 psa_key_type_t key_type = key_type_arg;
4055 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004057 unsigned char *output1 = NULL;
4058 size_t output1_buffer_size = 0;
4059 size_t output1_length = 0;
4060 unsigned char *output2 = NULL;
4061 size_t output2_buffer_size = 0;
4062 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004063 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004064 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4070 psa_set_key_algorithm(&attributes, alg);
4071 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4074 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4075 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4076 ASSERT_ALLOC(output1, output1_buffer_size);
4077 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4080 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004081
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004082 /* The one-shot cipher encryption uses generated iv so validating
4083 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4085 output1_buffer_size, &output1_length));
4086 TEST_LE_U(output1_length,
4087 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4088 TEST_LE_U(output1_length,
4089 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4092 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 PSA_ASSERT(psa_cipher_update(&operation,
4095 input->x, input->len,
4096 output2, output2_buffer_size,
4097 &function_output_length));
4098 TEST_LE_U(function_output_length,
4099 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4100 TEST_LE_U(function_output_length,
4101 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004102 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 PSA_ASSERT(psa_cipher_finish(&operation,
4105 output2 + output2_length,
4106 output2_buffer_size - output2_length,
4107 &function_output_length));
4108 TEST_LE_U(function_output_length,
4109 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4110 TEST_LE_U(function_output_length,
4111 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004112 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 PSA_ASSERT(psa_cipher_abort(&operation));
4115 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4116 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004117
Gilles Peskine50e586b2018-06-08 14:28:46 +02004118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 psa_cipher_abort(&operation);
4120 mbedtls_free(output1);
4121 mbedtls_free(output2);
4122 psa_destroy_key(key);
4123 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004124}
4125/* END_CASE */
4126
4127/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004128void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4129 data_t *key_data, data_t *iv,
4130 data_t *input,
4131 int first_part_size_arg,
4132 int output1_length_arg, int output2_length_arg,
4133 data_t *expected_output,
4134 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004135{
Ronald Cron5425a212020-08-04 14:58:35 +02004136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004137 psa_key_type_t key_type = key_type_arg;
4138 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004139 psa_status_t status;
4140 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004141 size_t first_part_size = first_part_size_arg;
4142 size_t output1_length = output1_length_arg;
4143 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004144 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004145 size_t output_buffer_size = 0;
4146 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004147 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004148 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004150
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4154 psa_set_key_algorithm(&attributes, alg);
4155 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4158 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 if (iv->len > 0) {
4163 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004164 }
4165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4167 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4168 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 TEST_LE_U(first_part_size, input->len);
4171 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4172 output, output_buffer_size,
4173 &function_output_length));
4174 TEST_ASSERT(function_output_length == output1_length);
4175 TEST_LE_U(function_output_length,
4176 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4177 TEST_LE_U(function_output_length,
4178 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004179 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (first_part_size < input->len) {
4182 PSA_ASSERT(psa_cipher_update(&operation,
4183 input->x + first_part_size,
4184 input->len - first_part_size,
4185 (output_buffer_size == 0 ? NULL :
4186 output + total_output_length),
4187 output_buffer_size - total_output_length,
4188 &function_output_length));
4189 TEST_ASSERT(function_output_length == output2_length);
4190 TEST_LE_U(function_output_length,
4191 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4192 alg,
4193 input->len - first_part_size));
4194 TEST_LE_U(function_output_length,
4195 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004196 total_output_length += function_output_length;
4197 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 status = psa_cipher_finish(&operation,
4200 (output_buffer_size == 0 ? NULL :
4201 output + total_output_length),
4202 output_buffer_size - total_output_length,
4203 &function_output_length);
4204 TEST_LE_U(function_output_length,
4205 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4206 TEST_LE_U(function_output_length,
4207 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004208 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 if (expected_status == PSA_SUCCESS) {
4212 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 ASSERT_COMPARE(expected_output->x, expected_output->len,
4215 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004216 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004217
4218exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 psa_cipher_abort(&operation);
4220 mbedtls_free(output);
4221 psa_destroy_key(key);
4222 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004223}
4224/* END_CASE */
4225
4226/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004227void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4228 data_t *key_data, data_t *iv,
4229 data_t *input,
4230 int first_part_size_arg,
4231 int output1_length_arg, int output2_length_arg,
4232 data_t *expected_output,
4233 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234{
Ronald Cron5425a212020-08-04 14:58:35 +02004235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004236 psa_key_type_t key_type = key_type_arg;
4237 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004238 psa_status_t status;
4239 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004240 size_t first_part_size = first_part_size_arg;
4241 size_t output1_length = output1_length_arg;
4242 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004243 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244 size_t output_buffer_size = 0;
4245 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004246 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004247 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4253 psa_set_key_algorithm(&attributes, alg);
4254 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4257 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 if (iv->len > 0) {
4262 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004263 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4266 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4267 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 TEST_LE_U(first_part_size, input->len);
4270 PSA_ASSERT(psa_cipher_update(&operation,
4271 input->x, first_part_size,
4272 output, output_buffer_size,
4273 &function_output_length));
4274 TEST_ASSERT(function_output_length == output1_length);
4275 TEST_LE_U(function_output_length,
4276 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4277 TEST_LE_U(function_output_length,
4278 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004279 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (first_part_size < input->len) {
4282 PSA_ASSERT(psa_cipher_update(&operation,
4283 input->x + first_part_size,
4284 input->len - first_part_size,
4285 (output_buffer_size == 0 ? NULL :
4286 output + total_output_length),
4287 output_buffer_size - total_output_length,
4288 &function_output_length));
4289 TEST_ASSERT(function_output_length == output2_length);
4290 TEST_LE_U(function_output_length,
4291 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4292 alg,
4293 input->len - first_part_size));
4294 TEST_LE_U(function_output_length,
4295 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004296 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004297 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 status = psa_cipher_finish(&operation,
4300 (output_buffer_size == 0 ? NULL :
4301 output + total_output_length),
4302 output_buffer_size - total_output_length,
4303 &function_output_length);
4304 TEST_LE_U(function_output_length,
4305 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4306 TEST_LE_U(function_output_length,
4307 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004308 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004310
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 if (expected_status == PSA_SUCCESS) {
4312 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004313
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 ASSERT_COMPARE(expected_output->x, expected_output->len,
4315 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316 }
4317
Gilles Peskine50e586b2018-06-08 14:28:46 +02004318exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 psa_cipher_abort(&operation);
4320 mbedtls_free(output);
4321 psa_destroy_key(key);
4322 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004323}
4324/* END_CASE */
4325
Gilles Peskine50e586b2018-06-08 14:28:46 +02004326/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004327void cipher_decrypt_fail(int alg_arg,
4328 int key_type_arg,
4329 data_t *key_data,
4330 data_t *iv,
4331 data_t *input_arg,
4332 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004333{
4334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4335 psa_status_t status;
4336 psa_key_type_t key_type = key_type_arg;
4337 psa_algorithm_t alg = alg_arg;
4338 psa_status_t expected_status = expected_status_arg;
4339 unsigned char *input = NULL;
4340 size_t input_buffer_size = 0;
4341 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004342 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004343 size_t output_buffer_size = 0;
4344 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004345 size_t function_output_length;
4346 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (PSA_ERROR_BAD_STATE != expected_status) {
4350 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004351
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4353 psa_set_key_algorithm(&attributes, alg);
4354 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4357 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004358 }
4359
4360 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4362 if (input_buffer_size > 0) {
4363 ASSERT_ALLOC(input, input_buffer_size);
4364 memcpy(input, iv->x, iv->len);
4365 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004366 }
4367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4369 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004370
Neil Armstrong66a479f2022-02-07 15:41:19 +01004371 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4373 output_buffer_size, &output_length);
4374 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004375
Neil Armstrong66a479f2022-02-07 15:41:19 +01004376 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 status = psa_cipher_decrypt_setup(&operation, key, alg);
4378 if (status == PSA_SUCCESS) {
4379 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4380 input_arg->len) +
4381 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4382 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 if (iv->len > 0) {
4385 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (status != PSA_SUCCESS) {
4388 TEST_EQUAL(status, expected_status);
4389 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004390 }
4391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 if (status == PSA_SUCCESS) {
4393 status = psa_cipher_update(&operation,
4394 input_arg->x, input_arg->len,
4395 output_multi, output_buffer_size,
4396 &function_output_length);
4397 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004398 output_length = function_output_length;
4399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 status = psa_cipher_finish(&operation,
4401 output_multi + output_length,
4402 output_buffer_size - output_length,
4403 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 TEST_EQUAL(status, expected_status);
4406 } else {
4407 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 } else {
4410 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004411 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 } else {
4413 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004414 }
4415
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 psa_cipher_abort(&operation);
4418 mbedtls_free(input);
4419 mbedtls_free(output);
4420 mbedtls_free(output_multi);
4421 psa_destroy_key(key);
4422 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004423}
4424/* END_CASE */
4425
4426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004427void cipher_decrypt(int alg_arg,
4428 int key_type_arg,
4429 data_t *key_data,
4430 data_t *iv,
4431 data_t *input_arg,
4432 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004433{
4434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4435 psa_key_type_t key_type = key_type_arg;
4436 psa_algorithm_t alg = alg_arg;
4437 unsigned char *input = NULL;
4438 size_t input_buffer_size = 0;
4439 unsigned char *output = NULL;
4440 size_t output_buffer_size = 0;
4441 size_t output_length = 0;
4442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4447 psa_set_key_algorithm(&attributes, alg);
4448 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004449
4450 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4452 if (input_buffer_size > 0) {
4453 ASSERT_ALLOC(input, input_buffer_size);
4454 memcpy(input, iv->x, iv->len);
4455 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004456 }
4457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4459 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4462 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4465 output_buffer_size, &output_length));
4466 TEST_LE_U(output_length,
4467 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4468 TEST_LE_U(output_length,
4469 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 ASSERT_COMPARE(expected_output->x, expected_output->len,
4472 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004473exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 mbedtls_free(input);
4475 mbedtls_free(output);
4476 psa_destroy_key(key);
4477 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004478}
4479/* END_CASE */
4480
4481/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004482void cipher_verify_output(int alg_arg,
4483 int key_type_arg,
4484 data_t *key_data,
4485 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004486{
Ronald Cron5425a212020-08-04 14:58:35 +02004487 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004488 psa_key_type_t key_type = key_type_arg;
4489 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004490 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004491 size_t output1_size = 0;
4492 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004493 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004494 size_t output2_size = 0;
4495 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004499
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4501 psa_set_key_algorithm(&attributes, alg);
4502 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4505 &key));
4506 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4507 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4510 output1, output1_size,
4511 &output1_length));
4512 TEST_LE_U(output1_length,
4513 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4514 TEST_LE_U(output1_length,
4515 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004516
4517 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4521 output2, output2_size,
4522 &output2_length));
4523 TEST_LE_U(output2_length,
4524 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4525 TEST_LE_U(output2_length,
4526 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004529
4530exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 mbedtls_free(output1);
4532 mbedtls_free(output2);
4533 psa_destroy_key(key);
4534 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004535}
4536/* END_CASE */
4537
4538/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004539void cipher_verify_output_multipart(int alg_arg,
4540 int key_type_arg,
4541 data_t *key_data,
4542 data_t *input,
4543 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004544{
Ronald Cron5425a212020-08-04 14:58:35 +02004545 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004546 psa_key_type_t key_type = key_type_arg;
4547 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004548 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004550 size_t iv_size = 16;
4551 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004552 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004553 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004554 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004555 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004556 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004557 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004558 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004559 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4560 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4566 psa_set_key_algorithm(&attributes, alg);
4567 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4570 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4573 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 if (alg != PSA_ALG_ECB_NO_PADDING) {
4576 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4577 iv, iv_size,
4578 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004579 }
4580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4582 TEST_LE_U(output1_buffer_size,
4583 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4584 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4589 output1, output1_buffer_size,
4590 &function_output_length));
4591 TEST_LE_U(function_output_length,
4592 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4593 TEST_LE_U(function_output_length,
4594 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004595 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 PSA_ASSERT(psa_cipher_update(&operation1,
4598 input->x + first_part_size,
4599 input->len - first_part_size,
4600 output1, output1_buffer_size,
4601 &function_output_length));
4602 TEST_LE_U(function_output_length,
4603 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4604 alg,
4605 input->len - first_part_size));
4606 TEST_LE_U(function_output_length,
4607 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004608 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 PSA_ASSERT(psa_cipher_finish(&operation1,
4611 output1 + output1_length,
4612 output1_buffer_size - output1_length,
4613 &function_output_length));
4614 TEST_LE_U(function_output_length,
4615 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4616 TEST_LE_U(function_output_length,
4617 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004618 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004621
Gilles Peskine048b7f02018-06-08 14:20:49 +02004622 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 TEST_LE_U(output2_buffer_size,
4624 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4625 TEST_LE_U(output2_buffer_size,
4626 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4627 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (iv_length > 0) {
4630 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4631 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004632 }
Moran Pekerded84402018-06-06 16:36:50 +03004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4635 output2, output2_buffer_size,
4636 &function_output_length));
4637 TEST_LE_U(function_output_length,
4638 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4639 TEST_LE_U(function_output_length,
4640 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004641 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 PSA_ASSERT(psa_cipher_update(&operation2,
4644 output1 + first_part_size,
4645 output1_length - first_part_size,
4646 output2, output2_buffer_size,
4647 &function_output_length));
4648 TEST_LE_U(function_output_length,
4649 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4650 alg,
4651 output1_length - first_part_size));
4652 TEST_LE_U(function_output_length,
4653 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004654 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 PSA_ASSERT(psa_cipher_finish(&operation2,
4657 output2 + output2_length,
4658 output2_buffer_size - output2_length,
4659 &function_output_length));
4660 TEST_LE_U(function_output_length,
4661 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4662 TEST_LE_U(function_output_length,
4663 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004664 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004669
4670exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 psa_cipher_abort(&operation1);
4672 psa_cipher_abort(&operation2);
4673 mbedtls_free(output1);
4674 mbedtls_free(output2);
4675 psa_destroy_key(key);
4676 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004677}
4678/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004679
Gilles Peskine20035e32018-02-03 22:44:14 +01004680/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004681void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4682 int alg_arg,
4683 data_t *nonce,
4684 data_t *additional_data,
4685 data_t *input_data,
4686 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004687{
Ronald Cron5425a212020-08-04 14:58:35 +02004688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004689 psa_key_type_t key_type = key_type_arg;
4690 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004691 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004692 unsigned char *output_data = NULL;
4693 size_t output_size = 0;
4694 size_t output_length = 0;
4695 unsigned char *output_data2 = NULL;
4696 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004697 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004698 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4704 psa_set_key_algorithm(&attributes, alg);
4705 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4708 &key));
4709 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4710 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4713 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004714 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4715 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4717 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4718 TEST_EQUAL(output_size,
4719 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4720 TEST_LE_U(output_size,
4721 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004722 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 status = psa_aead_encrypt(key, alg,
4726 nonce->x, nonce->len,
4727 additional_data->x,
4728 additional_data->len,
4729 input_data->x, input_data->len,
4730 output_data, output_size,
4731 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004732
4733 /* If the operation is not supported, just skip and not fail in case the
4734 * encryption involves a common limitation of cryptography hardwares and
4735 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (status == PSA_ERROR_NOT_SUPPORTED) {
4737 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4738 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004739 }
4740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 if (PSA_SUCCESS == expected_result) {
4744 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004745
Gilles Peskine003a4a92019-05-14 16:09:40 +02004746 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4747 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 TEST_EQUAL(input_data->len,
4749 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 TEST_LE_U(input_data->len,
4752 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 TEST_EQUAL(psa_aead_decrypt(key, alg,
4755 nonce->x, nonce->len,
4756 additional_data->x,
4757 additional_data->len,
4758 output_data, output_length,
4759 output_data2, output_length,
4760 &output_length2),
4761 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 ASSERT_COMPARE(input_data->x, input_data->len,
4764 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004765 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004766
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 psa_destroy_key(key);
4769 mbedtls_free(output_data);
4770 mbedtls_free(output_data2);
4771 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004772}
4773/* END_CASE */
4774
4775/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004776void aead_encrypt(int key_type_arg, data_t *key_data,
4777 int alg_arg,
4778 data_t *nonce,
4779 data_t *additional_data,
4780 data_t *input_data,
4781 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004782{
Ronald Cron5425a212020-08-04 14:58:35 +02004783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004784 psa_key_type_t key_type = key_type_arg;
4785 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004786 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004787 unsigned char *output_data = NULL;
4788 size_t output_size = 0;
4789 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004791 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4796 psa_set_key_algorithm(&attributes, alg);
4797 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4800 &key));
4801 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4802 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4805 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004806 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4807 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 TEST_EQUAL(output_size,
4809 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4810 TEST_LE_U(output_size,
4811 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4812 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 status = psa_aead_encrypt(key, alg,
4815 nonce->x, nonce->len,
4816 additional_data->x, additional_data->len,
4817 input_data->x, input_data->len,
4818 output_data, output_size,
4819 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004820
Ronald Cron28a45ed2021-02-09 20:35:42 +01004821 /* If the operation is not supported, just skip and not fail in case the
4822 * encryption involves a common limitation of cryptography hardwares and
4823 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (status == PSA_ERROR_NOT_SUPPORTED) {
4825 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4826 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004827 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 PSA_ASSERT(status);
4830 ASSERT_COMPARE(expected_result->x, expected_result->len,
4831 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004832
Gilles Peskinea1cac842018-06-11 19:33:02 +02004833exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 psa_destroy_key(key);
4835 mbedtls_free(output_data);
4836 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004837}
4838/* END_CASE */
4839
4840/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004841void aead_decrypt(int key_type_arg, data_t *key_data,
4842 int alg_arg,
4843 data_t *nonce,
4844 data_t *additional_data,
4845 data_t *input_data,
4846 data_t *expected_data,
4847 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848{
Ronald Cron5425a212020-08-04 14:58:35 +02004849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850 psa_key_type_t key_type = key_type_arg;
4851 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004852 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004853 unsigned char *output_data = NULL;
4854 size_t output_size = 0;
4855 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004857 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004858 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4863 psa_set_key_algorithm(&attributes, alg);
4864 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4867 &key));
4868 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4869 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4872 alg);
4873 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4874 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004875 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4876 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 TEST_EQUAL(output_size,
4878 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4879 TEST_LE_U(output_size,
4880 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004881 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 status = psa_aead_decrypt(key, alg,
4885 nonce->x, nonce->len,
4886 additional_data->x,
4887 additional_data->len,
4888 input_data->x, input_data->len,
4889 output_data, output_size,
4890 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004891
Ronald Cron28a45ed2021-02-09 20:35:42 +01004892 /* If the operation is not supported, just skip and not fail in case the
4893 * decryption involves a common limitation of cryptography hardwares and
4894 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 if (status == PSA_ERROR_NOT_SUPPORTED) {
4896 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4897 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004898 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 if (expected_result == PSA_SUCCESS) {
4903 ASSERT_COMPARE(expected_data->x, expected_data->len,
4904 output_data, output_length);
4905 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004906
Gilles Peskinea1cac842018-06-11 19:33:02 +02004907exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 psa_destroy_key(key);
4909 mbedtls_free(output_data);
4910 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004911}
4912/* END_CASE */
4913
4914/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004915void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4916 int alg_arg,
4917 data_t *nonce,
4918 data_t *additional_data,
4919 data_t *input_data,
4920 int do_set_lengths,
4921 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004922{
Paul Elliottd3f82412021-06-16 16:52:21 +01004923 size_t ad_part_len = 0;
4924 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004925 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4928 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (do_set_lengths) {
4931 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004932 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004934 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004936 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004937
4938 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (!aead_multipart_internal_func(key_type_arg, key_data,
4940 alg_arg, nonce,
4941 additional_data,
4942 ad_part_len,
4943 input_data, -1,
4944 set_lengths_method,
4945 expected_output,
4946 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004947 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004948 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 /* length(0) part, length(ad_part_len) part, length(0) part... */
4951 mbedtls_test_set_step(1000 + ad_part_len);
4952
4953 if (!aead_multipart_internal_func(key_type_arg, key_data,
4954 alg_arg, nonce,
4955 additional_data,
4956 ad_part_len,
4957 input_data, -1,
4958 set_lengths_method,
4959 expected_output,
4960 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004961 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 }
4963 }
4964
4965 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4966 /* Split data into length(data_part_len) parts. */
4967 mbedtls_test_set_step(2000 + data_part_len);
4968
4969 if (do_set_lengths) {
4970 if (data_part_len & 0x01) {
4971 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4972 } else {
4973 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4974 }
4975 }
4976
4977 if (!aead_multipart_internal_func(key_type_arg, key_data,
4978 alg_arg, nonce,
4979 additional_data, -1,
4980 input_data, data_part_len,
4981 set_lengths_method,
4982 expected_output,
4983 1, 0)) {
4984 break;
4985 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004986
4987 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (!aead_multipart_internal_func(key_type_arg, key_data,
4991 alg_arg, nonce,
4992 additional_data, -1,
4993 input_data, data_part_len,
4994 set_lengths_method,
4995 expected_output,
4996 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004997 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005000
Paul Elliott8fc45162021-06-23 16:06:01 +01005001 /* Goto is required to silence warnings about unused labels, as we
5002 * don't actually do any test assertions in this function. */
5003 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005004}
5005/* END_CASE */
5006
5007/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5009 int alg_arg,
5010 data_t *nonce,
5011 data_t *additional_data,
5012 data_t *input_data,
5013 int do_set_lengths,
5014 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005015{
Paul Elliottd3f82412021-06-16 16:52:21 +01005016 size_t ad_part_len = 0;
5017 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005018 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005021 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 if (do_set_lengths) {
5025 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005026 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005028 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005030 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 if (!aead_multipart_internal_func(key_type_arg, key_data,
5033 alg_arg, nonce,
5034 additional_data,
5035 ad_part_len,
5036 input_data, -1,
5037 set_lengths_method,
5038 expected_output,
5039 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005040 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005042
5043 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (!aead_multipart_internal_func(key_type_arg, key_data,
5047 alg_arg, nonce,
5048 additional_data,
5049 ad_part_len,
5050 input_data, -1,
5051 set_lengths_method,
5052 expected_output,
5053 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005054 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005056 }
5057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005059 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005061
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 if (do_set_lengths) {
5063 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005064 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005066 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005068 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (!aead_multipart_internal_func(key_type_arg, key_data,
5071 alg_arg, nonce,
5072 additional_data, -1,
5073 input_data, data_part_len,
5074 set_lengths_method,
5075 expected_output,
5076 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005077 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079
5080 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (!aead_multipart_internal_func(key_type_arg, key_data,
5084 alg_arg, nonce,
5085 additional_data, -1,
5086 input_data, data_part_len,
5087 set_lengths_method,
5088 expected_output,
5089 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005092 }
5093
Paul Elliott8fc45162021-06-23 16:06:01 +01005094 /* Goto is required to silence warnings about unused labels, as we
5095 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005096 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005097}
5098/* END_CASE */
5099
5100/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005101void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5102 int alg_arg,
5103 int nonce_length,
5104 int expected_nonce_length_arg,
5105 data_t *additional_data,
5106 data_t *input_data,
5107 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005108{
5109
5110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5111 psa_key_type_t key_type = key_type_arg;
5112 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005113 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005114 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5116 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005117 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005118 size_t actual_nonce_length = 0;
5119 size_t expected_nonce_length = expected_nonce_length_arg;
5120 unsigned char *output = NULL;
5121 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005122 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005123 size_t ciphertext_size = 0;
5124 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005125 size_t tag_length = 0;
5126 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5131 psa_set_key_algorithm(&attributes, alg);
5132 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5135 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005150
5151 /* If the operation is not supported, just skip and not fail in case the
5152 * encryption involves a common limitation of cryptography hardwares and
5153 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 if (status == PSA_ERROR_NOT_SUPPORTED) {
5155 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5156 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005157 }
5158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5162 nonce_length,
5163 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005164
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (expected_status == PSA_SUCCESS) {
5170 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5171 alg));
5172 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005175
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005177 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5179 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5182 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5185 output, output_size,
5186 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5189 &ciphertext_length, tag_buffer,
5190 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005191 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005192
5193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 psa_destroy_key(key);
5195 mbedtls_free(output);
5196 mbedtls_free(ciphertext);
5197 psa_aead_abort(&operation);
5198 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005199}
5200/* END_CASE */
5201
5202/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005203void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5204 int alg_arg,
5205 int nonce_length_arg,
5206 int set_lengths_method_arg,
5207 data_t *additional_data,
5208 data_t *input_data,
5209 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005210{
5211
5212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5213 psa_key_type_t key_type = key_type_arg;
5214 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005215 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005216 uint8_t *nonce_buffer = NULL;
5217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5218 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5219 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005220 unsigned char *output = NULL;
5221 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005222 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005223 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005224 size_t ciphertext_size = 0;
5225 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005226 size_t tag_length = 0;
5227 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005228 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005229 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5234 psa_set_key_algorithm(&attributes, alg);
5235 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5238 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005239
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005245
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
5254 /* If the operation is not supported, just skip and not fail in case the
5255 * encryption involves a common limitation of cryptography hardwares and
5256 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 if (status == PSA_ERROR_NOT_SUPPORTED) {
5258 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5259 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005260 }
5261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005263
Paul Elliott4023ffd2021-09-10 16:21:22 +01005264 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (nonce_length_arg == -1) {
5266 /* Arbitrary size buffer, to test zero length valid buffer. */
5267 ASSERT_ALLOC(nonce_buffer, 4);
5268 nonce_length = 0;
5269 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005270 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 nonce_length = (size_t) nonce_length_arg;
5272 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 if (nonce_buffer) {
5275 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005276 nonce_buffer[index] = 'a' + index;
5277 }
Paul Elliott66696b52021-08-16 18:42:41 +01005278 }
Paul Elliott863864a2021-07-23 17:28:31 +01005279 }
5280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5282 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5283 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005284 }
5285
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005287
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005289
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 if (expected_status == PSA_SUCCESS) {
5291 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5292 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5293 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005294 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005296 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 }
Paul Elliott863864a2021-07-23 17:28:31 +01005298
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005299 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5301 additional_data->len),
5302 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5305 output, output_size,
5306 &ciphertext_length),
5307 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5310 &ciphertext_length, tag_buffer,
5311 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5312 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005313 }
5314
5315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 psa_destroy_key(key);
5317 mbedtls_free(output);
5318 mbedtls_free(ciphertext);
5319 mbedtls_free(nonce_buffer);
5320 psa_aead_abort(&operation);
5321 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005322}
5323/* END_CASE */
5324
5325/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005326void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005327 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005328 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005329 data_t *nonce,
5330 data_t *additional_data,
5331 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005333{
5334
5335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5336 psa_key_type_t key_type = key_type_arg;
5337 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005338 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5340 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5341 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005342 unsigned char *output = NULL;
5343 unsigned char *ciphertext = NULL;
5344 size_t output_size = output_size_arg;
5345 size_t ciphertext_size = 0;
5346 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005347 size_t tag_length = 0;
5348 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5353 psa_set_key_algorithm(&attributes, alg);
5354 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5357 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
5369 /* If the operation is not supported, just skip and not fail in case the
5370 * encryption involves a common limitation of cryptography hardwares and
5371 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 if (status == PSA_ERROR_NOT_SUPPORTED) {
5373 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5374 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005375 }
5376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5380 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5385 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 status = psa_aead_update(&operation, input_data->x, input_data->len,
5388 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005393 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5395 &ciphertext_length, tag_buffer,
5396 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005397 }
5398
5399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 psa_destroy_key(key);
5401 mbedtls_free(output);
5402 mbedtls_free(ciphertext);
5403 psa_aead_abort(&operation);
5404 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005405}
5406/* END_CASE */
5407
Paul Elliott91b021e2021-07-23 18:52:31 +01005408/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005409void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5410 int alg_arg,
5411 int finish_ciphertext_size_arg,
5412 int tag_size_arg,
5413 data_t *nonce,
5414 data_t *additional_data,
5415 data_t *input_data,
5416 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005417{
5418
5419 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5420 psa_key_type_t key_type = key_type_arg;
5421 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005422 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5424 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5425 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005426 unsigned char *ciphertext = NULL;
5427 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005428 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005429 size_t ciphertext_size = 0;
5430 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5432 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005433 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5438 psa_set_key_algorithm(&attributes, alg);
5439 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5442 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
5456 /* If the operation is not supported, just skip and not fail in case the
5457 * encryption involves a common limitation of cryptography hardwares and
5458 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 if (status == PSA_ERROR_NOT_SUPPORTED) {
5460 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5461 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005462 }
5463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5469 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005470
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5472 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5475 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005476
5477 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 status = psa_aead_finish(&operation, finish_ciphertext,
5479 finish_ciphertext_size,
5480 &ciphertext_length, tag_buffer,
5481 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005484
5485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 psa_destroy_key(key);
5487 mbedtls_free(ciphertext);
5488 mbedtls_free(finish_ciphertext);
5489 mbedtls_free(tag_buffer);
5490 psa_aead_abort(&operation);
5491 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005492}
5493/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005494
5495/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005496void aead_multipart_verify(int key_type_arg, data_t *key_data,
5497 int alg_arg,
5498 data_t *nonce,
5499 data_t *additional_data,
5500 data_t *input_data,
5501 data_t *tag,
5502 int tag_usage_arg,
5503 int expected_setup_status_arg,
5504 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005505{
5506 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5507 psa_key_type_t key_type = key_type_arg;
5508 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005509 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5511 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5512 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005513 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005514 unsigned char *plaintext = NULL;
5515 unsigned char *finish_plaintext = NULL;
5516 size_t plaintext_size = 0;
5517 size_t plaintext_length = 0;
5518 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005519 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005520 unsigned char *tag_buffer = NULL;
5521 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5526 psa_set_key_algorithm(&attributes, alg);
5527 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5530 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005531
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5535 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005544
5545 /* If the operation is not supported, just skip and not fail in case the
5546 * encryption involves a common limitation of cryptography hardwares and
5547 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 if (status == PSA_ERROR_NOT_SUPPORTED) {
5549 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5550 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005551 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 }
Paul Elliott9961a662021-09-17 19:19:02 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 status = psa_aead_set_lengths(&operation, additional_data->len,
5563 input_data->len);
5564 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5567 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5570 input_data->len,
5571 plaintext, plaintext_size,
5572 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005575 tag_buffer = tag->x;
5576 tag_size = tag->len;
5577 }
5578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 status = psa_aead_verify(&operation, finish_plaintext,
5580 verify_plaintext_size,
5581 &plaintext_length,
5582 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005585
5586exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 psa_destroy_key(key);
5588 mbedtls_free(plaintext);
5589 mbedtls_free(finish_plaintext);
5590 psa_aead_abort(&operation);
5591 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005592}
5593/* END_CASE */
5594
Paul Elliott9961a662021-09-17 19:19:02 +01005595/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005596void aead_multipart_setup(int key_type_arg, data_t *key_data,
5597 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005598{
5599 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5600 psa_key_type_t key_type = key_type_arg;
5601 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005602 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5604 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5605 psa_status_t expected_status = expected_status_arg;
5606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005608
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 psa_set_key_usage_flags(&attributes,
5610 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5611 psa_set_key_algorithm(&attributes, alg);
5612 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005613
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5615 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005622
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
5627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 psa_destroy_key(key);
5629 psa_aead_abort(&operation);
5630 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005631}
5632/* END_CASE */
5633
5634/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005635void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5636 int alg_arg,
5637 data_t *nonce,
5638 data_t *additional_data,
5639 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005640{
5641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5642 psa_key_type_t key_type = key_type_arg;
5643 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005644 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005645 unsigned char *output_data = NULL;
5646 unsigned char *final_data = NULL;
5647 size_t output_size = 0;
5648 size_t finish_output_size = 0;
5649 size_t output_length = 0;
5650 size_t key_bits = 0;
5651 size_t tag_length = 0;
5652 size_t tag_size = 0;
5653 size_t nonce_length = 0;
5654 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5655 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5656 size_t output_part_length = 0;
5657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005660
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 psa_set_key_usage_flags(&attributes,
5662 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5663 psa_set_key_algorithm(&attributes, alg);
5664 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5667 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5670 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
5686 /* Test all operations error without calling setup first. */
5687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5689 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5694 PSA_AEAD_NONCE_MAX_SIZE,
5695 &nonce_length),
5696 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699
Paul Elliott481be342021-07-16 17:38:47 +01005700 /* ------------------------------------------------------- */
5701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5703 input_data->len),
5704 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707
Paul Elliott481be342021-07-16 17:38:47 +01005708 /* ------------------------------------------------------- */
5709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5711 additional_data->len),
5712 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
Paul Elliott481be342021-07-16 17:38:47 +01005716 /* ------------------------------------------------------- */
5717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5719 input_data->len, output_data,
5720 output_size, &output_length),
5721 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724
Paul Elliott481be342021-07-16 17:38:47 +01005725 /* ------------------------------------------------------- */
5726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5728 finish_output_size,
5729 &output_part_length,
5730 tag_buffer, tag_length,
5731 &tag_size),
5732 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Paul Elliott481be342021-07-16 17:38:47 +01005736 /* ------------------------------------------------------- */
5737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5739 finish_output_size,
5740 &output_part_length,
5741 tag_buffer,
5742 tag_length),
5743 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005746
5747 /* Test for double setups. */
5748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5752 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005755
Paul Elliott481be342021-07-16 17:38:47 +01005756 /* ------------------------------------------------------- */
5757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5761 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005762
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764
Paul Elliott374a2be2021-07-16 17:53:40 +01005765 /* ------------------------------------------------------- */
5766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5770 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005773
5774 /* ------------------------------------------------------- */
5775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5779 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005782
Paul Elliottc23a9a02021-06-21 18:32:46 +01005783 /* Test for not setting a nonce. */
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5788 additional_data->len),
5789 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005792
Paul Elliott7f628422021-09-01 12:08:29 +01005793 /* ------------------------------------------------------- */
5794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5798 input_data->len, output_data,
5799 output_size, &output_length),
5800 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005803
Paul Elliottbdc2c682021-09-21 18:37:10 +01005804 /* ------------------------------------------------------- */
5805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5809 finish_output_size,
5810 &output_part_length,
5811 tag_buffer, tag_length,
5812 &tag_size),
5813 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005816
5817 /* ------------------------------------------------------- */
5818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5822 finish_output_size,
5823 &output_part_length,
5824 tag_buffer,
5825 tag_length),
5826 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005829
Paul Elliottc23a9a02021-06-21 18:32:46 +01005830 /* Test for double setting nonce. */
5831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5837 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840
Paul Elliott374a2be2021-07-16 17:53:40 +01005841 /* Test for double generating nonce. */
5842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5846 PSA_AEAD_NONCE_MAX_SIZE,
5847 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5850 PSA_AEAD_NONCE_MAX_SIZE,
5851 &nonce_length),
5852 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005853
5854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005856
5857 /* Test for generate nonce then set and vice versa */
5858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5862 PSA_AEAD_NONCE_MAX_SIZE,
5863 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5866 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005869
Andrzej Kurekad837522021-12-15 15:28:49 +01005870 /* Test for generating nonce after calling set lengths */
5871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5875 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5878 PSA_AEAD_NONCE_MAX_SIZE,
5879 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005882
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005883 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 if (operation.alg == PSA_ALG_CCM) {
5888 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5889 input_data->len),
5890 PSA_ERROR_INVALID_ARGUMENT);
5891 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5892 PSA_AEAD_NONCE_MAX_SIZE,
5893 &nonce_length),
5894 PSA_ERROR_BAD_STATE);
5895 } else {
5896 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5897 input_data->len));
5898 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 }
5902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005904
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005905 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005906#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005908
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5910 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5911 input_data->len),
5912 PSA_ERROR_INVALID_ARGUMENT);
5913 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5914 PSA_AEAD_NONCE_MAX_SIZE,
5915 &nonce_length),
5916 PSA_ERROR_BAD_STATE);
5917 } else {
5918 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5919 input_data->len));
5920 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5921 PSA_AEAD_NONCE_MAX_SIZE,
5922 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005923 }
5924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005926#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005927
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005928 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5933 PSA_AEAD_NONCE_MAX_SIZE,
5934 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (operation.alg == PSA_ALG_CCM) {
5937 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5938 input_data->len),
5939 PSA_ERROR_INVALID_ARGUMENT);
5940 } else {
5941 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5942 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005943 }
5944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005946
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005947 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005948 /* Test for setting nonce after calling set lengths */
5949
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5953 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005958
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005959 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 if (operation.alg == PSA_ALG_CCM) {
5964 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5965 input_data->len),
5966 PSA_ERROR_INVALID_ARGUMENT);
5967 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5968 PSA_ERROR_BAD_STATE);
5969 } else {
5970 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5971 input_data->len));
5972 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005973 }
5974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005976
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005977 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005978#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5982 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5983 input_data->len),
5984 PSA_ERROR_INVALID_ARGUMENT);
5985 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5986 PSA_ERROR_BAD_STATE);
5987 } else {
5988 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5989 input_data->len));
5990 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005991 }
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005994#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005995
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005996 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 if (operation.alg == PSA_ALG_CCM) {
6003 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6004 input_data->len),
6005 PSA_ERROR_INVALID_ARGUMENT);
6006 } else {
6007 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6008 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006009 }
6010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006012
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006013 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006014#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006016
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 if (operation.alg == PSA_ALG_GCM) {
6018 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6019 SIZE_MAX),
6020 PSA_ERROR_INVALID_ARGUMENT);
6021 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6022 PSA_ERROR_BAD_STATE);
6023 } else if (operation.alg != PSA_ALG_CCM) {
6024 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6025 SIZE_MAX));
6026 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006027 }
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006030#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006031
Tom Cosgrove1797b052022-12-04 17:19:59 +00006032 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006033#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 if (operation.alg == PSA_ALG_GCM) {
6039 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6040 SIZE_MAX),
6041 PSA_ERROR_INVALID_ARGUMENT);
6042 } else if (operation.alg != PSA_ALG_CCM) {
6043 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6044 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006045 }
6046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006048#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006049
6050 /* ------------------------------------------------------- */
6051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6057 PSA_AEAD_NONCE_MAX_SIZE,
6058 &nonce_length),
6059 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006060
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006062
Paul Elliott7220cae2021-06-22 17:25:57 +01006063 /* Test for generating nonce in decrypt setup. */
6064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6068 PSA_AEAD_NONCE_MAX_SIZE,
6069 &nonce_length),
6070 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006073
Paul Elliottc23a9a02021-06-21 18:32:46 +01006074 /* Test for setting lengths twice. */
6075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6081 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6084 input_data->len),
6085 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006088
Andrzej Kurekad837522021-12-15 15:28:49 +01006089 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6098 additional_data->len),
6099 PSA_ERROR_BAD_STATE);
6100 } else {
6101 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6102 additional_data->len));
6103
6104 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6105 input_data->len),
6106 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006107 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006109
6110 /* ------------------------------------------------------- */
6111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 if (operation.alg == PSA_ALG_CCM) {
6117 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6118 input_data->len, output_data,
6119 output_size, &output_length),
6120 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 } else {
6123 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6124 input_data->len, output_data,
6125 output_size, &output_length));
6126
6127 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6128 input_data->len),
6129 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006132
6133 /* ------------------------------------------------------- */
6134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006136
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (operation.alg == PSA_ALG_CCM) {
6140 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6141 finish_output_size,
6142 &output_part_length,
6143 tag_buffer, tag_length,
6144 &tag_size));
6145 } else {
6146 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6147 finish_output_size,
6148 &output_part_length,
6149 tag_buffer, tag_length,
6150 &tag_size));
6151
6152 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6153 input_data->len),
6154 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006155 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006157
6158 /* Test for setting lengths after generating nonce + already starting data. */
6159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6163 PSA_AEAD_NONCE_MAX_SIZE,
6164 &nonce_length));
6165 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6168 additional_data->len),
6169 PSA_ERROR_BAD_STATE);
6170 } else {
6171 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6172 additional_data->len));
6173
6174 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6175 input_data->len),
6176 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006177 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006179
6180 /* ------------------------------------------------------- */
6181
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6185 PSA_AEAD_NONCE_MAX_SIZE,
6186 &nonce_length));
6187 if (operation.alg == PSA_ALG_CCM) {
6188 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6189 input_data->len, output_data,
6190 output_size, &output_length),
6191 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 } else {
6194 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6195 input_data->len, output_data,
6196 output_size, &output_length));
6197
6198 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6199 input_data->len),
6200 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006203
6204 /* ------------------------------------------------------- */
6205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6209 PSA_AEAD_NONCE_MAX_SIZE,
6210 &nonce_length));
6211 if (operation.alg == PSA_ALG_CCM) {
6212 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6213 finish_output_size,
6214 &output_part_length,
6215 tag_buffer, tag_length,
6216 &tag_size));
6217 } else {
6218 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6219 finish_output_size,
6220 &output_part_length,
6221 tag_buffer, tag_length,
6222 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6225 input_data->len),
6226 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006227 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006229
Paul Elliott243080c2021-07-21 19:01:17 +01006230 /* Test for not sending any additional data or data after setting non zero
6231 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6238 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6241 finish_output_size,
6242 &output_part_length,
6243 tag_buffer, tag_length,
6244 &tag_size),
6245 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006248
Paul Elliott243080c2021-07-21 19:01:17 +01006249 /* Test for not sending any additional data or data after setting non-zero
6250 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006251
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6257 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6260 finish_output_size,
6261 &output_part_length,
6262 tag_buffer,
6263 tag_length),
6264 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006267
Paul Elliott243080c2021-07-21 19:01:17 +01006268 /* Test for not sending any additional data after setting a non-zero length
6269 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6276 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6279 input_data->len, output_data,
6280 output_size, &output_length),
6281 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006284
Paul Elliottf94bd992021-09-19 18:15:59 +01006285 /* Test for not sending any data after setting a non-zero length for it.*/
6286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6292 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6295 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6298 finish_output_size,
6299 &output_part_length,
6300 tag_buffer, tag_length,
6301 &tag_size),
6302 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006305
Paul Elliottb0450fe2021-09-01 15:06:26 +01006306 /* Test for sending too much additional data after setting lengths. */
6307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006313
6314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6316 additional_data->len),
6317 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006320
Paul Elliotta2a09b02021-09-22 14:56:40 +01006321 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6328 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6331 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6334 1),
6335 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006338
Paul Elliottb0450fe2021-09-01 15:06:26 +01006339 /* Test for sending too much data after setting lengths. */
6340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6348 input_data->len, output_data,
6349 output_size, &output_length),
6350 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006353
Paul Elliotta2a09b02021-09-22 14:56:40 +01006354 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6361 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6364 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6367 input_data->len, output_data,
6368 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6371 1, output_data,
6372 output_size, &output_length),
6373 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006376
Paul Elliottc23a9a02021-06-21 18:32:46 +01006377 /* Test sending additional data after data. */
6378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 if (operation.alg != PSA_ALG_CCM) {
6384 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6385 input_data->len, output_data,
6386 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6389 additional_data->len),
6390 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006391 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006393
Paul Elliott534d0b42021-06-22 19:15:20 +01006394 /* Test calling finish on decryption. */
6395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6401 finish_output_size,
6402 &output_part_length,
6403 tag_buffer, tag_length,
6404 &tag_size),
6405 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006408
6409 /* Test calling verify on encryption. */
6410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6416 finish_output_size,
6417 &output_part_length,
6418 tag_buffer,
6419 tag_length),
6420 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006423
6424
Paul Elliottc23a9a02021-06-21 18:32:46 +01006425exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 psa_destroy_key(key);
6427 psa_aead_abort(&operation);
6428 mbedtls_free(output_data);
6429 mbedtls_free(final_data);
6430 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006431}
6432/* END_CASE */
6433
6434/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006435void signature_size(int type_arg,
6436 int bits,
6437 int alg_arg,
6438 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006439{
6440 psa_key_type_t type = type_arg;
6441 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006445
Gilles Peskinee59236f2018-01-27 23:32:46 +01006446exit:
6447 ;
6448}
6449/* END_CASE */
6450
6451/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006452void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6453 int alg_arg, data_t *input_data,
6454 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006455{
Ronald Cron5425a212020-08-04 14:58:35 +02006456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006457 psa_key_type_t key_type = key_type_arg;
6458 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006459 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006460 unsigned char *signature = NULL;
6461 size_t signature_size;
6462 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6468 psa_set_key_algorithm(&attributes, alg);
6469 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6472 &key));
6473 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6474 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006475
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006476 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006477 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6479 key_bits, alg);
6480 TEST_ASSERT(signature_size != 0);
6481 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6482 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006483
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006484 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 PSA_ASSERT(psa_sign_hash(key, alg,
6486 input_data->x, input_data->len,
6487 signature, signature_size,
6488 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006489 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 ASSERT_COMPARE(output_data->x, output_data->len,
6491 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006492
6493exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006494 /*
6495 * Key attributes may have been returned by psa_get_key_attributes()
6496 * thus reset them as required.
6497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 psa_destroy_key(key);
6501 mbedtls_free(signature);
6502 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006503}
6504/* END_CASE */
6505
Paul Elliott712d5122022-12-07 14:03:10 +00006506/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006507/**
6508 * sign_hash_interruptible() test intentions:
6509 *
6510 * Note: This test can currently only handle ECDSA.
6511 *
6512 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006513 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006514 *
6515 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6516 * expected for different max_ops values.
6517 *
6518 * 3. Test that the number of ops done prior to start and after abort is zero
6519 * and that each successful stage completes some ops (this is not mandated by
6520 * the PSA specification, but is currently the case).
6521 *
6522 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6523 * complete() calls does not alter the number of ops returned.
6524 */
Paul Elliott712d5122022-12-07 14:03:10 +00006525void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6526 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006527 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006528{
6529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6530 psa_key_type_t key_type = key_type_arg;
6531 psa_algorithm_t alg = alg_arg;
6532 size_t key_bits;
6533 unsigned char *signature = NULL;
6534 size_t signature_size;
6535 size_t signature_length = 0xdeadbeef;
6536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6537 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006538 uint32_t num_ops = 0;
6539 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006540 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006541 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006542 size_t min_completes = 0;
6543 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006544
Paul Elliott712d5122022-12-07 14:03:10 +00006545 psa_sign_hash_interruptible_operation_t operation =
6546 psa_sign_hash_interruptible_operation_init();
6547
6548 PSA_ASSERT(psa_crypto_init());
6549
6550 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6551 psa_set_key_algorithm(&attributes, alg);
6552 psa_set_key_type(&attributes, key_type);
6553
6554 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6555 &key));
6556 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6557 key_bits = psa_get_key_bits(&attributes);
6558
6559 /* Allocate a buffer which has the size advertised by the
6560 * library. */
6561 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6562 key_bits, alg);
6563 TEST_ASSERT(signature_size != 0);
6564 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6565 ASSERT_ALLOC(signature, signature_size);
6566
Paul Elliott0c683352022-12-16 19:16:56 +00006567 psa_interruptible_set_max_ops(max_ops);
6568
Paul Elliott6f600372023-02-06 18:41:05 +00006569 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6570 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006571
Paul Elliott712d5122022-12-07 14:03:10 +00006572 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6573 TEST_ASSERT(num_ops_prior == 0);
6574
6575 /* Start performing the signature. */
6576 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6577 input_data->x, input_data->len));
6578
6579 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6580 TEST_ASSERT(num_ops_prior == 0);
6581
6582 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006583 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006584 status = psa_sign_hash_complete(&operation, signature, signature_size,
6585 &signature_length);
6586
Paul Elliott0c683352022-12-16 19:16:56 +00006587 num_completes++;
6588
Paul Elliott712d5122022-12-07 14:03:10 +00006589 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6590 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006591 /* We are asserting here that every complete makes progress
6592 * (completes some ops), which is true of the internal
6593 * implementation and probably any implementation, however this is
6594 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006595 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006596
Paul Elliott712d5122022-12-07 14:03:10 +00006597 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006598
6599 /* Ensure calling get_num_ops() twice still returns the same
6600 * number of ops as previously reported. */
6601 num_ops = psa_sign_hash_get_num_ops(&operation);
6602
6603 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006604 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006605 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006606
6607 TEST_ASSERT(status == PSA_SUCCESS);
6608
Paul Elliott0c683352022-12-16 19:16:56 +00006609 TEST_LE_U(min_completes, num_completes);
6610 TEST_LE_U(num_completes, max_completes);
6611
Paul Elliott712d5122022-12-07 14:03:10 +00006612 /* Verify that the signature is what is expected. */
6613 ASSERT_COMPARE(output_data->x, output_data->len,
6614 signature, signature_length);
6615
6616 PSA_ASSERT(psa_sign_hash_abort(&operation));
6617
Paul Elliott59ad9452022-12-18 15:09:02 +00006618 num_ops = psa_sign_hash_get_num_ops(&operation);
6619 TEST_ASSERT(num_ops == 0);
6620
Paul Elliott712d5122022-12-07 14:03:10 +00006621exit:
6622
6623 /*
6624 * Key attributes may have been returned by psa_get_key_attributes()
6625 * thus reset them as required.
6626 */
6627 psa_reset_key_attributes(&attributes);
6628
6629 psa_destroy_key(key);
6630 mbedtls_free(signature);
6631 PSA_DONE();
6632}
6633/* END_CASE */
6634
Gilles Peskine20035e32018-02-03 22:44:14 +01006635/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006636void sign_hash_fail(int key_type_arg, data_t *key_data,
6637 int alg_arg, data_t *input_data,
6638 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006639{
Ronald Cron5425a212020-08-04 14:58:35 +02006640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006641 psa_key_type_t key_type = key_type_arg;
6642 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006643 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006644 psa_status_t actual_status;
6645 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006646 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006647 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006648 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006651
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006653
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6655 psa_set_key_algorithm(&attributes, alg);
6656 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6659 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 actual_status = psa_sign_hash(key, alg,
6662 input_data->x, input_data->len,
6663 signature, signature_size,
6664 &signature_length);
6665 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006666 /* The value of *signature_length is unspecified on error, but
6667 * whatever it is, it should be less than signature_size, so that
6668 * if the caller tries to read *signature_length bytes without
6669 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006671
6672exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 psa_reset_key_attributes(&attributes);
6674 psa_destroy_key(key);
6675 mbedtls_free(signature);
6676 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006677}
6678/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006679
Paul Elliott91007972022-12-16 12:21:24 +00006680/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006681/**
6682 * sign_hash_fail_interruptible() test intentions:
6683 *
6684 * Note: This test can currently only handle ECDSA.
6685 *
6686 * 1. Test that various failure cases for interruptible sign hash fail with the
6687 * correct error codes, and at the correct point (at start or during
6688 * complete).
6689 *
6690 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6691 * expected for different max_ops values.
6692 *
6693 * 3. Test that the number of ops done prior to start and after abort is zero
6694 * and that each successful stage completes some ops (this is not mandated by
6695 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006696 *
6697 * 4. Check that calling complete() when start() fails and complete()
6698 * after completion results in a BAD_STATE error.
6699 *
6700 * 5. Check that calling start() again after start fails results in a BAD_STATE
6701 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006702 */
Paul Elliott91007972022-12-16 12:21:24 +00006703void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6704 int alg_arg, data_t *input_data,
6705 int signature_size_arg,
6706 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006707 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006708 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006709{
6710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6711 psa_key_type_t key_type = key_type_arg;
6712 psa_algorithm_t alg = alg_arg;
6713 size_t signature_size = signature_size_arg;
6714 psa_status_t actual_status;
6715 psa_status_t expected_start_status = expected_start_status_arg;
6716 psa_status_t expected_complete_status = expected_complete_status_arg;
6717 unsigned char *signature = NULL;
6718 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006719 uint32_t num_ops = 0;
6720 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006721 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006722 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006723 size_t min_completes = 0;
6724 size_t max_completes = 0;
6725
Paul Elliott91007972022-12-16 12:21:24 +00006726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6727 psa_sign_hash_interruptible_operation_t operation =
6728 psa_sign_hash_interruptible_operation_init();
6729
6730 ASSERT_ALLOC(signature, signature_size);
6731
6732 PSA_ASSERT(psa_crypto_init());
6733
6734 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6735 psa_set_key_algorithm(&attributes, alg);
6736 psa_set_key_type(&attributes, key_type);
6737
6738 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6739 &key));
6740
Paul Elliott0c683352022-12-16 19:16:56 +00006741 psa_interruptible_set_max_ops(max_ops);
6742
Paul Elliott6f600372023-02-06 18:41:05 +00006743 interruptible_signverify_get_minmax_completes(max_ops,
6744 expected_complete_status,
6745 &min_completes,
6746 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006747
Paul Elliott91007972022-12-16 12:21:24 +00006748 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6749 TEST_ASSERT(num_ops_prior == 0);
6750
6751 /* Start performing the signature. */
6752 actual_status = psa_sign_hash_start(&operation, key, alg,
6753 input_data->x, input_data->len);
6754
6755 TEST_EQUAL(actual_status, expected_start_status);
6756
Paul Elliottc9774412023-02-06 15:14:07 +00006757 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006758 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006759 * start failed. */
6760 actual_status = psa_sign_hash_complete(&operation, signature,
6761 signature_size,
6762 &signature_length);
6763
6764 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6765
6766 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006767 actual_status = psa_sign_hash_start(&operation, key, alg,
6768 input_data->x, input_data->len);
6769
6770 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6771 }
6772
Paul Elliott91007972022-12-16 12:21:24 +00006773 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6774 TEST_ASSERT(num_ops_prior == 0);
6775
Paul Elliott91007972022-12-16 12:21:24 +00006776 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006777 do {
Paul Elliott91007972022-12-16 12:21:24 +00006778 actual_status = psa_sign_hash_complete(&operation, signature,
6779 signature_size,
6780 &signature_length);
6781
Paul Elliott0c683352022-12-16 19:16:56 +00006782 num_completes++;
6783
Paul Elliott334d7262023-01-20 17:29:41 +00006784 if (actual_status == PSA_SUCCESS ||
6785 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006786 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006787 /* We are asserting here that every complete makes progress
6788 * (completes some ops), which is true of the internal
6789 * implementation and probably any implementation, however this is
6790 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006791 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006792
Paul Elliott91007972022-12-16 12:21:24 +00006793 num_ops_prior = num_ops;
6794 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006795 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006796
Paul Elliottc9774412023-02-06 15:14:07 +00006797 TEST_EQUAL(actual_status, expected_complete_status);
6798
Paul Elliottefebad02023-02-15 16:56:45 +00006799 /* Check that another complete returns BAD_STATE. */
6800 actual_status = psa_sign_hash_complete(&operation, signature,
6801 signature_size,
6802 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006803
Paul Elliottefebad02023-02-15 16:56:45 +00006804 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006805
Paul Elliott91007972022-12-16 12:21:24 +00006806 PSA_ASSERT(psa_sign_hash_abort(&operation));
6807
Paul Elliott59ad9452022-12-18 15:09:02 +00006808 num_ops = psa_sign_hash_get_num_ops(&operation);
6809 TEST_ASSERT(num_ops == 0);
6810
Paul Elliott91007972022-12-16 12:21:24 +00006811 /* The value of *signature_length is unspecified on error, but
6812 * whatever it is, it should be less than signature_size, so that
6813 * if the caller tries to read *signature_length bytes without
6814 * checking the error code then they don't overflow a buffer. */
6815 TEST_LE_U(signature_length, signature_size);
6816
Paul Elliott0c683352022-12-16 19:16:56 +00006817 TEST_LE_U(min_completes, num_completes);
6818 TEST_LE_U(num_completes, max_completes);
6819
Paul Elliott91007972022-12-16 12:21:24 +00006820exit:
6821 psa_reset_key_attributes(&attributes);
6822 psa_destroy_key(key);
6823 mbedtls_free(signature);
6824 PSA_DONE();
6825}
6826/* END_CASE */
6827
mohammad16038cc1cee2018-03-28 01:21:33 +03006828/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006829void sign_verify_hash(int key_type_arg, data_t *key_data,
6830 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006831{
Ronald Cron5425a212020-08-04 14:58:35 +02006832 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006833 psa_key_type_t key_type = key_type_arg;
6834 psa_algorithm_t alg = alg_arg;
6835 size_t key_bits;
6836 unsigned char *signature = NULL;
6837 size_t signature_size;
6838 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006840
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006842
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6844 psa_set_key_algorithm(&attributes, alg);
6845 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006846
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6848 &key));
6849 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6850 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006851
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006852 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006853 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6855 key_bits, alg);
6856 TEST_ASSERT(signature_size != 0);
6857 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6858 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006859
6860 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 PSA_ASSERT(psa_sign_hash(key, alg,
6862 input_data->x, input_data->len,
6863 signature, signature_size,
6864 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006865 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 TEST_LE_U(signature_length, signature_size);
6867 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006868
6869 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 PSA_ASSERT(psa_verify_hash(key, alg,
6871 input_data->x, input_data->len,
6872 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006873
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006875 /* Flip a bit in the input and verify that the signature is now
6876 * detected as invalid. Flip a bit at the beginning, not at the end,
6877 * because ECDSA may ignore the last few bits of the input. */
6878 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 TEST_EQUAL(psa_verify_hash(key, alg,
6880 input_data->x, input_data->len,
6881 signature, signature_length),
6882 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006883 }
6884
6885exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006886 /*
6887 * Key attributes may have been returned by psa_get_key_attributes()
6888 * thus reset them as required.
6889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 psa_destroy_key(key);
6893 mbedtls_free(signature);
6894 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006895}
6896/* END_CASE */
6897
Paul Elliott712d5122022-12-07 14:03:10 +00006898/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006899/**
6900 * sign_verify_hash_interruptible() test intentions:
6901 *
6902 * Note: This test can currently only handle ECDSA.
6903 *
Paul Elliott8c092052023-03-06 17:49:14 +00006904 * 1. Test that we can sign an input hash with the given keypair and then
6905 * afterwards verify that signature. This is currently the only way to test
6906 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006907 *
6908 * 2. Test that after corrupting the hash, the verification detects an invalid
6909 * signature.
6910 *
6911 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6912 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006913 *
6914 * 4. Test that the number of ops done prior to starting signing and after abort
6915 * is zero and that each successful signing stage completes some ops (this is
6916 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006917 */
Paul Elliott712d5122022-12-07 14:03:10 +00006918void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006919 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006920 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006921{
6922 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6923 psa_key_type_t key_type = key_type_arg;
6924 psa_algorithm_t alg = alg_arg;
6925 size_t key_bits;
6926 unsigned char *signature = NULL;
6927 size_t signature_size;
6928 size_t signature_length = 0xdeadbeef;
6929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6930 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006931 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006932 uint32_t num_ops = 0;
6933 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006934 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006935 size_t min_completes = 0;
6936 size_t max_completes = 0;
6937
Paul Elliott712d5122022-12-07 14:03:10 +00006938 psa_sign_hash_interruptible_operation_t sign_operation =
6939 psa_sign_hash_interruptible_operation_init();
6940 psa_verify_hash_interruptible_operation_t verify_operation =
6941 psa_verify_hash_interruptible_operation_init();
6942
6943 PSA_ASSERT(psa_crypto_init());
6944
Paul Elliott0c683352022-12-16 19:16:56 +00006945 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6946 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006947 psa_set_key_algorithm(&attributes, alg);
6948 psa_set_key_type(&attributes, key_type);
6949
6950 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6951 &key));
6952 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6953 key_bits = psa_get_key_bits(&attributes);
6954
6955 /* Allocate a buffer which has the size advertised by the
6956 * library. */
6957 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6958 key_bits, alg);
6959 TEST_ASSERT(signature_size != 0);
6960 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6961 ASSERT_ALLOC(signature, signature_size);
6962
Paul Elliott0c683352022-12-16 19:16:56 +00006963 psa_interruptible_set_max_ops(max_ops);
6964
Paul Elliott6f600372023-02-06 18:41:05 +00006965 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6966 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006967
Paul Elliott7c173082023-02-26 18:44:45 +00006968 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6969 TEST_ASSERT(num_ops_prior == 0);
6970
Paul Elliott712d5122022-12-07 14:03:10 +00006971 /* Start performing the signature. */
6972 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6973 input_data->x, input_data->len));
6974
Paul Elliott7c173082023-02-26 18:44:45 +00006975 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6976 TEST_ASSERT(num_ops_prior == 0);
6977
Paul Elliott712d5122022-12-07 14:03:10 +00006978 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006979 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006980
Paul Elliott0c683352022-12-16 19:16:56 +00006981 status = psa_sign_hash_complete(&sign_operation, signature,
6982 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006983 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006984
6985 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006986
6987 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6988 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6989 /* We are asserting here that every complete makes progress
6990 * (completes some ops), which is true of the internal
6991 * implementation and probably any implementation, however this is
6992 * not mandated by the PSA specification. */
6993 TEST_ASSERT(num_ops > num_ops_prior);
6994
6995 num_ops_prior = num_ops;
6996 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006997 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006998
6999 TEST_ASSERT(status == PSA_SUCCESS);
7000
Paul Elliott0c683352022-12-16 19:16:56 +00007001 TEST_LE_U(min_completes, num_completes);
7002 TEST_LE_U(num_completes, max_completes);
7003
Paul Elliott712d5122022-12-07 14:03:10 +00007004 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7005
Paul Elliott7c173082023-02-26 18:44:45 +00007006 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7007 TEST_ASSERT(num_ops == 0);
7008
Paul Elliott712d5122022-12-07 14:03:10 +00007009 /* Check that the signature length looks sensible. */
7010 TEST_LE_U(signature_length, signature_size);
7011 TEST_ASSERT(signature_length > 0);
7012
Paul Elliott0c683352022-12-16 19:16:56 +00007013 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007014
7015 /* Start verification. */
7016 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7017 input_data->x, input_data->len,
7018 signature, signature_length));
7019
7020 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007021 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007022 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007023
7024 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007025 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007026
7027 TEST_ASSERT(status == PSA_SUCCESS);
7028
Paul Elliott0c683352022-12-16 19:16:56 +00007029 TEST_LE_U(min_completes, num_completes);
7030 TEST_LE_U(num_completes, max_completes);
7031
Paul Elliott712d5122022-12-07 14:03:10 +00007032 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7033
7034 verify_operation = psa_verify_hash_interruptible_operation_init();
7035
7036 if (input_data->len != 0) {
7037 /* Flip a bit in the input and verify that the signature is now
7038 * detected as invalid. Flip a bit at the beginning, not at the end,
7039 * because ECDSA may ignore the last few bits of the input. */
7040 input_data->x[0] ^= 1;
7041
Paul Elliott712d5122022-12-07 14:03:10 +00007042 /* Start verification. */
7043 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7044 input_data->x, input_data->len,
7045 signature, signature_length));
7046
7047 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007048 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007049 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007050 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007051
7052 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7053 }
7054
7055 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7056
7057exit:
7058 /*
7059 * Key attributes may have been returned by psa_get_key_attributes()
7060 * thus reset them as required.
7061 */
7062 psa_reset_key_attributes(&attributes);
7063
7064 psa_destroy_key(key);
7065 mbedtls_free(signature);
7066 PSA_DONE();
7067}
7068/* END_CASE */
7069
Gilles Peskine9911b022018-06-29 17:30:48 +02007070/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007071void verify_hash(int key_type_arg, data_t *key_data,
7072 int alg_arg, data_t *hash_data,
7073 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007074{
Ronald Cron5425a212020-08-04 14:58:35 +02007075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007076 psa_key_type_t key_type = key_type_arg;
7077 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007079
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007083
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7085 psa_set_key_algorithm(&attributes, alg);
7086 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7089 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 PSA_ASSERT(psa_verify_hash(key, alg,
7092 hash_data->x, hash_data->len,
7093 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007094
itayzafrir5c753392018-05-08 11:18:38 +03007095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 psa_reset_key_attributes(&attributes);
7097 psa_destroy_key(key);
7098 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007099}
7100/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007101
Paul Elliott712d5122022-12-07 14:03:10 +00007102/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007103/**
7104 * verify_hash_interruptible() test intentions:
7105 *
7106 * Note: This test can currently only handle ECDSA.
7107 *
7108 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007109 * only). Given this test only does verification it can accept public keys as
7110 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007111 *
7112 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7113 * expected for different max_ops values.
7114 *
7115 * 3. Test that the number of ops done prior to start and after abort is zero
7116 * and that each successful stage completes some ops (this is not mandated by
7117 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007118 *
7119 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7120 * complete() calls does not alter the number of ops returned.
7121 *
7122 * 5. Test that after corrupting the hash, the verification detects an invalid
7123 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007124 */
Paul Elliott712d5122022-12-07 14:03:10 +00007125void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7126 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007127 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007128{
7129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7130 psa_key_type_t key_type = key_type_arg;
7131 psa_algorithm_t alg = alg_arg;
7132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7133 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007134 uint32_t num_ops = 0;
7135 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007136 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007137 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007138 size_t min_completes = 0;
7139 size_t max_completes = 0;
7140
Paul Elliott712d5122022-12-07 14:03:10 +00007141 psa_verify_hash_interruptible_operation_t operation =
7142 psa_verify_hash_interruptible_operation_init();
7143
7144 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7145
7146 PSA_ASSERT(psa_crypto_init());
7147
7148 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7149 psa_set_key_algorithm(&attributes, alg);
7150 psa_set_key_type(&attributes, key_type);
7151
7152 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7153 &key));
7154
Paul Elliott0c683352022-12-16 19:16:56 +00007155 psa_interruptible_set_max_ops(max_ops);
7156
Paul Elliott6f600372023-02-06 18:41:05 +00007157 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7158 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007159
Paul Elliott712d5122022-12-07 14:03:10 +00007160 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7161
7162 TEST_ASSERT(num_ops_prior == 0);
7163
7164 /* Start verification. */
7165 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7166 hash_data->x, hash_data->len,
7167 signature_data->x, signature_data->len)
7168 );
7169
7170 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7171
7172 TEST_ASSERT(num_ops_prior == 0);
7173
7174 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007175 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007176 status = psa_verify_hash_complete(&operation);
7177
Paul Elliott0c683352022-12-16 19:16:56 +00007178 num_completes++;
7179
Paul Elliott712d5122022-12-07 14:03:10 +00007180 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7181 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007182 /* We are asserting here that every complete makes progress
7183 * (completes some ops), which is true of the internal
7184 * implementation and probably any implementation, however this is
7185 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007186 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007187
Paul Elliott712d5122022-12-07 14:03:10 +00007188 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007189
7190 /* Ensure calling get_num_ops() twice still returns the same
7191 * number of ops as previously reported. */
7192 num_ops = psa_verify_hash_get_num_ops(&operation);
7193
7194 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007195 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007196 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007197
7198 TEST_ASSERT(status == PSA_SUCCESS);
7199
Paul Elliott0c683352022-12-16 19:16:56 +00007200 TEST_LE_U(min_completes, num_completes);
7201 TEST_LE_U(num_completes, max_completes);
7202
Paul Elliott712d5122022-12-07 14:03:10 +00007203 PSA_ASSERT(psa_verify_hash_abort(&operation));
7204
Paul Elliott59ad9452022-12-18 15:09:02 +00007205 num_ops = psa_verify_hash_get_num_ops(&operation);
7206 TEST_ASSERT(num_ops == 0);
7207
Paul Elliott8359c142023-02-24 18:40:10 +00007208 if (hash_data->len != 0) {
7209 /* Flip a bit in the hash and verify that the signature is now detected
7210 * as invalid. Flip a bit at the beginning, not at the end, because
7211 * ECDSA may ignore the last few bits of the input. */
7212 hash_data->x[0] ^= 1;
7213
7214 /* Start verification. */
7215 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7216 hash_data->x, hash_data->len,
7217 signature_data->x, signature_data->len));
7218
7219 /* Continue performing the signature until complete. */
7220 do {
7221 status = psa_verify_hash_complete(&operation);
7222 } while (status == PSA_OPERATION_INCOMPLETE);
7223
7224 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7225 }
7226
Paul Elliott712d5122022-12-07 14:03:10 +00007227exit:
7228 psa_reset_key_attributes(&attributes);
7229 psa_destroy_key(key);
7230 PSA_DONE();
7231}
7232/* END_CASE */
7233
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007234/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007235void verify_hash_fail(int key_type_arg, data_t *key_data,
7236 int alg_arg, data_t *hash_data,
7237 data_t *signature_data,
7238 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007239{
Ronald Cron5425a212020-08-04 14:58:35 +02007240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007241 psa_key_type_t key_type = key_type_arg;
7242 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007243 psa_status_t actual_status;
7244 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007246
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007248
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7250 psa_set_key_algorithm(&attributes, alg);
7251 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007252
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7254 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007255
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 actual_status = psa_verify_hash(key, alg,
7257 hash_data->x, hash_data->len,
7258 signature_data->x, signature_data->len);
7259 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007260
7261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 psa_reset_key_attributes(&attributes);
7263 psa_destroy_key(key);
7264 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007265}
7266/* END_CASE */
7267
Paul Elliott91007972022-12-16 12:21:24 +00007268/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007269/**
7270 * verify_hash_fail_interruptible() test intentions:
7271 *
7272 * Note: This test can currently only handle ECDSA.
7273 *
7274 * 1. Test that various failure cases for interruptible verify hash fail with
7275 * the correct error codes, and at the correct point (at start or during
7276 * complete).
7277 *
7278 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7279 * expected for different max_ops values.
7280 *
7281 * 3. Test that the number of ops done prior to start and after abort is zero
7282 * and that each successful stage completes some ops (this is not mandated by
7283 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007284 *
7285 * 4. Check that calling complete() when start() fails and complete()
7286 * after completion results in a BAD_STATE error.
7287 *
7288 * 5. Check that calling start() again after start fails results in a BAD_STATE
7289 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007290 */
Paul Elliott91007972022-12-16 12:21:24 +00007291void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7292 int alg_arg, data_t *hash_data,
7293 data_t *signature_data,
7294 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007295 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007296 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007297{
7298 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7299 psa_key_type_t key_type = key_type_arg;
7300 psa_algorithm_t alg = alg_arg;
7301 psa_status_t actual_status;
7302 psa_status_t expected_start_status = expected_start_status_arg;
7303 psa_status_t expected_complete_status = expected_complete_status_arg;
7304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007305 uint32_t num_ops = 0;
7306 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007307 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007308 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007309 size_t min_completes = 0;
7310 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007311 psa_verify_hash_interruptible_operation_t operation =
7312 psa_verify_hash_interruptible_operation_init();
7313
7314 PSA_ASSERT(psa_crypto_init());
7315
7316 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7317 psa_set_key_algorithm(&attributes, alg);
7318 psa_set_key_type(&attributes, key_type);
7319
7320 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7321 &key));
7322
Paul Elliott0c683352022-12-16 19:16:56 +00007323 psa_interruptible_set_max_ops(max_ops);
7324
Paul Elliott6f600372023-02-06 18:41:05 +00007325 interruptible_signverify_get_minmax_completes(max_ops,
7326 expected_complete_status,
7327 &min_completes,
7328 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007329
Paul Elliott91007972022-12-16 12:21:24 +00007330 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7331 TEST_ASSERT(num_ops_prior == 0);
7332
7333 /* Start verification. */
7334 actual_status = psa_verify_hash_start(&operation, key, alg,
7335 hash_data->x, hash_data->len,
7336 signature_data->x,
7337 signature_data->len);
7338
7339 TEST_EQUAL(actual_status, expected_start_status);
7340
Paul Elliottc9774412023-02-06 15:14:07 +00007341 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007342 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007343 * start failed. */
7344 actual_status = psa_verify_hash_complete(&operation);
7345
7346 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7347
7348 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007349 actual_status = psa_verify_hash_start(&operation, key, alg,
7350 hash_data->x, hash_data->len,
7351 signature_data->x,
7352 signature_data->len);
7353
7354 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7355 }
7356
Paul Elliott91007972022-12-16 12:21:24 +00007357 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7358 TEST_ASSERT(num_ops_prior == 0);
7359
Paul Elliott91007972022-12-16 12:21:24 +00007360 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007361 do {
Paul Elliott91007972022-12-16 12:21:24 +00007362 actual_status = psa_verify_hash_complete(&operation);
7363
Paul Elliott0c683352022-12-16 19:16:56 +00007364 num_completes++;
7365
Paul Elliott334d7262023-01-20 17:29:41 +00007366 if (actual_status == PSA_SUCCESS ||
7367 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007368 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007369 /* We are asserting here that every complete makes progress
7370 * (completes some ops), which is true of the internal
7371 * implementation and probably any implementation, however this is
7372 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007373 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007374
Paul Elliott91007972022-12-16 12:21:24 +00007375 num_ops_prior = num_ops;
7376 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007377 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007378
Paul Elliottc9774412023-02-06 15:14:07 +00007379 TEST_EQUAL(actual_status, expected_complete_status);
7380
Paul Elliottefebad02023-02-15 16:56:45 +00007381 /* Check that another complete returns BAD_STATE. */
7382 actual_status = psa_verify_hash_complete(&operation);
7383 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007384
Paul Elliott0c683352022-12-16 19:16:56 +00007385 TEST_LE_U(min_completes, num_completes);
7386 TEST_LE_U(num_completes, max_completes);
7387
Paul Elliott91007972022-12-16 12:21:24 +00007388 PSA_ASSERT(psa_verify_hash_abort(&operation));
7389
Paul Elliott59ad9452022-12-18 15:09:02 +00007390 num_ops = psa_verify_hash_get_num_ops(&operation);
7391 TEST_ASSERT(num_ops == 0);
7392
Paul Elliott91007972022-12-16 12:21:24 +00007393exit:
7394 psa_reset_key_attributes(&attributes);
7395 psa_destroy_key(key);
7396 PSA_DONE();
7397}
7398/* END_CASE */
7399
Paul Elliott20a36062022-12-18 13:21:25 +00007400/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007401/**
7402 * interruptible_signverify_hash_state_test() test intentions:
7403 *
7404 * Note: This test can currently only handle ECDSA.
7405 *
7406 * 1. Test that calling the various interruptible sign and verify hash functions
7407 * in incorrect orders returns BAD_STATE errors.
7408 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007409void interruptible_signverify_hash_state_test(int key_type_arg,
7410 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007411{
7412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7413 psa_key_type_t key_type = key_type_arg;
7414 psa_algorithm_t alg = alg_arg;
7415 size_t key_bits;
7416 unsigned char *signature = NULL;
7417 size_t signature_size;
7418 size_t signature_length = 0xdeadbeef;
7419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7420 psa_sign_hash_interruptible_operation_t sign_operation =
7421 psa_sign_hash_interruptible_operation_init();
7422 psa_verify_hash_interruptible_operation_t verify_operation =
7423 psa_verify_hash_interruptible_operation_init();
7424
7425 PSA_ASSERT(psa_crypto_init());
7426
7427 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7428 PSA_KEY_USAGE_VERIFY_HASH);
7429 psa_set_key_algorithm(&attributes, alg);
7430 psa_set_key_type(&attributes, key_type);
7431
7432 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7433 &key));
7434 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7435 key_bits = psa_get_key_bits(&attributes);
7436
7437 /* Allocate a buffer which has the size advertised by the
7438 * library. */
7439 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7440 key_bits, alg);
7441 TEST_ASSERT(signature_size != 0);
7442 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7443 ASSERT_ALLOC(signature, signature_size);
7444
7445 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7446
7447 /* --- Attempt completes prior to starts --- */
7448 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7449 signature_size,
7450 &signature_length),
7451 PSA_ERROR_BAD_STATE);
7452
7453 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7454
7455 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7456 PSA_ERROR_BAD_STATE);
7457
7458 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7459
7460 /* --- Aborts in all other places. --- */
7461 psa_sign_hash_abort(&sign_operation);
7462
7463 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7464 input_data->x, input_data->len));
7465
7466 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7467
7468 psa_interruptible_set_max_ops(1);
7469
7470 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7471 input_data->x, input_data->len));
7472
7473 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7474 signature_size,
7475 &signature_length),
7476 PSA_OPERATION_INCOMPLETE);
7477
7478 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7479
7480 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7481
7482 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7483 input_data->x, input_data->len));
7484
7485 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7486 signature_size,
7487 &signature_length));
7488
7489 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7490
7491 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7492
7493 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7494 input_data->x, input_data->len,
7495 signature, signature_length));
7496
7497 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7498
7499 psa_interruptible_set_max_ops(1);
7500
7501 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7502 input_data->x, input_data->len,
7503 signature, signature_length));
7504
7505 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7506 PSA_OPERATION_INCOMPLETE);
7507
7508 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7509
7510 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7511
7512 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7513 input_data->x, input_data->len,
7514 signature, signature_length));
7515
7516 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7517
7518 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7519
7520 /* --- Attempt double starts. --- */
7521
7522 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7523 input_data->x, input_data->len));
7524
7525 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7526 input_data->x, input_data->len),
7527 PSA_ERROR_BAD_STATE);
7528
7529 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7530
7531 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7532 input_data->x, input_data->len,
7533 signature, signature_length));
7534
7535 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7536 input_data->x, input_data->len,
7537 signature, signature_length),
7538 PSA_ERROR_BAD_STATE);
7539
7540 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7541
Paul Elliott76d671a2023-02-07 17:45:18 +00007542exit:
7543 /*
7544 * Key attributes may have been returned by psa_get_key_attributes()
7545 * thus reset them as required.
7546 */
7547 psa_reset_key_attributes(&attributes);
7548
7549 psa_destroy_key(key);
7550 mbedtls_free(signature);
7551 PSA_DONE();
7552}
7553/* END_CASE */
7554
7555/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007556/**
Paul Elliottc2033502023-02-26 17:09:14 +00007557 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007558 *
7559 * Note: This test can currently only handle ECDSA.
7560 *
7561 * 1. Test various edge cases in the interruptible sign and verify hash
7562 * interfaces.
7563 */
Paul Elliottc2033502023-02-26 17:09:14 +00007564void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007565 data_t *key_data, int alg_arg, data_t *input_data)
7566{
7567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7568 psa_key_type_t key_type = key_type_arg;
7569 psa_algorithm_t alg = alg_arg;
7570 size_t key_bits;
7571 unsigned char *signature = NULL;
7572 size_t signature_size;
7573 size_t signature_length = 0xdeadbeef;
7574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7575 uint8_t *input_buffer = NULL;
7576 psa_sign_hash_interruptible_operation_t sign_operation =
7577 psa_sign_hash_interruptible_operation_init();
7578 psa_verify_hash_interruptible_operation_t verify_operation =
7579 psa_verify_hash_interruptible_operation_init();
7580
7581 PSA_ASSERT(psa_crypto_init());
7582
7583 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7584 PSA_KEY_USAGE_VERIFY_HASH);
7585 psa_set_key_algorithm(&attributes, alg);
7586 psa_set_key_type(&attributes, key_type);
7587
7588 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7589 &key));
7590 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7591 key_bits = psa_get_key_bits(&attributes);
7592
7593 /* Allocate a buffer which has the size advertised by the
7594 * library. */
7595 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7596 key_bits, alg);
7597 TEST_ASSERT(signature_size != 0);
7598 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7599 ASSERT_ALLOC(signature, signature_size);
7600
Paul Elliott20a36062022-12-18 13:21:25 +00007601 /* --- Change function inputs mid run, to cause an error (sign only,
7602 * verify passes all inputs to start. --- */
7603
7604 psa_interruptible_set_max_ops(1);
7605
7606 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7607 input_data->x, input_data->len));
7608
7609 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7610 signature_size,
7611 &signature_length),
7612 PSA_OPERATION_INCOMPLETE);
7613
7614 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7615 0,
7616 &signature_length),
7617 PSA_ERROR_BUFFER_TOO_SMALL);
7618
Paul Elliottc9774412023-02-06 15:14:07 +00007619 /* And test that this invalidates the operation. */
7620 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7621 0,
7622 &signature_length),
7623 PSA_ERROR_BAD_STATE);
7624
Paul Elliott20a36062022-12-18 13:21:25 +00007625 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7626
Paul Elliottf9c91a72023-02-05 18:06:38 +00007627 /* Trash the hash buffer in between start and complete, to ensure
7628 * no reliance on external buffers. */
7629 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7630
7631 input_buffer = mbedtls_calloc(1, input_data->len);
7632 TEST_ASSERT(input_buffer != NULL);
7633
7634 memcpy(input_buffer, input_data->x, input_data->len);
7635
7636 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7637 input_buffer, input_data->len));
7638
7639 memset(input_buffer, '!', input_data->len);
7640 mbedtls_free(input_buffer);
7641 input_buffer = NULL;
7642
7643 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7644 signature_size,
7645 &signature_length));
7646
7647 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7648
7649 input_buffer = mbedtls_calloc(1, input_data->len);
7650 TEST_ASSERT(input_buffer != NULL);
7651
7652 memcpy(input_buffer, input_data->x, input_data->len);
7653
7654 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7655 input_buffer, input_data->len,
7656 signature, signature_length));
7657
7658 memset(input_buffer, '!', input_data->len);
7659 mbedtls_free(input_buffer);
7660 input_buffer = NULL;
7661
7662 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7663
7664 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7665
Paul Elliott20a36062022-12-18 13:21:25 +00007666exit:
7667 /*
7668 * Key attributes may have been returned by psa_get_key_attributes()
7669 * thus reset them as required.
7670 */
7671 psa_reset_key_attributes(&attributes);
7672
7673 psa_destroy_key(key);
7674 mbedtls_free(signature);
7675 PSA_DONE();
7676}
7677/* END_CASE */
7678
Paul Elliotta4cb9092023-02-07 18:01:55 +00007679/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007680/**
Paul Elliott57702242023-02-26 20:36:10 +00007681 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007682 *
7683 * Note: This test can currently only handle ECDSA.
7684 *
7685 * 1. Test that setting max ops is reflected in both interruptible sign and
7686 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007687 * 2. Test that changing the value of max_ops to unlimited during an operation
7688 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007689 *
7690 * 3. Test that calling get_num_ops() between complete calls gives the same
7691 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007692 */
Paul Elliott57702242023-02-26 20:36:10 +00007693void interruptible_signverify_hash_ops_tests(int key_type_arg,
7694 data_t *key_data, int alg_arg,
7695 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007696{
7697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7698 psa_key_type_t key_type = key_type_arg;
7699 psa_algorithm_t alg = alg_arg;
7700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007701 size_t key_bits;
7702 unsigned char *signature = NULL;
7703 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007704 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007705 uint32_t num_ops = 0;
7706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7707
Paul Elliotta4cb9092023-02-07 18:01:55 +00007708 psa_sign_hash_interruptible_operation_t sign_operation =
7709 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007710 psa_verify_hash_interruptible_operation_t verify_operation =
7711 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007712
7713 PSA_ASSERT(psa_crypto_init());
7714
7715 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7716 PSA_KEY_USAGE_VERIFY_HASH);
7717 psa_set_key_algorithm(&attributes, alg);
7718 psa_set_key_type(&attributes, key_type);
7719
Paul Elliottf1743e22023-02-15 18:44:16 +00007720 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7721 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7722 key_bits = psa_get_key_bits(&attributes);
7723
7724 /* Allocate a buffer which has the size advertised by the
7725 * library. */
7726 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7727
7728 TEST_ASSERT(signature_size != 0);
7729 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7730 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007731
7732 /* Check that default max ops gets set if we don't set it. */
7733 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7734 input_data->x, input_data->len));
7735
7736 TEST_EQUAL(psa_interruptible_get_max_ops(),
7737 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7738
7739 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7740
Paul Elliottf1743e22023-02-15 18:44:16 +00007741 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7742 input_data->x, input_data->len,
7743 signature, signature_size));
7744
7745 TEST_EQUAL(psa_interruptible_get_max_ops(),
7746 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7747
7748 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7749
Paul Elliotta4cb9092023-02-07 18:01:55 +00007750 /* Check that max ops gets set properly. */
7751
7752 psa_interruptible_set_max_ops(0xbeef);
7753
Paul Elliottf1743e22023-02-15 18:44:16 +00007754 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007755
Paul Elliott9e8819f2023-02-26 19:01:35 +00007756 /* --- Ensure changing the max ops mid operation works (operation should
7757 * complete successfully after setting max ops to unlimited --- */
7758 psa_interruptible_set_max_ops(1);
7759
7760 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7761 input_data->x, input_data->len));
7762
7763 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7764 signature_size,
7765 &signature_length),
7766 PSA_OPERATION_INCOMPLETE);
7767
7768 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7769
7770 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7771 signature_size,
7772 &signature_length));
7773
7774 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7775
7776 psa_interruptible_set_max_ops(1);
7777
7778 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7779 input_data->x, input_data->len,
7780 signature, signature_length));
7781
7782 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7783 PSA_OPERATION_INCOMPLETE);
7784
7785 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7786
7787 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7788
7789 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7790
Paul Elliottc1e04002023-02-26 20:27:23 +00007791 /* --- Test that not calling get_num_ops inbetween complete calls does not
7792 * result in lost ops. ---*/
7793
7794 psa_interruptible_set_max_ops(1);
7795
7796 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7797 input_data->x, input_data->len));
7798
7799 /* Continue performing the signature until complete. */
7800 do {
7801 status = psa_sign_hash_complete(&sign_operation, signature,
7802 signature_size,
7803 &signature_length);
7804
7805 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7806
7807 } while (status == PSA_OPERATION_INCOMPLETE);
7808
7809 PSA_ASSERT(status);
7810
7811 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7812
7813 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7814 input_data->x, input_data->len));
7815
7816 /* Continue performing the signature until complete. */
7817 do {
7818 status = psa_sign_hash_complete(&sign_operation, signature,
7819 signature_size,
7820 &signature_length);
7821 } while (status == PSA_OPERATION_INCOMPLETE);
7822
7823 PSA_ASSERT(status);
7824
7825 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7826
7827 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7828
7829 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7830 input_data->x, input_data->len,
7831 signature, signature_length));
7832
7833 /* Continue performing the verification until complete. */
7834 do {
7835 status = psa_verify_hash_complete(&verify_operation);
7836
7837 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7838
7839 } while (status == PSA_OPERATION_INCOMPLETE);
7840
7841 PSA_ASSERT(status);
7842
7843 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7844
7845 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7846 input_data->x, input_data->len,
7847 signature, signature_length));
7848
7849 /* Continue performing the verification until complete. */
7850 do {
7851 status = psa_verify_hash_complete(&verify_operation);
7852
7853 } while (status == PSA_OPERATION_INCOMPLETE);
7854
7855 PSA_ASSERT(status);
7856
7857 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7858
7859 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7860
Paul Elliotta4cb9092023-02-07 18:01:55 +00007861exit:
7862 /*
7863 * Key attributes may have been returned by psa_get_key_attributes()
7864 * thus reset them as required.
7865 */
7866 psa_reset_key_attributes(&attributes);
7867
7868 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007869 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007870 PSA_DONE();
7871}
7872/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007873
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007874/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007875void sign_message_deterministic(int key_type_arg,
7876 data_t *key_data,
7877 int alg_arg,
7878 data_t *input_data,
7879 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007880{
7881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7882 psa_key_type_t key_type = key_type_arg;
7883 psa_algorithm_t alg = alg_arg;
7884 size_t key_bits;
7885 unsigned char *signature = NULL;
7886 size_t signature_size;
7887 size_t signature_length = 0xdeadbeef;
7888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7889
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7893 psa_set_key_algorithm(&attributes, alg);
7894 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007895
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7897 &key));
7898 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7899 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7902 TEST_ASSERT(signature_size != 0);
7903 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7904 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007905
Gilles Peskine449bd832023-01-11 14:50:10 +01007906 PSA_ASSERT(psa_sign_message(key, alg,
7907 input_data->x, input_data->len,
7908 signature, signature_size,
7909 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007910
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 ASSERT_COMPARE(output_data->x, output_data->len,
7912 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007913
7914exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007916
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 psa_destroy_key(key);
7918 mbedtls_free(signature);
7919 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007920
7921}
7922/* END_CASE */
7923
7924/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007925void sign_message_fail(int key_type_arg,
7926 data_t *key_data,
7927 int alg_arg,
7928 data_t *input_data,
7929 int signature_size_arg,
7930 int expected_status_arg)
7931{
7932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7933 psa_key_type_t key_type = key_type_arg;
7934 psa_algorithm_t alg = alg_arg;
7935 size_t signature_size = signature_size_arg;
7936 psa_status_t actual_status;
7937 psa_status_t expected_status = expected_status_arg;
7938 unsigned char *signature = NULL;
7939 size_t signature_length = 0xdeadbeef;
7940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7941
7942 ASSERT_ALLOC(signature, signature_size);
7943
7944 PSA_ASSERT(psa_crypto_init());
7945
7946 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7947 psa_set_key_algorithm(&attributes, alg);
7948 psa_set_key_type(&attributes, key_type);
7949
7950 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7951 &key));
7952
7953 actual_status = psa_sign_message(key, alg,
7954 input_data->x, input_data->len,
7955 signature, signature_size,
7956 &signature_length);
7957 TEST_EQUAL(actual_status, expected_status);
7958 /* The value of *signature_length is unspecified on error, but
7959 * whatever it is, it should be less than signature_size, so that
7960 * if the caller tries to read *signature_length bytes without
7961 * checking the error code then they don't overflow a buffer. */
7962 TEST_LE_U(signature_length, signature_size);
7963
7964exit:
7965 psa_reset_key_attributes(&attributes);
7966 psa_destroy_key(key);
7967 mbedtls_free(signature);
7968 PSA_DONE();
7969}
7970/* END_CASE */
7971
7972/* BEGIN_CASE */
7973void sign_verify_message(int key_type_arg,
7974 data_t *key_data,
7975 int alg_arg,
7976 data_t *input_data)
7977{
7978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7979 psa_key_type_t key_type = key_type_arg;
7980 psa_algorithm_t alg = alg_arg;
7981 size_t key_bits;
7982 unsigned char *signature = NULL;
7983 size_t signature_size;
7984 size_t signature_length = 0xdeadbeef;
7985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7986
7987 PSA_ASSERT(psa_crypto_init());
7988
7989 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7990 PSA_KEY_USAGE_VERIFY_MESSAGE);
7991 psa_set_key_algorithm(&attributes, alg);
7992 psa_set_key_type(&attributes, key_type);
7993
7994 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7995 &key));
7996 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7997 key_bits = psa_get_key_bits(&attributes);
7998
7999 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8000 TEST_ASSERT(signature_size != 0);
8001 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
8002 ASSERT_ALLOC(signature, signature_size);
8003
8004 PSA_ASSERT(psa_sign_message(key, alg,
8005 input_data->x, input_data->len,
8006 signature, signature_size,
8007 &signature_length));
8008 TEST_LE_U(signature_length, signature_size);
8009 TEST_ASSERT(signature_length > 0);
8010
8011 PSA_ASSERT(psa_verify_message(key, alg,
8012 input_data->x, input_data->len,
8013 signature, signature_length));
8014
8015 if (input_data->len != 0) {
8016 /* Flip a bit in the input and verify that the signature is now
8017 * detected as invalid. Flip a bit at the beginning, not at the end,
8018 * because ECDSA may ignore the last few bits of the input. */
8019 input_data->x[0] ^= 1;
8020 TEST_EQUAL(psa_verify_message(key, alg,
8021 input_data->x, input_data->len,
8022 signature, signature_length),
8023 PSA_ERROR_INVALID_SIGNATURE);
8024 }
8025
8026exit:
8027 psa_reset_key_attributes(&attributes);
8028
8029 psa_destroy_key(key);
8030 mbedtls_free(signature);
8031 PSA_DONE();
8032}
8033/* END_CASE */
8034
8035/* BEGIN_CASE */
8036void verify_message(int key_type_arg,
8037 data_t *key_data,
8038 int alg_arg,
8039 data_t *input_data,
8040 data_t *signature_data)
8041{
8042 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8043 psa_key_type_t key_type = key_type_arg;
8044 psa_algorithm_t alg = alg_arg;
8045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8046
8047 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8048
8049 PSA_ASSERT(psa_crypto_init());
8050
8051 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8052 psa_set_key_algorithm(&attributes, alg);
8053 psa_set_key_type(&attributes, key_type);
8054
8055 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8056 &key));
8057
8058 PSA_ASSERT(psa_verify_message(key, alg,
8059 input_data->x, input_data->len,
8060 signature_data->x, signature_data->len));
8061
8062exit:
8063 psa_reset_key_attributes(&attributes);
8064 psa_destroy_key(key);
8065 PSA_DONE();
8066}
8067/* END_CASE */
8068
8069/* BEGIN_CASE */
8070void verify_message_fail(int key_type_arg,
8071 data_t *key_data,
8072 int alg_arg,
8073 data_t *hash_data,
8074 data_t *signature_data,
8075 int expected_status_arg)
8076{
8077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8078 psa_key_type_t key_type = key_type_arg;
8079 psa_algorithm_t alg = alg_arg;
8080 psa_status_t actual_status;
8081 psa_status_t expected_status = expected_status_arg;
8082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8083
8084 PSA_ASSERT(psa_crypto_init());
8085
8086 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8087 psa_set_key_algorithm(&attributes, alg);
8088 psa_set_key_type(&attributes, key_type);
8089
8090 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8091 &key));
8092
8093 actual_status = psa_verify_message(key, alg,
8094 hash_data->x, hash_data->len,
8095 signature_data->x,
8096 signature_data->len);
8097 TEST_EQUAL(actual_status, expected_status);
8098
8099exit:
8100 psa_reset_key_attributes(&attributes);
8101 psa_destroy_key(key);
8102 PSA_DONE();
8103}
8104/* END_CASE */
8105
8106/* BEGIN_CASE */
8107void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008108 data_t *key_data,
8109 int alg_arg,
8110 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 data_t *label,
8112 int expected_output_length_arg,
8113 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008114{
Ronald Cron5425a212020-08-04 14:58:35 +02008115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008116 psa_key_type_t key_type = key_type_arg;
8117 psa_algorithm_t alg = alg_arg;
8118 size_t expected_output_length = expected_output_length_arg;
8119 size_t key_bits;
8120 unsigned char *output = NULL;
8121 size_t output_size;
8122 size_t output_length = ~0;
8123 psa_status_t actual_status;
8124 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008126
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008128
Gilles Peskine656896e2018-06-29 19:12:28 +02008129 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8131 psa_set_key_algorithm(&attributes, alg);
8132 psa_set_key_type(&attributes, key_type);
8133 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8134 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008135
8136 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008137 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8138 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008139
Gilles Peskine449bd832023-01-11 14:50:10 +01008140 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8141 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8142 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008143
8144 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008145 actual_status = psa_asymmetric_encrypt(key, alg,
8146 input_data->x, input_data->len,
8147 label->x, label->len,
8148 output, output_size,
8149 &output_length);
8150 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008151 if (actual_status == PSA_SUCCESS) {
8152 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008153 } else {
8154 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008155 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008156
Gilles Peskine68428122018-06-30 18:42:41 +02008157 /* If the label is empty, the test framework puts a non-null pointer
8158 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008160 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008161 if (output_size != 0) {
8162 memset(output, 0, output_size);
8163 }
8164 actual_status = psa_asymmetric_encrypt(key, alg,
8165 input_data->x, input_data->len,
8166 NULL, label->len,
8167 output, output_size,
8168 &output_length);
8169 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008170 if (actual_status == PSA_SUCCESS) {
8171 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008172 } else {
8173 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008174 }
Gilles Peskine68428122018-06-30 18:42:41 +02008175 }
8176
Gilles Peskine656896e2018-06-29 19:12:28 +02008177exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008178 /*
8179 * Key attributes may have been returned by psa_get_key_attributes()
8180 * thus reset them as required.
8181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008183
Gilles Peskine449bd832023-01-11 14:50:10 +01008184 psa_destroy_key(key);
8185 mbedtls_free(output);
8186 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008187}
8188/* END_CASE */
8189
8190/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008191void asymmetric_encrypt_decrypt(int key_type_arg,
8192 data_t *key_data,
8193 int alg_arg,
8194 data_t *input_data,
8195 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008196{
Ronald Cron5425a212020-08-04 14:58:35 +02008197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008198 psa_key_type_t key_type = key_type_arg;
8199 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008200 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008201 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008202 size_t output_size;
8203 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008204 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008205 size_t output2_size;
8206 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008208
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008210
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8212 psa_set_key_algorithm(&attributes, alg);
8213 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008214
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8216 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008217
8218 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8220 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008221
Gilles Peskine449bd832023-01-11 14:50:10 +01008222 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8223 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8224 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008225
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008226 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 TEST_LE_U(output2_size,
8228 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8229 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8230 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008231
Gilles Peskineeebd7382018-06-08 18:11:54 +02008232 /* We test encryption by checking that encrypt-then-decrypt gives back
8233 * the original plaintext because of the non-optional random
8234 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8236 input_data->x, input_data->len,
8237 label->x, label->len,
8238 output, output_size,
8239 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008240 /* We don't know what ciphertext length to expect, but check that
8241 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008243
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8245 output, output_length,
8246 label->x, label->len,
8247 output2, output2_size,
8248 &output2_length));
8249 ASSERT_COMPARE(input_data->x, input_data->len,
8250 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008251
8252exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008253 /*
8254 * Key attributes may have been returned by psa_get_key_attributes()
8255 * thus reset them as required.
8256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008258
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 psa_destroy_key(key);
8260 mbedtls_free(output);
8261 mbedtls_free(output2);
8262 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008263}
8264/* END_CASE */
8265
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008267void asymmetric_decrypt(int key_type_arg,
8268 data_t *key_data,
8269 int alg_arg,
8270 data_t *input_data,
8271 data_t *label,
8272 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008273{
Ronald Cron5425a212020-08-04 14:58:35 +02008274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008275 psa_key_type_t key_type = key_type_arg;
8276 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008277 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008278 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008279 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008280 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008282
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008284
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8286 psa_set_key_algorithm(&attributes, alg);
8287 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008288
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8290 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008291
Gilles Peskine449bd832023-01-11 14:50:10 +01008292 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8293 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008294
8295 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8297 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8298 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008299
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8301 input_data->x, input_data->len,
8302 label->x, label->len,
8303 output,
8304 output_size,
8305 &output_length));
8306 ASSERT_COMPARE(expected_data->x, expected_data->len,
8307 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008308
Gilles Peskine68428122018-06-30 18:42:41 +02008309 /* If the label is empty, the test framework puts a non-null pointer
8310 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008312 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 if (output_size != 0) {
8314 memset(output, 0, output_size);
8315 }
8316 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8317 input_data->x, input_data->len,
8318 NULL, label->len,
8319 output,
8320 output_size,
8321 &output_length));
8322 ASSERT_COMPARE(expected_data->x, expected_data->len,
8323 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008324 }
8325
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008327 psa_reset_key_attributes(&attributes);
8328 psa_destroy_key(key);
8329 mbedtls_free(output);
8330 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008331}
8332/* END_CASE */
8333
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008334/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008335void asymmetric_decrypt_fail(int key_type_arg,
8336 data_t *key_data,
8337 int alg_arg,
8338 data_t *input_data,
8339 data_t *label,
8340 int output_size_arg,
8341 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008342{
Ronald Cron5425a212020-08-04 14:58:35 +02008343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008344 psa_key_type_t key_type = key_type_arg;
8345 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008346 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008347 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008348 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008349 psa_status_t actual_status;
8350 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008352
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008354
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8358 psa_set_key_algorithm(&attributes, alg);
8359 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008360
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8362 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008363
Gilles Peskine449bd832023-01-11 14:50:10 +01008364 actual_status = psa_asymmetric_decrypt(key, alg,
8365 input_data->x, input_data->len,
8366 label->x, label->len,
8367 output, output_size,
8368 &output_length);
8369 TEST_EQUAL(actual_status, expected_status);
8370 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371
Gilles Peskine68428122018-06-30 18:42:41 +02008372 /* If the label is empty, the test framework puts a non-null pointer
8373 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008375 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 if (output_size != 0) {
8377 memset(output, 0, output_size);
8378 }
8379 actual_status = psa_asymmetric_decrypt(key, alg,
8380 input_data->x, input_data->len,
8381 NULL, label->len,
8382 output, output_size,
8383 &output_length);
8384 TEST_EQUAL(actual_status, expected_status);
8385 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008386 }
8387
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008388exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 psa_reset_key_attributes(&attributes);
8390 psa_destroy_key(key);
8391 mbedtls_free(output);
8392 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008393}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008394/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008395
8396/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008397void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008398{
8399 /* Test each valid way of initializing the object, except for `= {0}`, as
8400 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8401 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008402 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008403 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008405 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8406 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008409
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008410 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8412 PSA_ERROR_BAD_STATE);
8413 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8414 PSA_ERROR_BAD_STATE);
8415 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8416 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008417
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008418 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 PSA_ASSERT(psa_key_derivation_abort(&func));
8420 PSA_ASSERT(psa_key_derivation_abort(&init));
8421 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008422}
8423/* END_CASE */
8424
Janos Follath16de4a42019-06-13 16:32:24 +01008425/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008426void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008427{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008428 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008429 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008430 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008431
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008433
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8435 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008436
8437exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 psa_key_derivation_abort(&operation);
8439 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008440}
8441/* END_CASE */
8442
Janos Follathaf3c2a02019-06-12 12:34:34 +01008443/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008444void derive_set_capacity(int alg_arg, int capacity_arg,
8445 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008446{
8447 psa_algorithm_t alg = alg_arg;
8448 size_t capacity = capacity_arg;
8449 psa_status_t expected_status = expected_status_arg;
8450 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8457 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008458
8459exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 psa_key_derivation_abort(&operation);
8461 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008462}
8463/* END_CASE */
8464
8465/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008466void derive_input(int alg_arg,
8467 int step_arg1, int key_type_arg1, data_t *input1,
8468 int expected_status_arg1,
8469 int step_arg2, int key_type_arg2, data_t *input2,
8470 int expected_status_arg2,
8471 int step_arg3, int key_type_arg3, data_t *input3,
8472 int expected_status_arg3,
8473 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008474{
8475 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8477 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8478 psa_status_t expected_statuses[] = { expected_status_arg1,
8479 expected_status_arg2,
8480 expected_status_arg3 };
8481 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008482 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8483 MBEDTLS_SVC_KEY_ID_INIT,
8484 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008485 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8487 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008488 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008489 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008490 psa_status_t expected_output_status = expected_output_status_arg;
8491 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008492
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8496 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008497
Gilles Peskine449bd832023-01-11 14:50:10 +01008498 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008499
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8501 mbedtls_test_set_step(i);
8502 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008503 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8505 psa_set_key_type(&attributes, key_types[i]);
8506 PSA_ASSERT(psa_import_key(&attributes,
8507 inputs[i]->x, inputs[i]->len,
8508 &keys[i]));
8509 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8510 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008511 // When taking a private key as secret input, use key agreement
8512 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8514 &operation, keys[i]),
8515 expected_statuses[i]);
8516 } else {
8517 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8518 keys[i]),
8519 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008520 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 } else {
8522 TEST_EQUAL(psa_key_derivation_input_bytes(
8523 &operation, steps[i],
8524 inputs[i]->x, inputs[i]->len),
8525 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008526 }
8527 }
8528
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 if (output_key_type != PSA_KEY_TYPE_NONE) {
8530 psa_reset_key_attributes(&attributes);
8531 psa_set_key_type(&attributes, output_key_type);
8532 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008533 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 psa_key_derivation_output_key(&attributes, &operation,
8535 &output_key);
8536 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008537 uint8_t buffer[1];
8538 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 psa_key_derivation_output_bytes(&operation,
8540 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008541 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008543
Janos Follathaf3c2a02019-06-12 12:34:34 +01008544exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 psa_key_derivation_abort(&operation);
8546 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8547 psa_destroy_key(keys[i]);
8548 }
8549 psa_destroy_key(output_key);
8550 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008551}
8552/* END_CASE */
8553
Janos Follathd958bb72019-07-03 15:02:16 +01008554/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008555void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008556{
Janos Follathd958bb72019-07-03 15:02:16 +01008557 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008559 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008560 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008561 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008563 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008565 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008567 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8568 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008571
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008573
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8575 psa_set_key_algorithm(&attributes, alg);
8576 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008577
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 PSA_ASSERT(psa_import_key(&attributes,
8579 key_data, sizeof(key_data),
8580 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008581
8582 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8584 input1, input1_length,
8585 input2, input2_length,
8586 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008587 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008588 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008589
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008590 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8592 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008593
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008595
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8597 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008598
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008599exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 psa_key_derivation_abort(&operation);
8601 psa_destroy_key(key);
8602 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008603}
8604/* END_CASE */
8605
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008606/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008607void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008608{
8609 uint8_t output_buffer[16];
8610 size_t buffer_size = 16;
8611 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008612 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008613
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8615 output_buffer, buffer_size)
8616 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008617
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8619 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008622
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8624 output_buffer, buffer_size)
8625 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008626
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8628 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008629
8630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008632}
8633/* END_CASE */
8634
8635/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008636void derive_output(int alg_arg,
8637 int step1_arg, data_t *input1, int expected_status_arg1,
8638 int step2_arg, data_t *input2, int expected_status_arg2,
8639 int step3_arg, data_t *input3, int expected_status_arg3,
8640 int step4_arg, data_t *input4, int expected_status_arg4,
8641 data_t *key_agreement_peer_key,
8642 int requested_capacity_arg,
8643 data_t *expected_output1,
8644 data_t *expected_output2,
8645 int other_key_input_type,
8646 int key_input_type,
8647 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008648{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008649 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8651 data_t *inputs[] = { input1, input2, input3, input4 };
8652 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8653 MBEDTLS_SVC_KEY_ID_INIT,
8654 MBEDTLS_SVC_KEY_ID_INIT,
8655 MBEDTLS_SVC_KEY_ID_INIT };
8656 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8657 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008658 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008659 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008660 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008662 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008664 size_t output_buffer_size = 0;
8665 uint8_t *output_buffer = NULL;
8666 size_t expected_capacity;
8667 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008668 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8669 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8670 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8671 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008672 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008673 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008674 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008675
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8677 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008678 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 }
8680 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008681 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008683 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 ASSERT_ALLOC(output_buffer, output_buffer_size);
8685 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008686
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008687 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8689 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8690 requested_capacity));
8691 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8692 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008693 case 0:
8694 break;
8695 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008697 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 TEST_EQUAL(psa_key_derivation_input_bytes(
8699 &operation, steps[i],
8700 inputs[i]->x, inputs[i]->len),
8701 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008702
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008704 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008706 break;
8707 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8709 psa_set_key_algorithm(&attributes1, alg);
8710 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 PSA_ASSERT(psa_import_key(&attributes1,
8713 inputs[i]->x, inputs[i]->len,
8714 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8717 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8718 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8719 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008720 }
8721
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8723 steps[i],
8724 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008725 break;
8726 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008728 break;
8729 }
8730 break;
8731 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008733 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8735 steps[i],
8736 inputs[i]->x,
8737 inputs[i]->len),
8738 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008739 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008740 case 1: // input key, type DERIVE
8741 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8743 psa_set_key_algorithm(&attributes2, alg);
8744 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008745
8746 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 if (other_key_input_type == 11) {
8748 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8749 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008750
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 PSA_ASSERT(psa_import_key(&attributes2,
8752 inputs[i]->x, inputs[i]->len,
8753 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008754
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8756 steps[i],
8757 keys[i]),
8758 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008759 break;
8760 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8762 psa_set_key_algorithm(&attributes3, alg);
8763 psa_set_key_type(&attributes3,
8764 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008765
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 PSA_ASSERT(psa_import_key(&attributes3,
8767 inputs[i]->x, inputs[i]->len,
8768 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008769
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 TEST_EQUAL(psa_key_derivation_key_agreement(
8771 &operation,
8772 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8773 keys[i], key_agreement_peer_key->x,
8774 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008775 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008776 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008778 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008779 }
8780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008782 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008784 break;
8785 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 TEST_EQUAL(psa_key_derivation_input_bytes(
8787 &operation, steps[i],
8788 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008789
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008791 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008793 break;
8794 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008795 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008796
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8798 &current_capacity));
8799 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008800 expected_capacity = requested_capacity;
8801
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008803 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8804
8805 /* For output key derivation secret must be provided using
8806 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008808 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008810
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8812 psa_set_key_algorithm(&attributes4, alg);
8813 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8814 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8817 &derived_key), expected_status);
8818 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008819 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008821 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 status = psa_key_derivation_output_bytes(&operation,
8823 output_buffer, output_sizes[i]);
8824 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008825 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 TEST_ASSERT(status == PSA_SUCCESS ||
8827 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008828 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 } else if (expected_capacity == 0 ||
8830 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008831 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008833 expected_capacity = 0;
8834 continue;
8835 }
8836 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 PSA_ASSERT(status);
8838 if (output_sizes[i] != 0) {
8839 ASSERT_COMPARE(output_buffer, output_sizes[i],
8840 expected_outputs[i], output_sizes[i]);
8841 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008842 /* Check the operation status. */
8843 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008844 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8845 &current_capacity));
8846 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008847 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008848 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008850
8851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 mbedtls_free(output_buffer);
8853 psa_key_derivation_abort(&operation);
8854 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8855 psa_destroy_key(keys[i]);
8856 }
8857 psa_destroy_key(derived_key);
8858 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008859}
8860/* END_CASE */
8861
8862/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008863void derive_full(int alg_arg,
8864 data_t *key_data,
8865 data_t *input1,
8866 data_t *input2,
8867 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008868{
Ronald Cron5425a212020-08-04 14:58:35 +02008869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008870 psa_algorithm_t alg = alg_arg;
8871 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008872 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008873 unsigned char output_buffer[16];
8874 size_t expected_capacity = requested_capacity;
8875 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008877
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008879
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8881 psa_set_key_algorithm(&attributes, alg);
8882 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008883
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8885 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008886
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8888 input1->x, input1->len,
8889 input2->x, input2->len,
8890 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008891 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8895 &current_capacity));
8896 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008897
8898 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 while (current_capacity > 0) {
8900 size_t read_size = sizeof(output_buffer);
8901 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008902 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 }
8904 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8905 output_buffer,
8906 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008907 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8909 &current_capacity));
8910 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008911 }
8912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008913 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8915 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008916
Gilles Peskine449bd832023-01-11 14:50:10 +01008917 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008918
8919exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 psa_key_derivation_abort(&operation);
8921 psa_destroy_key(key);
8922 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008923}
8924/* END_CASE */
8925
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008926/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008927void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8928 int derivation_step,
8929 int capacity, int expected_capacity_status_arg,
8930 data_t *expected_output,
8931 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008932{
8933 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8934 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008935 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008936 uint8_t *output_buffer = NULL;
8937 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008938 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8939 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8940 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008941
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 ASSERT_ALLOC(output_buffer, expected_output->len);
8943 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008944
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8946 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8947 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008948
Gilles Peskine449bd832023-01-11 14:50:10 +01008949 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8950 step, input->x, input->len),
8951 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008952
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008954 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008956
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8958 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008959
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 TEST_EQUAL(status, expected_output_status);
8961 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8962 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8963 expected_output->len);
8964 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008965
8966exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008967 mbedtls_free(output_buffer);
8968 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008969 PSA_DONE();
8970}
8971/* END_CASE */
8972
Janos Follathe60c9052019-07-03 13:51:30 +01008973/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008974void derive_key_exercise(int alg_arg,
8975 data_t *key_data,
8976 data_t *input1,
8977 data_t *input2,
8978 int derived_type_arg,
8979 int derived_bits_arg,
8980 int derived_usage_arg,
8981 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008982{
Ronald Cron5425a212020-08-04 14:58:35 +02008983 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8984 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008985 psa_algorithm_t alg = alg_arg;
8986 psa_key_type_t derived_type = derived_type_arg;
8987 size_t derived_bits = derived_bits_arg;
8988 psa_key_usage_t derived_usage = derived_usage_arg;
8989 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008991 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008993 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008994
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8998 psa_set_key_algorithm(&attributes, alg);
8999 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9000 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9001 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009002
9003 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9005 input1->x, input1->len,
9006 input2->x, input2->len,
9007 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009008 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 }
Janos Follathe60c9052019-07-03 13:51:30 +01009010
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 psa_set_key_usage_flags(&attributes, derived_usage);
9012 psa_set_key_algorithm(&attributes, derived_alg);
9013 psa_set_key_type(&attributes, derived_type);
9014 psa_set_key_bits(&attributes, derived_bits);
9015 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9016 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009017
9018 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9020 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9021 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009022
9023 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009027
9028exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009029 /*
9030 * Key attributes may have been returned by psa_get_key_attributes()
9031 * thus reset them as required.
9032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 psa_key_derivation_abort(&operation);
9036 psa_destroy_key(base_key);
9037 psa_destroy_key(derived_key);
9038 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009039}
9040/* END_CASE */
9041
Janos Follath42fd8882019-07-03 14:17:09 +01009042/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009043void derive_key_export(int alg_arg,
9044 data_t *key_data,
9045 data_t *input1,
9046 data_t *input2,
9047 int bytes1_arg,
9048 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009049{
Ronald Cron5425a212020-08-04 14:58:35 +02009050 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9051 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009052 psa_algorithm_t alg = alg_arg;
9053 size_t bytes1 = bytes1_arg;
9054 size_t bytes2 = bytes2_arg;
9055 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009056 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009057 uint8_t *output_buffer = NULL;
9058 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009059 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9060 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009061 size_t length;
9062
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 ASSERT_ALLOC(output_buffer, capacity);
9064 ASSERT_ALLOC(export_buffer, capacity);
9065 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009066
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9068 psa_set_key_algorithm(&base_attributes, alg);
9069 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9070 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9071 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009072
9073 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9075 input1->x, input1->len,
9076 input2->x, input2->len,
9077 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 }
Janos Follath42fd8882019-07-03 14:17:09 +01009080
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9082 output_buffer,
9083 capacity));
9084 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009085
9086 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9088 input1->x, input1->len,
9089 input2->x, input2->len,
9090 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 }
Janos Follath42fd8882019-07-03 14:17:09 +01009093
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9095 psa_set_key_algorithm(&derived_attributes, 0);
9096 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9097 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9098 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9099 &derived_key));
9100 PSA_ASSERT(psa_export_key(derived_key,
9101 export_buffer, bytes1,
9102 &length));
9103 TEST_EQUAL(length, bytes1);
9104 PSA_ASSERT(psa_destroy_key(derived_key));
9105 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9106 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9107 &derived_key));
9108 PSA_ASSERT(psa_export_key(derived_key,
9109 export_buffer + bytes1, bytes2,
9110 &length));
9111 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009112
9113 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009114 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9115 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009116
9117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 mbedtls_free(output_buffer);
9119 mbedtls_free(export_buffer);
9120 psa_key_derivation_abort(&operation);
9121 psa_destroy_key(base_key);
9122 psa_destroy_key(derived_key);
9123 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009124}
9125/* END_CASE */
9126
9127/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009128void derive_key_type(int alg_arg,
9129 data_t *key_data,
9130 data_t *input1,
9131 data_t *input2,
9132 int key_type_arg, int bits_arg,
9133 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009134{
9135 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9136 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9137 const psa_algorithm_t alg = alg_arg;
9138 const psa_key_type_t key_type = key_type_arg;
9139 const size_t bits = bits_arg;
9140 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9141 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009143 uint8_t *export_buffer = NULL;
9144 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9145 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9146 size_t export_length;
9147
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 ASSERT_ALLOC(export_buffer, export_buffer_size);
9149 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009150
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9152 psa_set_key_algorithm(&base_attributes, alg);
9153 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9154 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9155 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009156
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009158 &operation, base_key, alg,
9159 input1->x, input1->len,
9160 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009164
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9166 psa_set_key_algorithm(&derived_attributes, 0);
9167 psa_set_key_type(&derived_attributes, key_type);
9168 psa_set_key_bits(&derived_attributes, bits);
9169 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9170 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 PSA_ASSERT(psa_export_key(derived_key,
9173 export_buffer, export_buffer_size,
9174 &export_length));
9175 ASSERT_COMPARE(export_buffer, export_length,
9176 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009177
9178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 mbedtls_free(export_buffer);
9180 psa_key_derivation_abort(&operation);
9181 psa_destroy_key(base_key);
9182 psa_destroy_key(derived_key);
9183 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009184}
9185/* END_CASE */
9186
9187/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009188void derive_key(int alg_arg,
9189 data_t *key_data, data_t *input1, data_t *input2,
9190 int type_arg, int bits_arg,
9191 int expected_status_arg,
9192 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009193{
Ronald Cron5425a212020-08-04 14:58:35 +02009194 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9195 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009196 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009197 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009198 size_t bits = bits_arg;
9199 psa_status_t expected_status = expected_status_arg;
9200 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9201 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9202 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9203
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009205
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9207 psa_set_key_algorithm(&base_attributes, alg);
9208 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9209 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9210 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009211
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9213 input1->x, input1->len,
9214 input2->x, input2->len,
9215 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009218
Gilles Peskine449bd832023-01-11 14:50:10 +01009219 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9220 psa_set_key_algorithm(&derived_attributes, 0);
9221 psa_set_key_type(&derived_attributes, type);
9222 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009223
9224 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 psa_key_derivation_output_key(&derived_attributes,
9226 &operation,
9227 &derived_key);
9228 if (is_large_output > 0) {
9229 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9230 }
9231 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009232
9233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 psa_key_derivation_abort(&operation);
9235 psa_destroy_key(base_key);
9236 psa_destroy_key(derived_key);
9237 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009238}
9239/* END_CASE */
9240
9241/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009242void key_agreement_setup(int alg_arg,
9243 int our_key_type_arg, int our_key_alg_arg,
9244 data_t *our_key_data, data_t *peer_key_data,
9245 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009246{
Ronald Cron5425a212020-08-04 14:58:35 +02009247 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009248 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009249 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009250 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009253 psa_status_t expected_status = expected_status_arg;
9254 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009255
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009257
Gilles Peskine449bd832023-01-11 14:50:10 +01009258 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9259 psa_set_key_algorithm(&attributes, our_key_alg);
9260 psa_set_key_type(&attributes, our_key_type);
9261 PSA_ASSERT(psa_import_key(&attributes,
9262 our_key_data->x, our_key_data->len,
9263 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009264
Gilles Peskine77f40d82019-04-11 21:27:06 +02009265 /* The tests currently include inputs that should fail at either step.
9266 * Test cases that fail at the setup step should be changed to call
9267 * key_derivation_setup instead, and this function should be renamed
9268 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 status = psa_key_derivation_setup(&operation, alg);
9270 if (status == PSA_SUCCESS) {
9271 TEST_EQUAL(psa_key_derivation_key_agreement(
9272 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9273 our_key,
9274 peer_key_data->x, peer_key_data->len),
9275 expected_status);
9276 } else {
9277 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009278 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009279
9280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 psa_key_derivation_abort(&operation);
9282 psa_destroy_key(our_key);
9283 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009284}
9285/* END_CASE */
9286
9287/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009288void raw_key_agreement(int alg_arg,
9289 int our_key_type_arg, data_t *our_key_data,
9290 data_t *peer_key_data,
9291 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009292{
Ronald Cron5425a212020-08-04 14:58:35 +02009293 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009294 psa_algorithm_t alg = alg_arg;
9295 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009296 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009297 unsigned char *output = NULL;
9298 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009299 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009300
Gilles Peskine449bd832023-01-11 14:50:10 +01009301 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009302
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9304 psa_set_key_algorithm(&attributes, alg);
9305 psa_set_key_type(&attributes, our_key_type);
9306 PSA_ASSERT(psa_import_key(&attributes,
9307 our_key_data->x, our_key_data->len,
9308 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009309
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9311 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009312
Gilles Peskine992bee82022-04-13 23:25:52 +02009313 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 TEST_LE_U(expected_output->len,
9315 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9316 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9317 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009318
9319 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009320 ASSERT_ALLOC(output, expected_output->len);
9321 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9322 peer_key_data->x, peer_key_data->len,
9323 output, expected_output->len,
9324 &output_length));
9325 ASSERT_COMPARE(output, output_length,
9326 expected_output->x, expected_output->len);
9327 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009328 output = NULL;
9329 output_length = ~0;
9330
9331 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009332 ASSERT_ALLOC(output, expected_output->len + 1);
9333 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9334 peer_key_data->x, peer_key_data->len,
9335 output, expected_output->len + 1,
9336 &output_length));
9337 ASSERT_COMPARE(output, output_length,
9338 expected_output->x, expected_output->len);
9339 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009340 output = NULL;
9341 output_length = ~0;
9342
9343 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 ASSERT_ALLOC(output, expected_output->len - 1);
9345 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9346 peer_key_data->x, peer_key_data->len,
9347 output, expected_output->len - 1,
9348 &output_length),
9349 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009350 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009351 TEST_LE_U(output_length, expected_output->len - 1);
9352 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009353 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009354
9355exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009356 mbedtls_free(output);
9357 psa_destroy_key(our_key);
9358 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009359}
9360/* END_CASE */
9361
9362/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009363void key_agreement_capacity(int alg_arg,
9364 int our_key_type_arg, data_t *our_key_data,
9365 data_t *peer_key_data,
9366 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009367{
Ronald Cron5425a212020-08-04 14:58:35 +02009368 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009369 psa_algorithm_t alg = alg_arg;
9370 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009371 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009373 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009374 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009375
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009377
Gilles Peskine449bd832023-01-11 14:50:10 +01009378 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9379 psa_set_key_algorithm(&attributes, alg);
9380 psa_set_key_type(&attributes, our_key_type);
9381 PSA_ASSERT(psa_import_key(&attributes,
9382 our_key_data->x, our_key_data->len,
9383 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009384
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9386 PSA_ASSERT(psa_key_derivation_key_agreement(
9387 &operation,
9388 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9389 peer_key_data->x, peer_key_data->len));
9390 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009391 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009392 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9393 PSA_KEY_DERIVATION_INPUT_INFO,
9394 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009395 }
Gilles Peskine59685592018-09-18 12:11:34 +02009396
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009397 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009398 PSA_ASSERT(psa_key_derivation_get_capacity(
9399 &operation, &actual_capacity));
9400 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009401
Gilles Peskinebf491972018-10-25 22:36:12 +02009402 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 while (actual_capacity > sizeof(output)) {
9404 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9405 output, sizeof(output)));
9406 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009407 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9409 output, actual_capacity));
9410 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9411 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009412
Gilles Peskine59685592018-09-18 12:11:34 +02009413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 psa_key_derivation_abort(&operation);
9415 psa_destroy_key(our_key);
9416 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009417}
9418/* END_CASE */
9419
9420/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009421void key_agreement_output(int alg_arg,
9422 int our_key_type_arg, data_t *our_key_data,
9423 data_t *peer_key_data,
9424 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009425{
Ronald Cron5425a212020-08-04 14:58:35 +02009426 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009427 psa_algorithm_t alg = alg_arg;
9428 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009429 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009430 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009431 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009432
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9434 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009435
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009437
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9439 psa_set_key_algorithm(&attributes, alg);
9440 psa_set_key_type(&attributes, our_key_type);
9441 PSA_ASSERT(psa_import_key(&attributes,
9442 our_key_data->x, our_key_data->len,
9443 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009444
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9446 PSA_ASSERT(psa_key_derivation_key_agreement(
9447 &operation,
9448 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9449 peer_key_data->x, peer_key_data->len));
9450 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009451 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9453 PSA_KEY_DERIVATION_INPUT_INFO,
9454 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009455 }
Gilles Peskine59685592018-09-18 12:11:34 +02009456
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9458 actual_output,
9459 expected_output1->len));
9460 ASSERT_COMPARE(actual_output, expected_output1->len,
9461 expected_output1->x, expected_output1->len);
9462 if (expected_output2->len != 0) {
9463 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9464 actual_output,
9465 expected_output2->len));
9466 ASSERT_COMPARE(actual_output, expected_output2->len,
9467 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009468 }
Gilles Peskine59685592018-09-18 12:11:34 +02009469
9470exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 psa_key_derivation_abort(&operation);
9472 psa_destroy_key(our_key);
9473 PSA_DONE();
9474 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009475}
9476/* END_CASE */
9477
9478/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009479void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009480{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009481 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009482 unsigned char *output = NULL;
9483 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009484 size_t i;
9485 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009486
Gilles Peskine449bd832023-01-11 14:50:10 +01009487 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009488
Gilles Peskine449bd832023-01-11 14:50:10 +01009489 ASSERT_ALLOC(output, bytes);
9490 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009491
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009493
Gilles Peskinea50d7392018-06-21 10:22:13 +02009494 /* Run several times, to ensure that every output byte will be
9495 * nonzero at least once with overwhelming probability
9496 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 for (run = 0; run < 10; run++) {
9498 if (bytes != 0) {
9499 memset(output, 0, bytes);
9500 }
9501 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009502
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 for (i = 0; i < bytes; i++) {
9504 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009505 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009506 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009507 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009508 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009509
9510 /* Check that every byte was changed to nonzero at least once. This
9511 * validates that psa_generate_random is overwriting every byte of
9512 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009513 for (i = 0; i < bytes; i++) {
9514 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009515 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009516
9517exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009518 PSA_DONE();
9519 mbedtls_free(output);
9520 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009521}
9522/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009523
9524/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009525void generate_key(int type_arg,
9526 int bits_arg,
9527 int usage_arg,
9528 int alg_arg,
9529 int expected_status_arg,
9530 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009531{
Ronald Cron5425a212020-08-04 14:58:35 +02009532 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009533 psa_key_type_t type = type_arg;
9534 psa_key_usage_t usage = usage_arg;
9535 size_t bits = bits_arg;
9536 psa_algorithm_t alg = alg_arg;
9537 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009539 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009540
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009542
Gilles Peskine449bd832023-01-11 14:50:10 +01009543 psa_set_key_usage_flags(&attributes, usage);
9544 psa_set_key_algorithm(&attributes, alg);
9545 psa_set_key_type(&attributes, type);
9546 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009547
9548 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009549 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009550
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 if (is_large_key > 0) {
9552 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9553 }
9554 TEST_EQUAL(status, expected_status);
9555 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009558
9559 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9561 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9562 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009563
Gilles Peskine818ca122018-06-20 18:16:48 +02009564 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009568
9569exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009570 /*
9571 * Key attributes may have been returned by psa_get_key_attributes()
9572 * thus reset them as required.
9573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009575
Gilles Peskine449bd832023-01-11 14:50:10 +01009576 psa_destroy_key(key);
9577 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009578}
9579/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009580
Ronald Cronee414c72021-03-18 18:50:08 +01009581/* 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 +01009582void generate_key_rsa(int bits_arg,
9583 data_t *e_arg,
9584 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009585{
Ronald Cron5425a212020-08-04 14:58:35 +02009586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009587 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009588 size_t bits = bits_arg;
9589 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9590 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9591 psa_status_t expected_status = expected_status_arg;
9592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9593 uint8_t *exported = NULL;
9594 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009596 size_t exported_length = SIZE_MAX;
9597 uint8_t *e_read_buffer = NULL;
9598 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009600 size_t e_read_length = SIZE_MAX;
9601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 if (e_arg->len == 0 ||
9603 (e_arg->len == 3 &&
9604 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009605 is_default_public_exponent = 1;
9606 e_read_size = 0;
9607 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009608 ASSERT_ALLOC(e_read_buffer, e_read_size);
9609 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009610
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 psa_set_key_usage_flags(&attributes, usage);
9614 psa_set_key_algorithm(&attributes, alg);
9615 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9616 e_arg->x, e_arg->len));
9617 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009618
9619 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9621 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009624
9625 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9627 TEST_EQUAL(psa_get_key_type(&attributes), type);
9628 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9629 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9630 e_read_buffer, e_read_size,
9631 &e_read_length));
9632 if (is_default_public_exponent) {
9633 TEST_EQUAL(e_read_length, 0);
9634 } else {
9635 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9636 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009637
9638 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009639 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009640 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009642
9643 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009644 PSA_ASSERT(psa_export_public_key(key,
9645 exported, exported_size,
9646 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009647 {
9648 uint8_t *p = exported;
9649 uint8_t *end = exported + exported_length;
9650 size_t len;
9651 /* RSAPublicKey ::= SEQUENCE {
9652 * modulus INTEGER, -- n
9653 * publicExponent INTEGER } -- e
9654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9656 MBEDTLS_ASN1_SEQUENCE |
9657 MBEDTLS_ASN1_CONSTRUCTED));
9658 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9659 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9660 MBEDTLS_ASN1_INTEGER));
9661 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009662 ++p;
9663 --len;
9664 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 if (e_arg->len == 0) {
9666 TEST_EQUAL(len, 3);
9667 TEST_EQUAL(p[0], 1);
9668 TEST_EQUAL(p[1], 0);
9669 TEST_EQUAL(p[2], 1);
9670 } else {
9671 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009672 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009673 }
9674
9675exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009676 /*
9677 * Key attributes may have been returned by psa_get_key_attributes() or
9678 * set by psa_set_key_domain_parameters() thus reset them as required.
9679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009680 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009681
Gilles Peskine449bd832023-01-11 14:50:10 +01009682 psa_destroy_key(key);
9683 PSA_DONE();
9684 mbedtls_free(e_read_buffer);
9685 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009686}
9687/* END_CASE */
9688
Darryl Greend49a4992018-06-18 17:27:26 +01009689/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009690void persistent_key_load_key_from_storage(data_t *data,
9691 int type_arg, int bits_arg,
9692 int usage_flags_arg, int alg_arg,
9693 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009694{
Gilles Peskine449bd832023-01-11 14:50:10 +01009695 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9698 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009699 psa_key_type_t type = type_arg;
9700 size_t bits = bits_arg;
9701 psa_key_usage_t usage_flags = usage_flags_arg;
9702 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009703 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009704 unsigned char *first_export = NULL;
9705 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009707 size_t first_exported_length;
9708 size_t second_exported_length;
9709
Gilles Peskine449bd832023-01-11 14:50:10 +01009710 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9711 ASSERT_ALLOC(first_export, export_size);
9712 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009713 }
Darryl Greend49a4992018-06-18 17:27:26 +01009714
Gilles Peskine449bd832023-01-11 14:50:10 +01009715 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009716
Gilles Peskine449bd832023-01-11 14:50:10 +01009717 psa_set_key_id(&attributes, key_id);
9718 psa_set_key_usage_flags(&attributes, usage_flags);
9719 psa_set_key_algorithm(&attributes, alg);
9720 psa_set_key_type(&attributes, type);
9721 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009722
Gilles Peskine449bd832023-01-11 14:50:10 +01009723 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009724 case IMPORT_KEY:
9725 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009726 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9727 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009728 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009729
Darryl Green0c6575a2018-11-07 16:05:30 +00009730 case GENERATE_KEY:
9731 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009732 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009733 break;
9734
9735 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009736#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 {
9738 /* Create base key */
9739 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9740 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9741 psa_set_key_usage_flags(&base_attributes,
9742 PSA_KEY_USAGE_DERIVE);
9743 psa_set_key_algorithm(&base_attributes, derive_alg);
9744 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9745 PSA_ASSERT(psa_import_key(&base_attributes,
9746 data->x, data->len,
9747 &base_key));
9748 /* Derive a key. */
9749 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9750 PSA_ASSERT(psa_key_derivation_input_key(
9751 &operation,
9752 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9753 PSA_ASSERT(psa_key_derivation_input_bytes(
9754 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9755 NULL, 0));
9756 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9757 &operation,
9758 &key));
9759 PSA_ASSERT(psa_key_derivation_abort(&operation));
9760 PSA_ASSERT(psa_destroy_key(base_key));
9761 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9762 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009763#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009764 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009765#endif
9766 break;
9767
9768 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009769 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009770 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009771 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009772 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009773
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009774 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9776 PSA_ASSERT(psa_export_key(key,
9777 first_export, export_size,
9778 &first_exported_length));
9779 if (generation_method == IMPORT_KEY) {
9780 ASSERT_COMPARE(data->x, data->len,
9781 first_export, first_exported_length);
9782 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009783 }
Darryl Greend49a4992018-06-18 17:27:26 +01009784
9785 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009786 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009787 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009788 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009789
Darryl Greend49a4992018-06-18 17:27:26 +01009790 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009791 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9792 TEST_ASSERT(mbedtls_svc_key_id_equal(
9793 psa_get_key_id(&attributes), key_id));
9794 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9795 PSA_KEY_LIFETIME_PERSISTENT);
9796 TEST_EQUAL(psa_get_key_type(&attributes), type);
9797 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9798 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9799 mbedtls_test_update_key_usage_flags(usage_flags));
9800 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009801
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009802 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009803 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9804 PSA_ASSERT(psa_export_key(key,
9805 second_export, export_size,
9806 &second_exported_length));
9807 ASSERT_COMPARE(first_export, first_exported_length,
9808 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009809 }
9810
9811 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009812 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009813 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009814 }
Darryl Greend49a4992018-06-18 17:27:26 +01009815
9816exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009817 /*
9818 * Key attributes may have been returned by psa_get_key_attributes()
9819 * thus reset them as required.
9820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009821 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009822
Gilles Peskine449bd832023-01-11 14:50:10 +01009823 mbedtls_free(first_export);
9824 mbedtls_free(second_export);
9825 psa_key_derivation_abort(&operation);
9826 psa_destroy_key(base_key);
9827 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009828 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009829}
9830/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009831
Neil Armstronga557cb82022-06-10 08:58:32 +02009832/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009833void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9834 int primitive_arg, int hash_arg, int role_arg,
9835 int test_input, data_t *pw_data,
9836 int inj_err_type_arg,
9837 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009838{
9839 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9840 psa_pake_operation_t operation = psa_pake_operation_init();
9841 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009842 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009843 psa_key_type_t key_type_pw = key_type_pw_arg;
9844 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009845 psa_algorithm_t hash_alg = hash_arg;
9846 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9848 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009849 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9850 psa_status_t expected_error = expected_error_arg;
9851 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009852 unsigned char *output_buffer = NULL;
9853 size_t output_len = 0;
9854
Gilles Peskine449bd832023-01-11 14:50:10 +01009855 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009856
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009857 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009858 PSA_PAKE_STEP_KEY_SHARE);
9859 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009860
Gilles Peskine449bd832023-01-11 14:50:10 +01009861 if (pw_data->len > 0) {
9862 psa_set_key_usage_flags(&attributes, key_usage_pw);
9863 psa_set_key_algorithm(&attributes, alg);
9864 psa_set_key_type(&attributes, key_type_pw);
9865 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9866 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009867 }
9868
Gilles Peskine449bd832023-01-11 14:50:10 +01009869 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9870 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9871 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009872
Gilles Peskine449bd832023-01-11 14:50:10 +01009873 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009874
Gilles Peskine449bd832023-01-11 14:50:10 +01009875 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9876 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9877 expected_error);
9878 PSA_ASSERT(psa_pake_abort(&operation));
9879 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9880 expected_error);
9881 PSA_ASSERT(psa_pake_abort(&operation));
9882 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9883 expected_error);
9884 PSA_ASSERT(psa_pake_abort(&operation));
9885 TEST_EQUAL(psa_pake_set_role(&operation, role),
9886 expected_error);
9887 PSA_ASSERT(psa_pake_abort(&operation));
9888 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9889 NULL, 0, NULL),
9890 expected_error);
9891 PSA_ASSERT(psa_pake_abort(&operation));
9892 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9893 expected_error);
9894 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009895 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009896 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009897
Gilles Peskine449bd832023-01-11 14:50:10 +01009898 status = psa_pake_setup(&operation, &cipher_suite);
9899 if (status != PSA_SUCCESS) {
9900 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009901 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009902 }
9903
Gilles Peskine449bd832023-01-11 14:50:10 +01009904 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9905 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9906 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009907 goto exit;
9908 }
9909
Gilles Peskine449bd832023-01-11 14:50:10 +01009910 status = psa_pake_set_role(&operation, role);
9911 if (status != PSA_SUCCESS) {
9912 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009913 goto exit;
9914 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009915
Gilles Peskine449bd832023-01-11 14:50:10 +01009916 if (pw_data->len > 0) {
9917 status = psa_pake_set_password_key(&operation, key);
9918 if (status != PSA_SUCCESS) {
9919 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009920 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009921 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009922 }
9923
Gilles Peskine449bd832023-01-11 14:50:10 +01009924 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9925 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9926 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009927 goto exit;
9928 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009929
Gilles Peskine449bd832023-01-11 14:50:10 +01009930 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9931 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9932 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009933 goto exit;
9934 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009935
Gilles Peskine449bd832023-01-11 14:50:10 +01009936 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009937 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009938 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9939 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009940 goto exit;
9941 }
9942
Gilles Peskine449bd832023-01-11 14:50:10 +01009943 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009944 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009945 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9946 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009947 goto exit;
9948 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009949
Gilles Peskine449bd832023-01-11 14:50:10 +01009950 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9951 PSA_PAKE_STEP_KEY_SHARE);
9952 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9953 PSA_PAKE_STEP_ZK_PUBLIC);
9954 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9955 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009956
Gilles Peskine449bd832023-01-11 14:50:10 +01009957 if (test_input) {
9958 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9959 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9960 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009961 goto exit;
9962 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009963
Gilles Peskine449bd832023-01-11 14:50:10 +01009964 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9965 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9966 output_buffer, size_zk_proof),
9967 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009968 goto exit;
9969 }
9970
Gilles Peskine449bd832023-01-11 14:50:10 +01009971 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9972 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9973 output_buffer, size_zk_proof),
9974 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009975 goto exit;
9976 }
9977
Gilles Peskine449bd832023-01-11 14:50:10 +01009978 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9979 output_buffer, size_key_share);
9980 if (status != PSA_SUCCESS) {
9981 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009982 goto exit;
9983 }
9984
Gilles Peskine449bd832023-01-11 14:50:10 +01009985 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9986 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9987 output_buffer, size_zk_public + 1),
9988 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009989 goto exit;
9990 }
9991
Gilles Peskine449bd832023-01-11 14:50:10 +01009992 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009993 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009994 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9995 output_buffer, size_zk_public + 1);
9996 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9997 output_buffer, size_zk_public),
9998 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009999 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010000 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010001 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010002 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10003 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10004 NULL, 0, NULL),
10005 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010006 goto exit;
10007 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010008
Gilles Peskine449bd832023-01-11 14:50:10 +010010009 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10010 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10011 output_buffer, buf_size, &output_len),
10012 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010013 goto exit;
10014 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010015
Gilles Peskine449bd832023-01-11 14:50:10 +010010016 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10017 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10018 output_buffer, buf_size, &output_len),
10019 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010020 goto exit;
10021 }
10022
Gilles Peskine449bd832023-01-11 14:50:10 +010010023 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10024 output_buffer, buf_size, &output_len);
10025 if (status != PSA_SUCCESS) {
10026 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010027 goto exit;
10028 }
10029
Gilles Peskine449bd832023-01-11 14:50:10 +010010030 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010031
Gilles Peskine449bd832023-01-11 14:50:10 +010010032 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10033 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10034 output_buffer, size_zk_public - 1, &output_len),
10035 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010036 goto exit;
10037 }
10038
Gilles Peskine449bd832023-01-11 14:50:10 +010010039 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010040 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10042 output_buffer, size_zk_public - 1, &output_len);
10043 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10044 output_buffer, buf_size, &output_len),
10045 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010046 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010047 }
10048 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010049
10050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010051 PSA_ASSERT(psa_destroy_key(key));
10052 PSA_ASSERT(psa_pake_abort(&operation));
10053 mbedtls_free(output_buffer);
10054 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010055}
10056/* END_CASE */
10057
Neil Armstronga557cb82022-06-10 08:58:32 +020010058/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010059void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10060 int client_input_first, int inject_error,
10061 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010062{
10063 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10064 psa_pake_operation_t server = psa_pake_operation_init();
10065 psa_pake_operation_t client = psa_pake_operation_init();
10066 psa_algorithm_t alg = alg_arg;
10067 psa_algorithm_t hash_alg = hash_arg;
10068 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10070
Gilles Peskine449bd832023-01-11 14:50:10 +010010071 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010072
Gilles Peskine449bd832023-01-11 14:50:10 +010010073 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10074 psa_set_key_algorithm(&attributes, alg);
10075 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10076 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10077 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010078
Gilles Peskine449bd832023-01-11 14:50:10 +010010079 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10080 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10081 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010082
10083
Gilles Peskine449bd832023-01-11 14:50:10 +010010084 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10085 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010086
Gilles Peskine449bd832023-01-11 14:50:10 +010010087 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10088 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010089
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10091 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010092
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 ecjpake_do_round(alg, primitive_arg, &server, &client,
10094 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010095
Gilles Peskine449bd832023-01-11 14:50:10 +010010096 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010098 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010099
Gilles Peskine449bd832023-01-11 14:50:10 +010010100 ecjpake_do_round(alg, primitive_arg, &server, &client,
10101 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010102
10103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010104 psa_destroy_key(key);
10105 psa_pake_abort(&server);
10106 psa_pake_abort(&client);
10107 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010108}
10109/* END_CASE */
10110
10111/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010112void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10113 int derive_alg_arg, data_t *pw_data,
10114 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010115{
10116 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10117 psa_pake_operation_t server = psa_pake_operation_init();
10118 psa_pake_operation_t client = psa_pake_operation_init();
10119 psa_algorithm_t alg = alg_arg;
10120 psa_algorithm_t hash_alg = hash_arg;
10121 psa_algorithm_t derive_alg = derive_alg_arg;
10122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10124 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010125 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010126 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010127 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010128 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010129
Gilles Peskine449bd832023-01-11 14:50:10 +010010130 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010131
Gilles Peskine449bd832023-01-11 14:50:10 +010010132 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10133 psa_set_key_algorithm(&attributes, alg);
10134 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10135 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10136 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010137
Gilles Peskine449bd832023-01-11 14:50:10 +010010138 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10139 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10140 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010141
Neil Armstrong1e855602022-06-15 11:32:11 +020010142 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010143 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10144 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010145
Gilles Peskine449bd832023-01-11 14:50:10 +010010146 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10147 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10148 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10149 PSA_KEY_DERIVATION_INPUT_SEED,
10150 (const uint8_t *) "", 0));
10151 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10152 PSA_KEY_DERIVATION_INPUT_SEED,
10153 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010154 }
10155
Gilles Peskine449bd832023-01-11 14:50:10 +010010156 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10157 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010158
Gilles Peskine449bd832023-01-11 14:50:10 +010010159 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10160 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010161
Gilles Peskine449bd832023-01-11 14:50:10 +010010162 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10163 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010164
Gilles Peskine449bd832023-01-11 14:50:10 +010010165 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10166 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10167 PSA_ERROR_BAD_STATE);
10168 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10169 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010170 goto exit;
10171 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010172
Neil Armstrongf983caf2022-06-15 15:27:48 +020010173 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010174 ecjpake_do_round(alg, primitive_arg, &server, &client,
10175 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010176
Gilles Peskine449bd832023-01-11 14:50:10 +010010177 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10178 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10179 PSA_ERROR_BAD_STATE);
10180 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10181 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010182 goto exit;
10183 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010184
Neil Armstrongf983caf2022-06-15 15:27:48 +020010185 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010186 ecjpake_do_round(alg, primitive_arg, &server, &client,
10187 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010188
Gilles Peskine449bd832023-01-11 14:50:10 +010010189 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10190 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010191
10192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010193 psa_key_derivation_abort(&server_derive);
10194 psa_key_derivation_abort(&client_derive);
10195 psa_destroy_key(key);
10196 psa_pake_abort(&server);
10197 psa_pake_abort(&client);
10198 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010199}
10200/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010201
10202/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010203void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010204{
10205 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10206 const size_t bits = 256;
10207 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010208 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010209 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010210 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010211
10212 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10213 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010214 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10215 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10216 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10217 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010218 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010219 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10220 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010221
10222 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010223 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10224 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10225 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10226 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10227 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10228 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010229
10230 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010231 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10232 PSA_PAKE_OUTPUT_MAX_SIZE);
10233 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10234 PSA_PAKE_OUTPUT_MAX_SIZE);
10235 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10236 PSA_PAKE_OUTPUT_MAX_SIZE);
10237 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10238 PSA_PAKE_INPUT_MAX_SIZE);
10239 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10240 PSA_PAKE_INPUT_MAX_SIZE);
10241 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10242 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010243}
10244/* END_CASE */