blob: 8e25ad2e83338e82ffe318fcaa07ca659c5e834f [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;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506
Moran Pekercb088e72018-07-17 17:36:59 +03001507 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 ASSERT_ALLOC(exported, export_size);
1509 if (!canonical_input) {
1510 ASSERT_ALLOC(reexported, export_size);
1511 }
1512 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 psa_set_key_lifetime(&attributes, lifetime);
1515 psa_set_key_usage_flags(&attributes, usage_arg);
1516 psa_set_key_algorithm(&attributes, alg);
1517 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001518
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001519 if (PSA_KEY_TYPE_IS_DH(type) &&
1520 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001521 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1522 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001523 }
1524
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001526 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001527 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528
1529 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1531 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1532 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1533 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534
1535 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 status = psa_export_key(key, exported, export_size, &exported_length);
1537 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001538
1539 /* The exported length must be set by psa_export_key() to a value between 0
1540 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1542 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1543 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1546 export_size - exported_length));
1547 if (status != PSA_SUCCESS) {
1548 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001549 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001550 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001551
Gilles Peskineea38a922021-02-13 00:05:16 +01001552 /* Run sanity checks on the exported key. For non-canonical inputs,
1553 * this validates the canonical representations. For canonical inputs,
1554 * this doesn't directly validate the implementation, but it still helps
1555 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (!psa_key_lifetime_is_external(lifetime)) {
1557 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301558 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 }
Archana4d7ae1d2021-07-07 02:50:22 +05301560 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (canonical_input) {
1563 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1564 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001565 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1567 &key2));
1568 PSA_ASSERT(psa_export_key(key2,
1569 reexported,
1570 export_size,
1571 &reexported_length));
1572 ASSERT_COMPARE(exported, exported_length,
1573 reexported, reexported_length);
1574 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 TEST_LE_U(exported_length,
1577 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1578 psa_get_key_bits(&got_attributes)));
1579 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580
1581destroy:
1582 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 PSA_ASSERT(psa_destroy_key(key));
1584 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585
1586exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001587 /*
1588 * Key attributes may have been returned by psa_get_key_attributes()
1589 * thus reset them as required.
1590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 psa_reset_key_attributes(&got_attributes);
1592 psa_destroy_key(key);
1593 mbedtls_free(exported);
1594 mbedtls_free(reexported);
1595 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596}
1597/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001598
Moran Pekerf709f4a2018-06-06 17:26:04 +03001599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600void import_export_public_key(data_t *data,
1601 int type_arg, // key pair or public key
1602 int alg_arg,
1603 int lifetime_arg,
1604 int export_size_delta,
1605 int expected_export_status_arg,
1606 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001607{
Ronald Cron5425a212020-08-04 14:58:35 +02001608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001609 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001610 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001611 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001612 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301613 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001614 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001615 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001616 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 psa_set_key_lifetime(&attributes, lifetime);
1622 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1623 psa_set_key_algorithm(&attributes, alg);
1624 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001625
1626 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001628
Gilles Peskine49c25912018-10-29 15:15:31 +01001629 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 ASSERT_ALLOC(exported, export_size);
1631 status = psa_export_public_key(key,
1632 exported, export_size,
1633 &exported_length);
1634 TEST_EQUAL(status, expected_export_status);
1635 if (status == PSA_SUCCESS) {
1636 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001637 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1639 bits = psa_get_key_bits(&attributes);
1640 TEST_LE_U(expected_public_key->len,
1641 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1642 TEST_LE_U(expected_public_key->len,
1643 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1644 TEST_LE_U(expected_public_key->len,
1645 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1646 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1647 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001648 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001650 /*
1651 * Key attributes may have been returned by psa_get_key_attributes()
1652 * thus reset them as required.
1653 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 mbedtls_free(exported);
1657 psa_destroy_key(key);
1658 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001659}
1660/* END_CASE */
1661
Gilles Peskine20035e32018-02-03 22:44:14 +01001662/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void import_and_exercise_key(data_t *data,
1664 int type_arg,
1665 int bits_arg,
1666 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001667{
Ronald Cron5425a212020-08-04 14:58:35 +02001668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001669 psa_key_type_t type = type_arg;
1670 size_t bits = bits_arg;
1671 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001674 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 psa_set_key_usage_flags(&attributes, usage);
1679 psa_set_key_algorithm(&attributes, alg);
1680 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001681
1682 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001684
1685 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1687 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1688 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689
1690 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 PSA_ASSERT(psa_destroy_key(key));
1696 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001697
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001698exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001699 /*
1700 * Key attributes may have been returned by psa_get_key_attributes()
1701 * thus reset them as required.
1702 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 psa_reset_key_attributes(&attributes);
1706 psa_destroy_key(key);
1707 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001712void effective_key_attributes(int type_arg, int expected_type_arg,
1713 int bits_arg, int expected_bits_arg,
1714 int usage_arg, int expected_usage_arg,
1715 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001716{
Ronald Cron5425a212020-08-04 14:58:35 +02001717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001718 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001719 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001720 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001721 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001722 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001723 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001724 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001725 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 psa_set_key_usage_flags(&attributes, usage);
1731 psa_set_key_algorithm(&attributes, alg);
1732 psa_set_key_type(&attributes, key_type);
1733 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 PSA_ASSERT(psa_generate_key(&attributes, &key));
1736 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1739 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1740 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1741 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1742 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001743
1744exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001745 /*
1746 * Key attributes may have been returned by psa_get_key_attributes()
1747 * thus reset them as required.
1748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 psa_destroy_key(key);
1752 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001753}
1754/* END_CASE */
1755
1756/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001757void check_key_policy(int type_arg, int bits_arg,
1758 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001759{
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1761 usage_arg,
1762 mbedtls_test_update_key_usage_flags(usage_arg),
1763 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001764 goto exit;
1765}
1766/* END_CASE */
1767
1768/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001770{
1771 /* Test each valid way of initializing the object, except for `= {0}`, as
1772 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1773 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001774 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001776 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1777 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1782 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1783 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 TEST_EQUAL(psa_get_key_type(&func), 0);
1786 TEST_EQUAL(psa_get_key_type(&init), 0);
1787 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 TEST_EQUAL(psa_get_key_bits(&func), 0);
1790 TEST_EQUAL(psa_get_key_bits(&init), 0);
1791 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1794 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1795 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1798 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1799 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001800}
1801/* END_CASE */
1802
1803/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804void mac_key_policy(int policy_usage_arg,
1805 int policy_alg_arg,
1806 int key_type_arg,
1807 data_t *key_data,
1808 int exercise_alg_arg,
1809 int expected_status_sign_arg,
1810 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001811{
Ronald Cron5425a212020-08-04 14:58:35 +02001812 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001814 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001815 psa_key_type_t key_type = key_type_arg;
1816 psa_algorithm_t policy_alg = policy_alg_arg;
1817 psa_algorithm_t exercise_alg = exercise_alg_arg;
1818 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001819 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001820 psa_status_t expected_status_sign = expected_status_sign_arg;
1821 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001822 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 psa_set_key_usage_flags(&attributes, policy_usage);
1827 psa_set_key_algorithm(&attributes, policy_alg);
1828 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1831 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1834 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1837 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001838
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001839 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001841 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1843 input, 128,
1844 mac, PSA_MAC_MAX_SIZE, &mac_len),
1845 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001846
Neil Armstrong3af9b972022-02-07 12:20:21 +01001847 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 PSA_ASSERT(psa_mac_abort(&operation));
1849 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1850 if (status == PSA_SUCCESS) {
1851 status = psa_mac_update(&operation, input, 128);
1852 if (status == PSA_SUCCESS) {
1853 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1854 &mac_len),
1855 expected_status_sign);
1856 } else {
1857 TEST_EQUAL(status, expected_status_sign);
1858 }
1859 } else {
1860 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001861 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001863
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001864 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 status = psa_mac_verify(key, exercise_alg, input, 128,
1866 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1869 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1870 } else {
1871 TEST_EQUAL(status, expected_status_verify);
1872 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001873
Neil Armstrong3af9b972022-02-07 12:20:21 +01001874 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1876 if (status == PSA_SUCCESS) {
1877 status = psa_mac_update(&operation, input, 128);
1878 if (status == PSA_SUCCESS) {
1879 status = psa_mac_verify_finish(&operation, mac, mac_len);
1880 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1881 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1882 } else {
1883 TEST_EQUAL(status, expected_status_verify);
1884 }
1885 } else {
1886 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001887 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 } else {
1889 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001890 }
1891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 memset(mac, 0, sizeof(mac));
1895 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1896 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001897
1898exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 psa_mac_abort(&operation);
1900 psa_destroy_key(key);
1901 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902}
1903/* END_CASE */
1904
1905/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001906void cipher_key_policy(int policy_usage_arg,
1907 int policy_alg,
1908 int key_type,
1909 data_t *key_data,
1910 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911{
Ronald Cron5425a212020-08-04 14:58:35 +02001912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001914 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001915 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001916 size_t output_buffer_size = 0;
1917 size_t input_buffer_size = 0;
1918 size_t output_length = 0;
1919 uint8_t *output = NULL;
1920 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 psa_status_t status;
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1924 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1925 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 ASSERT_ALLOC(input, input_buffer_size);
1928 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 psa_set_key_usage_flags(&attributes, policy_usage);
1933 psa_set_key_algorithm(&attributes, policy_alg);
1934 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1937 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001938
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001939 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 TEST_EQUAL(policy_usage,
1941 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001942
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001943 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1945 output, output_buffer_size,
1946 &output_length);
1947 if (policy_alg == exercise_alg &&
1948 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1949 PSA_ASSERT(status);
1950 } else {
1951 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1952 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001953
1954 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1956 if (policy_alg == exercise_alg &&
1957 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1958 PSA_ASSERT(status);
1959 } else {
1960 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1961 }
1962 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001964 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1966 input, input_buffer_size,
1967 &output_length);
1968 if (policy_alg == exercise_alg &&
1969 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1970 PSA_ASSERT(status);
1971 } else {
1972 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1973 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001974
1975 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1977 if (policy_alg == exercise_alg &&
1978 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1979 PSA_ASSERT(status);
1980 } else {
1981 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1982 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
1984exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 psa_cipher_abort(&operation);
1986 mbedtls_free(input);
1987 mbedtls_free(output);
1988 psa_destroy_key(key);
1989 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990}
1991/* END_CASE */
1992
1993/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994void aead_key_policy(int policy_usage_arg,
1995 int policy_alg,
1996 int key_type,
1997 data_t *key_data,
1998 int nonce_length_arg,
1999 int tag_length_arg,
2000 int exercise_alg,
2001 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002{
Ronald Cron5425a212020-08-04 14:58:35 +02002003 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002004 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002005 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002006 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002008 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010 size_t nonce_length = nonce_length_arg;
2011 unsigned char tag[16];
2012 size_t tag_length = tag_length_arg;
2013 size_t output_length;
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 TEST_LE_U(nonce_length, sizeof(nonce));
2016 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 psa_set_key_usage_flags(&attributes, policy_usage);
2021 psa_set_key_algorithm(&attributes, policy_alg);
2022 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2025 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002027 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 TEST_EQUAL(policy_usage,
2029 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002030
Neil Armstrong752d8112022-02-07 14:51:11 +01002031 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 status = psa_aead_encrypt(key, exercise_alg,
2033 nonce, nonce_length,
2034 NULL, 0,
2035 NULL, 0,
2036 tag, tag_length,
2037 &output_length);
2038 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2039 TEST_EQUAL(status, expected_status);
2040 } else {
2041 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2042 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Neil Armstrong752d8112022-02-07 14:51:11 +01002044 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2046 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2047 TEST_EQUAL(status, expected_status);
2048 } else {
2049 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2050 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002051
2052 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 memset(tag, 0, sizeof(tag));
2054 status = psa_aead_decrypt(key, exercise_alg,
2055 nonce, nonce_length,
2056 NULL, 0,
2057 tag, tag_length,
2058 NULL, 0,
2059 &output_length);
2060 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2061 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2062 } else if (expected_status == PSA_SUCCESS) {
2063 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2064 } else {
2065 TEST_EQUAL(status, expected_status);
2066 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067
Neil Armstrong752d8112022-02-07 14:51:11 +01002068 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 PSA_ASSERT(psa_aead_abort(&operation));
2070 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2071 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2072 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2073 } else {
2074 TEST_EQUAL(status, expected_status);
2075 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002076
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 PSA_ASSERT(psa_aead_abort(&operation));
2079 psa_destroy_key(key);
2080 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081}
2082/* END_CASE */
2083
2084/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085void asymmetric_encryption_key_policy(int policy_usage_arg,
2086 int policy_alg,
2087 int key_type,
2088 data_t *key_data,
2089 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090{
Ronald Cron5425a212020-08-04 14:58:35 +02002091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002093 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094 psa_status_t status;
2095 size_t key_bits;
2096 size_t buffer_length;
2097 unsigned char *buffer = NULL;
2098 size_t output_length;
2099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 psa_set_key_usage_flags(&attributes, policy_usage);
2103 psa_set_key_algorithm(&attributes, policy_alg);
2104 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2107 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002109 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 TEST_EQUAL(policy_usage,
2111 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2114 key_bits = psa_get_key_bits(&attributes);
2115 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2116 exercise_alg);
2117 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 status = psa_asymmetric_encrypt(key, exercise_alg,
2120 NULL, 0,
2121 NULL, 0,
2122 buffer, buffer_length,
2123 &output_length);
2124 if (policy_alg == exercise_alg &&
2125 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2126 PSA_ASSERT(status);
2127 } else {
2128 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2129 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (buffer_length != 0) {
2132 memset(buffer, 0, buffer_length);
2133 }
2134 status = psa_asymmetric_decrypt(key, exercise_alg,
2135 buffer, buffer_length,
2136 NULL, 0,
2137 buffer, buffer_length,
2138 &output_length);
2139 if (policy_alg == exercise_alg &&
2140 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2141 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2142 } else {
2143 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2144 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145
2146exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002147 /*
2148 * Key attributes may have been returned by psa_get_key_attributes()
2149 * thus reset them as required.
2150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 psa_destroy_key(key);
2154 PSA_DONE();
2155 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156}
2157/* END_CASE */
2158
2159/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002160void asymmetric_signature_key_policy(int policy_usage_arg,
2161 int policy_alg,
2162 int key_type,
2163 data_t *key_data,
2164 int exercise_alg,
2165 int payload_length_arg,
2166 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167{
Ronald Cron5425a212020-08-04 14:58:35 +02002168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002170 psa_key_usage_t policy_usage = policy_usage_arg;
2171 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002172 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002174 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2175 * compatible with the policy and `payload_length_arg` is supposed to be
2176 * a valid input length to sign. If `payload_length_arg <= 0`,
2177 * `exercise_alg` is supposed to be forbidden by the policy. */
2178 int compatible_alg = payload_length_arg > 0;
2179 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002181 size_t signature_length;
2182
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002183 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002184 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 TEST_EQUAL(expected_usage,
2186 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 psa_set_key_usage_flags(&attributes, policy_usage);
2191 psa_set_key_algorithm(&attributes, policy_alg);
2192 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2195 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 status = psa_sign_hash(key, exercise_alg,
2200 payload, payload_length,
2201 signature, sizeof(signature),
2202 &signature_length);
2203 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2204 PSA_ASSERT(status);
2205 } else {
2206 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2207 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 memset(signature, 0, sizeof(signature));
2210 status = psa_verify_hash(key, exercise_alg,
2211 payload, payload_length,
2212 signature, sizeof(signature));
2213 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2214 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2215 } else {
2216 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2217 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2220 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2221 status = psa_sign_message(key, exercise_alg,
2222 payload, payload_length,
2223 signature, sizeof(signature),
2224 &signature_length);
2225 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2226 PSA_ASSERT(status);
2227 } else {
2228 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2229 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 memset(signature, 0, sizeof(signature));
2232 status = psa_verify_message(key, exercise_alg,
2233 payload, payload_length,
2234 signature, sizeof(signature));
2235 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2236 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2237 } else {
2238 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2239 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002240 }
2241
Gilles Peskined5b33222018-06-18 22:20:03 +02002242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 psa_destroy_key(key);
2244 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002245}
2246/* END_CASE */
2247
Janos Follathba3fab92019-06-11 14:50:16 +01002248/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249void derive_key_policy(int policy_usage,
2250 int policy_alg,
2251 int key_type,
2252 data_t *key_data,
2253 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002254{
Ronald Cron5425a212020-08-04 14:58:35 +02002255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002258 psa_status_t status;
2259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 psa_set_key_usage_flags(&attributes, policy_usage);
2263 psa_set_key_algorithm(&attributes, policy_alg);
2264 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2267 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2272 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2273 PSA_ASSERT(psa_key_derivation_input_bytes(
2274 &operation,
2275 PSA_KEY_DERIVATION_INPUT_SEED,
2276 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002277 }
Janos Follathba3fab92019-06-11 14:50:16 +01002278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 status = psa_key_derivation_input_key(&operation,
2280 PSA_KEY_DERIVATION_INPUT_SECRET,
2281 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (policy_alg == exercise_alg &&
2284 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2285 PSA_ASSERT(status);
2286 } else {
2287 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2288 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002289
2290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 psa_key_derivation_abort(&operation);
2292 psa_destroy_key(key);
2293 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002294}
2295/* END_CASE */
2296
2297/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298void agreement_key_policy(int policy_usage,
2299 int policy_alg,
2300 int key_type_arg,
2301 data_t *key_data,
2302 int exercise_alg,
2303 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304{
Ronald Cron5425a212020-08-04 14:58:35 +02002305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002307 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002308 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002310 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 psa_set_key_usage_flags(&attributes, policy_usage);
2315 psa_set_key_algorithm(&attributes, policy_alg);
2316 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2319 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2322 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002325
2326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 psa_key_derivation_abort(&operation);
2328 psa_destroy_key(key);
2329 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002330}
2331/* END_CASE */
2332
2333/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334void key_policy_alg2(int key_type_arg, data_t *key_data,
2335 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002336{
Ronald Cron5425a212020-08-04 14:58:35 +02002337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002338 psa_key_type_t key_type = key_type_arg;
2339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2340 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2341 psa_key_usage_t usage = usage_arg;
2342 psa_algorithm_t alg = alg_arg;
2343 psa_algorithm_t alg2 = alg2_arg;
2344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 psa_set_key_usage_flags(&attributes, usage);
2348 psa_set_key_algorithm(&attributes, alg);
2349 psa_set_key_enrollment_algorithm(&attributes, alg2);
2350 psa_set_key_type(&attributes, key_type);
2351 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2352 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002353
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002354 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 usage = mbedtls_test_update_key_usage_flags(usage);
2356 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2357 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2358 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2359 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002362 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 }
2364 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002367
2368exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002369 /*
2370 * Key attributes may have been returned by psa_get_key_attributes()
2371 * thus reset them as required.
2372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 psa_destroy_key(key);
2376 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002377}
2378/* END_CASE */
2379
2380/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002381void raw_agreement_key_policy(int policy_usage,
2382 int policy_alg,
2383 int key_type_arg,
2384 data_t *key_data,
2385 int exercise_alg,
2386 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002387{
Ronald Cron5425a212020-08-04 14:58:35 +02002388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002390 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002391 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002392 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002393 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 psa_set_key_usage_flags(&attributes, policy_usage);
2398 psa_set_key_algorithm(&attributes, policy_alg);
2399 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2402 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002407
2408exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 psa_key_derivation_abort(&operation);
2410 psa_destroy_key(key);
2411 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412}
2413/* END_CASE */
2414
2415/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416void copy_success(int source_usage_arg,
2417 int source_alg_arg, int source_alg2_arg,
2418 unsigned int source_lifetime_arg,
2419 int type_arg, data_t *material,
2420 int copy_attributes,
2421 int target_usage_arg,
2422 int target_alg_arg, int target_alg2_arg,
2423 unsigned int target_lifetime_arg,
2424 int expected_usage_arg,
2425 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002426{
Gilles Peskineca25db92019-04-19 11:43:08 +02002427 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2428 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002429 psa_key_usage_t expected_usage = expected_usage_arg;
2430 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002431 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302432 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2433 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002434 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2435 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002436 uint8_t *export_buffer = NULL;
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002439
Gilles Peskineca25db92019-04-19 11:43:08 +02002440 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2442 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2443 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2444 psa_set_key_type(&source_attributes, type_arg);
2445 psa_set_key_lifetime(&source_attributes, source_lifetime);
2446 PSA_ASSERT(psa_import_key(&source_attributes,
2447 material->x, material->len,
2448 &source_key));
2449 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450
Gilles Peskineca25db92019-04-19 11:43:08 +02002451 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002453 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002454 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (target_usage_arg != -1) {
2458 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2459 }
2460 if (target_alg_arg != -1) {
2461 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2462 }
2463 if (target_alg2_arg != -1) {
2464 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2465 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002466
Archana8a180362021-07-05 02:18:48 +05302467
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 PSA_ASSERT(psa_copy_key(source_key,
2470 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002471
2472 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002474
2475 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2477 TEST_EQUAL(psa_get_key_type(&source_attributes),
2478 psa_get_key_type(&target_attributes));
2479 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2480 psa_get_key_bits(&target_attributes));
2481 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2482 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2483 TEST_EQUAL(expected_alg2,
2484 psa_get_key_enrollment_algorithm(&target_attributes));
2485 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 ASSERT_ALLOC(export_buffer, material->len);
2488 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2489 material->len, &length));
2490 ASSERT_COMPARE(material->x, material->len,
2491 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002492 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (!psa_key_lifetime_is_external(target_lifetime)) {
2495 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 }
2498 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302499 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 }
Archana8a180362021-07-05 02:18:48 +05302501 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002504
2505exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002506 /*
2507 * Source and target key attributes may have been returned by
2508 * psa_get_key_attributes() thus reset them as required.
2509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 psa_reset_key_attributes(&source_attributes);
2511 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 PSA_DONE();
2514 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002515}
2516/* END_CASE */
2517
2518/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002519void copy_fail(int source_usage_arg,
2520 int source_alg_arg, int source_alg2_arg,
2521 int source_lifetime_arg,
2522 int type_arg, data_t *material,
2523 int target_type_arg, int target_bits_arg,
2524 int target_usage_arg,
2525 int target_alg_arg, int target_alg2_arg,
2526 int target_id_arg, int target_lifetime_arg,
2527 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002528{
2529 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2530 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002531 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2532 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002536
2537 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2539 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2540 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2541 psa_set_key_type(&source_attributes, type_arg);
2542 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2543 PSA_ASSERT(psa_import_key(&source_attributes,
2544 material->x, material->len,
2545 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002546
2547 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 psa_set_key_id(&target_attributes, key_id);
2549 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2550 psa_set_key_type(&target_attributes, target_type_arg);
2551 psa_set_key_bits(&target_attributes, target_bits_arg);
2552 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2553 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2554 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002555
2556 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 TEST_EQUAL(psa_copy_key(source_key,
2558 &target_attributes, &target_key),
2559 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002562
Gilles Peskine4a644642019-05-03 17:14:08 +02002563exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 psa_reset_key_attributes(&source_attributes);
2565 psa_reset_key_attributes(&target_attributes);
2566 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002567}
2568/* END_CASE */
2569
2570/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002572{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002573 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002574 /* Test each valid way of initializing the object, except for `= {0}`, as
2575 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2576 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002577 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002579 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2580 psa_hash_operation_t zero;
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002583
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002584 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2586 PSA_ERROR_BAD_STATE);
2587 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2588 PSA_ERROR_BAD_STATE);
2589 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2590 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002591
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002592 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 PSA_ASSERT(psa_hash_abort(&func));
2594 PSA_ASSERT(psa_hash_abort(&init));
2595 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002596}
2597/* END_CASE */
2598
2599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600void hash_setup(int alg_arg,
2601 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002602{
2603 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002604 uint8_t *output = NULL;
2605 size_t output_size = 0;
2606 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002607 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002608 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002609 psa_status_t status;
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002612
Neil Armstrongedb20862022-02-07 15:47:44 +01002613 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 output_size = PSA_HASH_LENGTH(alg);
2615 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 status = psa_hash_compute(alg, NULL, 0,
2618 output, output_size, &output_length);
2619 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002620
2621 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 status = psa_hash_setup(&operation, alg);
2623 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002624
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002625 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002627
2628 /* If setup failed, reproduce the failure, so as to
2629 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (status != PSA_SUCCESS) {
2631 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2632 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002633
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002634 /* Now the operation object should be reusable. */
2635#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2637 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002638#endif
2639
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 mbedtls_free(output);
2642 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643}
2644/* END_CASE */
2645
2646/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647void hash_compute_fail(int alg_arg, data_t *input,
2648 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002649{
2650 psa_algorithm_t alg = alg_arg;
2651 uint8_t *output = NULL;
2652 size_t output_size = output_size_arg;
2653 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002654 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002655 psa_status_t expected_status = expected_status_arg;
2656 psa_status_t status;
2657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002661
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002662 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_hash_compute(alg, input->x, input->len,
2664 output, output_size, &output_length);
2665 TEST_EQUAL(status, expected_status);
2666 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002667
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002668 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 status = psa_hash_setup(&operation, alg);
2670 if (status == PSA_SUCCESS) {
2671 status = psa_hash_update(&operation, input->x, input->len);
2672 if (status == PSA_SUCCESS) {
2673 status = psa_hash_finish(&operation, output, output_size,
2674 &output_length);
2675 if (status == PSA_SUCCESS) {
2676 TEST_LE_U(output_length, output_size);
2677 } else {
2678 TEST_EQUAL(status, expected_status);
2679 }
2680 } else {
2681 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002682 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 } else {
2684 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002685 }
2686
Gilles Peskine0a749c82019-11-28 19:33:58 +01002687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 PSA_ASSERT(psa_hash_abort(&operation));
2689 mbedtls_free(output);
2690 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002691}
2692/* END_CASE */
2693
2694/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002695void hash_compare_fail(int alg_arg, data_t *input,
2696 data_t *reference_hash,
2697 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002698{
2699 psa_algorithm_t alg = alg_arg;
2700 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002701 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002702 psa_status_t status;
2703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002705
Neil Armstrong55a1be12022-02-07 11:23:20 +01002706 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 status = psa_hash_compare(alg, input->x, input->len,
2708 reference_hash->x, reference_hash->len);
2709 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002710
Neil Armstrong55a1be12022-02-07 11:23:20 +01002711 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 status = psa_hash_setup(&operation, alg);
2713 if (status == PSA_SUCCESS) {
2714 status = psa_hash_update(&operation, input->x, input->len);
2715 if (status == PSA_SUCCESS) {
2716 status = psa_hash_verify(&operation, reference_hash->x,
2717 reference_hash->len);
2718 TEST_EQUAL(status, expected_status);
2719 } else {
2720 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002721 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 } else {
2723 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002724 }
2725
Gilles Peskine88e08462020-01-28 20:43:00 +01002726exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 PSA_ASSERT(psa_hash_abort(&operation));
2728 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002729}
2730/* END_CASE */
2731
2732/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002733void hash_compute_compare(int alg_arg, data_t *input,
2734 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002735{
2736 psa_algorithm_t alg = alg_arg;
2737 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2738 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002739 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002740 size_t i;
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002743
Neil Armstrongca30a002022-02-07 11:40:23 +01002744 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2746 output, PSA_HASH_LENGTH(alg),
2747 &output_length));
2748 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2749 ASSERT_COMPARE(output, output_length,
2750 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002751
Neil Armstrongca30a002022-02-07 11:40:23 +01002752 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 PSA_ASSERT(psa_hash_setup(&operation, alg));
2754 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2755 PSA_ASSERT(psa_hash_finish(&operation, output,
2756 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);
Neil Armstrongca30a002022-02-07 11:40:23 +01002761
2762 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2764 output, sizeof(output),
2765 &output_length));
2766 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2767 ASSERT_COMPARE(output, output_length,
2768 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002769
Neil Armstrongca30a002022-02-07 11:40:23 +01002770 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 PSA_ASSERT(psa_hash_setup(&operation, alg));
2772 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2773 PSA_ASSERT(psa_hash_finish(&operation, output,
2774 sizeof(output), &output_length));
2775 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2776 ASSERT_COMPARE(output, output_length,
2777 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002778
2779 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2781 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002782
Neil Armstrongca30a002022-02-07 11:40:23 +01002783 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 PSA_ASSERT(psa_hash_setup(&operation, alg));
2785 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2786 PSA_ASSERT(psa_hash_verify(&operation, output,
2787 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002788
2789 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2791 output, output_length + 1),
2792 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002793
Neil Armstrongca30a002022-02-07 11:40:23 +01002794 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 PSA_ASSERT(psa_hash_setup(&operation, alg));
2796 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2797 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2798 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002799
2800 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2802 output, output_length - 1),
2803 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002804
Neil Armstrongca30a002022-02-07 11:40:23 +01002805 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 PSA_ASSERT(psa_hash_setup(&operation, alg));
2807 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2808 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2809 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002810
Gilles Peskine0a749c82019-11-28 19:33:58 +01002811 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 for (i = 0; i < output_length; i++) {
2813 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002814 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002815
2816 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2818 output, output_length),
2819 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002820
2821 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 PSA_ASSERT(psa_hash_setup(&operation, alg));
2823 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2824 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2825 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002826
Gilles Peskine0a749c82019-11-28 19:33:58 +01002827 output[i] ^= 1;
2828 }
2829
2830exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 PSA_ASSERT(psa_hash_abort(&operation));
2832 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002833}
2834/* END_CASE */
2835
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002836/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002838{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002839 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002840 unsigned char input[] = "";
2841 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002842 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002843 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2844 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2846 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002847 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002848 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002849 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002852
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002853 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 PSA_ASSERT(psa_hash_setup(&operation, alg));
2855 ASSERT_OPERATION_IS_ACTIVE(operation);
2856 TEST_EQUAL(psa_hash_setup(&operation, alg),
2857 PSA_ERROR_BAD_STATE);
2858 ASSERT_OPERATION_IS_INACTIVE(operation);
2859 PSA_ASSERT(psa_hash_abort(&operation));
2860 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2864 PSA_ERROR_BAD_STATE);
2865 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002866
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002867 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002869 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 ASSERT_OPERATION_IS_ACTIVE(operation);
2871 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2872 PSA_ERROR_BAD_STATE);
2873 ASSERT_OPERATION_IS_INACTIVE(operation);
2874 PSA_ASSERT(psa_hash_abort(&operation));
2875 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002876
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002877 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 PSA_ASSERT(psa_hash_setup(&operation, alg));
2879 PSA_ASSERT(psa_hash_finish(&operation,
2880 hash, sizeof(hash), &hash_len));
2881 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2882 PSA_ERROR_BAD_STATE);
2883 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002884
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002885 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 TEST_EQUAL(psa_hash_verify(&operation,
2887 valid_hash, sizeof(valid_hash)),
2888 PSA_ERROR_BAD_STATE);
2889 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002890
2891 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 PSA_ASSERT(psa_hash_setup(&operation, alg));
2893 PSA_ASSERT(psa_hash_finish(&operation,
2894 hash, sizeof(hash), &hash_len));
2895 TEST_EQUAL(psa_hash_verify(&operation,
2896 valid_hash, sizeof(valid_hash)),
2897 PSA_ERROR_BAD_STATE);
2898 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002899
2900 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 PSA_ASSERT(psa_hash_setup(&operation, alg));
2902 ASSERT_OPERATION_IS_ACTIVE(operation);
2903 PSA_ASSERT(psa_hash_verify(&operation,
2904 valid_hash, sizeof(valid_hash)));
2905 ASSERT_OPERATION_IS_INACTIVE(operation);
2906 TEST_EQUAL(psa_hash_verify(&operation,
2907 valid_hash, sizeof(valid_hash)),
2908 PSA_ERROR_BAD_STATE);
2909 ASSERT_OPERATION_IS_INACTIVE(operation);
2910 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002911
2912 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 TEST_EQUAL(psa_hash_finish(&operation,
2914 hash, sizeof(hash), &hash_len),
2915 PSA_ERROR_BAD_STATE);
2916 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002917
2918 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 PSA_ASSERT(psa_hash_setup(&operation, alg));
2920 PSA_ASSERT(psa_hash_finish(&operation,
2921 hash, sizeof(hash), &hash_len));
2922 TEST_EQUAL(psa_hash_finish(&operation,
2923 hash, sizeof(hash), &hash_len),
2924 PSA_ERROR_BAD_STATE);
2925 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002926
2927 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 PSA_ASSERT(psa_hash_setup(&operation, alg));
2929 PSA_ASSERT(psa_hash_verify(&operation,
2930 valid_hash, sizeof(valid_hash)));
2931 TEST_EQUAL(psa_hash_finish(&operation,
2932 hash, sizeof(hash), &hash_len),
2933 PSA_ERROR_BAD_STATE);
2934 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002935
2936exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002938}
2939/* END_CASE */
2940
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002941/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002943{
2944 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002945 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2946 * appended to it */
2947 unsigned char hash[] = {
2948 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2949 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2951 };
2952 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002953 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002956
itayzafrir27e69452018-11-01 14:26:34 +02002957 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 PSA_ASSERT(psa_hash_setup(&operation, alg));
2959 ASSERT_OPERATION_IS_ACTIVE(operation);
2960 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2961 PSA_ERROR_INVALID_SIGNATURE);
2962 ASSERT_OPERATION_IS_INACTIVE(operation);
2963 PSA_ASSERT(psa_hash_abort(&operation));
2964 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002965
itayzafrir27e69452018-11-01 14:26:34 +02002966 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 PSA_ASSERT(psa_hash_setup(&operation, alg));
2968 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2969 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002970
itayzafrir27e69452018-11-01 14:26:34 +02002971 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 PSA_ASSERT(psa_hash_setup(&operation, alg));
2973 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2974 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002975
itayzafrirec93d302018-10-18 18:01:10 +03002976exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002978}
2979/* END_CASE */
2980
Ronald Cronee414c72021-03-18 18:50:08 +01002981/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002982void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002983{
2984 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002985 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002987 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002988 size_t hash_len;
2989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002991
itayzafrir58028322018-10-25 10:22:01 +03002992 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 PSA_ASSERT(psa_hash_setup(&operation, alg));
2994 TEST_EQUAL(psa_hash_finish(&operation,
2995 hash, expected_size - 1, &hash_len),
2996 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002997
2998exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003000}
3001/* END_CASE */
3002
Ronald Cronee414c72021-03-18 18:50:08 +01003003/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003004void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003005{
3006 psa_algorithm_t alg = PSA_ALG_SHA_256;
3007 unsigned char hash[PSA_HASH_MAX_SIZE];
3008 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3009 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3010 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3011 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3012 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3013 size_t hash_len;
3014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 PSA_ASSERT(psa_crypto_init());
3016 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3019 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3020 PSA_ASSERT(psa_hash_finish(&op_finished,
3021 hash, sizeof(hash), &hash_len));
3022 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3023 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3026 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3029 PSA_ASSERT(psa_hash_finish(&op_init,
3030 hash, sizeof(hash), &hash_len));
3031 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3032 PSA_ASSERT(psa_hash_finish(&op_finished,
3033 hash, sizeof(hash), &hash_len));
3034 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3035 PSA_ASSERT(psa_hash_finish(&op_aborted,
3036 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037
3038exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 psa_hash_abort(&op_source);
3040 psa_hash_abort(&op_init);
3041 psa_hash_abort(&op_setup);
3042 psa_hash_abort(&op_finished);
3043 psa_hash_abort(&op_aborted);
3044 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003045}
3046/* END_CASE */
3047
Ronald Cronee414c72021-03-18 18:50:08 +01003048/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003050{
3051 psa_algorithm_t alg = PSA_ALG_SHA_256;
3052 unsigned char hash[PSA_HASH_MAX_SIZE];
3053 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3054 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3055 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3056 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3057 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3058 size_t hash_len;
3059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3063 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3064 PSA_ASSERT(psa_hash_finish(&op_finished,
3065 hash, sizeof(hash), &hash_len));
3066 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3067 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3070 PSA_ASSERT(psa_hash_finish(&op_target,
3071 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3074 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3075 PSA_ERROR_BAD_STATE);
3076 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3077 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003078
3079exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 psa_hash_abort(&op_target);
3081 psa_hash_abort(&op_init);
3082 psa_hash_abort(&op_setup);
3083 psa_hash_abort(&op_finished);
3084 psa_hash_abort(&op_aborted);
3085 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003086}
3087/* END_CASE */
3088
itayzafrir58028322018-10-25 10:22:01 +03003089/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003090void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003091{
Jaeden Amero252ef282019-02-15 14:05:35 +00003092 const uint8_t input[1] = { 0 };
3093
Jaeden Amero769ce272019-01-04 11:48:03 +00003094 /* Test each valid way of initializing the object, except for `= {0}`, as
3095 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3096 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003097 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003099 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3100 psa_mac_operation_t zero;
3101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003103
Jaeden Amero252ef282019-02-15 14:05:35 +00003104 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 TEST_EQUAL(psa_mac_update(&func,
3106 input, sizeof(input)),
3107 PSA_ERROR_BAD_STATE);
3108 TEST_EQUAL(psa_mac_update(&init,
3109 input, sizeof(input)),
3110 PSA_ERROR_BAD_STATE);
3111 TEST_EQUAL(psa_mac_update(&zero,
3112 input, sizeof(input)),
3113 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003114
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003115 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 PSA_ASSERT(psa_mac_abort(&func));
3117 PSA_ASSERT(psa_mac_abort(&init));
3118 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003119}
3120/* END_CASE */
3121
3122/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003123void mac_setup(int key_type_arg,
3124 data_t *key,
3125 int alg_arg,
3126 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003127{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003128 psa_key_type_t key_type = key_type_arg;
3129 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003130 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003131 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003132 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3133#if defined(KNOWN_SUPPORTED_MAC_ALG)
3134 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3135#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3140 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 }
3143 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003144
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003145 /* The operation object should be reusable. */
3146#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3148 smoke_test_key_data,
3149 sizeof(smoke_test_key_data),
3150 KNOWN_SUPPORTED_MAC_ALG,
3151 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 }
3154 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003155#endif
3156
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159}
3160/* END_CASE */
3161
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003162/* 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 +01003163void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003164{
Ronald Cron5425a212020-08-04 14:58:35 +02003165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003166 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3167 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003168 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003169 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3170 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3172 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003174 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3175 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3176 size_t sign_mac_length = 0;
3177 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3178 const uint8_t verify_mac[] = {
3179 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3180 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3182 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 PSA_ASSERT(psa_crypto_init());
3185 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3186 psa_set_key_algorithm(&attributes, alg);
3187 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003188
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3190 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003191
Jaeden Amero252ef282019-02-15 14:05:35 +00003192 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3194 PSA_ERROR_BAD_STATE);
3195 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003196
3197 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3199 &sign_mac_length),
3200 PSA_ERROR_BAD_STATE);
3201 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003202
3203 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 TEST_EQUAL(psa_mac_verify_finish(&operation,
3205 verify_mac, sizeof(verify_mac)),
3206 PSA_ERROR_BAD_STATE);
3207 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003208
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003209 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3211 ASSERT_OPERATION_IS_ACTIVE(operation);
3212 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3213 PSA_ERROR_BAD_STATE);
3214 ASSERT_OPERATION_IS_INACTIVE(operation);
3215 PSA_ASSERT(psa_mac_abort(&operation));
3216 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003217
Jaeden Amero252ef282019-02-15 14:05:35 +00003218 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3220 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3221 PSA_ASSERT(psa_mac_sign_finish(&operation,
3222 sign_mac, sizeof(sign_mac),
3223 &sign_mac_length));
3224 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3225 PSA_ERROR_BAD_STATE);
3226 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003227
3228 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3230 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3231 PSA_ASSERT(psa_mac_verify_finish(&operation,
3232 verify_mac, sizeof(verify_mac)));
3233 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3234 PSA_ERROR_BAD_STATE);
3235 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003236
3237 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3239 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3240 PSA_ASSERT(psa_mac_sign_finish(&operation,
3241 sign_mac, sizeof(sign_mac),
3242 &sign_mac_length));
3243 TEST_EQUAL(psa_mac_sign_finish(&operation,
3244 sign_mac, sizeof(sign_mac),
3245 &sign_mac_length),
3246 PSA_ERROR_BAD_STATE);
3247 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003248
3249 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3251 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3252 PSA_ASSERT(psa_mac_verify_finish(&operation,
3253 verify_mac, sizeof(verify_mac)));
3254 TEST_EQUAL(psa_mac_verify_finish(&operation,
3255 verify_mac, sizeof(verify_mac)),
3256 PSA_ERROR_BAD_STATE);
3257 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003258
3259 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3261 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3262 ASSERT_OPERATION_IS_ACTIVE(operation);
3263 TEST_EQUAL(psa_mac_verify_finish(&operation,
3264 verify_mac, sizeof(verify_mac)),
3265 PSA_ERROR_BAD_STATE);
3266 ASSERT_OPERATION_IS_INACTIVE(operation);
3267 PSA_ASSERT(psa_mac_abort(&operation));
3268 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003269
3270 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3272 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3273 ASSERT_OPERATION_IS_ACTIVE(operation);
3274 TEST_EQUAL(psa_mac_sign_finish(&operation,
3275 sign_mac, sizeof(sign_mac),
3276 &sign_mac_length),
3277 PSA_ERROR_BAD_STATE);
3278 ASSERT_OPERATION_IS_INACTIVE(operation);
3279 PSA_ASSERT(psa_mac_abort(&operation));
3280 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003283
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003286}
3287/* END_CASE */
3288
3289/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003290void mac_sign_verify_multi(int key_type_arg,
3291 data_t *key_data,
3292 int alg_arg,
3293 data_t *input,
3294 int is_verify,
3295 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003296{
3297 size_t data_part_len = 0;
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003300 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (mac_multipart_internal_func(key_type_arg, key_data,
3304 alg_arg,
3305 input, data_part_len,
3306 expected_mac,
3307 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003308 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003310
3311 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 if (mac_multipart_internal_func(key_type_arg, key_data,
3315 alg_arg,
3316 input, data_part_len,
3317 expected_mac,
3318 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003319 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003321 }
3322
3323 /* Goto is required to silence warnings about unused labels, as we
3324 * don't actually do any test assertions in this function. */
3325 goto exit;
3326}
3327/* END_CASE */
3328
3329/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003330void mac_sign(int key_type_arg,
3331 data_t *key_data,
3332 int alg_arg,
3333 data_t *input,
3334 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003335{
Ronald Cron5425a212020-08-04 14:58:35 +02003336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003337 psa_key_type_t key_type = key_type_arg;
3338 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003339 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003341 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003342 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003344 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003345 const size_t output_sizes_to_test[] = {
3346 0,
3347 1,
3348 expected_mac->len - 1,
3349 expected_mac->len,
3350 expected_mac->len + 1,
3351 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003354 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3360 psa_set_key_algorithm(&attributes, alg);
3361 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3364 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003367 const size_t output_size = output_sizes_to_test[i];
3368 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 (output_size >= expected_mac->len ? PSA_SUCCESS :
3370 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 mbedtls_test_set_step(output_size);
3373 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003374
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003375 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 TEST_EQUAL(psa_mac_compute(key, alg,
3377 input->x, input->len,
3378 actual_mac, output_size, &mac_length),
3379 expected_status);
3380 if (expected_status == PSA_SUCCESS) {
3381 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3382 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003383 }
3384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 if (output_size > 0) {
3386 memset(actual_mac, 0, output_size);
3387 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003388
3389 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3391 PSA_ASSERT(psa_mac_update(&operation,
3392 input->x, input->len));
3393 TEST_EQUAL(psa_mac_sign_finish(&operation,
3394 actual_mac, output_size,
3395 &mac_length),
3396 expected_status);
3397 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 if (expected_status == PSA_SUCCESS) {
3400 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3401 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003402 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003404 actual_mac = NULL;
3405 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003406
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003407exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 psa_mac_abort(&operation);
3409 psa_destroy_key(key);
3410 PSA_DONE();
3411 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003412}
3413/* END_CASE */
3414
3415/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003416void mac_verify(int key_type_arg,
3417 data_t *key_data,
3418 int alg_arg,
3419 data_t *input,
3420 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003421{
Ronald Cron5425a212020-08-04 14:58:35 +02003422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003423 psa_key_type_t key_type = key_type_arg;
3424 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003425 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003427 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003430
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003432
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3434 psa_set_key_algorithm(&attributes, alg);
3435 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003436
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3438 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003439
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003440 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3442 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003443
3444 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3446 PSA_ASSERT(psa_mac_update(&operation,
3447 input->x, input->len));
3448 PSA_ASSERT(psa_mac_verify_finish(&operation,
3449 expected_mac->x,
3450 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003451
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003452 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 TEST_EQUAL(psa_mac_verify(key, alg,
3454 input->x, input->len,
3455 expected_mac->x,
3456 expected_mac->len - 1),
3457 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003458
3459 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3461 PSA_ASSERT(psa_mac_update(&operation,
3462 input->x, input->len));
3463 TEST_EQUAL(psa_mac_verify_finish(&operation,
3464 expected_mac->x,
3465 expected_mac->len - 1),
3466 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003467
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003468 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3470 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3471 TEST_EQUAL(psa_mac_verify(key, alg,
3472 input->x, input->len,
3473 perturbed_mac, expected_mac->len + 1),
3474 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003475
3476 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3478 PSA_ASSERT(psa_mac_update(&operation,
3479 input->x, input->len));
3480 TEST_EQUAL(psa_mac_verify_finish(&operation,
3481 perturbed_mac,
3482 expected_mac->len + 1),
3483 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003484
3485 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 for (size_t i = 0; i < expected_mac->len; i++) {
3487 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003488 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003489
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 TEST_EQUAL(psa_mac_verify(key, alg,
3491 input->x, input->len,
3492 perturbed_mac, expected_mac->len),
3493 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003494
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3496 PSA_ASSERT(psa_mac_update(&operation,
3497 input->x, input->len));
3498 TEST_EQUAL(psa_mac_verify_finish(&operation,
3499 perturbed_mac,
3500 expected_mac->len),
3501 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003502 perturbed_mac[i] ^= 1;
3503 }
3504
Gilles Peskine8c9def32018-02-08 10:02:12 +01003505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 psa_mac_abort(&operation);
3507 psa_destroy_key(key);
3508 PSA_DONE();
3509 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003510}
3511/* END_CASE */
3512
3513/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003514void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003515{
Jaeden Ameroab439972019-02-15 14:12:05 +00003516 const uint8_t input[1] = { 0 };
3517 unsigned char output[1] = { 0 };
3518 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003519 /* Test each valid way of initializing the object, except for `= {0}`, as
3520 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3521 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003522 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003524 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3525 psa_cipher_operation_t zero;
3526
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003528
Jaeden Ameroab439972019-02-15 14:12:05 +00003529 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 TEST_EQUAL(psa_cipher_update(&func,
3531 input, sizeof(input),
3532 output, sizeof(output),
3533 &output_length),
3534 PSA_ERROR_BAD_STATE);
3535 TEST_EQUAL(psa_cipher_update(&init,
3536 input, sizeof(input),
3537 output, sizeof(output),
3538 &output_length),
3539 PSA_ERROR_BAD_STATE);
3540 TEST_EQUAL(psa_cipher_update(&zero,
3541 input, sizeof(input),
3542 output, sizeof(output),
3543 &output_length),
3544 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003545
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003546 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 PSA_ASSERT(psa_cipher_abort(&func));
3548 PSA_ASSERT(psa_cipher_abort(&init));
3549 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003550}
3551/* END_CASE */
3552
3553/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003554void cipher_setup(int key_type_arg,
3555 data_t *key,
3556 int alg_arg,
3557 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003558{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003559 psa_key_type_t key_type = key_type_arg;
3560 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003561 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003562 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003563 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003564#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003565 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3566#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003567
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003569
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3571 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003572 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 }
3574 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003575
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003576 /* The operation object should be reusable. */
3577#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3579 smoke_test_key_data,
3580 sizeof(smoke_test_key_data),
3581 KNOWN_SUPPORTED_CIPHER_ALG,
3582 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 }
3585 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003586#endif
3587
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 psa_cipher_abort(&operation);
3590 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003591}
3592/* END_CASE */
3593
Ronald Cronee414c72021-03-18 18:50:08 +01003594/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003595void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003596{
Ronald Cron5425a212020-08-04 14:58:35 +02003597 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003598 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3599 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003601 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003602 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003603 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003604 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003605 0xaa, 0xaa, 0xaa, 0xaa
3606 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003607 const uint8_t text[] = {
3608 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003609 0xbb, 0xbb, 0xbb, 0xbb
3610 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003611 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003612 size_t length = 0;
3613
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 PSA_ASSERT(psa_crypto_init());
3615 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3616 psa_set_key_algorithm(&attributes, alg);
3617 psa_set_key_type(&attributes, key_type);
3618 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3619 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003620
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003621 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3623 ASSERT_OPERATION_IS_ACTIVE(operation);
3624 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3625 PSA_ERROR_BAD_STATE);
3626 ASSERT_OPERATION_IS_INACTIVE(operation);
3627 PSA_ASSERT(psa_cipher_abort(&operation));
3628 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003629
3630 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003631 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3632 ASSERT_OPERATION_IS_ACTIVE(operation);
3633 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3634 PSA_ERROR_BAD_STATE);
3635 ASSERT_OPERATION_IS_INACTIVE(operation);
3636 PSA_ASSERT(psa_cipher_abort(&operation));
3637 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003638
Jaeden Ameroab439972019-02-15 14:12:05 +00003639 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3641 buffer, sizeof(buffer),
3642 &length),
3643 PSA_ERROR_BAD_STATE);
3644 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003645
3646 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3648 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3649 buffer, sizeof(buffer),
3650 &length));
3651 ASSERT_OPERATION_IS_ACTIVE(operation);
3652 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3653 buffer, sizeof(buffer),
3654 &length),
3655 PSA_ERROR_BAD_STATE);
3656 ASSERT_OPERATION_IS_INACTIVE(operation);
3657 PSA_ASSERT(psa_cipher_abort(&operation));
3658 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003659
3660 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3662 PSA_ASSERT(psa_cipher_set_iv(&operation,
3663 iv, sizeof(iv)));
3664 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3665 buffer, sizeof(buffer),
3666 &length),
3667 PSA_ERROR_BAD_STATE);
3668 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003669
3670 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003671 TEST_EQUAL(psa_cipher_set_iv(&operation,
3672 iv, sizeof(iv)),
3673 PSA_ERROR_BAD_STATE);
3674 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003675
3676 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003677 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3678 PSA_ASSERT(psa_cipher_set_iv(&operation,
3679 iv, sizeof(iv)));
3680 ASSERT_OPERATION_IS_ACTIVE(operation);
3681 TEST_EQUAL(psa_cipher_set_iv(&operation,
3682 iv, sizeof(iv)),
3683 PSA_ERROR_BAD_STATE);
3684 ASSERT_OPERATION_IS_INACTIVE(operation);
3685 PSA_ASSERT(psa_cipher_abort(&operation));
3686 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003687
3688 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3690 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3691 buffer, sizeof(buffer),
3692 &length));
3693 TEST_EQUAL(psa_cipher_set_iv(&operation,
3694 iv, sizeof(iv)),
3695 PSA_ERROR_BAD_STATE);
3696 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003697
3698 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 TEST_EQUAL(psa_cipher_update(&operation,
3700 text, sizeof(text),
3701 buffer, sizeof(buffer),
3702 &length),
3703 PSA_ERROR_BAD_STATE);
3704 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003705
3706 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3708 ASSERT_OPERATION_IS_ACTIVE(operation);
3709 TEST_EQUAL(psa_cipher_update(&operation,
3710 text, sizeof(text),
3711 buffer, sizeof(buffer),
3712 &length),
3713 PSA_ERROR_BAD_STATE);
3714 ASSERT_OPERATION_IS_INACTIVE(operation);
3715 PSA_ASSERT(psa_cipher_abort(&operation));
3716 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003717
3718 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3720 PSA_ASSERT(psa_cipher_set_iv(&operation,
3721 iv, sizeof(iv)));
3722 PSA_ASSERT(psa_cipher_finish(&operation,
3723 buffer, sizeof(buffer), &length));
3724 TEST_EQUAL(psa_cipher_update(&operation,
3725 text, sizeof(text),
3726 buffer, sizeof(buffer),
3727 &length),
3728 PSA_ERROR_BAD_STATE);
3729 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003730
3731 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003732 TEST_EQUAL(psa_cipher_finish(&operation,
3733 buffer, sizeof(buffer), &length),
3734 PSA_ERROR_BAD_STATE);
3735 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003736
3737 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003738 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003739 /* Not calling update means we are encrypting an empty buffer, which is OK
3740 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 ASSERT_OPERATION_IS_ACTIVE(operation);
3742 TEST_EQUAL(psa_cipher_finish(&operation,
3743 buffer, sizeof(buffer), &length),
3744 PSA_ERROR_BAD_STATE);
3745 ASSERT_OPERATION_IS_INACTIVE(operation);
3746 PSA_ASSERT(psa_cipher_abort(&operation));
3747 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003748
3749 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003750 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3751 PSA_ASSERT(psa_cipher_set_iv(&operation,
3752 iv, sizeof(iv)));
3753 PSA_ASSERT(psa_cipher_finish(&operation,
3754 buffer, sizeof(buffer), &length));
3755 TEST_EQUAL(psa_cipher_finish(&operation,
3756 buffer, sizeof(buffer), &length),
3757 PSA_ERROR_BAD_STATE);
3758 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003759
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003761
Jaeden Ameroab439972019-02-15 14:12:05 +00003762exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 psa_cipher_abort(&operation);
3764 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003765}
3766/* END_CASE */
3767
3768/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003769void cipher_encrypt_fail(int alg_arg,
3770 int key_type_arg,
3771 data_t *key_data,
3772 data_t *input,
3773 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003774{
Ronald Cron5425a212020-08-04 14:58:35 +02003775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003776 psa_status_t status;
3777 psa_key_type_t key_type = key_type_arg;
3778 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003779 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003781 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3782 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003783 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003784 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003785 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003786 size_t function_output_length;
3787 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 if (PSA_ERROR_BAD_STATE != expected_status) {
3791 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003792
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3794 psa_set_key_algorithm(&attributes, alg);
3795 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003796
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3798 input->len);
3799 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003800
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3802 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003803 }
3804
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003805 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003806 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3807 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003808
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003810
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003811 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 status = psa_cipher_encrypt_setup(&operation, key, alg);
3813 if (status == PSA_SUCCESS) {
3814 if (alg != PSA_ALG_ECB_NO_PADDING) {
3815 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3816 iv, iv_size,
3817 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003818 }
3819
Gilles Peskine449bd832023-01-11 14:50:10 +01003820 status = psa_cipher_update(&operation, input->x, input->len,
3821 output, output_buffer_size,
3822 &function_output_length);
3823 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003824 output_length += function_output_length;
3825
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 status = psa_cipher_finish(&operation, output + output_length,
3827 output_buffer_size - output_length,
3828 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 TEST_EQUAL(status, expected_status);
3831 } else {
3832 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 } else {
3835 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003836 }
3837
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003838exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 psa_cipher_abort(&operation);
3840 mbedtls_free(output);
3841 psa_destroy_key(key);
3842 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003843}
3844/* END_CASE */
3845
3846/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003847void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3848 data_t *input, int iv_length,
3849 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003850{
3851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3852 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3854 size_t output_buffer_size = 0;
3855 unsigned char *output = NULL;
3856
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3858 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003859
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003861
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3863 psa_set_key_algorithm(&attributes, alg);
3864 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003865
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3867 &key));
3868 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3869 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3870 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003871
3872exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 psa_cipher_abort(&operation);
3874 mbedtls_free(output);
3875 psa_destroy_key(key);
3876 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003877}
3878/* END_CASE */
3879
3880/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003881void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3882 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003883{
3884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3885 psa_key_type_t key_type = key_type_arg;
3886 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003887 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3888 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003889 unsigned char *output = NULL;
3890 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003891 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3893
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003895
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003896 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 TEST_LE_U(ciphertext->len,
3898 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3899 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3900 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3901 TEST_LE_U(plaintext->len,
3902 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3903 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3904 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003905
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003906
3907 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 psa_set_key_usage_flags(&attributes,
3909 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3910 psa_set_key_algorithm(&attributes, alg);
3911 psa_set_key_type(&attributes, key_type);
3912 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3913 &key));
3914 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3915 plaintext->len);
3916 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003917
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003918 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3920 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3921 PSA_ERROR_BAD_STATE);
3922 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3923 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3924 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003925
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003926 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3928 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3929 &length),
3930 PSA_ERROR_BAD_STATE);
3931 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3932 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3933 &length),
3934 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003935
Gilles Peskine286c3142022-04-20 17:09:38 +02003936 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003938 output_length = 0;
3939 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 PSA_ASSERT(psa_cipher_update(&operation,
3941 plaintext->x, plaintext->len,
3942 output, output_buffer_size,
3943 &length));
3944 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003945 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 PSA_ASSERT(psa_cipher_finish(&operation,
3947 mbedtls_buffer_offset(output, output_length),
3948 output_buffer_size - output_length,
3949 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003950 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3952 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003953
Gilles Peskine286c3142022-04-20 17:09:38 +02003954 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003956 output_length = 0;
3957 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_cipher_update(&operation,
3959 ciphertext->x, ciphertext->len,
3960 output, output_buffer_size,
3961 &length));
3962 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003963 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 PSA_ASSERT(psa_cipher_finish(&operation,
3965 mbedtls_buffer_offset(output, output_length),
3966 output_buffer_size - output_length,
3967 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 ASSERT_COMPARE(plaintext->x, plaintext->len,
3970 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003971
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003972 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003973 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3975 output, output_buffer_size,
3976 &output_length));
3977 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3978 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003979
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003980 /* One-shot decryption */
3981 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3983 output, output_buffer_size,
3984 &output_length));
3985 ASSERT_COMPARE(plaintext->x, plaintext->len,
3986 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003987
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 PSA_ASSERT(psa_cipher_abort(&operation));
3990 mbedtls_free(output);
3991 psa_cipher_abort(&operation);
3992 psa_destroy_key(key);
3993 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003994}
3995/* END_CASE */
3996
3997/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003998void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003999{
4000 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4001 psa_algorithm_t alg = alg_arg;
4002 psa_key_type_t key_type = key_type_arg;
4003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4004 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4005 psa_status_t status;
4006
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4010 psa_set_key_algorithm(&attributes, alg);
4011 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004012
4013 /* Usage of either of these two size macros would cause divide by zero
4014 * with incorrect key types previously. Input length should be irrelevant
4015 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4017 0);
4018 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004019
4020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4022 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004023
4024 /* Should fail due to invalid alg type (to support invalid key type).
4025 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004029
4030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 psa_cipher_abort(&operation);
4032 psa_destroy_key(key);
4033 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004034}
4035/* END_CASE */
4036
4037/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004038void cipher_encrypt_validation(int alg_arg,
4039 int key_type_arg,
4040 data_t *key_data,
4041 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004042{
4043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4044 psa_key_type_t key_type = key_type_arg;
4045 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004047 unsigned char *output1 = NULL;
4048 size_t output1_buffer_size = 0;
4049 size_t output1_length = 0;
4050 unsigned char *output2 = NULL;
4051 size_t output2_buffer_size = 0;
4052 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004053 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004054 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4060 psa_set_key_algorithm(&attributes, alg);
4061 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4064 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4065 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4066 ASSERT_ALLOC(output1, output1_buffer_size);
4067 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4070 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004071
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004072 /* The one-shot cipher encryption uses generated iv so validating
4073 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4075 output1_buffer_size, &output1_length));
4076 TEST_LE_U(output1_length,
4077 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4078 TEST_LE_U(output1_length,
4079 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4082 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 PSA_ASSERT(psa_cipher_update(&operation,
4085 input->x, input->len,
4086 output2, output2_buffer_size,
4087 &function_output_length));
4088 TEST_LE_U(function_output_length,
4089 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4090 TEST_LE_U(function_output_length,
4091 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004092 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 PSA_ASSERT(psa_cipher_finish(&operation,
4095 output2 + output2_length,
4096 output2_buffer_size - output2_length,
4097 &function_output_length));
4098 TEST_LE_U(function_output_length,
4099 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4100 TEST_LE_U(function_output_length,
4101 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004102 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 PSA_ASSERT(psa_cipher_abort(&operation));
4105 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4106 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004107
Gilles Peskine50e586b2018-06-08 14:28:46 +02004108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 psa_cipher_abort(&operation);
4110 mbedtls_free(output1);
4111 mbedtls_free(output2);
4112 psa_destroy_key(key);
4113 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004114}
4115/* END_CASE */
4116
4117/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004118void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4119 data_t *key_data, data_t *iv,
4120 data_t *input,
4121 int first_part_size_arg,
4122 int output1_length_arg, int output2_length_arg,
4123 data_t *expected_output,
4124 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004125{
Ronald Cron5425a212020-08-04 14:58:35 +02004126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004127 psa_key_type_t key_type = key_type_arg;
4128 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004129 psa_status_t status;
4130 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004131 size_t first_part_size = first_part_size_arg;
4132 size_t output1_length = output1_length_arg;
4133 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004134 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004135 size_t output_buffer_size = 0;
4136 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004137 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004138 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4144 psa_set_key_algorithm(&attributes, alg);
4145 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4148 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (iv->len > 0) {
4153 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004154 }
4155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4157 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4158 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 TEST_LE_U(first_part_size, input->len);
4161 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4162 output, output_buffer_size,
4163 &function_output_length));
4164 TEST_ASSERT(function_output_length == output1_length);
4165 TEST_LE_U(function_output_length,
4166 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4167 TEST_LE_U(function_output_length,
4168 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004169 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 if (first_part_size < input->len) {
4172 PSA_ASSERT(psa_cipher_update(&operation,
4173 input->x + first_part_size,
4174 input->len - first_part_size,
4175 (output_buffer_size == 0 ? NULL :
4176 output + total_output_length),
4177 output_buffer_size - total_output_length,
4178 &function_output_length));
4179 TEST_ASSERT(function_output_length == output2_length);
4180 TEST_LE_U(function_output_length,
4181 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4182 alg,
4183 input->len - first_part_size));
4184 TEST_LE_U(function_output_length,
4185 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004186 total_output_length += function_output_length;
4187 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 status = psa_cipher_finish(&operation,
4190 (output_buffer_size == 0 ? NULL :
4191 output + total_output_length),
4192 output_buffer_size - total_output_length,
4193 &function_output_length);
4194 TEST_LE_U(function_output_length,
4195 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4196 TEST_LE_U(function_output_length,
4197 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004198 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 if (expected_status == PSA_SUCCESS) {
4202 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 ASSERT_COMPARE(expected_output->x, expected_output->len,
4205 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004206 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004207
4208exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 psa_cipher_abort(&operation);
4210 mbedtls_free(output);
4211 psa_destroy_key(key);
4212 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004213}
4214/* END_CASE */
4215
4216/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004217void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4218 data_t *key_data, data_t *iv,
4219 data_t *input,
4220 int first_part_size_arg,
4221 int output1_length_arg, int output2_length_arg,
4222 data_t *expected_output,
4223 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004224{
Ronald Cron5425a212020-08-04 14:58:35 +02004225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004226 psa_key_type_t key_type = key_type_arg;
4227 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004228 psa_status_t status;
4229 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004230 size_t first_part_size = first_part_size_arg;
4231 size_t output1_length = output1_length_arg;
4232 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004233 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234 size_t output_buffer_size = 0;
4235 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004236 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004237 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4243 psa_set_key_algorithm(&attributes, alg);
4244 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4247 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 if (iv->len > 0) {
4252 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004253 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4256 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4257 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 TEST_LE_U(first_part_size, input->len);
4260 PSA_ASSERT(psa_cipher_update(&operation,
4261 input->x, first_part_size,
4262 output, output_buffer_size,
4263 &function_output_length));
4264 TEST_ASSERT(function_output_length == output1_length);
4265 TEST_LE_U(function_output_length,
4266 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4267 TEST_LE_U(function_output_length,
4268 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004269 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 if (first_part_size < input->len) {
4272 PSA_ASSERT(psa_cipher_update(&operation,
4273 input->x + first_part_size,
4274 input->len - first_part_size,
4275 (output_buffer_size == 0 ? NULL :
4276 output + total_output_length),
4277 output_buffer_size - total_output_length,
4278 &function_output_length));
4279 TEST_ASSERT(function_output_length == output2_length);
4280 TEST_LE_U(function_output_length,
4281 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4282 alg,
4283 input->len - first_part_size));
4284 TEST_LE_U(function_output_length,
4285 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004286 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004287 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004288
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 status = psa_cipher_finish(&operation,
4290 (output_buffer_size == 0 ? NULL :
4291 output + total_output_length),
4292 output_buffer_size - total_output_length,
4293 &function_output_length);
4294 TEST_LE_U(function_output_length,
4295 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4296 TEST_LE_U(function_output_length,
4297 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004298 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 if (expected_status == PSA_SUCCESS) {
4302 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 ASSERT_COMPARE(expected_output->x, expected_output->len,
4305 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004306 }
4307
Gilles Peskine50e586b2018-06-08 14:28:46 +02004308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 psa_cipher_abort(&operation);
4310 mbedtls_free(output);
4311 psa_destroy_key(key);
4312 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004313}
4314/* END_CASE */
4315
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004317void cipher_decrypt_fail(int alg_arg,
4318 int key_type_arg,
4319 data_t *key_data,
4320 data_t *iv,
4321 data_t *input_arg,
4322 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004323{
4324 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4325 psa_status_t status;
4326 psa_key_type_t key_type = key_type_arg;
4327 psa_algorithm_t alg = alg_arg;
4328 psa_status_t expected_status = expected_status_arg;
4329 unsigned char *input = NULL;
4330 size_t input_buffer_size = 0;
4331 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004332 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004333 size_t output_buffer_size = 0;
4334 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004335 size_t function_output_length;
4336 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (PSA_ERROR_BAD_STATE != expected_status) {
4340 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4343 psa_set_key_algorithm(&attributes, alg);
4344 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4347 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004348 }
4349
4350 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4352 if (input_buffer_size > 0) {
4353 ASSERT_ALLOC(input, input_buffer_size);
4354 memcpy(input, iv->x, iv->len);
4355 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004356 }
4357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4359 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004360
Neil Armstrong66a479f2022-02-07 15:41:19 +01004361 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4363 output_buffer_size, &output_length);
4364 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004365
Neil Armstrong66a479f2022-02-07 15:41:19 +01004366 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 status = psa_cipher_decrypt_setup(&operation, key, alg);
4368 if (status == PSA_SUCCESS) {
4369 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4370 input_arg->len) +
4371 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4372 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (iv->len > 0) {
4375 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004376
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 if (status != PSA_SUCCESS) {
4378 TEST_EQUAL(status, expected_status);
4379 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004380 }
4381
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (status == PSA_SUCCESS) {
4383 status = psa_cipher_update(&operation,
4384 input_arg->x, input_arg->len,
4385 output_multi, output_buffer_size,
4386 &function_output_length);
4387 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004388 output_length = function_output_length;
4389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 status = psa_cipher_finish(&operation,
4391 output_multi + output_length,
4392 output_buffer_size - output_length,
4393 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 TEST_EQUAL(status, expected_status);
4396 } else {
4397 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004398 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 } else {
4400 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 } else {
4403 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004404 }
4405
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004406exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 psa_cipher_abort(&operation);
4408 mbedtls_free(input);
4409 mbedtls_free(output);
4410 mbedtls_free(output_multi);
4411 psa_destroy_key(key);
4412 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004413}
4414/* END_CASE */
4415
4416/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004417void cipher_decrypt(int alg_arg,
4418 int key_type_arg,
4419 data_t *key_data,
4420 data_t *iv,
4421 data_t *input_arg,
4422 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004423{
4424 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4425 psa_key_type_t key_type = key_type_arg;
4426 psa_algorithm_t alg = alg_arg;
4427 unsigned char *input = NULL;
4428 size_t input_buffer_size = 0;
4429 unsigned char *output = NULL;
4430 size_t output_buffer_size = 0;
4431 size_t output_length = 0;
4432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4433
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004435
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4437 psa_set_key_algorithm(&attributes, alg);
4438 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004439
4440 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4442 if (input_buffer_size > 0) {
4443 ASSERT_ALLOC(input, input_buffer_size);
4444 memcpy(input, iv->x, iv->len);
4445 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446 }
4447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4449 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4452 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4455 output_buffer_size, &output_length));
4456 TEST_LE_U(output_length,
4457 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4458 TEST_LE_U(output_length,
4459 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 ASSERT_COMPARE(expected_output->x, expected_output->len,
4462 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004463exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 mbedtls_free(input);
4465 mbedtls_free(output);
4466 psa_destroy_key(key);
4467 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004468}
4469/* END_CASE */
4470
4471/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004472void cipher_verify_output(int alg_arg,
4473 int key_type_arg,
4474 data_t *key_data,
4475 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004476{
Ronald Cron5425a212020-08-04 14:58:35 +02004477 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004478 psa_key_type_t key_type = key_type_arg;
4479 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004480 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004481 size_t output1_size = 0;
4482 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004483 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004484 size_t output2_size = 0;
4485 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4491 psa_set_key_algorithm(&attributes, alg);
4492 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4495 &key));
4496 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4497 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4500 output1, output1_size,
4501 &output1_length));
4502 TEST_LE_U(output1_length,
4503 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4504 TEST_LE_U(output1_length,
4505 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004506
4507 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4511 output2, output2_size,
4512 &output2_length));
4513 TEST_LE_U(output2_length,
4514 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4515 TEST_LE_U(output2_length,
4516 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004519
4520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 mbedtls_free(output1);
4522 mbedtls_free(output2);
4523 psa_destroy_key(key);
4524 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004525}
4526/* END_CASE */
4527
4528/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004529void cipher_verify_output_multipart(int alg_arg,
4530 int key_type_arg,
4531 data_t *key_data,
4532 data_t *input,
4533 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004534{
Ronald Cron5425a212020-08-04 14:58:35 +02004535 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004536 psa_key_type_t key_type = key_type_arg;
4537 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004538 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004540 size_t iv_size = 16;
4541 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004542 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004543 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004544 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004545 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004546 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004547 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004548 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004549 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4550 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4556 psa_set_key_algorithm(&attributes, alg);
4557 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4560 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4563 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 if (alg != PSA_ALG_ECB_NO_PADDING) {
4566 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4567 iv, iv_size,
4568 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004569 }
4570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4572 TEST_LE_U(output1_buffer_size,
4573 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4574 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4579 output1, output1_buffer_size,
4580 &function_output_length));
4581 TEST_LE_U(function_output_length,
4582 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4583 TEST_LE_U(function_output_length,
4584 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004585 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 PSA_ASSERT(psa_cipher_update(&operation1,
4588 input->x + first_part_size,
4589 input->len - first_part_size,
4590 output1, output1_buffer_size,
4591 &function_output_length));
4592 TEST_LE_U(function_output_length,
4593 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4594 alg,
4595 input->len - first_part_size));
4596 TEST_LE_U(function_output_length,
4597 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004598 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 PSA_ASSERT(psa_cipher_finish(&operation1,
4601 output1 + output1_length,
4602 output1_buffer_size - output1_length,
4603 &function_output_length));
4604 TEST_LE_U(function_output_length,
4605 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4606 TEST_LE_U(function_output_length,
4607 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004608 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004611
Gilles Peskine048b7f02018-06-08 14:20:49 +02004612 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 TEST_LE_U(output2_buffer_size,
4614 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4615 TEST_LE_U(output2_buffer_size,
4616 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4617 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 if (iv_length > 0) {
4620 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4621 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004622 }
Moran Pekerded84402018-06-06 16:36:50 +03004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4625 output2, output2_buffer_size,
4626 &function_output_length));
4627 TEST_LE_U(function_output_length,
4628 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4629 TEST_LE_U(function_output_length,
4630 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004631 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 PSA_ASSERT(psa_cipher_update(&operation2,
4634 output1 + first_part_size,
4635 output1_length - first_part_size,
4636 output2, output2_buffer_size,
4637 &function_output_length));
4638 TEST_LE_U(function_output_length,
4639 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4640 alg,
4641 output1_length - first_part_size));
4642 TEST_LE_U(function_output_length,
4643 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004644 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004645
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 PSA_ASSERT(psa_cipher_finish(&operation2,
4647 output2 + output2_length,
4648 output2_buffer_size - output2_length,
4649 &function_output_length));
4650 TEST_LE_U(function_output_length,
4651 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4652 TEST_LE_U(function_output_length,
4653 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004654 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004659
4660exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 psa_cipher_abort(&operation1);
4662 psa_cipher_abort(&operation2);
4663 mbedtls_free(output1);
4664 mbedtls_free(output2);
4665 psa_destroy_key(key);
4666 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004667}
4668/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004669
Gilles Peskine20035e32018-02-03 22:44:14 +01004670/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004671void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4672 int alg_arg,
4673 data_t *nonce,
4674 data_t *additional_data,
4675 data_t *input_data,
4676 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004677{
Ronald Cron5425a212020-08-04 14:58:35 +02004678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004679 psa_key_type_t key_type = key_type_arg;
4680 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004681 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004682 unsigned char *output_data = NULL;
4683 size_t output_size = 0;
4684 size_t output_length = 0;
4685 unsigned char *output_data2 = NULL;
4686 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004687 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004688 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004689 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4694 psa_set_key_algorithm(&attributes, alg);
4695 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004696
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4698 &key));
4699 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4700 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4703 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004704 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4705 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4707 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4708 TEST_EQUAL(output_size,
4709 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4710 TEST_LE_U(output_size,
4711 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004712 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 status = psa_aead_encrypt(key, alg,
4716 nonce->x, nonce->len,
4717 additional_data->x,
4718 additional_data->len,
4719 input_data->x, input_data->len,
4720 output_data, output_size,
4721 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004722
4723 /* If the operation is not supported, just skip and not fail in case the
4724 * encryption involves a common limitation of cryptography hardwares and
4725 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (status == PSA_ERROR_NOT_SUPPORTED) {
4727 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4728 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004729 }
4730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if (PSA_SUCCESS == expected_result) {
4734 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004735
Gilles Peskine003a4a92019-05-14 16:09:40 +02004736 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4737 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 TEST_EQUAL(input_data->len,
4739 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 TEST_LE_U(input_data->len,
4742 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 TEST_EQUAL(psa_aead_decrypt(key, alg,
4745 nonce->x, nonce->len,
4746 additional_data->x,
4747 additional_data->len,
4748 output_data, output_length,
4749 output_data2, output_length,
4750 &output_length2),
4751 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 ASSERT_COMPARE(input_data->x, input_data->len,
4754 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004756
Gilles Peskinea1cac842018-06-11 19:33:02 +02004757exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 psa_destroy_key(key);
4759 mbedtls_free(output_data);
4760 mbedtls_free(output_data2);
4761 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004762}
4763/* END_CASE */
4764
4765/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004766void aead_encrypt(int key_type_arg, data_t *key_data,
4767 int alg_arg,
4768 data_t *nonce,
4769 data_t *additional_data,
4770 data_t *input_data,
4771 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004772{
Ronald Cron5425a212020-08-04 14:58:35 +02004773 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774 psa_key_type_t key_type = key_type_arg;
4775 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004776 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004777 unsigned char *output_data = NULL;
4778 size_t output_size = 0;
4779 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004781 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4786 psa_set_key_algorithm(&attributes, alg);
4787 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4790 &key));
4791 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4792 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4795 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004796 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4797 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 TEST_EQUAL(output_size,
4799 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4800 TEST_LE_U(output_size,
4801 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4802 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 status = psa_aead_encrypt(key, alg,
4805 nonce->x, nonce->len,
4806 additional_data->x, additional_data->len,
4807 input_data->x, input_data->len,
4808 output_data, output_size,
4809 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004810
Ronald Cron28a45ed2021-02-09 20:35:42 +01004811 /* If the operation is not supported, just skip and not fail in case the
4812 * encryption involves a common limitation of cryptography hardwares and
4813 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (status == PSA_ERROR_NOT_SUPPORTED) {
4815 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4816 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004817 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 PSA_ASSERT(status);
4820 ASSERT_COMPARE(expected_result->x, expected_result->len,
4821 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004822
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 psa_destroy_key(key);
4825 mbedtls_free(output_data);
4826 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004827}
4828/* END_CASE */
4829
4830/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004831void aead_decrypt(int key_type_arg, data_t *key_data,
4832 int alg_arg,
4833 data_t *nonce,
4834 data_t *additional_data,
4835 data_t *input_data,
4836 data_t *expected_data,
4837 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004838{
Ronald Cron5425a212020-08-04 14:58:35 +02004839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004840 psa_key_type_t key_type = key_type_arg;
4841 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004842 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843 unsigned char *output_data = NULL;
4844 size_t output_size = 0;
4845 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004847 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004848 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4853 psa_set_key_algorithm(&attributes, alg);
4854 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4857 &key));
4858 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4859 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4862 alg);
4863 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4864 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004865 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4866 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 TEST_EQUAL(output_size,
4868 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4869 TEST_LE_U(output_size,
4870 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004871 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 status = psa_aead_decrypt(key, alg,
4875 nonce->x, nonce->len,
4876 additional_data->x,
4877 additional_data->len,
4878 input_data->x, input_data->len,
4879 output_data, output_size,
4880 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004881
Ronald Cron28a45ed2021-02-09 20:35:42 +01004882 /* If the operation is not supported, just skip and not fail in case the
4883 * decryption involves a common limitation of cryptography hardwares and
4884 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (status == PSA_ERROR_NOT_SUPPORTED) {
4886 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4887 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004888 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 if (expected_result == PSA_SUCCESS) {
4893 ASSERT_COMPARE(expected_data->x, expected_data->len,
4894 output_data, output_length);
4895 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004896
Gilles Peskinea1cac842018-06-11 19:33:02 +02004897exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 psa_destroy_key(key);
4899 mbedtls_free(output_data);
4900 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004901}
4902/* END_CASE */
4903
4904/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004905void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4906 int alg_arg,
4907 data_t *nonce,
4908 data_t *additional_data,
4909 data_t *input_data,
4910 int do_set_lengths,
4911 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004912{
Paul Elliottd3f82412021-06-16 16:52:21 +01004913 size_t ad_part_len = 0;
4914 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004915 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4918 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004919
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 if (do_set_lengths) {
4921 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004922 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004924 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004926 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004927
4928 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 if (!aead_multipart_internal_func(key_type_arg, key_data,
4930 alg_arg, nonce,
4931 additional_data,
4932 ad_part_len,
4933 input_data, -1,
4934 set_lengths_method,
4935 expected_output,
4936 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004937 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004938 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 /* length(0) part, length(ad_part_len) part, length(0) part... */
4941 mbedtls_test_set_step(1000 + ad_part_len);
4942
4943 if (!aead_multipart_internal_func(key_type_arg, key_data,
4944 alg_arg, nonce,
4945 additional_data,
4946 ad_part_len,
4947 input_data, -1,
4948 set_lengths_method,
4949 expected_output,
4950 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004951 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 }
4953 }
4954
4955 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4956 /* Split data into length(data_part_len) parts. */
4957 mbedtls_test_set_step(2000 + data_part_len);
4958
4959 if (do_set_lengths) {
4960 if (data_part_len & 0x01) {
4961 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4962 } else {
4963 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4964 }
4965 }
4966
4967 if (!aead_multipart_internal_func(key_type_arg, key_data,
4968 alg_arg, nonce,
4969 additional_data, -1,
4970 input_data, data_part_len,
4971 set_lengths_method,
4972 expected_output,
4973 1, 0)) {
4974 break;
4975 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004976
4977 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 if (!aead_multipart_internal_func(key_type_arg, key_data,
4981 alg_arg, nonce,
4982 additional_data, -1,
4983 input_data, data_part_len,
4984 set_lengths_method,
4985 expected_output,
4986 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004987 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004989 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004990
Paul Elliott8fc45162021-06-23 16:06:01 +01004991 /* Goto is required to silence warnings about unused labels, as we
4992 * don't actually do any test assertions in this function. */
4993 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004994}
4995/* END_CASE */
4996
4997/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004998void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4999 int alg_arg,
5000 data_t *nonce,
5001 data_t *additional_data,
5002 data_t *input_data,
5003 int do_set_lengths,
5004 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005005{
Paul Elliottd3f82412021-06-16 16:52:21 +01005006 size_t ad_part_len = 0;
5007 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005008 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005011 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 if (do_set_lengths) {
5015 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005016 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005020 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 if (!aead_multipart_internal_func(key_type_arg, key_data,
5023 alg_arg, nonce,
5024 additional_data,
5025 ad_part_len,
5026 input_data, -1,
5027 set_lengths_method,
5028 expected_output,
5029 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005030 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032
5033 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (!aead_multipart_internal_func(key_type_arg, key_data,
5037 alg_arg, nonce,
5038 additional_data,
5039 ad_part_len,
5040 input_data, -1,
5041 set_lengths_method,
5042 expected_output,
5043 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005046 }
5047
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (do_set_lengths) {
5053 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005054 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005058 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (!aead_multipart_internal_func(key_type_arg, key_data,
5061 alg_arg, nonce,
5062 additional_data, -1,
5063 input_data, data_part_len,
5064 set_lengths_method,
5065 expected_output,
5066 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005067 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005069
5070 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005072
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 if (!aead_multipart_internal_func(key_type_arg, key_data,
5074 alg_arg, nonce,
5075 additional_data, -1,
5076 input_data, data_part_len,
5077 set_lengths_method,
5078 expected_output,
5079 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005080 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005082 }
5083
Paul Elliott8fc45162021-06-23 16:06:01 +01005084 /* Goto is required to silence warnings about unused labels, as we
5085 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005086 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005087}
5088/* END_CASE */
5089
5090/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5092 int alg_arg,
5093 int nonce_length,
5094 int expected_nonce_length_arg,
5095 data_t *additional_data,
5096 data_t *input_data,
5097 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005098{
5099
5100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5101 psa_key_type_t key_type = key_type_arg;
5102 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005103 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005104 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5106 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005107 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005108 size_t actual_nonce_length = 0;
5109 size_t expected_nonce_length = expected_nonce_length_arg;
5110 unsigned char *output = NULL;
5111 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005112 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005113 size_t ciphertext_size = 0;
5114 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005115 size_t tag_length = 0;
5116 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5121 psa_set_key_algorithm(&attributes, alg);
5122 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005123
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5125 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140
5141 /* If the operation is not supported, just skip and not fail in case the
5142 * encryption involves a common limitation of cryptography hardwares and
5143 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (status == PSA_ERROR_NOT_SUPPORTED) {
5145 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5146 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005147 }
5148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5152 nonce_length,
5153 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (expected_status == PSA_SUCCESS) {
5160 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5161 alg));
5162 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005165
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005167 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5169 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5172 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5175 output, output_size,
5176 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5179 &ciphertext_length, tag_buffer,
5180 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005181 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005182
5183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 psa_destroy_key(key);
5185 mbedtls_free(output);
5186 mbedtls_free(ciphertext);
5187 psa_aead_abort(&operation);
5188 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005189}
5190/* END_CASE */
5191
5192/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005193void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5194 int alg_arg,
5195 int nonce_length_arg,
5196 int set_lengths_method_arg,
5197 data_t *additional_data,
5198 data_t *input_data,
5199 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005200{
5201
5202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5203 psa_key_type_t key_type = key_type_arg;
5204 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005205 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005206 uint8_t *nonce_buffer = NULL;
5207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5208 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5209 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005210 unsigned char *output = NULL;
5211 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005212 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005213 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005214 size_t ciphertext_size = 0;
5215 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005216 size_t tag_length = 0;
5217 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005218 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005219 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5224 psa_set_key_algorithm(&attributes, alg);
5225 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5228 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005239
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005243
5244 /* If the operation is not supported, just skip and not fail in case the
5245 * encryption involves a common limitation of cryptography hardwares and
5246 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 if (status == PSA_ERROR_NOT_SUPPORTED) {
5248 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5249 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005250 }
5251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Paul Elliott4023ffd2021-09-10 16:21:22 +01005254 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 if (nonce_length_arg == -1) {
5256 /* Arbitrary size buffer, to test zero length valid buffer. */
5257 ASSERT_ALLOC(nonce_buffer, 4);
5258 nonce_length = 0;
5259 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005260 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 nonce_length = (size_t) nonce_length_arg;
5262 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (nonce_buffer) {
5265 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005266 nonce_buffer[index] = 'a' + index;
5267 }
Paul Elliott66696b52021-08-16 18:42:41 +01005268 }
Paul Elliott863864a2021-07-23 17:28:31 +01005269 }
5270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5272 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5273 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005274 }
5275
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005279
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 if (expected_status == PSA_SUCCESS) {
5281 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5282 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5283 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005284 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005286 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 }
Paul Elliott863864a2021-07-23 17:28:31 +01005288
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005289 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5291 additional_data->len),
5292 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5295 output, output_size,
5296 &ciphertext_length),
5297 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5300 &ciphertext_length, tag_buffer,
5301 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5302 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005303 }
5304
5305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 psa_destroy_key(key);
5307 mbedtls_free(output);
5308 mbedtls_free(ciphertext);
5309 mbedtls_free(nonce_buffer);
5310 psa_aead_abort(&operation);
5311 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005312}
5313/* END_CASE */
5314
5315/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005316void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005317 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005318 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005319 data_t *nonce,
5320 data_t *additional_data,
5321 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005323{
5324
5325 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5326 psa_key_type_t key_type = key_type_arg;
5327 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005328 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5330 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5331 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005332 unsigned char *output = NULL;
5333 unsigned char *ciphertext = NULL;
5334 size_t output_size = output_size_arg;
5335 size_t ciphertext_size = 0;
5336 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005337 size_t tag_length = 0;
5338 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5343 psa_set_key_algorithm(&attributes, alg);
5344 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5347 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005358
5359 /* If the operation is not supported, just skip and not fail in case the
5360 * encryption involves a common limitation of cryptography hardwares and
5361 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 if (status == PSA_ERROR_NOT_SUPPORTED) {
5363 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5364 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005365 }
5366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5370 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5375 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 status = psa_aead_update(&operation, input_data->x, input_data->len,
5378 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005383 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5385 &ciphertext_length, tag_buffer,
5386 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005387 }
5388
5389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 psa_destroy_key(key);
5391 mbedtls_free(output);
5392 mbedtls_free(ciphertext);
5393 psa_aead_abort(&operation);
5394 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005395}
5396/* END_CASE */
5397
Paul Elliott91b021e2021-07-23 18:52:31 +01005398/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005399void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5400 int alg_arg,
5401 int finish_ciphertext_size_arg,
5402 int tag_size_arg,
5403 data_t *nonce,
5404 data_t *additional_data,
5405 data_t *input_data,
5406 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005407{
5408
5409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5410 psa_key_type_t key_type = key_type_arg;
5411 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005412 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5414 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5415 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005416 unsigned char *ciphertext = NULL;
5417 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005418 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005419 size_t ciphertext_size = 0;
5420 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5422 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005423 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5428 psa_set_key_algorithm(&attributes, alg);
5429 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5432 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005445
5446 /* If the operation is not supported, just skip and not fail in case the
5447 * encryption involves a common limitation of cryptography hardwares and
5448 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 if (status == PSA_ERROR_NOT_SUPPORTED) {
5450 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005452 }
5453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5459 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5462 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5465 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005466
5467 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 status = psa_aead_finish(&operation, finish_ciphertext,
5469 finish_ciphertext_size,
5470 &ciphertext_length, tag_buffer,
5471 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005474
5475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 psa_destroy_key(key);
5477 mbedtls_free(ciphertext);
5478 mbedtls_free(finish_ciphertext);
5479 mbedtls_free(tag_buffer);
5480 psa_aead_abort(&operation);
5481 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005482}
5483/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005484
5485/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486void aead_multipart_verify(int key_type_arg, data_t *key_data,
5487 int alg_arg,
5488 data_t *nonce,
5489 data_t *additional_data,
5490 data_t *input_data,
5491 data_t *tag,
5492 int tag_usage_arg,
5493 int expected_setup_status_arg,
5494 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005495{
5496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5497 psa_key_type_t key_type = key_type_arg;
5498 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005499 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5501 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5502 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005503 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005504 unsigned char *plaintext = NULL;
5505 unsigned char *finish_plaintext = NULL;
5506 size_t plaintext_size = 0;
5507 size_t plaintext_length = 0;
5508 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005509 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005510 unsigned char *tag_buffer = NULL;
5511 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5516 psa_set_key_algorithm(&attributes, alg);
5517 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005518
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5520 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5525 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005534
5535 /* If the operation is not supported, just skip and not fail in case the
5536 * encryption involves a common limitation of cryptography hardwares and
5537 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (status == PSA_ERROR_NOT_SUPPORTED) {
5539 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5540 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005541 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005545 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 }
Paul Elliott9961a662021-09-17 19:19:02 +01005547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 status = psa_aead_set_lengths(&operation, additional_data->len,
5553 input_data->len);
5554 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5557 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5560 input_data->len,
5561 plaintext, plaintext_size,
5562 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005565 tag_buffer = tag->x;
5566 tag_size = tag->len;
5567 }
5568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 status = psa_aead_verify(&operation, finish_plaintext,
5570 verify_plaintext_size,
5571 &plaintext_length,
5572 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005575
5576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 psa_destroy_key(key);
5578 mbedtls_free(plaintext);
5579 mbedtls_free(finish_plaintext);
5580 psa_aead_abort(&operation);
5581 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005582}
5583/* END_CASE */
5584
Paul Elliott9961a662021-09-17 19:19:02 +01005585/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005586void aead_multipart_setup(int key_type_arg, data_t *key_data,
5587 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005588{
5589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5590 psa_key_type_t key_type = key_type_arg;
5591 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005592 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5594 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5595 psa_status_t expected_status = expected_status_arg;
5596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 psa_set_key_usage_flags(&attributes,
5600 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5601 psa_set_key_algorithm(&attributes, alg);
5602 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5605 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005608
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005616
5617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 psa_destroy_key(key);
5619 psa_aead_abort(&operation);
5620 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005621}
5622/* END_CASE */
5623
5624/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005625void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5626 int alg_arg,
5627 data_t *nonce,
5628 data_t *additional_data,
5629 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005630{
5631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5632 psa_key_type_t key_type = key_type_arg;
5633 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005634 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005635 unsigned char *output_data = NULL;
5636 unsigned char *final_data = NULL;
5637 size_t output_size = 0;
5638 size_t finish_output_size = 0;
5639 size_t output_length = 0;
5640 size_t key_bits = 0;
5641 size_t tag_length = 0;
5642 size_t tag_size = 0;
5643 size_t nonce_length = 0;
5644 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5645 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5646 size_t output_part_length = 0;
5647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 psa_set_key_usage_flags(&attributes,
5652 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5653 psa_set_key_algorithm(&attributes, alg);
5654 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005655
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5657 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5660 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005675
5676 /* Test all operations error without calling setup first. */
5677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5679 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5684 PSA_AEAD_NONCE_MAX_SIZE,
5685 &nonce_length),
5686 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
Paul Elliott481be342021-07-16 17:38:47 +01005690 /* ------------------------------------------------------- */
5691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5693 input_data->len),
5694 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
Paul Elliott481be342021-07-16 17:38:47 +01005698 /* ------------------------------------------------------- */
5699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5701 additional_data->len),
5702 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Paul Elliott481be342021-07-16 17:38:47 +01005706 /* ------------------------------------------------------- */
5707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5709 input_data->len, output_data,
5710 output_size, &output_length),
5711 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005714
Paul Elliott481be342021-07-16 17:38:47 +01005715 /* ------------------------------------------------------- */
5716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5718 finish_output_size,
5719 &output_part_length,
5720 tag_buffer, tag_length,
5721 &tag_size),
5722 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725
Paul Elliott481be342021-07-16 17:38:47 +01005726 /* ------------------------------------------------------- */
5727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5729 finish_output_size,
5730 &output_part_length,
5731 tag_buffer,
5732 tag_length),
5733 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005736
5737 /* Test for double setups. */
5738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5742 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005745
Paul Elliott481be342021-07-16 17:38:47 +01005746 /* ------------------------------------------------------- */
5747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5751 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005754
Paul Elliott374a2be2021-07-16 17:53:40 +01005755 /* ------------------------------------------------------- */
5756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5760 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005763
5764 /* ------------------------------------------------------- */
5765
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5769 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005772
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773 /* Test for not setting a nonce. */
5774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5778 additional_data->len),
5779 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005782
Paul Elliott7f628422021-09-01 12:08:29 +01005783 /* ------------------------------------------------------- */
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5788 input_data->len, output_data,
5789 output_size, &output_length),
5790 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005793
Paul Elliottbdc2c682021-09-21 18:37:10 +01005794 /* ------------------------------------------------------- */
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5799 finish_output_size,
5800 &output_part_length,
5801 tag_buffer, tag_length,
5802 &tag_size),
5803 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005806
5807 /* ------------------------------------------------------- */
5808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5812 finish_output_size,
5813 &output_part_length,
5814 tag_buffer,
5815 tag_length),
5816 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005819
Paul Elliottc23a9a02021-06-21 18:32:46 +01005820 /* Test for double setting nonce. */
5821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5827 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005830
Paul Elliott374a2be2021-07-16 17:53:40 +01005831 /* Test for double generating nonce. */
5832
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5836 PSA_AEAD_NONCE_MAX_SIZE,
5837 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5840 PSA_AEAD_NONCE_MAX_SIZE,
5841 &nonce_length),
5842 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005843
5844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005846
5847 /* Test for generate nonce then set and vice versa */
5848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5852 PSA_AEAD_NONCE_MAX_SIZE,
5853 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5856 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005859
Andrzej Kurekad837522021-12-15 15:28:49 +01005860 /* Test for generating nonce after calling set lengths */
5861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5865 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5868 PSA_AEAD_NONCE_MAX_SIZE,
5869 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005872
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005873 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (operation.alg == PSA_ALG_CCM) {
5878 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5879 input_data->len),
5880 PSA_ERROR_INVALID_ARGUMENT);
5881 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5882 PSA_AEAD_NONCE_MAX_SIZE,
5883 &nonce_length),
5884 PSA_ERROR_BAD_STATE);
5885 } else {
5886 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5887 input_data->len));
5888 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5889 PSA_AEAD_NONCE_MAX_SIZE,
5890 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005891 }
5892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005894
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005895 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005896#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5900 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5901 input_data->len),
5902 PSA_ERROR_INVALID_ARGUMENT);
5903 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5904 PSA_AEAD_NONCE_MAX_SIZE,
5905 &nonce_length),
5906 PSA_ERROR_BAD_STATE);
5907 } else {
5908 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5909 input_data->len));
5910 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5911 PSA_AEAD_NONCE_MAX_SIZE,
5912 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005913 }
5914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005916#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005917
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005918 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5923 PSA_AEAD_NONCE_MAX_SIZE,
5924 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 if (operation.alg == PSA_ALG_CCM) {
5927 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5928 input_data->len),
5929 PSA_ERROR_INVALID_ARGUMENT);
5930 } else {
5931 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5932 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005933 }
5934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005936
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005937 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005938 /* Test for setting nonce after calling set lengths */
5939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5943 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005948
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005949 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005950
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (operation.alg == PSA_ALG_CCM) {
5954 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5955 input_data->len),
5956 PSA_ERROR_INVALID_ARGUMENT);
5957 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5958 PSA_ERROR_BAD_STATE);
5959 } else {
5960 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5961 input_data->len));
5962 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005963 }
5964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005966
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005967 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005968#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5972 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5973 input_data->len),
5974 PSA_ERROR_INVALID_ARGUMENT);
5975 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5976 PSA_ERROR_BAD_STATE);
5977 } else {
5978 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5979 input_data->len));
5980 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005981 }
5982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005984#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005985
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005986 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 if (operation.alg == PSA_ALG_CCM) {
5993 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5994 input_data->len),
5995 PSA_ERROR_INVALID_ARGUMENT);
5996 } else {
5997 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5998 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999 }
6000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006002
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006003 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006004#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006006
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 if (operation.alg == PSA_ALG_GCM) {
6008 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6009 SIZE_MAX),
6010 PSA_ERROR_INVALID_ARGUMENT);
6011 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6012 PSA_ERROR_BAD_STATE);
6013 } else if (operation.alg != PSA_ALG_CCM) {
6014 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6015 SIZE_MAX));
6016 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006017 }
6018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006020#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006021
Tom Cosgrove1797b052022-12-04 17:19:59 +00006022 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006023#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (operation.alg == PSA_ALG_GCM) {
6029 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6030 SIZE_MAX),
6031 PSA_ERROR_INVALID_ARGUMENT);
6032 } else if (operation.alg != PSA_ALG_CCM) {
6033 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6034 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006035 }
6036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006038#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006039
6040 /* ------------------------------------------------------- */
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6047 PSA_AEAD_NONCE_MAX_SIZE,
6048 &nonce_length),
6049 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006052
Paul Elliott7220cae2021-06-22 17:25:57 +01006053 /* Test for generating nonce in decrypt setup. */
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6058 PSA_AEAD_NONCE_MAX_SIZE,
6059 &nonce_length),
6060 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006063
Paul Elliottc23a9a02021-06-21 18:32:46 +01006064 /* Test for setting lengths twice. */
6065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6071 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6074 input_data->len),
6075 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006078
Andrzej Kurekad837522021-12-15 15:28:49 +01006079 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6088 additional_data->len),
6089 PSA_ERROR_BAD_STATE);
6090 } else {
6091 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6092 additional_data->len));
6093
6094 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6095 input_data->len),
6096 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006097 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006099
6100 /* ------------------------------------------------------- */
6101
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 if (operation.alg == PSA_ALG_CCM) {
6107 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6108 input_data->len, output_data,
6109 output_size, &output_length),
6110 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 } else {
6113 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6114 input_data->len, output_data,
6115 output_size, &output_length));
6116
6117 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6118 input_data->len),
6119 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006122
6123 /* ------------------------------------------------------- */
6124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (operation.alg == PSA_ALG_CCM) {
6130 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6131 finish_output_size,
6132 &output_part_length,
6133 tag_buffer, tag_length,
6134 &tag_size));
6135 } else {
6136 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6137 finish_output_size,
6138 &output_part_length,
6139 tag_buffer, tag_length,
6140 &tag_size));
6141
6142 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6143 input_data->len),
6144 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006145 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006147
6148 /* Test for setting lengths after generating nonce + already starting data. */
6149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006151
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6153 PSA_AEAD_NONCE_MAX_SIZE,
6154 &nonce_length));
6155 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6158 additional_data->len),
6159 PSA_ERROR_BAD_STATE);
6160 } else {
6161 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6162 additional_data->len));
6163
6164 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6165 input_data->len),
6166 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006167 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006169
6170 /* ------------------------------------------------------- */
6171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6175 PSA_AEAD_NONCE_MAX_SIZE,
6176 &nonce_length));
6177 if (operation.alg == PSA_ALG_CCM) {
6178 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6179 input_data->len, output_data,
6180 output_size, &output_length),
6181 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 } else {
6184 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6185 input_data->len, output_data,
6186 output_size, &output_length));
6187
6188 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6189 input_data->len),
6190 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006191 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006193
6194 /* ------------------------------------------------------- */
6195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6199 PSA_AEAD_NONCE_MAX_SIZE,
6200 &nonce_length));
6201 if (operation.alg == PSA_ALG_CCM) {
6202 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6203 finish_output_size,
6204 &output_part_length,
6205 tag_buffer, tag_length,
6206 &tag_size));
6207 } else {
6208 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6209 finish_output_size,
6210 &output_part_length,
6211 tag_buffer, tag_length,
6212 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6215 input_data->len),
6216 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006217 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006219
Paul Elliott243080c2021-07-21 19:01:17 +01006220 /* Test for not sending any additional data or data after setting non zero
6221 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006222
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6228 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6231 finish_output_size,
6232 &output_part_length,
6233 tag_buffer, tag_length,
6234 &tag_size),
6235 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006238
Paul Elliott243080c2021-07-21 19:01:17 +01006239 /* Test for not sending any additional data or data after setting non-zero
6240 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6247 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6250 finish_output_size,
6251 &output_part_length,
6252 tag_buffer,
6253 tag_length),
6254 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Paul Elliott243080c2021-07-21 19:01:17 +01006258 /* Test for not sending any additional data after setting a non-zero length
6259 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6266 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6269 input_data->len, output_data,
6270 output_size, &output_length),
6271 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006274
Paul Elliottf94bd992021-09-19 18:15:59 +01006275 /* Test for not sending any data after setting a non-zero length for it.*/
6276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6282 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6285 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6288 finish_output_size,
6289 &output_part_length,
6290 tag_buffer, tag_length,
6291 &tag_size),
6292 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006295
Paul Elliottb0450fe2021-09-01 15:06:26 +01006296 /* Test for sending too much additional data after setting lengths. */
6297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006303
6304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6306 additional_data->len),
6307 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006310
Paul Elliotta2a09b02021-09-22 14:56:40 +01006311 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6318 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6321 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6324 1),
6325 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006328
Paul Elliottb0450fe2021-09-01 15:06:26 +01006329 /* Test for sending too much data after setting lengths. */
6330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6338 input_data->len, output_data,
6339 output_size, &output_length),
6340 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006343
Paul Elliotta2a09b02021-09-22 14:56:40 +01006344 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6351 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6354 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6357 input_data->len, output_data,
6358 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6361 1, output_data,
6362 output_size, &output_length),
6363 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006366
Paul Elliottc23a9a02021-06-21 18:32:46 +01006367 /* Test sending additional data after data. */
6368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (operation.alg != PSA_ALG_CCM) {
6374 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6375 input_data->len, output_data,
6376 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6379 additional_data->len),
6380 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006381 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006383
Paul Elliott534d0b42021-06-22 19:15:20 +01006384 /* Test calling finish on decryption. */
6385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6391 finish_output_size,
6392 &output_part_length,
6393 tag_buffer, tag_length,
6394 &tag_size),
6395 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006398
6399 /* Test calling verify on encryption. */
6400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6406 finish_output_size,
6407 &output_part_length,
6408 tag_buffer,
6409 tag_length),
6410 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006413
6414
Paul Elliottc23a9a02021-06-21 18:32:46 +01006415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 psa_destroy_key(key);
6417 psa_aead_abort(&operation);
6418 mbedtls_free(output_data);
6419 mbedtls_free(final_data);
6420 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006421}
6422/* END_CASE */
6423
6424/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006425void signature_size(int type_arg,
6426 int bits,
6427 int alg_arg,
6428 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006429{
6430 psa_key_type_t type = type_arg;
6431 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006435
Gilles Peskinee59236f2018-01-27 23:32:46 +01006436exit:
6437 ;
6438}
6439/* END_CASE */
6440
6441/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006442void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6443 int alg_arg, data_t *input_data,
6444 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006445{
Ronald Cron5425a212020-08-04 14:58:35 +02006446 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006447 psa_key_type_t key_type = key_type_arg;
6448 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006449 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006450 unsigned char *signature = NULL;
6451 size_t signature_size;
6452 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6458 psa_set_key_algorithm(&attributes, alg);
6459 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006460
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6462 &key));
6463 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6464 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006465
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006466 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006467 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6469 key_bits, alg);
6470 TEST_ASSERT(signature_size != 0);
6471 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6472 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006473
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006474 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 PSA_ASSERT(psa_sign_hash(key, alg,
6476 input_data->x, input_data->len,
6477 signature, signature_size,
6478 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006479 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 ASSERT_COMPARE(output_data->x, output_data->len,
6481 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006482
6483exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006484 /*
6485 * Key attributes may have been returned by psa_get_key_attributes()
6486 * thus reset them as required.
6487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 psa_destroy_key(key);
6491 mbedtls_free(signature);
6492 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006493}
6494/* END_CASE */
6495
Paul Elliott712d5122022-12-07 14:03:10 +00006496/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006497/**
6498 * sign_hash_interruptible() test intentions:
6499 *
6500 * Note: This test can currently only handle ECDSA.
6501 *
6502 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006503 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006504 *
6505 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6506 * expected for different max_ops values.
6507 *
6508 * 3. Test that the number of ops done prior to start and after abort is zero
6509 * and that each successful stage completes some ops (this is not mandated by
6510 * the PSA specification, but is currently the case).
6511 *
6512 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6513 * complete() calls does not alter the number of ops returned.
6514 */
Paul Elliott712d5122022-12-07 14:03:10 +00006515void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6516 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006517 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006518{
6519 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6520 psa_key_type_t key_type = key_type_arg;
6521 psa_algorithm_t alg = alg_arg;
6522 size_t key_bits;
6523 unsigned char *signature = NULL;
6524 size_t signature_size;
6525 size_t signature_length = 0xdeadbeef;
6526 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6527 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006528 uint32_t num_ops = 0;
6529 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006530 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006531 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006532 size_t min_completes = 0;
6533 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006534
Paul Elliott712d5122022-12-07 14:03:10 +00006535 psa_sign_hash_interruptible_operation_t operation =
6536 psa_sign_hash_interruptible_operation_init();
6537
6538 PSA_ASSERT(psa_crypto_init());
6539
6540 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6541 psa_set_key_algorithm(&attributes, alg);
6542 psa_set_key_type(&attributes, key_type);
6543
6544 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6545 &key));
6546 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6547 key_bits = psa_get_key_bits(&attributes);
6548
6549 /* Allocate a buffer which has the size advertised by the
6550 * library. */
6551 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6552 key_bits, alg);
6553 TEST_ASSERT(signature_size != 0);
6554 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6555 ASSERT_ALLOC(signature, signature_size);
6556
Paul Elliott0c683352022-12-16 19:16:56 +00006557 psa_interruptible_set_max_ops(max_ops);
6558
Paul Elliott6f600372023-02-06 18:41:05 +00006559 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6560 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006561
Paul Elliott712d5122022-12-07 14:03:10 +00006562 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6563 TEST_ASSERT(num_ops_prior == 0);
6564
6565 /* Start performing the signature. */
6566 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6567 input_data->x, input_data->len));
6568
6569 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6570 TEST_ASSERT(num_ops_prior == 0);
6571
6572 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006573 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006574 status = psa_sign_hash_complete(&operation, signature, signature_size,
6575 &signature_length);
6576
Paul Elliott0c683352022-12-16 19:16:56 +00006577 num_completes++;
6578
Paul Elliott712d5122022-12-07 14:03:10 +00006579 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6580 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006581 /* We are asserting here that every complete makes progress
6582 * (completes some ops), which is true of the internal
6583 * implementation and probably any implementation, however this is
6584 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006585 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006586
Paul Elliott712d5122022-12-07 14:03:10 +00006587 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006588
6589 /* Ensure calling get_num_ops() twice still returns the same
6590 * number of ops as previously reported. */
6591 num_ops = psa_sign_hash_get_num_ops(&operation);
6592
6593 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006594 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006595 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006596
6597 TEST_ASSERT(status == PSA_SUCCESS);
6598
Paul Elliott0c683352022-12-16 19:16:56 +00006599 TEST_LE_U(min_completes, num_completes);
6600 TEST_LE_U(num_completes, max_completes);
6601
Paul Elliott712d5122022-12-07 14:03:10 +00006602 /* Verify that the signature is what is expected. */
6603 ASSERT_COMPARE(output_data->x, output_data->len,
6604 signature, signature_length);
6605
6606 PSA_ASSERT(psa_sign_hash_abort(&operation));
6607
Paul Elliott59ad9452022-12-18 15:09:02 +00006608 num_ops = psa_sign_hash_get_num_ops(&operation);
6609 TEST_ASSERT(num_ops == 0);
6610
Paul Elliott712d5122022-12-07 14:03:10 +00006611exit:
6612
6613 /*
6614 * Key attributes may have been returned by psa_get_key_attributes()
6615 * thus reset them as required.
6616 */
6617 psa_reset_key_attributes(&attributes);
6618
6619 psa_destroy_key(key);
6620 mbedtls_free(signature);
6621 PSA_DONE();
6622}
6623/* END_CASE */
6624
Gilles Peskine20035e32018-02-03 22:44:14 +01006625/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006626void sign_hash_fail(int key_type_arg, data_t *key_data,
6627 int alg_arg, data_t *input_data,
6628 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006629{
Ronald Cron5425a212020-08-04 14:58:35 +02006630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006631 psa_key_type_t key_type = key_type_arg;
6632 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006633 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006634 psa_status_t actual_status;
6635 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006636 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006637 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6645 psa_set_key_algorithm(&attributes, alg);
6646 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6649 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 actual_status = psa_sign_hash(key, alg,
6652 input_data->x, input_data->len,
6653 signature, signature_size,
6654 &signature_length);
6655 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006656 /* The value of *signature_length is unspecified on error, but
6657 * whatever it is, it should be less than signature_size, so that
6658 * if the caller tries to read *signature_length bytes without
6659 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006661
6662exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 psa_reset_key_attributes(&attributes);
6664 psa_destroy_key(key);
6665 mbedtls_free(signature);
6666 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006667}
6668/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006669
Paul Elliott91007972022-12-16 12:21:24 +00006670/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006671/**
6672 * sign_hash_fail_interruptible() test intentions:
6673 *
6674 * Note: This test can currently only handle ECDSA.
6675 *
6676 * 1. Test that various failure cases for interruptible sign hash fail with the
6677 * correct error codes, and at the correct point (at start or during
6678 * complete).
6679 *
6680 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6681 * expected for different max_ops values.
6682 *
6683 * 3. Test that the number of ops done prior to start and after abort is zero
6684 * and that each successful stage completes some ops (this is not mandated by
6685 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006686 *
6687 * 4. Check that calling complete() when start() fails and complete()
6688 * after completion results in a BAD_STATE error.
6689 *
6690 * 5. Check that calling start() again after start fails results in a BAD_STATE
6691 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006692 */
Paul Elliott91007972022-12-16 12:21:24 +00006693void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6694 int alg_arg, data_t *input_data,
6695 int signature_size_arg,
6696 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006697 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006698 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006699{
6700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6701 psa_key_type_t key_type = key_type_arg;
6702 psa_algorithm_t alg = alg_arg;
6703 size_t signature_size = signature_size_arg;
6704 psa_status_t actual_status;
6705 psa_status_t expected_start_status = expected_start_status_arg;
6706 psa_status_t expected_complete_status = expected_complete_status_arg;
6707 unsigned char *signature = NULL;
6708 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006709 uint32_t num_ops = 0;
6710 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006711 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006712 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006713 size_t min_completes = 0;
6714 size_t max_completes = 0;
6715
Paul Elliott91007972022-12-16 12:21:24 +00006716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6717 psa_sign_hash_interruptible_operation_t operation =
6718 psa_sign_hash_interruptible_operation_init();
6719
6720 ASSERT_ALLOC(signature, signature_size);
6721
6722 PSA_ASSERT(psa_crypto_init());
6723
6724 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6725 psa_set_key_algorithm(&attributes, alg);
6726 psa_set_key_type(&attributes, key_type);
6727
6728 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6729 &key));
6730
Paul Elliott0c683352022-12-16 19:16:56 +00006731 psa_interruptible_set_max_ops(max_ops);
6732
Paul Elliott6f600372023-02-06 18:41:05 +00006733 interruptible_signverify_get_minmax_completes(max_ops,
6734 expected_complete_status,
6735 &min_completes,
6736 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006737
Paul Elliott91007972022-12-16 12:21:24 +00006738 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6739 TEST_ASSERT(num_ops_prior == 0);
6740
6741 /* Start performing the signature. */
6742 actual_status = psa_sign_hash_start(&operation, key, alg,
6743 input_data->x, input_data->len);
6744
6745 TEST_EQUAL(actual_status, expected_start_status);
6746
Paul Elliottc9774412023-02-06 15:14:07 +00006747 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006748 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006749 * start failed. */
6750 actual_status = psa_sign_hash_complete(&operation, signature,
6751 signature_size,
6752 &signature_length);
6753
6754 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6755
6756 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006757 actual_status = psa_sign_hash_start(&operation, key, alg,
6758 input_data->x, input_data->len);
6759
6760 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6761 }
6762
Paul Elliott91007972022-12-16 12:21:24 +00006763 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6764 TEST_ASSERT(num_ops_prior == 0);
6765
Paul Elliott91007972022-12-16 12:21:24 +00006766 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006767 do {
Paul Elliott91007972022-12-16 12:21:24 +00006768 actual_status = psa_sign_hash_complete(&operation, signature,
6769 signature_size,
6770 &signature_length);
6771
Paul Elliott0c683352022-12-16 19:16:56 +00006772 num_completes++;
6773
Paul Elliott334d7262023-01-20 17:29:41 +00006774 if (actual_status == PSA_SUCCESS ||
6775 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006776 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006777 /* We are asserting here that every complete makes progress
6778 * (completes some ops), which is true of the internal
6779 * implementation and probably any implementation, however this is
6780 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006781 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006782
Paul Elliott91007972022-12-16 12:21:24 +00006783 num_ops_prior = num_ops;
6784 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006785 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006786
Paul Elliottc9774412023-02-06 15:14:07 +00006787 TEST_EQUAL(actual_status, expected_complete_status);
6788
Paul Elliottefebad02023-02-15 16:56:45 +00006789 /* Check that another complete returns BAD_STATE. */
6790 actual_status = psa_sign_hash_complete(&operation, signature,
6791 signature_size,
6792 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006793
Paul Elliottefebad02023-02-15 16:56:45 +00006794 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006795
Paul Elliott91007972022-12-16 12:21:24 +00006796 PSA_ASSERT(psa_sign_hash_abort(&operation));
6797
Paul Elliott59ad9452022-12-18 15:09:02 +00006798 num_ops = psa_sign_hash_get_num_ops(&operation);
6799 TEST_ASSERT(num_ops == 0);
6800
Paul Elliott91007972022-12-16 12:21:24 +00006801 /* The value of *signature_length is unspecified on error, but
6802 * whatever it is, it should be less than signature_size, so that
6803 * if the caller tries to read *signature_length bytes without
6804 * checking the error code then they don't overflow a buffer. */
6805 TEST_LE_U(signature_length, signature_size);
6806
Paul Elliott0c683352022-12-16 19:16:56 +00006807 TEST_LE_U(min_completes, num_completes);
6808 TEST_LE_U(num_completes, max_completes);
6809
Paul Elliott91007972022-12-16 12:21:24 +00006810exit:
6811 psa_reset_key_attributes(&attributes);
6812 psa_destroy_key(key);
6813 mbedtls_free(signature);
6814 PSA_DONE();
6815}
6816/* END_CASE */
6817
mohammad16038cc1cee2018-03-28 01:21:33 +03006818/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006819void sign_verify_hash(int key_type_arg, data_t *key_data,
6820 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006821{
Ronald Cron5425a212020-08-04 14:58:35 +02006822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006823 psa_key_type_t key_type = key_type_arg;
6824 psa_algorithm_t alg = alg_arg;
6825 size_t key_bits;
6826 unsigned char *signature = NULL;
6827 size_t signature_size;
6828 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006829 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006832
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6834 psa_set_key_algorithm(&attributes, alg);
6835 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006836
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6838 &key));
6839 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6840 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006841
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006842 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006843 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6845 key_bits, alg);
6846 TEST_ASSERT(signature_size != 0);
6847 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6848 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006849
6850 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 PSA_ASSERT(psa_sign_hash(key, alg,
6852 input_data->x, input_data->len,
6853 signature, signature_size,
6854 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006855 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 TEST_LE_U(signature_length, signature_size);
6857 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006858
6859 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 PSA_ASSERT(psa_verify_hash(key, alg,
6861 input_data->x, input_data->len,
6862 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006863
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006865 /* Flip a bit in the input and verify that the signature is now
6866 * detected as invalid. Flip a bit at the beginning, not at the end,
6867 * because ECDSA may ignore the last few bits of the input. */
6868 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 TEST_EQUAL(psa_verify_hash(key, alg,
6870 input_data->x, input_data->len,
6871 signature, signature_length),
6872 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006873 }
6874
6875exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006876 /*
6877 * Key attributes may have been returned by psa_get_key_attributes()
6878 * thus reset them as required.
6879 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006881
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 psa_destroy_key(key);
6883 mbedtls_free(signature);
6884 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006885}
6886/* END_CASE */
6887
Paul Elliott712d5122022-12-07 14:03:10 +00006888/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006889/**
6890 * sign_verify_hash_interruptible() test intentions:
6891 *
6892 * Note: This test can currently only handle ECDSA.
6893 *
Paul Elliott8c092052023-03-06 17:49:14 +00006894 * 1. Test that we can sign an input hash with the given keypair and then
6895 * afterwards verify that signature. This is currently the only way to test
6896 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006897 *
6898 * 2. Test that after corrupting the hash, the verification detects an invalid
6899 * signature.
6900 *
6901 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6902 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006903 *
6904 * 4. Test that the number of ops done prior to starting signing and after abort
6905 * is zero and that each successful signing stage completes some ops (this is
6906 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006907 */
Paul Elliott712d5122022-12-07 14:03:10 +00006908void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006909 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006910 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006911{
6912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6913 psa_key_type_t key_type = key_type_arg;
6914 psa_algorithm_t alg = alg_arg;
6915 size_t key_bits;
6916 unsigned char *signature = NULL;
6917 size_t signature_size;
6918 size_t signature_length = 0xdeadbeef;
6919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6920 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006921 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006922 uint32_t num_ops = 0;
6923 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006924 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006925 size_t min_completes = 0;
6926 size_t max_completes = 0;
6927
Paul Elliott712d5122022-12-07 14:03:10 +00006928 psa_sign_hash_interruptible_operation_t sign_operation =
6929 psa_sign_hash_interruptible_operation_init();
6930 psa_verify_hash_interruptible_operation_t verify_operation =
6931 psa_verify_hash_interruptible_operation_init();
6932
6933 PSA_ASSERT(psa_crypto_init());
6934
Paul Elliott0c683352022-12-16 19:16:56 +00006935 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6936 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006937 psa_set_key_algorithm(&attributes, alg);
6938 psa_set_key_type(&attributes, key_type);
6939
6940 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6941 &key));
6942 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6943 key_bits = psa_get_key_bits(&attributes);
6944
6945 /* Allocate a buffer which has the size advertised by the
6946 * library. */
6947 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6948 key_bits, alg);
6949 TEST_ASSERT(signature_size != 0);
6950 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6951 ASSERT_ALLOC(signature, signature_size);
6952
Paul Elliott0c683352022-12-16 19:16:56 +00006953 psa_interruptible_set_max_ops(max_ops);
6954
Paul Elliott6f600372023-02-06 18:41:05 +00006955 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6956 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006957
Paul Elliott7c173082023-02-26 18:44:45 +00006958 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6959 TEST_ASSERT(num_ops_prior == 0);
6960
Paul Elliott712d5122022-12-07 14:03:10 +00006961 /* Start performing the signature. */
6962 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6963 input_data->x, input_data->len));
6964
Paul Elliott7c173082023-02-26 18:44:45 +00006965 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6966 TEST_ASSERT(num_ops_prior == 0);
6967
Paul Elliott712d5122022-12-07 14:03:10 +00006968 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006969 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006970
Paul Elliott0c683352022-12-16 19:16:56 +00006971 status = psa_sign_hash_complete(&sign_operation, signature,
6972 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006973 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006974
6975 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006976
6977 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6978 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6979 /* We are asserting here that every complete makes progress
6980 * (completes some ops), which is true of the internal
6981 * implementation and probably any implementation, however this is
6982 * not mandated by the PSA specification. */
6983 TEST_ASSERT(num_ops > num_ops_prior);
6984
6985 num_ops_prior = num_ops;
6986 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006987 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006988
6989 TEST_ASSERT(status == PSA_SUCCESS);
6990
Paul Elliott0c683352022-12-16 19:16:56 +00006991 TEST_LE_U(min_completes, num_completes);
6992 TEST_LE_U(num_completes, max_completes);
6993
Paul Elliott712d5122022-12-07 14:03:10 +00006994 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6995
Paul Elliott7c173082023-02-26 18:44:45 +00006996 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6997 TEST_ASSERT(num_ops == 0);
6998
Paul Elliott712d5122022-12-07 14:03:10 +00006999 /* Check that the signature length looks sensible. */
7000 TEST_LE_U(signature_length, signature_size);
7001 TEST_ASSERT(signature_length > 0);
7002
Paul Elliott0c683352022-12-16 19:16:56 +00007003 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007004
7005 /* Start verification. */
7006 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7007 input_data->x, input_data->len,
7008 signature, signature_length));
7009
7010 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007011 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007012 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007013
7014 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007015 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007016
7017 TEST_ASSERT(status == PSA_SUCCESS);
7018
Paul Elliott0c683352022-12-16 19:16:56 +00007019 TEST_LE_U(min_completes, num_completes);
7020 TEST_LE_U(num_completes, max_completes);
7021
Paul Elliott712d5122022-12-07 14:03:10 +00007022 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7023
7024 verify_operation = psa_verify_hash_interruptible_operation_init();
7025
7026 if (input_data->len != 0) {
7027 /* Flip a bit in the input and verify that the signature is now
7028 * detected as invalid. Flip a bit at the beginning, not at the end,
7029 * because ECDSA may ignore the last few bits of the input. */
7030 input_data->x[0] ^= 1;
7031
Paul Elliott712d5122022-12-07 14:03:10 +00007032 /* Start verification. */
7033 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7034 input_data->x, input_data->len,
7035 signature, signature_length));
7036
7037 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007038 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007039 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007040 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007041
7042 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7043 }
7044
7045 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7046
7047exit:
7048 /*
7049 * Key attributes may have been returned by psa_get_key_attributes()
7050 * thus reset them as required.
7051 */
7052 psa_reset_key_attributes(&attributes);
7053
7054 psa_destroy_key(key);
7055 mbedtls_free(signature);
7056 PSA_DONE();
7057}
7058/* END_CASE */
7059
Gilles Peskine9911b022018-06-29 17:30:48 +02007060/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007061void verify_hash(int key_type_arg, data_t *key_data,
7062 int alg_arg, data_t *hash_data,
7063 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007064{
Ronald Cron5425a212020-08-04 14:58:35 +02007065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007066 psa_key_type_t key_type = key_type_arg;
7067 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007069
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7075 psa_set_key_algorithm(&attributes, alg);
7076 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7079 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 PSA_ASSERT(psa_verify_hash(key, alg,
7082 hash_data->x, hash_data->len,
7083 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007084
itayzafrir5c753392018-05-08 11:18:38 +03007085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 psa_reset_key_attributes(&attributes);
7087 psa_destroy_key(key);
7088 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007089}
7090/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007091
Paul Elliott712d5122022-12-07 14:03:10 +00007092/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007093/**
7094 * verify_hash_interruptible() test intentions:
7095 *
7096 * Note: This test can currently only handle ECDSA.
7097 *
7098 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007099 * only). Given this test only does verification it can accept public keys as
7100 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007101 *
7102 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7103 * expected for different max_ops values.
7104 *
7105 * 3. Test that the number of ops done prior to start and after abort is zero
7106 * and that each successful stage completes some ops (this is not mandated by
7107 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007108 *
7109 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7110 * complete() calls does not alter the number of ops returned.
7111 *
7112 * 5. Test that after corrupting the hash, the verification detects an invalid
7113 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007114 */
Paul Elliott712d5122022-12-07 14:03:10 +00007115void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7116 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007117 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007118{
7119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7120 psa_key_type_t key_type = key_type_arg;
7121 psa_algorithm_t alg = alg_arg;
7122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7123 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007124 uint32_t num_ops = 0;
7125 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007126 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007127 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007128 size_t min_completes = 0;
7129 size_t max_completes = 0;
7130
Paul Elliott712d5122022-12-07 14:03:10 +00007131 psa_verify_hash_interruptible_operation_t operation =
7132 psa_verify_hash_interruptible_operation_init();
7133
7134 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7135
7136 PSA_ASSERT(psa_crypto_init());
7137
7138 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7139 psa_set_key_algorithm(&attributes, alg);
7140 psa_set_key_type(&attributes, key_type);
7141
7142 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7143 &key));
7144
Paul Elliott0c683352022-12-16 19:16:56 +00007145 psa_interruptible_set_max_ops(max_ops);
7146
Paul Elliott6f600372023-02-06 18:41:05 +00007147 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7148 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007149
Paul Elliott712d5122022-12-07 14:03:10 +00007150 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7151
7152 TEST_ASSERT(num_ops_prior == 0);
7153
7154 /* Start verification. */
7155 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7156 hash_data->x, hash_data->len,
7157 signature_data->x, signature_data->len)
7158 );
7159
7160 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7161
7162 TEST_ASSERT(num_ops_prior == 0);
7163
7164 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007165 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007166 status = psa_verify_hash_complete(&operation);
7167
Paul Elliott0c683352022-12-16 19:16:56 +00007168 num_completes++;
7169
Paul Elliott712d5122022-12-07 14:03:10 +00007170 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7171 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007172 /* We are asserting here that every complete makes progress
7173 * (completes some ops), which is true of the internal
7174 * implementation and probably any implementation, however this is
7175 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007176 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007177
Paul Elliott712d5122022-12-07 14:03:10 +00007178 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007179
7180 /* Ensure calling get_num_ops() twice still returns the same
7181 * number of ops as previously reported. */
7182 num_ops = psa_verify_hash_get_num_ops(&operation);
7183
7184 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007185 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007186 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007187
7188 TEST_ASSERT(status == PSA_SUCCESS);
7189
Paul Elliott0c683352022-12-16 19:16:56 +00007190 TEST_LE_U(min_completes, num_completes);
7191 TEST_LE_U(num_completes, max_completes);
7192
Paul Elliott712d5122022-12-07 14:03:10 +00007193 PSA_ASSERT(psa_verify_hash_abort(&operation));
7194
Paul Elliott59ad9452022-12-18 15:09:02 +00007195 num_ops = psa_verify_hash_get_num_ops(&operation);
7196 TEST_ASSERT(num_ops == 0);
7197
Paul Elliott8359c142023-02-24 18:40:10 +00007198 if (hash_data->len != 0) {
7199 /* Flip a bit in the hash and verify that the signature is now detected
7200 * as invalid. Flip a bit at the beginning, not at the end, because
7201 * ECDSA may ignore the last few bits of the input. */
7202 hash_data->x[0] ^= 1;
7203
7204 /* Start verification. */
7205 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7206 hash_data->x, hash_data->len,
7207 signature_data->x, signature_data->len));
7208
7209 /* Continue performing the signature until complete. */
7210 do {
7211 status = psa_verify_hash_complete(&operation);
7212 } while (status == PSA_OPERATION_INCOMPLETE);
7213
7214 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7215 }
7216
Paul Elliott712d5122022-12-07 14:03:10 +00007217exit:
7218 psa_reset_key_attributes(&attributes);
7219 psa_destroy_key(key);
7220 PSA_DONE();
7221}
7222/* END_CASE */
7223
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007224/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007225void verify_hash_fail(int key_type_arg, data_t *key_data,
7226 int alg_arg, data_t *hash_data,
7227 data_t *signature_data,
7228 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007229{
Ronald Cron5425a212020-08-04 14:58:35 +02007230 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007231 psa_key_type_t key_type = key_type_arg;
7232 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007233 psa_status_t actual_status;
7234 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7240 psa_set_key_algorithm(&attributes, alg);
7241 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7244 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007245
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 actual_status = psa_verify_hash(key, alg,
7247 hash_data->x, hash_data->len,
7248 signature_data->x, signature_data->len);
7249 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007250
7251exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 psa_reset_key_attributes(&attributes);
7253 psa_destroy_key(key);
7254 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007255}
7256/* END_CASE */
7257
Paul Elliott91007972022-12-16 12:21:24 +00007258/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007259/**
7260 * verify_hash_fail_interruptible() test intentions:
7261 *
7262 * Note: This test can currently only handle ECDSA.
7263 *
7264 * 1. Test that various failure cases for interruptible verify hash fail with
7265 * the correct error codes, and at the correct point (at start or during
7266 * complete).
7267 *
7268 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7269 * expected for different max_ops values.
7270 *
7271 * 3. Test that the number of ops done prior to start and after abort is zero
7272 * and that each successful stage completes some ops (this is not mandated by
7273 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007274 *
7275 * 4. Check that calling complete() when start() fails and complete()
7276 * after completion results in a BAD_STATE error.
7277 *
7278 * 5. Check that calling start() again after start fails results in a BAD_STATE
7279 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007280 */
Paul Elliott91007972022-12-16 12:21:24 +00007281void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7282 int alg_arg, data_t *hash_data,
7283 data_t *signature_data,
7284 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007285 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007286 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007287{
7288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7289 psa_key_type_t key_type = key_type_arg;
7290 psa_algorithm_t alg = alg_arg;
7291 psa_status_t actual_status;
7292 psa_status_t expected_start_status = expected_start_status_arg;
7293 psa_status_t expected_complete_status = expected_complete_status_arg;
7294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007295 uint32_t num_ops = 0;
7296 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007297 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007298 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007299 size_t min_completes = 0;
7300 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007301 psa_verify_hash_interruptible_operation_t operation =
7302 psa_verify_hash_interruptible_operation_init();
7303
7304 PSA_ASSERT(psa_crypto_init());
7305
7306 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7307 psa_set_key_algorithm(&attributes, alg);
7308 psa_set_key_type(&attributes, key_type);
7309
7310 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7311 &key));
7312
Paul Elliott0c683352022-12-16 19:16:56 +00007313 psa_interruptible_set_max_ops(max_ops);
7314
Paul Elliott6f600372023-02-06 18:41:05 +00007315 interruptible_signverify_get_minmax_completes(max_ops,
7316 expected_complete_status,
7317 &min_completes,
7318 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007319
Paul Elliott91007972022-12-16 12:21:24 +00007320 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7321 TEST_ASSERT(num_ops_prior == 0);
7322
7323 /* Start verification. */
7324 actual_status = psa_verify_hash_start(&operation, key, alg,
7325 hash_data->x, hash_data->len,
7326 signature_data->x,
7327 signature_data->len);
7328
7329 TEST_EQUAL(actual_status, expected_start_status);
7330
Paul Elliottc9774412023-02-06 15:14:07 +00007331 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007332 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007333 * start failed. */
7334 actual_status = psa_verify_hash_complete(&operation);
7335
7336 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7337
7338 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007339 actual_status = psa_verify_hash_start(&operation, key, alg,
7340 hash_data->x, hash_data->len,
7341 signature_data->x,
7342 signature_data->len);
7343
7344 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7345 }
7346
Paul Elliott91007972022-12-16 12:21:24 +00007347 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7348 TEST_ASSERT(num_ops_prior == 0);
7349
Paul Elliott91007972022-12-16 12:21:24 +00007350 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007351 do {
Paul Elliott91007972022-12-16 12:21:24 +00007352 actual_status = psa_verify_hash_complete(&operation);
7353
Paul Elliott0c683352022-12-16 19:16:56 +00007354 num_completes++;
7355
Paul Elliott334d7262023-01-20 17:29:41 +00007356 if (actual_status == PSA_SUCCESS ||
7357 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007358 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007359 /* We are asserting here that every complete makes progress
7360 * (completes some ops), which is true of the internal
7361 * implementation and probably any implementation, however this is
7362 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007363 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007364
Paul Elliott91007972022-12-16 12:21:24 +00007365 num_ops_prior = num_ops;
7366 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007367 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007368
Paul Elliottc9774412023-02-06 15:14:07 +00007369 TEST_EQUAL(actual_status, expected_complete_status);
7370
Paul Elliottefebad02023-02-15 16:56:45 +00007371 /* Check that another complete returns BAD_STATE. */
7372 actual_status = psa_verify_hash_complete(&operation);
7373 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007374
Paul Elliott0c683352022-12-16 19:16:56 +00007375 TEST_LE_U(min_completes, num_completes);
7376 TEST_LE_U(num_completes, max_completes);
7377
Paul Elliott91007972022-12-16 12:21:24 +00007378 PSA_ASSERT(psa_verify_hash_abort(&operation));
7379
Paul Elliott59ad9452022-12-18 15:09:02 +00007380 num_ops = psa_verify_hash_get_num_ops(&operation);
7381 TEST_ASSERT(num_ops == 0);
7382
Paul Elliott91007972022-12-16 12:21:24 +00007383exit:
7384 psa_reset_key_attributes(&attributes);
7385 psa_destroy_key(key);
7386 PSA_DONE();
7387}
7388/* END_CASE */
7389
Paul Elliott20a36062022-12-18 13:21:25 +00007390/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007391/**
7392 * interruptible_signverify_hash_state_test() test intentions:
7393 *
7394 * Note: This test can currently only handle ECDSA.
7395 *
7396 * 1. Test that calling the various interruptible sign and verify hash functions
7397 * in incorrect orders returns BAD_STATE errors.
7398 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007399void interruptible_signverify_hash_state_test(int key_type_arg,
7400 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007401{
7402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7403 psa_key_type_t key_type = key_type_arg;
7404 psa_algorithm_t alg = alg_arg;
7405 size_t key_bits;
7406 unsigned char *signature = NULL;
7407 size_t signature_size;
7408 size_t signature_length = 0xdeadbeef;
7409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7410 psa_sign_hash_interruptible_operation_t sign_operation =
7411 psa_sign_hash_interruptible_operation_init();
7412 psa_verify_hash_interruptible_operation_t verify_operation =
7413 psa_verify_hash_interruptible_operation_init();
7414
7415 PSA_ASSERT(psa_crypto_init());
7416
7417 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7418 PSA_KEY_USAGE_VERIFY_HASH);
7419 psa_set_key_algorithm(&attributes, alg);
7420 psa_set_key_type(&attributes, key_type);
7421
7422 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7423 &key));
7424 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7425 key_bits = psa_get_key_bits(&attributes);
7426
7427 /* Allocate a buffer which has the size advertised by the
7428 * library. */
7429 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7430 key_bits, alg);
7431 TEST_ASSERT(signature_size != 0);
7432 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7433 ASSERT_ALLOC(signature, signature_size);
7434
7435 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7436
7437 /* --- Attempt completes prior to starts --- */
7438 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7439 signature_size,
7440 &signature_length),
7441 PSA_ERROR_BAD_STATE);
7442
7443 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7444
7445 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7446 PSA_ERROR_BAD_STATE);
7447
7448 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7449
7450 /* --- Aborts in all other places. --- */
7451 psa_sign_hash_abort(&sign_operation);
7452
7453 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7454 input_data->x, input_data->len));
7455
7456 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7457
7458 psa_interruptible_set_max_ops(1);
7459
7460 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7461 input_data->x, input_data->len));
7462
7463 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7464 signature_size,
7465 &signature_length),
7466 PSA_OPERATION_INCOMPLETE);
7467
7468 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7469
7470 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7471
7472 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7473 input_data->x, input_data->len));
7474
7475 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7476 signature_size,
7477 &signature_length));
7478
7479 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7480
7481 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7482
7483 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7484 input_data->x, input_data->len,
7485 signature, signature_length));
7486
7487 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7488
7489 psa_interruptible_set_max_ops(1);
7490
7491 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7492 input_data->x, input_data->len,
7493 signature, signature_length));
7494
7495 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7496 PSA_OPERATION_INCOMPLETE);
7497
7498 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7499
7500 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7501
7502 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7503 input_data->x, input_data->len,
7504 signature, signature_length));
7505
7506 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7507
7508 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7509
7510 /* --- Attempt double starts. --- */
7511
7512 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7513 input_data->x, input_data->len));
7514
7515 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7516 input_data->x, input_data->len),
7517 PSA_ERROR_BAD_STATE);
7518
7519 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7520
7521 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7522 input_data->x, input_data->len,
7523 signature, signature_length));
7524
7525 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7526 input_data->x, input_data->len,
7527 signature, signature_length),
7528 PSA_ERROR_BAD_STATE);
7529
7530 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7531
Paul Elliott76d671a2023-02-07 17:45:18 +00007532exit:
7533 /*
7534 * Key attributes may have been returned by psa_get_key_attributes()
7535 * thus reset them as required.
7536 */
7537 psa_reset_key_attributes(&attributes);
7538
7539 psa_destroy_key(key);
7540 mbedtls_free(signature);
7541 PSA_DONE();
7542}
7543/* END_CASE */
7544
7545/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007546/**
Paul Elliottc2033502023-02-26 17:09:14 +00007547 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007548 *
7549 * Note: This test can currently only handle ECDSA.
7550 *
7551 * 1. Test various edge cases in the interruptible sign and verify hash
7552 * interfaces.
7553 */
Paul Elliottc2033502023-02-26 17:09:14 +00007554void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007555 data_t *key_data, int alg_arg, data_t *input_data)
7556{
7557 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7558 psa_key_type_t key_type = key_type_arg;
7559 psa_algorithm_t alg = alg_arg;
7560 size_t key_bits;
7561 unsigned char *signature = NULL;
7562 size_t signature_size;
7563 size_t signature_length = 0xdeadbeef;
7564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7565 uint8_t *input_buffer = NULL;
7566 psa_sign_hash_interruptible_operation_t sign_operation =
7567 psa_sign_hash_interruptible_operation_init();
7568 psa_verify_hash_interruptible_operation_t verify_operation =
7569 psa_verify_hash_interruptible_operation_init();
7570
7571 PSA_ASSERT(psa_crypto_init());
7572
7573 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7574 PSA_KEY_USAGE_VERIFY_HASH);
7575 psa_set_key_algorithm(&attributes, alg);
7576 psa_set_key_type(&attributes, key_type);
7577
7578 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7579 &key));
7580 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7581 key_bits = psa_get_key_bits(&attributes);
7582
7583 /* Allocate a buffer which has the size advertised by the
7584 * library. */
7585 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7586 key_bits, alg);
7587 TEST_ASSERT(signature_size != 0);
7588 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7589 ASSERT_ALLOC(signature, signature_size);
7590
Paul Elliott20a36062022-12-18 13:21:25 +00007591 /* --- Change function inputs mid run, to cause an error (sign only,
7592 * verify passes all inputs to start. --- */
7593
7594 psa_interruptible_set_max_ops(1);
7595
7596 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7597 input_data->x, input_data->len));
7598
7599 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7600 signature_size,
7601 &signature_length),
7602 PSA_OPERATION_INCOMPLETE);
7603
7604 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7605 0,
7606 &signature_length),
7607 PSA_ERROR_BUFFER_TOO_SMALL);
7608
Paul Elliottc9774412023-02-06 15:14:07 +00007609 /* And test that this invalidates the operation. */
7610 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7611 0,
7612 &signature_length),
7613 PSA_ERROR_BAD_STATE);
7614
Paul Elliott20a36062022-12-18 13:21:25 +00007615 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7616
Paul Elliottf9c91a72023-02-05 18:06:38 +00007617 /* Trash the hash buffer in between start and complete, to ensure
7618 * no reliance on external buffers. */
7619 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7620
7621 input_buffer = mbedtls_calloc(1, input_data->len);
7622 TEST_ASSERT(input_buffer != NULL);
7623
7624 memcpy(input_buffer, input_data->x, input_data->len);
7625
7626 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7627 input_buffer, input_data->len));
7628
7629 memset(input_buffer, '!', input_data->len);
7630 mbedtls_free(input_buffer);
7631 input_buffer = NULL;
7632
7633 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7634 signature_size,
7635 &signature_length));
7636
7637 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7638
7639 input_buffer = mbedtls_calloc(1, input_data->len);
7640 TEST_ASSERT(input_buffer != NULL);
7641
7642 memcpy(input_buffer, input_data->x, input_data->len);
7643
7644 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7645 input_buffer, input_data->len,
7646 signature, signature_length));
7647
7648 memset(input_buffer, '!', input_data->len);
7649 mbedtls_free(input_buffer);
7650 input_buffer = NULL;
7651
7652 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7653
7654 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7655
Paul Elliott20a36062022-12-18 13:21:25 +00007656exit:
7657 /*
7658 * Key attributes may have been returned by psa_get_key_attributes()
7659 * thus reset them as required.
7660 */
7661 psa_reset_key_attributes(&attributes);
7662
7663 psa_destroy_key(key);
7664 mbedtls_free(signature);
7665 PSA_DONE();
7666}
7667/* END_CASE */
7668
Paul Elliotta4cb9092023-02-07 18:01:55 +00007669/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007670/**
Paul Elliott57702242023-02-26 20:36:10 +00007671 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007672 *
7673 * Note: This test can currently only handle ECDSA.
7674 *
7675 * 1. Test that setting max ops is reflected in both interruptible sign and
7676 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007677 * 2. Test that changing the value of max_ops to unlimited during an operation
7678 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007679 *
7680 * 3. Test that calling get_num_ops() between complete calls gives the same
7681 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007682 */
Paul Elliott57702242023-02-26 20:36:10 +00007683void interruptible_signverify_hash_ops_tests(int key_type_arg,
7684 data_t *key_data, int alg_arg,
7685 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007686{
7687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7688 psa_key_type_t key_type = key_type_arg;
7689 psa_algorithm_t alg = alg_arg;
7690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007691 size_t key_bits;
7692 unsigned char *signature = NULL;
7693 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007694 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007695 uint32_t num_ops = 0;
7696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7697
Paul Elliotta4cb9092023-02-07 18:01:55 +00007698 psa_sign_hash_interruptible_operation_t sign_operation =
7699 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007700 psa_verify_hash_interruptible_operation_t verify_operation =
7701 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007702
7703 PSA_ASSERT(psa_crypto_init());
7704
7705 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7706 PSA_KEY_USAGE_VERIFY_HASH);
7707 psa_set_key_algorithm(&attributes, alg);
7708 psa_set_key_type(&attributes, key_type);
7709
Paul Elliottf1743e22023-02-15 18:44:16 +00007710 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7711 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7712 key_bits = psa_get_key_bits(&attributes);
7713
7714 /* Allocate a buffer which has the size advertised by the
7715 * library. */
7716 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7717
7718 TEST_ASSERT(signature_size != 0);
7719 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7720 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007721
7722 /* Check that default max ops gets set if we don't set it. */
7723 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7724 input_data->x, input_data->len));
7725
7726 TEST_EQUAL(psa_interruptible_get_max_ops(),
7727 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7728
7729 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7730
Paul Elliottf1743e22023-02-15 18:44:16 +00007731 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7732 input_data->x, input_data->len,
7733 signature, signature_size));
7734
7735 TEST_EQUAL(psa_interruptible_get_max_ops(),
7736 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7737
7738 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7739
Paul Elliotta4cb9092023-02-07 18:01:55 +00007740 /* Check that max ops gets set properly. */
7741
7742 psa_interruptible_set_max_ops(0xbeef);
7743
Paul Elliottf1743e22023-02-15 18:44:16 +00007744 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007745
Paul Elliott9e8819f2023-02-26 19:01:35 +00007746 /* --- Ensure changing the max ops mid operation works (operation should
7747 * complete successfully after setting max ops to unlimited --- */
7748 psa_interruptible_set_max_ops(1);
7749
7750 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7751 input_data->x, input_data->len));
7752
7753 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7754 signature_size,
7755 &signature_length),
7756 PSA_OPERATION_INCOMPLETE);
7757
7758 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7759
7760 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7761 signature_size,
7762 &signature_length));
7763
7764 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7765
7766 psa_interruptible_set_max_ops(1);
7767
7768 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7769 input_data->x, input_data->len,
7770 signature, signature_length));
7771
7772 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7773 PSA_OPERATION_INCOMPLETE);
7774
7775 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7776
7777 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7778
7779 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7780
Paul Elliottc1e04002023-02-26 20:27:23 +00007781 /* --- Test that not calling get_num_ops inbetween complete calls does not
7782 * result in lost ops. ---*/
7783
7784 psa_interruptible_set_max_ops(1);
7785
7786 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7787 input_data->x, input_data->len));
7788
7789 /* Continue performing the signature until complete. */
7790 do {
7791 status = psa_sign_hash_complete(&sign_operation, signature,
7792 signature_size,
7793 &signature_length);
7794
7795 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7796
7797 } while (status == PSA_OPERATION_INCOMPLETE);
7798
7799 PSA_ASSERT(status);
7800
7801 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7802
7803 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7804 input_data->x, input_data->len));
7805
7806 /* Continue performing the signature until complete. */
7807 do {
7808 status = psa_sign_hash_complete(&sign_operation, signature,
7809 signature_size,
7810 &signature_length);
7811 } while (status == PSA_OPERATION_INCOMPLETE);
7812
7813 PSA_ASSERT(status);
7814
7815 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7816
7817 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7818
7819 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7820 input_data->x, input_data->len,
7821 signature, signature_length));
7822
7823 /* Continue performing the verification until complete. */
7824 do {
7825 status = psa_verify_hash_complete(&verify_operation);
7826
7827 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7828
7829 } while (status == PSA_OPERATION_INCOMPLETE);
7830
7831 PSA_ASSERT(status);
7832
7833 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7834
7835 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7836 input_data->x, input_data->len,
7837 signature, signature_length));
7838
7839 /* Continue performing the verification until complete. */
7840 do {
7841 status = psa_verify_hash_complete(&verify_operation);
7842
7843 } while (status == PSA_OPERATION_INCOMPLETE);
7844
7845 PSA_ASSERT(status);
7846
7847 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7848
7849 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7850
Paul Elliotta4cb9092023-02-07 18:01:55 +00007851exit:
7852 /*
7853 * Key attributes may have been returned by psa_get_key_attributes()
7854 * thus reset them as required.
7855 */
7856 psa_reset_key_attributes(&attributes);
7857
7858 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007859 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007860 PSA_DONE();
7861}
7862/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007863
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007864/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007865void sign_message_deterministic(int key_type_arg,
7866 data_t *key_data,
7867 int alg_arg,
7868 data_t *input_data,
7869 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007870{
7871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7872 psa_key_type_t key_type = key_type_arg;
7873 psa_algorithm_t alg = alg_arg;
7874 size_t key_bits;
7875 unsigned char *signature = NULL;
7876 size_t signature_size;
7877 size_t signature_length = 0xdeadbeef;
7878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7879
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007881
Gilles Peskine449bd832023-01-11 14:50:10 +01007882 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7883 psa_set_key_algorithm(&attributes, alg);
7884 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007885
Gilles Peskine449bd832023-01-11 14:50:10 +01007886 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7887 &key));
7888 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7889 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007890
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7892 TEST_ASSERT(signature_size != 0);
7893 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7894 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007895
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 PSA_ASSERT(psa_sign_message(key, alg,
7897 input_data->x, input_data->len,
7898 signature, signature_size,
7899 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 ASSERT_COMPARE(output_data->x, output_data->len,
7902 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007903
7904exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 psa_destroy_key(key);
7908 mbedtls_free(signature);
7909 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007910
7911}
7912/* END_CASE */
7913
7914/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007915void sign_message_fail(int key_type_arg,
7916 data_t *key_data,
7917 int alg_arg,
7918 data_t *input_data,
7919 int signature_size_arg,
7920 int expected_status_arg)
7921{
7922 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7923 psa_key_type_t key_type = key_type_arg;
7924 psa_algorithm_t alg = alg_arg;
7925 size_t signature_size = signature_size_arg;
7926 psa_status_t actual_status;
7927 psa_status_t expected_status = expected_status_arg;
7928 unsigned char *signature = NULL;
7929 size_t signature_length = 0xdeadbeef;
7930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7931
7932 ASSERT_ALLOC(signature, signature_size);
7933
7934 PSA_ASSERT(psa_crypto_init());
7935
7936 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7937 psa_set_key_algorithm(&attributes, alg);
7938 psa_set_key_type(&attributes, key_type);
7939
7940 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7941 &key));
7942
7943 actual_status = psa_sign_message(key, alg,
7944 input_data->x, input_data->len,
7945 signature, signature_size,
7946 &signature_length);
7947 TEST_EQUAL(actual_status, expected_status);
7948 /* The value of *signature_length is unspecified on error, but
7949 * whatever it is, it should be less than signature_size, so that
7950 * if the caller tries to read *signature_length bytes without
7951 * checking the error code then they don't overflow a buffer. */
7952 TEST_LE_U(signature_length, signature_size);
7953
7954exit:
7955 psa_reset_key_attributes(&attributes);
7956 psa_destroy_key(key);
7957 mbedtls_free(signature);
7958 PSA_DONE();
7959}
7960/* END_CASE */
7961
7962/* BEGIN_CASE */
7963void sign_verify_message(int key_type_arg,
7964 data_t *key_data,
7965 int alg_arg,
7966 data_t *input_data)
7967{
7968 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7969 psa_key_type_t key_type = key_type_arg;
7970 psa_algorithm_t alg = alg_arg;
7971 size_t key_bits;
7972 unsigned char *signature = NULL;
7973 size_t signature_size;
7974 size_t signature_length = 0xdeadbeef;
7975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7976
7977 PSA_ASSERT(psa_crypto_init());
7978
7979 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7980 PSA_KEY_USAGE_VERIFY_MESSAGE);
7981 psa_set_key_algorithm(&attributes, alg);
7982 psa_set_key_type(&attributes, key_type);
7983
7984 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7985 &key));
7986 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7987 key_bits = psa_get_key_bits(&attributes);
7988
7989 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7990 TEST_ASSERT(signature_size != 0);
7991 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7992 ASSERT_ALLOC(signature, signature_size);
7993
7994 PSA_ASSERT(psa_sign_message(key, alg,
7995 input_data->x, input_data->len,
7996 signature, signature_size,
7997 &signature_length));
7998 TEST_LE_U(signature_length, signature_size);
7999 TEST_ASSERT(signature_length > 0);
8000
8001 PSA_ASSERT(psa_verify_message(key, alg,
8002 input_data->x, input_data->len,
8003 signature, signature_length));
8004
8005 if (input_data->len != 0) {
8006 /* Flip a bit in the input and verify that the signature is now
8007 * detected as invalid. Flip a bit at the beginning, not at the end,
8008 * because ECDSA may ignore the last few bits of the input. */
8009 input_data->x[0] ^= 1;
8010 TEST_EQUAL(psa_verify_message(key, alg,
8011 input_data->x, input_data->len,
8012 signature, signature_length),
8013 PSA_ERROR_INVALID_SIGNATURE);
8014 }
8015
8016exit:
8017 psa_reset_key_attributes(&attributes);
8018
8019 psa_destroy_key(key);
8020 mbedtls_free(signature);
8021 PSA_DONE();
8022}
8023/* END_CASE */
8024
8025/* BEGIN_CASE */
8026void verify_message(int key_type_arg,
8027 data_t *key_data,
8028 int alg_arg,
8029 data_t *input_data,
8030 data_t *signature_data)
8031{
8032 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8033 psa_key_type_t key_type = key_type_arg;
8034 psa_algorithm_t alg = alg_arg;
8035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8036
8037 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8038
8039 PSA_ASSERT(psa_crypto_init());
8040
8041 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8042 psa_set_key_algorithm(&attributes, alg);
8043 psa_set_key_type(&attributes, key_type);
8044
8045 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8046 &key));
8047
8048 PSA_ASSERT(psa_verify_message(key, alg,
8049 input_data->x, input_data->len,
8050 signature_data->x, signature_data->len));
8051
8052exit:
8053 psa_reset_key_attributes(&attributes);
8054 psa_destroy_key(key);
8055 PSA_DONE();
8056}
8057/* END_CASE */
8058
8059/* BEGIN_CASE */
8060void verify_message_fail(int key_type_arg,
8061 data_t *key_data,
8062 int alg_arg,
8063 data_t *hash_data,
8064 data_t *signature_data,
8065 int expected_status_arg)
8066{
8067 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8068 psa_key_type_t key_type = key_type_arg;
8069 psa_algorithm_t alg = alg_arg;
8070 psa_status_t actual_status;
8071 psa_status_t expected_status = expected_status_arg;
8072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8073
8074 PSA_ASSERT(psa_crypto_init());
8075
8076 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8077 psa_set_key_algorithm(&attributes, alg);
8078 psa_set_key_type(&attributes, key_type);
8079
8080 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8081 &key));
8082
8083 actual_status = psa_verify_message(key, alg,
8084 hash_data->x, hash_data->len,
8085 signature_data->x,
8086 signature_data->len);
8087 TEST_EQUAL(actual_status, expected_status);
8088
8089exit:
8090 psa_reset_key_attributes(&attributes);
8091 psa_destroy_key(key);
8092 PSA_DONE();
8093}
8094/* END_CASE */
8095
8096/* BEGIN_CASE */
8097void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008098 data_t *key_data,
8099 int alg_arg,
8100 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008101 data_t *label,
8102 int expected_output_length_arg,
8103 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008104{
Ronald Cron5425a212020-08-04 14:58:35 +02008105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008106 psa_key_type_t key_type = key_type_arg;
8107 psa_algorithm_t alg = alg_arg;
8108 size_t expected_output_length = expected_output_length_arg;
8109 size_t key_bits;
8110 unsigned char *output = NULL;
8111 size_t output_size;
8112 size_t output_length = ~0;
8113 psa_status_t actual_status;
8114 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008116
Gilles Peskine449bd832023-01-11 14:50:10 +01008117 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008118
Gilles Peskine656896e2018-06-29 19:12:28 +02008119 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8121 psa_set_key_algorithm(&attributes, alg);
8122 psa_set_key_type(&attributes, key_type);
8123 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8124 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008125
8126 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8128 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008129
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8131 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8132 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008133
8134 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 actual_status = psa_asymmetric_encrypt(key, alg,
8136 input_data->x, input_data->len,
8137 label->x, label->len,
8138 output, output_size,
8139 &output_length);
8140 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008141 if (actual_status == PSA_SUCCESS) {
8142 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008143 } else {
8144 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008145 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008146
Gilles Peskine68428122018-06-30 18:42:41 +02008147 /* If the label is empty, the test framework puts a non-null pointer
8148 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008150 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 if (output_size != 0) {
8152 memset(output, 0, output_size);
8153 }
8154 actual_status = psa_asymmetric_encrypt(key, alg,
8155 input_data->x, input_data->len,
8156 NULL, label->len,
8157 output, output_size,
8158 &output_length);
8159 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008160 if (actual_status == PSA_SUCCESS) {
8161 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008162 } else {
8163 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008164 }
Gilles Peskine68428122018-06-30 18:42:41 +02008165 }
8166
Gilles Peskine656896e2018-06-29 19:12:28 +02008167exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008168 /*
8169 * Key attributes may have been returned by psa_get_key_attributes()
8170 * thus reset them as required.
8171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008173
Gilles Peskine449bd832023-01-11 14:50:10 +01008174 psa_destroy_key(key);
8175 mbedtls_free(output);
8176 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008177}
8178/* END_CASE */
8179
8180/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008181void asymmetric_encrypt_decrypt(int key_type_arg,
8182 data_t *key_data,
8183 int alg_arg,
8184 data_t *input_data,
8185 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008186{
Ronald Cron5425a212020-08-04 14:58:35 +02008187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008188 psa_key_type_t key_type = key_type_arg;
8189 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008190 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008191 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008192 size_t output_size;
8193 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008194 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008195 size_t output2_size;
8196 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008198
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008200
Gilles Peskine449bd832023-01-11 14:50:10 +01008201 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8202 psa_set_key_algorithm(&attributes, alg);
8203 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008204
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8206 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008207
8208 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8210 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008211
Gilles Peskine449bd832023-01-11 14:50:10 +01008212 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8213 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8214 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008215
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008216 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 TEST_LE_U(output2_size,
8218 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8219 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8220 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008221
Gilles Peskineeebd7382018-06-08 18:11:54 +02008222 /* We test encryption by checking that encrypt-then-decrypt gives back
8223 * the original plaintext because of the non-optional random
8224 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8226 input_data->x, input_data->len,
8227 label->x, label->len,
8228 output, output_size,
8229 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008230 /* We don't know what ciphertext length to expect, but check that
8231 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008233
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8235 output, output_length,
8236 label->x, label->len,
8237 output2, output2_size,
8238 &output2_length));
8239 ASSERT_COMPARE(input_data->x, input_data->len,
8240 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008241
8242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008243 /*
8244 * Key attributes may have been returned by psa_get_key_attributes()
8245 * thus reset them as required.
8246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008248
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 psa_destroy_key(key);
8250 mbedtls_free(output);
8251 mbedtls_free(output2);
8252 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008253}
8254/* END_CASE */
8255
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008256/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008257void asymmetric_decrypt(int key_type_arg,
8258 data_t *key_data,
8259 int alg_arg,
8260 data_t *input_data,
8261 data_t *label,
8262 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008263{
Ronald Cron5425a212020-08-04 14:58:35 +02008264 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008265 psa_key_type_t key_type = key_type_arg;
8266 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008267 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008268 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008269 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008270 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008272
Gilles Peskine449bd832023-01-11 14:50:10 +01008273 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008274
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8276 psa_set_key_algorithm(&attributes, alg);
8277 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008278
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8280 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008281
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8283 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008284
8285 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8287 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8288 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008289
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8291 input_data->x, input_data->len,
8292 label->x, label->len,
8293 output,
8294 output_size,
8295 &output_length));
8296 ASSERT_COMPARE(expected_data->x, expected_data->len,
8297 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008298
Gilles Peskine68428122018-06-30 18:42:41 +02008299 /* If the label is empty, the test framework puts a non-null pointer
8300 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008302 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 if (output_size != 0) {
8304 memset(output, 0, output_size);
8305 }
8306 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8307 input_data->x, input_data->len,
8308 NULL, label->len,
8309 output,
8310 output_size,
8311 &output_length));
8312 ASSERT_COMPARE(expected_data->x, expected_data->len,
8313 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008314 }
8315
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 psa_reset_key_attributes(&attributes);
8318 psa_destroy_key(key);
8319 mbedtls_free(output);
8320 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008321}
8322/* END_CASE */
8323
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008324/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008325void asymmetric_decrypt_fail(int key_type_arg,
8326 data_t *key_data,
8327 int alg_arg,
8328 data_t *input_data,
8329 data_t *label,
8330 int output_size_arg,
8331 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008332{
Ronald Cron5425a212020-08-04 14:58:35 +02008333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008334 psa_key_type_t key_type = key_type_arg;
8335 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008336 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008337 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008338 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008339 psa_status_t actual_status;
8340 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008342
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008344
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008346
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8348 psa_set_key_algorithm(&attributes, alg);
8349 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008350
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8352 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 actual_status = psa_asymmetric_decrypt(key, alg,
8355 input_data->x, input_data->len,
8356 label->x, label->len,
8357 output, output_size,
8358 &output_length);
8359 TEST_EQUAL(actual_status, expected_status);
8360 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008361
Gilles Peskine68428122018-06-30 18:42:41 +02008362 /* If the label is empty, the test framework puts a non-null pointer
8363 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008364 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008365 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 if (output_size != 0) {
8367 memset(output, 0, output_size);
8368 }
8369 actual_status = psa_asymmetric_decrypt(key, alg,
8370 input_data->x, input_data->len,
8371 NULL, label->len,
8372 output, output_size,
8373 &output_length);
8374 TEST_EQUAL(actual_status, expected_status);
8375 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008376 }
8377
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008378exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 psa_reset_key_attributes(&attributes);
8380 psa_destroy_key(key);
8381 mbedtls_free(output);
8382 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008383}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008384/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008385
8386/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008387void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008388{
8389 /* Test each valid way of initializing the object, except for `= {0}`, as
8390 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8391 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008392 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008393 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008395 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8396 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008397
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008399
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008400 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8402 PSA_ERROR_BAD_STATE);
8403 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8404 PSA_ERROR_BAD_STATE);
8405 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8406 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008407
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008408 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 PSA_ASSERT(psa_key_derivation_abort(&func));
8410 PSA_ASSERT(psa_key_derivation_abort(&init));
8411 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008412}
8413/* END_CASE */
8414
Janos Follath16de4a42019-06-13 16:32:24 +01008415/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008416void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008417{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008418 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008419 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008421
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008423
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8425 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008426
8427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 psa_key_derivation_abort(&operation);
8429 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008430}
8431/* END_CASE */
8432
Janos Follathaf3c2a02019-06-12 12:34:34 +01008433/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008434void derive_set_capacity(int alg_arg, int capacity_arg,
8435 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008436{
8437 psa_algorithm_t alg = alg_arg;
8438 size_t capacity = capacity_arg;
8439 psa_status_t expected_status = expected_status_arg;
8440 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8441
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008443
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008445
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8447 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008448
8449exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 psa_key_derivation_abort(&operation);
8451 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008452}
8453/* END_CASE */
8454
8455/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008456void derive_input(int alg_arg,
8457 int step_arg1, int key_type_arg1, data_t *input1,
8458 int expected_status_arg1,
8459 int step_arg2, int key_type_arg2, data_t *input2,
8460 int expected_status_arg2,
8461 int step_arg3, int key_type_arg3, data_t *input3,
8462 int expected_status_arg3,
8463 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008464{
8465 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8467 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8468 psa_status_t expected_statuses[] = { expected_status_arg1,
8469 expected_status_arg2,
8470 expected_status_arg3 };
8471 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008472 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8473 MBEDTLS_SVC_KEY_ID_INIT,
8474 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008475 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8477 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008478 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008479 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008480 psa_status_t expected_output_status = expected_output_status_arg;
8481 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008482
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008484
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8486 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008487
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008489
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8491 mbedtls_test_set_step(i);
8492 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008493 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8495 psa_set_key_type(&attributes, key_types[i]);
8496 PSA_ASSERT(psa_import_key(&attributes,
8497 inputs[i]->x, inputs[i]->len,
8498 &keys[i]));
8499 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8500 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008501 // When taking a private key as secret input, use key agreement
8502 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008503 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8504 &operation, keys[i]),
8505 expected_statuses[i]);
8506 } else {
8507 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8508 keys[i]),
8509 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008510 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 } else {
8512 TEST_EQUAL(psa_key_derivation_input_bytes(
8513 &operation, steps[i],
8514 inputs[i]->x, inputs[i]->len),
8515 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008516 }
8517 }
8518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 if (output_key_type != PSA_KEY_TYPE_NONE) {
8520 psa_reset_key_attributes(&attributes);
8521 psa_set_key_type(&attributes, output_key_type);
8522 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008523 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 psa_key_derivation_output_key(&attributes, &operation,
8525 &output_key);
8526 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008527 uint8_t buffer[1];
8528 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 psa_key_derivation_output_bytes(&operation,
8530 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008531 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008533
Janos Follathaf3c2a02019-06-12 12:34:34 +01008534exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 psa_key_derivation_abort(&operation);
8536 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8537 psa_destroy_key(keys[i]);
8538 }
8539 psa_destroy_key(output_key);
8540 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008541}
8542/* END_CASE */
8543
Janos Follathd958bb72019-07-03 15:02:16 +01008544/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008545void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008546{
Janos Follathd958bb72019-07-03 15:02:16 +01008547 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008549 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008550 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008551 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008553 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008555 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008557 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8558 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008559 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008561
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008563
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8565 psa_set_key_algorithm(&attributes, alg);
8566 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 PSA_ASSERT(psa_import_key(&attributes,
8569 key_data, sizeof(key_data),
8570 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008571
8572 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8574 input1, input1_length,
8575 input2, input2_length,
8576 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008579
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008580 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8582 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008583
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008585
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8587 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008588
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008589exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008590 psa_key_derivation_abort(&operation);
8591 psa_destroy_key(key);
8592 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008593}
8594/* END_CASE */
8595
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008596/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008597void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008598{
8599 uint8_t output_buffer[16];
8600 size_t buffer_size = 16;
8601 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008602 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8605 output_buffer, buffer_size)
8606 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008607
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8609 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008610
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008612
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8614 output_buffer, buffer_size)
8615 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008616
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8618 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008619
8620exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008622}
8623/* END_CASE */
8624
8625/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008626void derive_output(int alg_arg,
8627 int step1_arg, data_t *input1, int expected_status_arg1,
8628 int step2_arg, data_t *input2, int expected_status_arg2,
8629 int step3_arg, data_t *input3, int expected_status_arg3,
8630 int step4_arg, data_t *input4, int expected_status_arg4,
8631 data_t *key_agreement_peer_key,
8632 int requested_capacity_arg,
8633 data_t *expected_output1,
8634 data_t *expected_output2,
8635 int other_key_input_type,
8636 int key_input_type,
8637 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008638{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008639 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8641 data_t *inputs[] = { input1, input2, input3, input4 };
8642 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8643 MBEDTLS_SVC_KEY_ID_INIT,
8644 MBEDTLS_SVC_KEY_ID_INIT,
8645 MBEDTLS_SVC_KEY_ID_INIT };
8646 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8647 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008648 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008649 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008650 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008651 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008652 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008654 size_t output_buffer_size = 0;
8655 uint8_t *output_buffer = NULL;
8656 size_t expected_capacity;
8657 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008658 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8659 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8660 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8661 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008662 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008663 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008664 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008665
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8667 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008668 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 }
8670 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008671 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008673 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 ASSERT_ALLOC(output_buffer, output_buffer_size);
8675 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008676
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008677 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8679 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8680 requested_capacity));
8681 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8682 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008683 case 0:
8684 break;
8685 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008687 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 TEST_EQUAL(psa_key_derivation_input_bytes(
8689 &operation, steps[i],
8690 inputs[i]->x, inputs[i]->len),
8691 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008692
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008694 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008696 break;
8697 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8699 psa_set_key_algorithm(&attributes1, alg);
8700 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008701
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 PSA_ASSERT(psa_import_key(&attributes1,
8703 inputs[i]->x, inputs[i]->len,
8704 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8707 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8708 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8709 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008710 }
8711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8713 steps[i],
8714 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008715 break;
8716 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008718 break;
8719 }
8720 break;
8721 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008723 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8725 steps[i],
8726 inputs[i]->x,
8727 inputs[i]->len),
8728 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008729 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008730 case 1: // input key, type DERIVE
8731 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8733 psa_set_key_algorithm(&attributes2, alg);
8734 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008735
8736 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 if (other_key_input_type == 11) {
8738 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8739 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008740
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 PSA_ASSERT(psa_import_key(&attributes2,
8742 inputs[i]->x, inputs[i]->len,
8743 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008744
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8746 steps[i],
8747 keys[i]),
8748 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008749 break;
8750 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8752 psa_set_key_algorithm(&attributes3, alg);
8753 psa_set_key_type(&attributes3,
8754 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008755
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 PSA_ASSERT(psa_import_key(&attributes3,
8757 inputs[i]->x, inputs[i]->len,
8758 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 TEST_EQUAL(psa_key_derivation_key_agreement(
8761 &operation,
8762 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8763 keys[i], key_agreement_peer_key->x,
8764 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008765 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008766 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008768 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008769 }
8770
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008772 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008774 break;
8775 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 TEST_EQUAL(psa_key_derivation_input_bytes(
8777 &operation, steps[i],
8778 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008779
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008783 break;
8784 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008785 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008786
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8788 &current_capacity));
8789 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008790 expected_capacity = requested_capacity;
8791
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008793 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8794
8795 /* For output key derivation secret must be provided using
8796 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008798 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008800
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8802 psa_set_key_algorithm(&attributes4, alg);
8803 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8804 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008805
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8807 &derived_key), expected_status);
8808 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008809 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008811 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 status = psa_key_derivation_output_bytes(&operation,
8813 output_buffer, output_sizes[i]);
8814 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008815 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 TEST_ASSERT(status == PSA_SUCCESS ||
8817 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008818 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 } else if (expected_capacity == 0 ||
8820 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008821 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008823 expected_capacity = 0;
8824 continue;
8825 }
8826 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 PSA_ASSERT(status);
8828 if (output_sizes[i] != 0) {
8829 ASSERT_COMPARE(output_buffer, output_sizes[i],
8830 expected_outputs[i], output_sizes[i]);
8831 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008832 /* Check the operation status. */
8833 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8835 &current_capacity));
8836 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008837 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008838 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008840
8841exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 mbedtls_free(output_buffer);
8843 psa_key_derivation_abort(&operation);
8844 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8845 psa_destroy_key(keys[i]);
8846 }
8847 psa_destroy_key(derived_key);
8848 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008849}
8850/* END_CASE */
8851
8852/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008853void derive_full(int alg_arg,
8854 data_t *key_data,
8855 data_t *input1,
8856 data_t *input2,
8857 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008858{
Ronald Cron5425a212020-08-04 14:58:35 +02008859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008860 psa_algorithm_t alg = alg_arg;
8861 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008862 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008863 unsigned char output_buffer[16];
8864 size_t expected_capacity = requested_capacity;
8865 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008867
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008869
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8871 psa_set_key_algorithm(&attributes, alg);
8872 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008873
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8875 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008876
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8878 input1->x, input1->len,
8879 input2->x, input2->len,
8880 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008881 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008883
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8885 &current_capacity));
8886 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008887
8888 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 while (current_capacity > 0) {
8890 size_t read_size = sizeof(output_buffer);
8891 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008892 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 }
8894 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8895 output_buffer,
8896 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008897 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8899 &current_capacity));
8900 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008901 }
8902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008903 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8905 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008906
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008908
8909exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 psa_key_derivation_abort(&operation);
8911 psa_destroy_key(key);
8912 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008913}
8914/* END_CASE */
8915
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008916/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008917void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8918 int derivation_step,
8919 int capacity, int expected_capacity_status_arg,
8920 data_t *expected_output,
8921 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008922{
8923 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8924 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008925 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008926 uint8_t *output_buffer = NULL;
8927 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008928 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8929 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8930 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 ASSERT_ALLOC(output_buffer, expected_output->len);
8933 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008934
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8936 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8937 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008938
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8940 step, input->x, input->len),
8941 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008942
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8948 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 TEST_EQUAL(status, expected_output_status);
8951 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8952 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8953 expected_output->len);
8954 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008955
8956exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 mbedtls_free(output_buffer);
8958 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008959 PSA_DONE();
8960}
8961/* END_CASE */
8962
Janos Follathe60c9052019-07-03 13:51:30 +01008963/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008964void derive_key_exercise(int alg_arg,
8965 data_t *key_data,
8966 data_t *input1,
8967 data_t *input2,
8968 int derived_type_arg,
8969 int derived_bits_arg,
8970 int derived_usage_arg,
8971 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008972{
Ronald Cron5425a212020-08-04 14:58:35 +02008973 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8974 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008975 psa_algorithm_t alg = alg_arg;
8976 psa_key_type_t derived_type = derived_type_arg;
8977 size_t derived_bits = derived_bits_arg;
8978 psa_key_usage_t derived_usage = derived_usage_arg;
8979 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008981 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008983 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008984
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008986
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8988 psa_set_key_algorithm(&attributes, alg);
8989 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8990 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8991 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008992
8993 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8995 input1->x, input1->len,
8996 input2->x, input2->len,
8997 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008998 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008999 }
Janos Follathe60c9052019-07-03 13:51:30 +01009000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 psa_set_key_usage_flags(&attributes, derived_usage);
9002 psa_set_key_algorithm(&attributes, derived_alg);
9003 psa_set_key_type(&attributes, derived_type);
9004 psa_set_key_bits(&attributes, derived_bits);
9005 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9006 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009007
9008 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9010 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9011 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009012
9013 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009015 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009017
9018exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009019 /*
9020 * Key attributes may have been returned by psa_get_key_attributes()
9021 * thus reset them as required.
9022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009024
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 psa_key_derivation_abort(&operation);
9026 psa_destroy_key(base_key);
9027 psa_destroy_key(derived_key);
9028 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009029}
9030/* END_CASE */
9031
Janos Follath42fd8882019-07-03 14:17:09 +01009032/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009033void derive_key_export(int alg_arg,
9034 data_t *key_data,
9035 data_t *input1,
9036 data_t *input2,
9037 int bytes1_arg,
9038 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009039{
Ronald Cron5425a212020-08-04 14:58:35 +02009040 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9041 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009042 psa_algorithm_t alg = alg_arg;
9043 size_t bytes1 = bytes1_arg;
9044 size_t bytes2 = bytes2_arg;
9045 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009046 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009047 uint8_t *output_buffer = NULL;
9048 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009049 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9050 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009051 size_t length;
9052
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 ASSERT_ALLOC(output_buffer, capacity);
9054 ASSERT_ALLOC(export_buffer, capacity);
9055 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009056
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9058 psa_set_key_algorithm(&base_attributes, alg);
9059 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9060 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9061 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009062
9063 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9065 input1->x, input1->len,
9066 input2->x, input2->len,
9067 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 }
Janos Follath42fd8882019-07-03 14:17:09 +01009070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9072 output_buffer,
9073 capacity));
9074 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009075
9076 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9078 input1->x, input1->len,
9079 input2->x, input2->len,
9080 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 }
Janos Follath42fd8882019-07-03 14:17:09 +01009083
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9085 psa_set_key_algorithm(&derived_attributes, 0);
9086 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9087 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9088 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9089 &derived_key));
9090 PSA_ASSERT(psa_export_key(derived_key,
9091 export_buffer, bytes1,
9092 &length));
9093 TEST_EQUAL(length, bytes1);
9094 PSA_ASSERT(psa_destroy_key(derived_key));
9095 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9096 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9097 &derived_key));
9098 PSA_ASSERT(psa_export_key(derived_key,
9099 export_buffer + bytes1, bytes2,
9100 &length));
9101 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009102
9103 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9105 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009106
9107exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 mbedtls_free(output_buffer);
9109 mbedtls_free(export_buffer);
9110 psa_key_derivation_abort(&operation);
9111 psa_destroy_key(base_key);
9112 psa_destroy_key(derived_key);
9113 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009114}
9115/* END_CASE */
9116
9117/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009118void derive_key_type(int alg_arg,
9119 data_t *key_data,
9120 data_t *input1,
9121 data_t *input2,
9122 int key_type_arg, int bits_arg,
9123 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009124{
9125 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9126 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9127 const psa_algorithm_t alg = alg_arg;
9128 const psa_key_type_t key_type = key_type_arg;
9129 const size_t bits = bits_arg;
9130 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9131 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009133 uint8_t *export_buffer = NULL;
9134 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9135 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9136 size_t export_length;
9137
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 ASSERT_ALLOC(export_buffer, export_buffer_size);
9139 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9142 psa_set_key_algorithm(&base_attributes, alg);
9143 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9144 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9145 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009146
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009148 &operation, base_key, alg,
9149 input1->x, input1->len,
9150 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9156 psa_set_key_algorithm(&derived_attributes, 0);
9157 psa_set_key_type(&derived_attributes, key_type);
9158 psa_set_key_bits(&derived_attributes, bits);
9159 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9160 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 PSA_ASSERT(psa_export_key(derived_key,
9163 export_buffer, export_buffer_size,
9164 &export_length));
9165 ASSERT_COMPARE(export_buffer, export_length,
9166 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009167
9168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009169 mbedtls_free(export_buffer);
9170 psa_key_derivation_abort(&operation);
9171 psa_destroy_key(base_key);
9172 psa_destroy_key(derived_key);
9173 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009174}
9175/* END_CASE */
9176
9177/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009178void derive_key(int alg_arg,
9179 data_t *key_data, data_t *input1, data_t *input2,
9180 int type_arg, int bits_arg,
9181 int expected_status_arg,
9182 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009183{
Ronald Cron5425a212020-08-04 14:58:35 +02009184 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9185 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009186 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009187 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009188 size_t bits = bits_arg;
9189 psa_status_t expected_status = expected_status_arg;
9190 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9191 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9192 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9193
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009195
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9197 psa_set_key_algorithm(&base_attributes, alg);
9198 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9199 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9200 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009201
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9203 input1->x, input1->len,
9204 input2->x, input2->len,
9205 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009208
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9210 psa_set_key_algorithm(&derived_attributes, 0);
9211 psa_set_key_type(&derived_attributes, type);
9212 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009213
9214 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 psa_key_derivation_output_key(&derived_attributes,
9216 &operation,
9217 &derived_key);
9218 if (is_large_output > 0) {
9219 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9220 }
9221 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009222
9223exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 psa_key_derivation_abort(&operation);
9225 psa_destroy_key(base_key);
9226 psa_destroy_key(derived_key);
9227 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009228}
9229/* END_CASE */
9230
9231/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009232void key_agreement_setup(int alg_arg,
9233 int our_key_type_arg, int our_key_alg_arg,
9234 data_t *our_key_data, data_t *peer_key_data,
9235 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009236{
Ronald Cron5425a212020-08-04 14:58:35 +02009237 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009238 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009239 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009240 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009243 psa_status_t expected_status = expected_status_arg;
9244 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009245
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009247
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9249 psa_set_key_algorithm(&attributes, our_key_alg);
9250 psa_set_key_type(&attributes, our_key_type);
9251 PSA_ASSERT(psa_import_key(&attributes,
9252 our_key_data->x, our_key_data->len,
9253 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009254
Gilles Peskine77f40d82019-04-11 21:27:06 +02009255 /* The tests currently include inputs that should fail at either step.
9256 * Test cases that fail at the setup step should be changed to call
9257 * key_derivation_setup instead, and this function should be renamed
9258 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009259 status = psa_key_derivation_setup(&operation, alg);
9260 if (status == PSA_SUCCESS) {
9261 TEST_EQUAL(psa_key_derivation_key_agreement(
9262 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9263 our_key,
9264 peer_key_data->x, peer_key_data->len),
9265 expected_status);
9266 } else {
9267 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009268 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009269
9270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009271 psa_key_derivation_abort(&operation);
9272 psa_destroy_key(our_key);
9273 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009274}
9275/* END_CASE */
9276
9277/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009278void raw_key_agreement(int alg_arg,
9279 int our_key_type_arg, data_t *our_key_data,
9280 data_t *peer_key_data,
9281 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009282{
Ronald Cron5425a212020-08-04 14:58:35 +02009283 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009284 psa_algorithm_t alg = alg_arg;
9285 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009287 unsigned char *output = NULL;
9288 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009289 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009290
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009292
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9294 psa_set_key_algorithm(&attributes, alg);
9295 psa_set_key_type(&attributes, our_key_type);
9296 PSA_ASSERT(psa_import_key(&attributes,
9297 our_key_data->x, our_key_data->len,
9298 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009299
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9301 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009302
Gilles Peskine992bee82022-04-13 23:25:52 +02009303 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 TEST_LE_U(expected_output->len,
9305 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9306 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9307 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009308
9309 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 ASSERT_ALLOC(output, expected_output->len);
9311 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9312 peer_key_data->x, peer_key_data->len,
9313 output, expected_output->len,
9314 &output_length));
9315 ASSERT_COMPARE(output, output_length,
9316 expected_output->x, expected_output->len);
9317 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009318 output = NULL;
9319 output_length = ~0;
9320
9321 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 ASSERT_ALLOC(output, expected_output->len + 1);
9323 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9324 peer_key_data->x, peer_key_data->len,
9325 output, expected_output->len + 1,
9326 &output_length));
9327 ASSERT_COMPARE(output, output_length,
9328 expected_output->x, expected_output->len);
9329 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009330 output = NULL;
9331 output_length = ~0;
9332
9333 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009334 ASSERT_ALLOC(output, expected_output->len - 1);
9335 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9336 peer_key_data->x, peer_key_data->len,
9337 output, expected_output->len - 1,
9338 &output_length),
9339 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009340 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009341 TEST_LE_U(output_length, expected_output->len - 1);
9342 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009343 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009344
9345exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009346 mbedtls_free(output);
9347 psa_destroy_key(our_key);
9348 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009349}
9350/* END_CASE */
9351
9352/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009353void key_agreement_capacity(int alg_arg,
9354 int our_key_type_arg, data_t *our_key_data,
9355 data_t *peer_key_data,
9356 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009357{
Ronald Cron5425a212020-08-04 14:58:35 +02009358 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009359 psa_algorithm_t alg = alg_arg;
9360 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009361 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009363 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009364 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009365
Gilles Peskine449bd832023-01-11 14:50:10 +01009366 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009367
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9369 psa_set_key_algorithm(&attributes, alg);
9370 psa_set_key_type(&attributes, our_key_type);
9371 PSA_ASSERT(psa_import_key(&attributes,
9372 our_key_data->x, our_key_data->len,
9373 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009374
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9376 PSA_ASSERT(psa_key_derivation_key_agreement(
9377 &operation,
9378 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9379 peer_key_data->x, peer_key_data->len));
9380 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009381 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9383 PSA_KEY_DERIVATION_INPUT_INFO,
9384 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009385 }
Gilles Peskine59685592018-09-18 12:11:34 +02009386
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009387 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009388 PSA_ASSERT(psa_key_derivation_get_capacity(
9389 &operation, &actual_capacity));
9390 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009391
Gilles Peskinebf491972018-10-25 22:36:12 +02009392 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009393 while (actual_capacity > sizeof(output)) {
9394 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9395 output, sizeof(output)));
9396 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009397 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009398 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9399 output, actual_capacity));
9400 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9401 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009402
Gilles Peskine59685592018-09-18 12:11:34 +02009403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 psa_key_derivation_abort(&operation);
9405 psa_destroy_key(our_key);
9406 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009407}
9408/* END_CASE */
9409
9410/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009411void key_agreement_output(int alg_arg,
9412 int our_key_type_arg, data_t *our_key_data,
9413 data_t *peer_key_data,
9414 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009415{
Ronald Cron5425a212020-08-04 14:58:35 +02009416 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009417 psa_algorithm_t alg = alg_arg;
9418 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009419 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009421 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009422
Gilles Peskine449bd832023-01-11 14:50:10 +01009423 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9424 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009425
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9429 psa_set_key_algorithm(&attributes, alg);
9430 psa_set_key_type(&attributes, our_key_type);
9431 PSA_ASSERT(psa_import_key(&attributes,
9432 our_key_data->x, our_key_data->len,
9433 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009434
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9436 PSA_ASSERT(psa_key_derivation_key_agreement(
9437 &operation,
9438 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9439 peer_key_data->x, peer_key_data->len));
9440 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009441 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009442 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9443 PSA_KEY_DERIVATION_INPUT_INFO,
9444 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009445 }
Gilles Peskine59685592018-09-18 12:11:34 +02009446
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9448 actual_output,
9449 expected_output1->len));
9450 ASSERT_COMPARE(actual_output, expected_output1->len,
9451 expected_output1->x, expected_output1->len);
9452 if (expected_output2->len != 0) {
9453 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9454 actual_output,
9455 expected_output2->len));
9456 ASSERT_COMPARE(actual_output, expected_output2->len,
9457 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009458 }
Gilles Peskine59685592018-09-18 12:11:34 +02009459
9460exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009461 psa_key_derivation_abort(&operation);
9462 psa_destroy_key(our_key);
9463 PSA_DONE();
9464 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009465}
9466/* END_CASE */
9467
9468/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009469void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009470{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009471 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009472 unsigned char *output = NULL;
9473 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009474 size_t i;
9475 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009476
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009478
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 ASSERT_ALLOC(output, bytes);
9480 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009481
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009483
Gilles Peskinea50d7392018-06-21 10:22:13 +02009484 /* Run several times, to ensure that every output byte will be
9485 * nonzero at least once with overwhelming probability
9486 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009487 for (run = 0; run < 10; run++) {
9488 if (bytes != 0) {
9489 memset(output, 0, bytes);
9490 }
9491 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009492
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 for (i = 0; i < bytes; i++) {
9494 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009495 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009497 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009498 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009499
9500 /* Check that every byte was changed to nonzero at least once. This
9501 * validates that psa_generate_random is overwriting every byte of
9502 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 for (i = 0; i < bytes; i++) {
9504 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009505 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009506
9507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 PSA_DONE();
9509 mbedtls_free(output);
9510 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009511}
9512/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009513
9514/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009515void generate_key(int type_arg,
9516 int bits_arg,
9517 int usage_arg,
9518 int alg_arg,
9519 int expected_status_arg,
9520 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009521{
Ronald Cron5425a212020-08-04 14:58:35 +02009522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009523 psa_key_type_t type = type_arg;
9524 psa_key_usage_t usage = usage_arg;
9525 size_t bits = bits_arg;
9526 psa_algorithm_t alg = alg_arg;
9527 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009529 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009530
Gilles Peskine449bd832023-01-11 14:50:10 +01009531 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009532
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 psa_set_key_usage_flags(&attributes, usage);
9534 psa_set_key_algorithm(&attributes, alg);
9535 psa_set_key_type(&attributes, type);
9536 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009537
9538 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009540
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 if (is_large_key > 0) {
9542 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9543 }
9544 TEST_EQUAL(status, expected_status);
9545 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009546 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009548
9549 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9551 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9552 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009553
Gilles Peskine818ca122018-06-20 18:16:48 +02009554 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009558
9559exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009560 /*
9561 * Key attributes may have been returned by psa_get_key_attributes()
9562 * thus reset them as required.
9563 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 psa_destroy_key(key);
9567 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009568}
9569/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009570
Ronald Cronee414c72021-03-18 18:50:08 +01009571/* 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 +01009572void generate_key_rsa(int bits_arg,
9573 data_t *e_arg,
9574 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009575{
Ronald Cron5425a212020-08-04 14:58:35 +02009576 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009577 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009578 size_t bits = bits_arg;
9579 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9580 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9581 psa_status_t expected_status = expected_status_arg;
9582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9583 uint8_t *exported = NULL;
9584 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009586 size_t exported_length = SIZE_MAX;
9587 uint8_t *e_read_buffer = NULL;
9588 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009589 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009590 size_t e_read_length = SIZE_MAX;
9591
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 if (e_arg->len == 0 ||
9593 (e_arg->len == 3 &&
9594 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009595 is_default_public_exponent = 1;
9596 e_read_size = 0;
9597 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 ASSERT_ALLOC(e_read_buffer, e_read_size);
9599 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009600
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 psa_set_key_usage_flags(&attributes, usage);
9604 psa_set_key_algorithm(&attributes, alg);
9605 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9606 e_arg->x, e_arg->len));
9607 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009608
9609 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9611 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009612 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009614
9615 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9617 TEST_EQUAL(psa_get_key_type(&attributes), type);
9618 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9619 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9620 e_read_buffer, e_read_size,
9621 &e_read_length));
9622 if (is_default_public_exponent) {
9623 TEST_EQUAL(e_read_length, 0);
9624 } else {
9625 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9626 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009627
9628 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009629 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009632
9633 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 PSA_ASSERT(psa_export_public_key(key,
9635 exported, exported_size,
9636 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009637 {
9638 uint8_t *p = exported;
9639 uint8_t *end = exported + exported_length;
9640 size_t len;
9641 /* RSAPublicKey ::= SEQUENCE {
9642 * modulus INTEGER, -- n
9643 * publicExponent INTEGER } -- e
9644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9646 MBEDTLS_ASN1_SEQUENCE |
9647 MBEDTLS_ASN1_CONSTRUCTED));
9648 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9649 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9650 MBEDTLS_ASN1_INTEGER));
9651 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009652 ++p;
9653 --len;
9654 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 if (e_arg->len == 0) {
9656 TEST_EQUAL(len, 3);
9657 TEST_EQUAL(p[0], 1);
9658 TEST_EQUAL(p[1], 0);
9659 TEST_EQUAL(p[2], 1);
9660 } else {
9661 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009662 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009663 }
9664
9665exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009666 /*
9667 * Key attributes may have been returned by psa_get_key_attributes() or
9668 * set by psa_set_key_domain_parameters() thus reset them as required.
9669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009670 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 psa_destroy_key(key);
9673 PSA_DONE();
9674 mbedtls_free(e_read_buffer);
9675 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009676}
9677/* END_CASE */
9678
Darryl Greend49a4992018-06-18 17:27:26 +01009679/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009680void persistent_key_load_key_from_storage(data_t *data,
9681 int type_arg, int bits_arg,
9682 int usage_flags_arg, int alg_arg,
9683 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009684{
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9688 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009689 psa_key_type_t type = type_arg;
9690 size_t bits = bits_arg;
9691 psa_key_usage_t usage_flags = usage_flags_arg;
9692 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009693 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009694 unsigned char *first_export = NULL;
9695 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009696 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009697 size_t first_exported_length;
9698 size_t second_exported_length;
9699
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9701 ASSERT_ALLOC(first_export, export_size);
9702 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009703 }
Darryl Greend49a4992018-06-18 17:27:26 +01009704
Gilles Peskine449bd832023-01-11 14:50:10 +01009705 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009706
Gilles Peskine449bd832023-01-11 14:50:10 +01009707 psa_set_key_id(&attributes, key_id);
9708 psa_set_key_usage_flags(&attributes, usage_flags);
9709 psa_set_key_algorithm(&attributes, alg);
9710 psa_set_key_type(&attributes, type);
9711 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009712
Gilles Peskine449bd832023-01-11 14:50:10 +01009713 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009714 case IMPORT_KEY:
9715 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009716 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9717 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009718 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009719
Darryl Green0c6575a2018-11-07 16:05:30 +00009720 case GENERATE_KEY:
9721 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009722 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009723 break;
9724
9725 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009726#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009727 {
9728 /* Create base key */
9729 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9730 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9731 psa_set_key_usage_flags(&base_attributes,
9732 PSA_KEY_USAGE_DERIVE);
9733 psa_set_key_algorithm(&base_attributes, derive_alg);
9734 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9735 PSA_ASSERT(psa_import_key(&base_attributes,
9736 data->x, data->len,
9737 &base_key));
9738 /* Derive a key. */
9739 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9740 PSA_ASSERT(psa_key_derivation_input_key(
9741 &operation,
9742 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9743 PSA_ASSERT(psa_key_derivation_input_bytes(
9744 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9745 NULL, 0));
9746 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9747 &operation,
9748 &key));
9749 PSA_ASSERT(psa_key_derivation_abort(&operation));
9750 PSA_ASSERT(psa_destroy_key(base_key));
9751 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9752 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009753#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009754 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009755#endif
9756 break;
9757
9758 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009759 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009760 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009761 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009762 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009763
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009764 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009765 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9766 PSA_ASSERT(psa_export_key(key,
9767 first_export, export_size,
9768 &first_exported_length));
9769 if (generation_method == IMPORT_KEY) {
9770 ASSERT_COMPARE(data->x, data->len,
9771 first_export, first_exported_length);
9772 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009773 }
Darryl Greend49a4992018-06-18 17:27:26 +01009774
9775 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009776 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009777 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009779
Darryl Greend49a4992018-06-18 17:27:26 +01009780 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009781 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9782 TEST_ASSERT(mbedtls_svc_key_id_equal(
9783 psa_get_key_id(&attributes), key_id));
9784 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9785 PSA_KEY_LIFETIME_PERSISTENT);
9786 TEST_EQUAL(psa_get_key_type(&attributes), type);
9787 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9788 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9789 mbedtls_test_update_key_usage_flags(usage_flags));
9790 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009791
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009792 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009793 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9794 PSA_ASSERT(psa_export_key(key,
9795 second_export, export_size,
9796 &second_exported_length));
9797 ASSERT_COMPARE(first_export, first_exported_length,
9798 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009799 }
9800
9801 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009802 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009803 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009804 }
Darryl Greend49a4992018-06-18 17:27:26 +01009805
9806exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009807 /*
9808 * Key attributes may have been returned by psa_get_key_attributes()
9809 * thus reset them as required.
9810 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009811 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009812
Gilles Peskine449bd832023-01-11 14:50:10 +01009813 mbedtls_free(first_export);
9814 mbedtls_free(second_export);
9815 psa_key_derivation_abort(&operation);
9816 psa_destroy_key(base_key);
9817 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009818 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009819}
9820/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009821
Neil Armstronga557cb82022-06-10 08:58:32 +02009822/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009823void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9824 int primitive_arg, int hash_arg, int role_arg,
9825 int test_input, data_t *pw_data,
9826 int inj_err_type_arg,
9827 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009828{
9829 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9830 psa_pake_operation_t operation = psa_pake_operation_init();
9831 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009832 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009833 psa_key_type_t key_type_pw = key_type_pw_arg;
9834 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009835 psa_algorithm_t hash_alg = hash_arg;
9836 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9838 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009839 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9840 psa_status_t expected_error = expected_error_arg;
9841 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009842 unsigned char *output_buffer = NULL;
9843 size_t output_len = 0;
9844
Gilles Peskine449bd832023-01-11 14:50:10 +01009845 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009846
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009847 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009848 PSA_PAKE_STEP_KEY_SHARE);
9849 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009850
Gilles Peskine449bd832023-01-11 14:50:10 +01009851 if (pw_data->len > 0) {
9852 psa_set_key_usage_flags(&attributes, key_usage_pw);
9853 psa_set_key_algorithm(&attributes, alg);
9854 psa_set_key_type(&attributes, key_type_pw);
9855 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9856 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009857 }
9858
Gilles Peskine449bd832023-01-11 14:50:10 +01009859 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9860 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9861 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009862
Gilles Peskine449bd832023-01-11 14:50:10 +01009863 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009864
Gilles Peskine449bd832023-01-11 14:50:10 +01009865 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9866 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9867 expected_error);
9868 PSA_ASSERT(psa_pake_abort(&operation));
9869 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9870 expected_error);
9871 PSA_ASSERT(psa_pake_abort(&operation));
9872 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9873 expected_error);
9874 PSA_ASSERT(psa_pake_abort(&operation));
9875 TEST_EQUAL(psa_pake_set_role(&operation, role),
9876 expected_error);
9877 PSA_ASSERT(psa_pake_abort(&operation));
9878 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9879 NULL, 0, NULL),
9880 expected_error);
9881 PSA_ASSERT(psa_pake_abort(&operation));
9882 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9883 expected_error);
9884 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009885 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009886 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009887
Gilles Peskine449bd832023-01-11 14:50:10 +01009888 status = psa_pake_setup(&operation, &cipher_suite);
9889 if (status != PSA_SUCCESS) {
9890 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009891 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009892 }
9893
Gilles Peskine449bd832023-01-11 14:50:10 +01009894 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9895 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9896 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009897 goto exit;
9898 }
9899
Gilles Peskine449bd832023-01-11 14:50:10 +01009900 status = psa_pake_set_role(&operation, role);
9901 if (status != PSA_SUCCESS) {
9902 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009903 goto exit;
9904 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009905
Gilles Peskine449bd832023-01-11 14:50:10 +01009906 if (pw_data->len > 0) {
9907 status = psa_pake_set_password_key(&operation, key);
9908 if (status != PSA_SUCCESS) {
9909 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009910 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009911 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009912 }
9913
Gilles Peskine449bd832023-01-11 14:50:10 +01009914 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9915 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9916 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009917 goto exit;
9918 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009919
Gilles Peskine449bd832023-01-11 14:50:10 +01009920 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9921 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9922 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009923 goto exit;
9924 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009925
Gilles Peskine449bd832023-01-11 14:50:10 +01009926 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009927 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009928 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9929 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009930 goto exit;
9931 }
9932
Gilles Peskine449bd832023-01-11 14:50:10 +01009933 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009934 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009935 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9936 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009937 goto exit;
9938 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009939
Gilles Peskine449bd832023-01-11 14:50:10 +01009940 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9941 PSA_PAKE_STEP_KEY_SHARE);
9942 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9943 PSA_PAKE_STEP_ZK_PUBLIC);
9944 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9945 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009946
Gilles Peskine449bd832023-01-11 14:50:10 +01009947 if (test_input) {
9948 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9949 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9950 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009951 goto exit;
9952 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009953
Gilles Peskine449bd832023-01-11 14:50:10 +01009954 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9955 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9956 output_buffer, size_zk_proof),
9957 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009958 goto exit;
9959 }
9960
Gilles Peskine449bd832023-01-11 14:50:10 +01009961 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9962 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9963 output_buffer, size_zk_proof),
9964 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009965 goto exit;
9966 }
9967
Gilles Peskine449bd832023-01-11 14:50:10 +01009968 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9969 output_buffer, size_key_share);
9970 if (status != PSA_SUCCESS) {
9971 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009972 goto exit;
9973 }
9974
Gilles Peskine449bd832023-01-11 14:50:10 +01009975 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9976 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9977 output_buffer, size_zk_public + 1),
9978 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009979 goto exit;
9980 }
9981
Gilles Peskine449bd832023-01-11 14:50:10 +01009982 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009983 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009984 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9985 output_buffer, size_zk_public + 1);
9986 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9987 output_buffer, size_zk_public),
9988 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009989 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009990 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009991 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009992 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9993 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9994 NULL, 0, NULL),
9995 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009996 goto exit;
9997 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009998
Gilles Peskine449bd832023-01-11 14:50:10 +01009999 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10000 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10001 output_buffer, buf_size, &output_len),
10002 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010003 goto exit;
10004 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010005
Gilles Peskine449bd832023-01-11 14:50:10 +010010006 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10007 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10008 output_buffer, buf_size, &output_len),
10009 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010010 goto exit;
10011 }
10012
Gilles Peskine449bd832023-01-11 14:50:10 +010010013 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10014 output_buffer, buf_size, &output_len);
10015 if (status != PSA_SUCCESS) {
10016 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010017 goto exit;
10018 }
10019
Gilles Peskine449bd832023-01-11 14:50:10 +010010020 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010021
Gilles Peskine449bd832023-01-11 14:50:10 +010010022 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10023 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10024 output_buffer, size_zk_public - 1, &output_len),
10025 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010026 goto exit;
10027 }
10028
Gilles Peskine449bd832023-01-11 14:50:10 +010010029 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010030 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010031 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10032 output_buffer, size_zk_public - 1, &output_len);
10033 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10034 output_buffer, buf_size, &output_len),
10035 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010036 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010037 }
10038 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010039
10040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 PSA_ASSERT(psa_destroy_key(key));
10042 PSA_ASSERT(psa_pake_abort(&operation));
10043 mbedtls_free(output_buffer);
10044 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010045}
10046/* END_CASE */
10047
Neil Armstronga557cb82022-06-10 08:58:32 +020010048/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010049void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10050 int client_input_first, int inject_error,
10051 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010052{
10053 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10054 psa_pake_operation_t server = psa_pake_operation_init();
10055 psa_pake_operation_t client = psa_pake_operation_init();
10056 psa_algorithm_t alg = alg_arg;
10057 psa_algorithm_t hash_alg = hash_arg;
10058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10060
Gilles Peskine449bd832023-01-11 14:50:10 +010010061 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010062
Gilles Peskine449bd832023-01-11 14:50:10 +010010063 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10064 psa_set_key_algorithm(&attributes, alg);
10065 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10066 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10067 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010068
Gilles Peskine449bd832023-01-11 14:50:10 +010010069 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10070 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10071 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010072
10073
Gilles Peskine449bd832023-01-11 14:50:10 +010010074 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10075 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010076
Gilles Peskine449bd832023-01-11 14:50:10 +010010077 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10078 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010079
Gilles Peskine449bd832023-01-11 14:50:10 +010010080 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10081 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010082
Gilles Peskine449bd832023-01-11 14:50:10 +010010083 ecjpake_do_round(alg, primitive_arg, &server, &client,
10084 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010085
Gilles Peskine449bd832023-01-11 14:50:10 +010010086 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010087 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010088 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010089
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 ecjpake_do_round(alg, primitive_arg, &server, &client,
10091 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010092
10093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010094 psa_destroy_key(key);
10095 psa_pake_abort(&server);
10096 psa_pake_abort(&client);
10097 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010098}
10099/* END_CASE */
10100
10101/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010102void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10103 int derive_alg_arg, data_t *pw_data,
10104 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010105{
10106 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10107 psa_pake_operation_t server = psa_pake_operation_init();
10108 psa_pake_operation_t client = psa_pake_operation_init();
10109 psa_algorithm_t alg = alg_arg;
10110 psa_algorithm_t hash_alg = hash_arg;
10111 psa_algorithm_t derive_alg = derive_alg_arg;
10112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10114 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010115 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010116 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010117 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010118 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010119
Gilles Peskine449bd832023-01-11 14:50:10 +010010120 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010121
Gilles Peskine449bd832023-01-11 14:50:10 +010010122 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10123 psa_set_key_algorithm(&attributes, alg);
10124 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10125 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10126 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010127
Gilles Peskine449bd832023-01-11 14:50:10 +010010128 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10129 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10130 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010131
Neil Armstrong1e855602022-06-15 11:32:11 +020010132 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10134 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010135
Gilles Peskine449bd832023-01-11 14:50:10 +010010136 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10137 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10138 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10139 PSA_KEY_DERIVATION_INPUT_SEED,
10140 (const uint8_t *) "", 0));
10141 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10142 PSA_KEY_DERIVATION_INPUT_SEED,
10143 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010144 }
10145
Gilles Peskine449bd832023-01-11 14:50:10 +010010146 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10147 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010148
Gilles Peskine449bd832023-01-11 14:50:10 +010010149 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10150 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010151
Gilles Peskine449bd832023-01-11 14:50:10 +010010152 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10153 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010154
Gilles Peskine449bd832023-01-11 14:50:10 +010010155 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10156 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10157 PSA_ERROR_BAD_STATE);
10158 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10159 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010160 goto exit;
10161 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010162
Neil Armstrongf983caf2022-06-15 15:27:48 +020010163 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010164 ecjpake_do_round(alg, primitive_arg, &server, &client,
10165 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010166
Gilles Peskine449bd832023-01-11 14:50:10 +010010167 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10168 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10169 PSA_ERROR_BAD_STATE);
10170 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10171 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010172 goto exit;
10173 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010174
Neil Armstrongf983caf2022-06-15 15:27:48 +020010175 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010176 ecjpake_do_round(alg, primitive_arg, &server, &client,
10177 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010178
Gilles Peskine449bd832023-01-11 14:50:10 +010010179 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10180 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010181
10182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010183 psa_key_derivation_abort(&server_derive);
10184 psa_key_derivation_abort(&client_derive);
10185 psa_destroy_key(key);
10186 psa_pake_abort(&server);
10187 psa_pake_abort(&client);
10188 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010189}
10190/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010191
10192/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010193void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010194{
10195 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10196 const size_t bits = 256;
10197 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010198 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010199 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010200 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010201
10202 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10203 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010204 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10205 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10206 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10207 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010208 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010209 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10210 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010211
10212 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010213 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10214 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10215 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10216 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10217 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10218 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010219
10220 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010221 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10222 PSA_PAKE_OUTPUT_MAX_SIZE);
10223 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10224 PSA_PAKE_OUTPUT_MAX_SIZE);
10225 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10226 PSA_PAKE_OUTPUT_MAX_SIZE);
10227 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10228 PSA_PAKE_INPUT_MAX_SIZE);
10229 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10230 PSA_PAKE_INPUT_MAX_SIZE);
10231 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10232 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010233}
10234/* END_CASE */