blob: cd8a7b5fffa71f337683decd607384ec471fc06a [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
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001521
1522 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1524 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1525 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1526 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527
1528 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 status = psa_export_key(key, exported, export_size, &exported_length);
1530 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001531
1532 /* The exported length must be set by psa_export_key() to a value between 0
1533 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1535 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1536 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1539 export_size - exported_length));
1540 if (status != PSA_SUCCESS) {
1541 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001543 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544
Gilles Peskineea38a922021-02-13 00:05:16 +01001545 /* Run sanity checks on the exported key. For non-canonical inputs,
1546 * this validates the canonical representations. For canonical inputs,
1547 * this doesn't directly validate the implementation, but it still helps
1548 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if (!psa_key_lifetime_is_external(lifetime)) {
1550 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301551 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 }
Archana4d7ae1d2021-07-07 02:50:22 +05301553 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (canonical_input) {
1556 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1557 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001558 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1560 &key2));
1561 PSA_ASSERT(psa_export_key(key2,
1562 reexported,
1563 export_size,
1564 &reexported_length));
1565 ASSERT_COMPARE(exported, exported_length,
1566 reexported, reexported_length);
1567 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 TEST_LE_U(exported_length,
1570 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1571 psa_get_key_bits(&got_attributes)));
1572 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001573
1574destroy:
1575 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 PSA_ASSERT(psa_destroy_key(key));
1577 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001578
1579exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001580 /*
1581 * Key attributes may have been returned by psa_get_key_attributes()
1582 * thus reset them as required.
1583 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 psa_reset_key_attributes(&got_attributes);
1585 psa_destroy_key(key);
1586 mbedtls_free(exported);
1587 mbedtls_free(reexported);
1588 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001589}
1590/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001591
Moran Pekerf709f4a2018-06-06 17:26:04 +03001592/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001593void import_export_public_key(data_t *data,
1594 int type_arg, // key pair or public key
1595 int alg_arg,
1596 int lifetime_arg,
1597 int export_size_delta,
1598 int expected_export_status_arg,
1599 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001600{
Ronald Cron5425a212020-08-04 14:58:35 +02001601 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001602 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001603 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001604 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001605 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301606 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001607 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001608 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001609 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 psa_set_key_lifetime(&attributes, lifetime);
1615 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1616 psa_set_key_algorithm(&attributes, alg);
1617 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001618
1619 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001621
Gilles Peskine49c25912018-10-29 15:15:31 +01001622 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 ASSERT_ALLOC(exported, export_size);
1624 status = psa_export_public_key(key,
1625 exported, export_size,
1626 &exported_length);
1627 TEST_EQUAL(status, expected_export_status);
1628 if (status == PSA_SUCCESS) {
1629 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001630 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1632 bits = psa_get_key_bits(&attributes);
1633 TEST_LE_U(expected_public_key->len,
1634 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1635 TEST_LE_U(expected_public_key->len,
1636 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1637 TEST_LE_U(expected_public_key->len,
1638 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1639 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1640 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001641 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001642exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001643 /*
1644 * Key attributes may have been returned by psa_get_key_attributes()
1645 * thus reset them as required.
1646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 mbedtls_free(exported);
1650 psa_destroy_key(key);
1651 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001652}
1653/* END_CASE */
1654
Gilles Peskine20035e32018-02-03 22:44:14 +01001655/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001656void import_and_exercise_key(data_t *data,
1657 int type_arg,
1658 int bits_arg,
1659 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001660{
Ronald Cron5425a212020-08-04 14:58:35 +02001661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001662 psa_key_type_t type = type_arg;
1663 size_t bits = bits_arg;
1664 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001667 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 psa_set_key_usage_flags(&attributes, usage);
1672 psa_set_key_algorithm(&attributes, alg);
1673 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001674
1675 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677
1678 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1680 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1681 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001682
1683 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001685 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 PSA_ASSERT(psa_destroy_key(key));
1689 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001690
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001692 /*
1693 * Key attributes may have been returned by psa_get_key_attributes()
1694 * thus reset them as required.
1695 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 psa_reset_key_attributes(&attributes);
1699 psa_destroy_key(key);
1700 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001701}
1702/* END_CASE */
1703
1704/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001705void effective_key_attributes(int type_arg, int expected_type_arg,
1706 int bits_arg, int expected_bits_arg,
1707 int usage_arg, int expected_usage_arg,
1708 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001709{
Ronald Cron5425a212020-08-04 14:58:35 +02001710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001711 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001712 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001713 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001714 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001715 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001716 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001717 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001718 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 psa_set_key_usage_flags(&attributes, usage);
1724 psa_set_key_algorithm(&attributes, alg);
1725 psa_set_key_type(&attributes, key_type);
1726 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 PSA_ASSERT(psa_generate_key(&attributes, &key));
1729 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1732 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1733 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1734 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1735 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001736
1737exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001738 /*
1739 * Key attributes may have been returned by psa_get_key_attributes()
1740 * thus reset them as required.
1741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 psa_destroy_key(key);
1745 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001746}
1747/* END_CASE */
1748
1749/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750void check_key_policy(int type_arg, int bits_arg,
1751 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001752{
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1754 usage_arg,
1755 mbedtls_test_update_key_usage_flags(usage_arg),
1756 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001757 goto exit;
1758}
1759/* END_CASE */
1760
1761/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001762void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001763{
1764 /* Test each valid way of initializing the object, except for `= {0}`, as
1765 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1766 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001767 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001769 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1770 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1775 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1776 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 TEST_EQUAL(psa_get_key_type(&func), 0);
1779 TEST_EQUAL(psa_get_key_type(&init), 0);
1780 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 TEST_EQUAL(psa_get_key_bits(&func), 0);
1783 TEST_EQUAL(psa_get_key_bits(&init), 0);
1784 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1787 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1788 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1791 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1792 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001793}
1794/* END_CASE */
1795
1796/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797void mac_key_policy(int policy_usage_arg,
1798 int policy_alg_arg,
1799 int key_type_arg,
1800 data_t *key_data,
1801 int exercise_alg_arg,
1802 int expected_status_sign_arg,
1803 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001804{
Ronald Cron5425a212020-08-04 14:58:35 +02001805 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001807 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001808 psa_key_type_t key_type = key_type_arg;
1809 psa_algorithm_t policy_alg = policy_alg_arg;
1810 psa_algorithm_t exercise_alg = exercise_alg_arg;
1811 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001812 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001813 psa_status_t expected_status_sign = expected_status_sign_arg;
1814 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001815 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 psa_set_key_usage_flags(&attributes, policy_usage);
1820 psa_set_key_algorithm(&attributes, policy_alg);
1821 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1824 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1827 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1830 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001831
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001832 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001834 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1836 input, 128,
1837 mac, PSA_MAC_MAX_SIZE, &mac_len),
1838 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001839
Neil Armstrong3af9b972022-02-07 12:20:21 +01001840 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 PSA_ASSERT(psa_mac_abort(&operation));
1842 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1843 if (status == PSA_SUCCESS) {
1844 status = psa_mac_update(&operation, input, 128);
1845 if (status == PSA_SUCCESS) {
1846 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1847 &mac_len),
1848 expected_status_sign);
1849 } else {
1850 TEST_EQUAL(status, expected_status_sign);
1851 }
1852 } else {
1853 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001856
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001857 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 status = psa_mac_verify(key, exercise_alg, input, 128,
1859 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1862 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1863 } else {
1864 TEST_EQUAL(status, expected_status_verify);
1865 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001866
Neil Armstrong3af9b972022-02-07 12:20:21 +01001867 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1869 if (status == PSA_SUCCESS) {
1870 status = psa_mac_update(&operation, input, 128);
1871 if (status == PSA_SUCCESS) {
1872 status = psa_mac_verify_finish(&operation, mac, mac_len);
1873 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1874 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1875 } else {
1876 TEST_EQUAL(status, expected_status_verify);
1877 }
1878 } else {
1879 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001880 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 } else {
1882 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001883 }
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 memset(mac, 0, sizeof(mac));
1888 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1889 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890
1891exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 psa_mac_abort(&operation);
1893 psa_destroy_key(key);
1894 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895}
1896/* END_CASE */
1897
1898/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001899void cipher_key_policy(int policy_usage_arg,
1900 int policy_alg,
1901 int key_type,
1902 data_t *key_data,
1903 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904{
Ronald Cron5425a212020-08-04 14:58:35 +02001905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001907 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001908 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001909 size_t output_buffer_size = 0;
1910 size_t input_buffer_size = 0;
1911 size_t output_length = 0;
1912 uint8_t *output = NULL;
1913 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914 psa_status_t status;
1915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1917 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1918 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 ASSERT_ALLOC(input, input_buffer_size);
1921 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 psa_set_key_usage_flags(&attributes, policy_usage);
1926 psa_set_key_algorithm(&attributes, policy_alg);
1927 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1930 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001932 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 TEST_EQUAL(policy_usage,
1934 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001935
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001936 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1938 output, output_buffer_size,
1939 &output_length);
1940 if (policy_alg == exercise_alg &&
1941 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1942 PSA_ASSERT(status);
1943 } else {
1944 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1945 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001946
1947 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1949 if (policy_alg == exercise_alg &&
1950 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1951 PSA_ASSERT(status);
1952 } else {
1953 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1954 }
1955 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001957 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1959 input, input_buffer_size,
1960 &output_length);
1961 if (policy_alg == exercise_alg &&
1962 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1963 PSA_ASSERT(status);
1964 } else {
1965 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1966 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001967
1968 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1970 if (policy_alg == exercise_alg &&
1971 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1972 PSA_ASSERT(status);
1973 } else {
1974 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1975 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001976
1977exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 psa_cipher_abort(&operation);
1979 mbedtls_free(input);
1980 mbedtls_free(output);
1981 psa_destroy_key(key);
1982 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983}
1984/* END_CASE */
1985
1986/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987void aead_key_policy(int policy_usage_arg,
1988 int policy_alg,
1989 int key_type,
1990 data_t *key_data,
1991 int nonce_length_arg,
1992 int tag_length_arg,
1993 int exercise_alg,
1994 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995{
Ronald Cron5425a212020-08-04 14:58:35 +02001996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001998 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001999 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002001 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003 size_t nonce_length = nonce_length_arg;
2004 unsigned char tag[16];
2005 size_t tag_length = tag_length_arg;
2006 size_t output_length;
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 TEST_LE_U(nonce_length, sizeof(nonce));
2009 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 psa_set_key_usage_flags(&attributes, policy_usage);
2014 psa_set_key_algorithm(&attributes, policy_alg);
2015 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2018 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002020 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 TEST_EQUAL(policy_usage,
2022 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002023
Neil Armstrong752d8112022-02-07 14:51:11 +01002024 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 status = psa_aead_encrypt(key, exercise_alg,
2026 nonce, nonce_length,
2027 NULL, 0,
2028 NULL, 0,
2029 tag, tag_length,
2030 &output_length);
2031 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2032 TEST_EQUAL(status, expected_status);
2033 } else {
2034 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2035 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002036
Neil Armstrong752d8112022-02-07 14:51:11 +01002037 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2039 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2040 TEST_EQUAL(status, expected_status);
2041 } else {
2042 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2043 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002044
2045 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 memset(tag, 0, sizeof(tag));
2047 status = psa_aead_decrypt(key, exercise_alg,
2048 nonce, nonce_length,
2049 NULL, 0,
2050 tag, tag_length,
2051 NULL, 0,
2052 &output_length);
2053 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2054 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2055 } else if (expected_status == PSA_SUCCESS) {
2056 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2057 } else {
2058 TEST_EQUAL(status, expected_status);
2059 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002060
Neil Armstrong752d8112022-02-07 14:51:11 +01002061 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 PSA_ASSERT(psa_aead_abort(&operation));
2063 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2064 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2065 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2066 } else {
2067 TEST_EQUAL(status, expected_status);
2068 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002069
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 PSA_ASSERT(psa_aead_abort(&operation));
2072 psa_destroy_key(key);
2073 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002074}
2075/* END_CASE */
2076
2077/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078void asymmetric_encryption_key_policy(int policy_usage_arg,
2079 int policy_alg,
2080 int key_type,
2081 data_t *key_data,
2082 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083{
Ronald Cron5425a212020-08-04 14:58:35 +02002084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002086 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087 psa_status_t status;
2088 size_t key_bits;
2089 size_t buffer_length;
2090 unsigned char *buffer = NULL;
2091 size_t output_length;
2092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 psa_set_key_usage_flags(&attributes, policy_usage);
2096 psa_set_key_algorithm(&attributes, policy_alg);
2097 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2100 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002102 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 TEST_EQUAL(policy_usage,
2104 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2107 key_bits = psa_get_key_bits(&attributes);
2108 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2109 exercise_alg);
2110 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 status = psa_asymmetric_encrypt(key, exercise_alg,
2113 NULL, 0,
2114 NULL, 0,
2115 buffer, buffer_length,
2116 &output_length);
2117 if (policy_alg == exercise_alg &&
2118 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2119 PSA_ASSERT(status);
2120 } else {
2121 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2122 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (buffer_length != 0) {
2125 memset(buffer, 0, buffer_length);
2126 }
2127 status = psa_asymmetric_decrypt(key, exercise_alg,
2128 buffer, buffer_length,
2129 NULL, 0,
2130 buffer, buffer_length,
2131 &output_length);
2132 if (policy_alg == exercise_alg &&
2133 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2134 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2135 } else {
2136 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2137 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138
2139exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002140 /*
2141 * Key attributes may have been returned by psa_get_key_attributes()
2142 * thus reset them as required.
2143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 psa_destroy_key(key);
2147 PSA_DONE();
2148 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002149}
2150/* END_CASE */
2151
2152/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153void asymmetric_signature_key_policy(int policy_usage_arg,
2154 int policy_alg,
2155 int key_type,
2156 data_t *key_data,
2157 int exercise_alg,
2158 int payload_length_arg,
2159 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002160{
Ronald Cron5425a212020-08-04 14:58:35 +02002161 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002163 psa_key_usage_t policy_usage = policy_usage_arg;
2164 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002167 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2168 * compatible with the policy and `payload_length_arg` is supposed to be
2169 * a valid input length to sign. If `payload_length_arg <= 0`,
2170 * `exercise_alg` is supposed to be forbidden by the policy. */
2171 int compatible_alg = payload_length_arg > 0;
2172 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174 size_t signature_length;
2175
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002176 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002177 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 TEST_EQUAL(expected_usage,
2179 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 psa_set_key_usage_flags(&attributes, policy_usage);
2184 psa_set_key_algorithm(&attributes, policy_alg);
2185 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2188 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 status = psa_sign_hash(key, exercise_alg,
2193 payload, payload_length,
2194 signature, sizeof(signature),
2195 &signature_length);
2196 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2197 PSA_ASSERT(status);
2198 } else {
2199 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2200 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 memset(signature, 0, sizeof(signature));
2203 status = psa_verify_hash(key, exercise_alg,
2204 payload, payload_length,
2205 signature, sizeof(signature));
2206 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2207 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2208 } else {
2209 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2210 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2213 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2214 status = psa_sign_message(key, exercise_alg,
2215 payload, payload_length,
2216 signature, sizeof(signature),
2217 &signature_length);
2218 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2219 PSA_ASSERT(status);
2220 } else {
2221 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2222 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 memset(signature, 0, sizeof(signature));
2225 status = psa_verify_message(key, exercise_alg,
2226 payload, payload_length,
2227 signature, sizeof(signature));
2228 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2229 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2230 } else {
2231 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2232 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002233 }
2234
Gilles Peskined5b33222018-06-18 22:20:03 +02002235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 psa_destroy_key(key);
2237 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002238}
2239/* END_CASE */
2240
Janos Follathba3fab92019-06-11 14:50:16 +01002241/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002242void derive_key_policy(int policy_usage,
2243 int policy_alg,
2244 int key_type,
2245 data_t *key_data,
2246 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002247{
Ronald Cron5425a212020-08-04 14:58:35 +02002248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002250 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002251 psa_status_t status;
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 psa_set_key_usage_flags(&attributes, policy_usage);
2256 psa_set_key_algorithm(&attributes, policy_alg);
2257 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2260 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2265 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2266 PSA_ASSERT(psa_key_derivation_input_bytes(
2267 &operation,
2268 PSA_KEY_DERIVATION_INPUT_SEED,
2269 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002270 }
Janos Follathba3fab92019-06-11 14:50:16 +01002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 status = psa_key_derivation_input_key(&operation,
2273 PSA_KEY_DERIVATION_INPUT_SECRET,
2274 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (policy_alg == exercise_alg &&
2277 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2278 PSA_ASSERT(status);
2279 } else {
2280 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2281 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002282
2283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 psa_key_derivation_abort(&operation);
2285 psa_destroy_key(key);
2286 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002287}
2288/* END_CASE */
2289
2290/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002291void agreement_key_policy(int policy_usage,
2292 int policy_alg,
2293 int key_type_arg,
2294 data_t *key_data,
2295 int exercise_alg,
2296 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002297{
Ronald Cron5425a212020-08-04 14:58:35 +02002298 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002301 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002302 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002303 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 psa_set_key_usage_flags(&attributes, policy_usage);
2308 psa_set_key_algorithm(&attributes, policy_alg);
2309 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2312 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2315 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002318
2319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 psa_key_derivation_abort(&operation);
2321 psa_destroy_key(key);
2322 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002323}
2324/* END_CASE */
2325
2326/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327void key_policy_alg2(int key_type_arg, data_t *key_data,
2328 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002329{
Ronald Cron5425a212020-08-04 14:58:35 +02002330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002331 psa_key_type_t key_type = key_type_arg;
2332 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2333 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2334 psa_key_usage_t usage = usage_arg;
2335 psa_algorithm_t alg = alg_arg;
2336 psa_algorithm_t alg2 = alg2_arg;
2337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 psa_set_key_usage_flags(&attributes, usage);
2341 psa_set_key_algorithm(&attributes, alg);
2342 psa_set_key_enrollment_algorithm(&attributes, alg2);
2343 psa_set_key_type(&attributes, key_type);
2344 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2345 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002346
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002347 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 usage = mbedtls_test_update_key_usage_flags(usage);
2349 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2350 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2351 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2352 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 }
2357 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002360
2361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002362 /*
2363 * Key attributes may have been returned by psa_get_key_attributes()
2364 * thus reset them as required.
2365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 psa_destroy_key(key);
2369 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002370}
2371/* END_CASE */
2372
2373/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374void raw_agreement_key_policy(int policy_usage,
2375 int policy_alg,
2376 int key_type_arg,
2377 data_t *key_data,
2378 int exercise_alg,
2379 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002380{
Ronald Cron5425a212020-08-04 14:58:35 +02002381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002384 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002385 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002386 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 psa_set_key_usage_flags(&attributes, policy_usage);
2391 psa_set_key_algorithm(&attributes, policy_alg);
2392 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2395 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002400
2401exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 psa_key_derivation_abort(&operation);
2403 psa_destroy_key(key);
2404 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002405}
2406/* END_CASE */
2407
2408/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002409void copy_success(int source_usage_arg,
2410 int source_alg_arg, int source_alg2_arg,
2411 unsigned int source_lifetime_arg,
2412 int type_arg, data_t *material,
2413 int copy_attributes,
2414 int target_usage_arg,
2415 int target_alg_arg, int target_alg2_arg,
2416 unsigned int target_lifetime_arg,
2417 int expected_usage_arg,
2418 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419{
Gilles Peskineca25db92019-04-19 11:43:08 +02002420 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2421 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002422 psa_key_usage_t expected_usage = expected_usage_arg;
2423 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002424 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302425 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2426 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002427 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2428 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002429 uint8_t *export_buffer = NULL;
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002432
Gilles Peskineca25db92019-04-19 11:43:08 +02002433 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2435 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2436 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2437 psa_set_key_type(&source_attributes, type_arg);
2438 psa_set_key_lifetime(&source_attributes, source_lifetime);
2439 PSA_ASSERT(psa_import_key(&source_attributes,
2440 material->x, material->len,
2441 &source_key));
2442 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443
Gilles Peskineca25db92019-04-19 11:43:08 +02002444 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002446 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002447 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (target_usage_arg != -1) {
2451 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2452 }
2453 if (target_alg_arg != -1) {
2454 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2455 }
2456 if (target_alg2_arg != -1) {
2457 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2458 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459
Archana8a180362021-07-05 02:18:48 +05302460
Gilles Peskine57ab7212019-01-28 13:03:09 +01002461 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 PSA_ASSERT(psa_copy_key(source_key,
2463 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464
2465 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002467
2468 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2470 TEST_EQUAL(psa_get_key_type(&source_attributes),
2471 psa_get_key_type(&target_attributes));
2472 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2473 psa_get_key_bits(&target_attributes));
2474 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2475 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2476 TEST_EQUAL(expected_alg2,
2477 psa_get_key_enrollment_algorithm(&target_attributes));
2478 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002479 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 ASSERT_ALLOC(export_buffer, material->len);
2481 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2482 material->len, &length));
2483 ASSERT_COMPARE(material->x, material->len,
2484 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002485 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (!psa_key_lifetime_is_external(target_lifetime)) {
2488 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302489 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 }
2491 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302492 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 }
Archana8a180362021-07-05 02:18:48 +05302494 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497
2498exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002499 /*
2500 * Source and target key attributes may have been returned by
2501 * psa_get_key_attributes() thus reset them as required.
2502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 psa_reset_key_attributes(&source_attributes);
2504 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 PSA_DONE();
2507 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002508}
2509/* END_CASE */
2510
2511/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002512void copy_fail(int source_usage_arg,
2513 int source_alg_arg, int source_alg2_arg,
2514 int source_lifetime_arg,
2515 int type_arg, data_t *material,
2516 int target_type_arg, int target_bits_arg,
2517 int target_usage_arg,
2518 int target_alg_arg, int target_alg2_arg,
2519 int target_id_arg, int target_lifetime_arg,
2520 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002521{
2522 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2523 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002524 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2525 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002529
2530 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2532 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2533 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2534 psa_set_key_type(&source_attributes, type_arg);
2535 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2536 PSA_ASSERT(psa_import_key(&source_attributes,
2537 material->x, material->len,
2538 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002539
2540 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 psa_set_key_id(&target_attributes, key_id);
2542 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2543 psa_set_key_type(&target_attributes, target_type_arg);
2544 psa_set_key_bits(&target_attributes, target_bits_arg);
2545 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2546 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2547 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002548
2549 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 TEST_EQUAL(psa_copy_key(source_key,
2551 &target_attributes, &target_key),
2552 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002555
Gilles Peskine4a644642019-05-03 17:14:08 +02002556exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 psa_reset_key_attributes(&source_attributes);
2558 psa_reset_key_attributes(&target_attributes);
2559 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002560}
2561/* END_CASE */
2562
2563/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002564void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002565{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002566 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002567 /* Test each valid way of initializing the object, except for `= {0}`, as
2568 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2569 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002570 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002572 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2573 psa_hash_operation_t zero;
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002576
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002577 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2579 PSA_ERROR_BAD_STATE);
2580 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2581 PSA_ERROR_BAD_STATE);
2582 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2583 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002584
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002585 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 PSA_ASSERT(psa_hash_abort(&func));
2587 PSA_ASSERT(psa_hash_abort(&init));
2588 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002589}
2590/* END_CASE */
2591
2592/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593void hash_setup(int alg_arg,
2594 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002595{
2596 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002597 uint8_t *output = NULL;
2598 size_t output_size = 0;
2599 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002600 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002601 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002602 psa_status_t status;
2603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002605
Neil Armstrongedb20862022-02-07 15:47:44 +01002606 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 output_size = PSA_HASH_LENGTH(alg);
2608 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 status = psa_hash_compute(alg, NULL, 0,
2611 output, output_size, &output_length);
2612 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002613
2614 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 status = psa_hash_setup(&operation, alg);
2616 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002617
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002618 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002620
2621 /* If setup failed, reproduce the failure, so as to
2622 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (status != PSA_SUCCESS) {
2624 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2625 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002626
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002627 /* Now the operation object should be reusable. */
2628#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2630 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002631#endif
2632
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002633exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 mbedtls_free(output);
2635 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636}
2637/* END_CASE */
2638
2639/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640void hash_compute_fail(int alg_arg, data_t *input,
2641 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002642{
2643 psa_algorithm_t alg = alg_arg;
2644 uint8_t *output = NULL;
2645 size_t output_size = output_size_arg;
2646 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002647 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002648 psa_status_t expected_status = expected_status_arg;
2649 psa_status_t status;
2650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002654
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002655 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 status = psa_hash_compute(alg, input->x, input->len,
2657 output, output_size, &output_length);
2658 TEST_EQUAL(status, expected_status);
2659 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002660
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002661 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 status = psa_hash_setup(&operation, alg);
2663 if (status == PSA_SUCCESS) {
2664 status = psa_hash_update(&operation, input->x, input->len);
2665 if (status == PSA_SUCCESS) {
2666 status = psa_hash_finish(&operation, output, output_size,
2667 &output_length);
2668 if (status == PSA_SUCCESS) {
2669 TEST_LE_U(output_length, output_size);
2670 } else {
2671 TEST_EQUAL(status, expected_status);
2672 }
2673 } else {
2674 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002675 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 } else {
2677 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002678 }
2679
Gilles Peskine0a749c82019-11-28 19:33:58 +01002680exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 PSA_ASSERT(psa_hash_abort(&operation));
2682 mbedtls_free(output);
2683 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002684}
2685/* END_CASE */
2686
2687/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688void hash_compare_fail(int alg_arg, data_t *input,
2689 data_t *reference_hash,
2690 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002691{
2692 psa_algorithm_t alg = alg_arg;
2693 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002694 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002695 psa_status_t status;
2696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002698
Neil Armstrong55a1be12022-02-07 11:23:20 +01002699 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 status = psa_hash_compare(alg, input->x, input->len,
2701 reference_hash->x, reference_hash->len);
2702 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002703
Neil Armstrong55a1be12022-02-07 11:23:20 +01002704 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 status = psa_hash_setup(&operation, alg);
2706 if (status == PSA_SUCCESS) {
2707 status = psa_hash_update(&operation, input->x, input->len);
2708 if (status == PSA_SUCCESS) {
2709 status = psa_hash_verify(&operation, reference_hash->x,
2710 reference_hash->len);
2711 TEST_EQUAL(status, expected_status);
2712 } else {
2713 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 } else {
2716 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002717 }
2718
Gilles Peskine88e08462020-01-28 20:43:00 +01002719exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 PSA_ASSERT(psa_hash_abort(&operation));
2721 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002722}
2723/* END_CASE */
2724
2725/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002726void hash_compute_compare(int alg_arg, data_t *input,
2727 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002728{
2729 psa_algorithm_t alg = alg_arg;
2730 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2731 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002732 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002733 size_t i;
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002736
Neil Armstrongca30a002022-02-07 11:40:23 +01002737 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2739 output, PSA_HASH_LENGTH(alg),
2740 &output_length));
2741 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2742 ASSERT_COMPARE(output, output_length,
2743 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002744
Neil Armstrongca30a002022-02-07 11:40:23 +01002745 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 PSA_ASSERT(psa_hash_setup(&operation, alg));
2747 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2748 PSA_ASSERT(psa_hash_finish(&operation, output,
2749 PSA_HASH_LENGTH(alg),
2750 &output_length));
2751 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2752 ASSERT_COMPARE(output, output_length,
2753 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002754
2755 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2757 output, sizeof(output),
2758 &output_length));
2759 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2760 ASSERT_COMPARE(output, output_length,
2761 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762
Neil Armstrongca30a002022-02-07 11:40:23 +01002763 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 PSA_ASSERT(psa_hash_setup(&operation, alg));
2765 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2766 PSA_ASSERT(psa_hash_finish(&operation, output,
2767 sizeof(output), &output_length));
2768 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2769 ASSERT_COMPARE(output, output_length,
2770 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002771
2772 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2774 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002775
Neil Armstrongca30a002022-02-07 11:40:23 +01002776 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 PSA_ASSERT(psa_hash_setup(&operation, alg));
2778 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2779 PSA_ASSERT(psa_hash_verify(&operation, output,
2780 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002781
2782 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2784 output, output_length + 1),
2785 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002786
Neil Armstrongca30a002022-02-07 11:40:23 +01002787 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 PSA_ASSERT(psa_hash_setup(&operation, alg));
2789 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2790 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2791 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002792
2793 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2795 output, output_length - 1),
2796 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002797
Neil Armstrongca30a002022-02-07 11:40:23 +01002798 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 PSA_ASSERT(psa_hash_setup(&operation, alg));
2800 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2801 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2802 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002803
Gilles Peskine0a749c82019-11-28 19:33:58 +01002804 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 for (i = 0; i < output_length; i++) {
2806 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002807 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002808
2809 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2811 output, output_length),
2812 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002813
2814 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 PSA_ASSERT(psa_hash_setup(&operation, alg));
2816 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2817 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2818 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002819
Gilles Peskine0a749c82019-11-28 19:33:58 +01002820 output[i] ^= 1;
2821 }
2822
2823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 PSA_ASSERT(psa_hash_abort(&operation));
2825 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002826}
2827/* END_CASE */
2828
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002829/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002830void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002831{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002832 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002833 unsigned char input[] = "";
2834 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002835 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002836 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2837 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2839 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002840 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002841 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002842 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002845
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002846 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 PSA_ASSERT(psa_hash_setup(&operation, alg));
2848 ASSERT_OPERATION_IS_ACTIVE(operation);
2849 TEST_EQUAL(psa_hash_setup(&operation, alg),
2850 PSA_ERROR_BAD_STATE);
2851 ASSERT_OPERATION_IS_INACTIVE(operation);
2852 PSA_ASSERT(psa_hash_abort(&operation));
2853 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002854
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002855 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2857 PSA_ERROR_BAD_STATE);
2858 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002859
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002860 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002862 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 ASSERT_OPERATION_IS_ACTIVE(operation);
2864 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2865 PSA_ERROR_BAD_STATE);
2866 ASSERT_OPERATION_IS_INACTIVE(operation);
2867 PSA_ASSERT(psa_hash_abort(&operation));
2868 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002869
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002870 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 PSA_ASSERT(psa_hash_setup(&operation, alg));
2872 PSA_ASSERT(psa_hash_finish(&operation,
2873 hash, sizeof(hash), &hash_len));
2874 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2875 PSA_ERROR_BAD_STATE);
2876 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002877
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002878 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 TEST_EQUAL(psa_hash_verify(&operation,
2880 valid_hash, sizeof(valid_hash)),
2881 PSA_ERROR_BAD_STATE);
2882 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002883
2884 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 PSA_ASSERT(psa_hash_setup(&operation, alg));
2886 PSA_ASSERT(psa_hash_finish(&operation,
2887 hash, sizeof(hash), &hash_len));
2888 TEST_EQUAL(psa_hash_verify(&operation,
2889 valid_hash, sizeof(valid_hash)),
2890 PSA_ERROR_BAD_STATE);
2891 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002892
2893 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 PSA_ASSERT(psa_hash_setup(&operation, alg));
2895 ASSERT_OPERATION_IS_ACTIVE(operation);
2896 PSA_ASSERT(psa_hash_verify(&operation,
2897 valid_hash, sizeof(valid_hash)));
2898 ASSERT_OPERATION_IS_INACTIVE(operation);
2899 TEST_EQUAL(psa_hash_verify(&operation,
2900 valid_hash, sizeof(valid_hash)),
2901 PSA_ERROR_BAD_STATE);
2902 ASSERT_OPERATION_IS_INACTIVE(operation);
2903 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002904
2905 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 TEST_EQUAL(psa_hash_finish(&operation,
2907 hash, sizeof(hash), &hash_len),
2908 PSA_ERROR_BAD_STATE);
2909 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002910
2911 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 PSA_ASSERT(psa_hash_setup(&operation, alg));
2913 PSA_ASSERT(psa_hash_finish(&operation,
2914 hash, sizeof(hash), &hash_len));
2915 TEST_EQUAL(psa_hash_finish(&operation,
2916 hash, sizeof(hash), &hash_len),
2917 PSA_ERROR_BAD_STATE);
2918 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002919
2920 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 PSA_ASSERT(psa_hash_setup(&operation, alg));
2922 PSA_ASSERT(psa_hash_verify(&operation,
2923 valid_hash, sizeof(valid_hash)));
2924 TEST_EQUAL(psa_hash_finish(&operation,
2925 hash, sizeof(hash), &hash_len),
2926 PSA_ERROR_BAD_STATE);
2927 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002928
2929exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002931}
2932/* END_CASE */
2933
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002934/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002936{
2937 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002938 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2939 * appended to it */
2940 unsigned char hash[] = {
2941 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2942 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2944 };
2945 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002946 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002949
itayzafrir27e69452018-11-01 14:26:34 +02002950 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 PSA_ASSERT(psa_hash_setup(&operation, alg));
2952 ASSERT_OPERATION_IS_ACTIVE(operation);
2953 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2954 PSA_ERROR_INVALID_SIGNATURE);
2955 ASSERT_OPERATION_IS_INACTIVE(operation);
2956 PSA_ASSERT(psa_hash_abort(&operation));
2957 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002958
itayzafrir27e69452018-11-01 14:26:34 +02002959 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 PSA_ASSERT(psa_hash_setup(&operation, alg));
2961 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2962 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002963
itayzafrir27e69452018-11-01 14:26:34 +02002964 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 PSA_ASSERT(psa_hash_setup(&operation, alg));
2966 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2967 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002968
itayzafrirec93d302018-10-18 18:01:10 +03002969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002971}
2972/* END_CASE */
2973
Ronald Cronee414c72021-03-18 18:50:08 +01002974/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002975void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002976{
2977 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002978 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002980 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002981 size_t hash_len;
2982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002984
itayzafrir58028322018-10-25 10:22:01 +03002985 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 PSA_ASSERT(psa_hash_setup(&operation, alg));
2987 TEST_EQUAL(psa_hash_finish(&operation,
2988 hash, expected_size - 1, &hash_len),
2989 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002990
2991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002993}
2994/* END_CASE */
2995
Ronald Cronee414c72021-03-18 18:50:08 +01002996/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002997void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002998{
2999 psa_algorithm_t alg = PSA_ALG_SHA_256;
3000 unsigned char hash[PSA_HASH_MAX_SIZE];
3001 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3002 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3003 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3004 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3005 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3006 size_t hash_len;
3007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 PSA_ASSERT(psa_crypto_init());
3009 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3012 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3013 PSA_ASSERT(psa_hash_finish(&op_finished,
3014 hash, sizeof(hash), &hash_len));
3015 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3016 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3019 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3022 PSA_ASSERT(psa_hash_finish(&op_init,
3023 hash, sizeof(hash), &hash_len));
3024 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3025 PSA_ASSERT(psa_hash_finish(&op_finished,
3026 hash, sizeof(hash), &hash_len));
3027 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3028 PSA_ASSERT(psa_hash_finish(&op_aborted,
3029 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003030
3031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 psa_hash_abort(&op_source);
3033 psa_hash_abort(&op_init);
3034 psa_hash_abort(&op_setup);
3035 psa_hash_abort(&op_finished);
3036 psa_hash_abort(&op_aborted);
3037 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003038}
3039/* END_CASE */
3040
Ronald Cronee414c72021-03-18 18:50:08 +01003041/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003042void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003043{
3044 psa_algorithm_t alg = PSA_ALG_SHA_256;
3045 unsigned char hash[PSA_HASH_MAX_SIZE];
3046 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3047 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3048 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3049 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3050 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3051 size_t hash_len;
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3056 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3057 PSA_ASSERT(psa_hash_finish(&op_finished,
3058 hash, sizeof(hash), &hash_len));
3059 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3060 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3063 PSA_ASSERT(psa_hash_finish(&op_target,
3064 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3067 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3068 PSA_ERROR_BAD_STATE);
3069 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3070 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003071
3072exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 psa_hash_abort(&op_target);
3074 psa_hash_abort(&op_init);
3075 psa_hash_abort(&op_setup);
3076 psa_hash_abort(&op_finished);
3077 psa_hash_abort(&op_aborted);
3078 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003079}
3080/* END_CASE */
3081
itayzafrir58028322018-10-25 10:22:01 +03003082/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003083void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003084{
Jaeden Amero252ef282019-02-15 14:05:35 +00003085 const uint8_t input[1] = { 0 };
3086
Jaeden Amero769ce272019-01-04 11:48:03 +00003087 /* Test each valid way of initializing the object, except for `= {0}`, as
3088 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3089 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003090 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003092 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3093 psa_mac_operation_t zero;
3094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003096
Jaeden Amero252ef282019-02-15 14:05:35 +00003097 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 TEST_EQUAL(psa_mac_update(&func,
3099 input, sizeof(input)),
3100 PSA_ERROR_BAD_STATE);
3101 TEST_EQUAL(psa_mac_update(&init,
3102 input, sizeof(input)),
3103 PSA_ERROR_BAD_STATE);
3104 TEST_EQUAL(psa_mac_update(&zero,
3105 input, sizeof(input)),
3106 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003107
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003108 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 PSA_ASSERT(psa_mac_abort(&func));
3110 PSA_ASSERT(psa_mac_abort(&init));
3111 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003112}
3113/* END_CASE */
3114
3115/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116void mac_setup(int key_type_arg,
3117 data_t *key,
3118 int alg_arg,
3119 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003120{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003121 psa_key_type_t key_type = key_type_arg;
3122 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003123 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003124 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003125 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3126#if defined(KNOWN_SUPPORTED_MAC_ALG)
3127 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3128#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3133 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 }
3136 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003137
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003138 /* The operation object should be reusable. */
3139#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3141 smoke_test_key_data,
3142 sizeof(smoke_test_key_data),
3143 KNOWN_SUPPORTED_MAC_ALG,
3144 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 }
3147 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003148#endif
3149
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003152}
3153/* END_CASE */
3154
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003155/* 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 +01003156void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003157{
Ronald Cron5425a212020-08-04 14:58:35 +02003158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003159 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3160 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003161 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003162 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3163 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3165 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003167 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3168 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3169 size_t sign_mac_length = 0;
3170 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3171 const uint8_t verify_mac[] = {
3172 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3173 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3175 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003176
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 PSA_ASSERT(psa_crypto_init());
3178 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3179 psa_set_key_algorithm(&attributes, alg);
3180 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3183 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003184
Jaeden Amero252ef282019-02-15 14:05:35 +00003185 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3187 PSA_ERROR_BAD_STATE);
3188 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003189
3190 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3192 &sign_mac_length),
3193 PSA_ERROR_BAD_STATE);
3194 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003195
3196 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 TEST_EQUAL(psa_mac_verify_finish(&operation,
3198 verify_mac, sizeof(verify_mac)),
3199 PSA_ERROR_BAD_STATE);
3200 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003201
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003202 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3204 ASSERT_OPERATION_IS_ACTIVE(operation);
3205 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3206 PSA_ERROR_BAD_STATE);
3207 ASSERT_OPERATION_IS_INACTIVE(operation);
3208 PSA_ASSERT(psa_mac_abort(&operation));
3209 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003210
Jaeden Amero252ef282019-02-15 14:05:35 +00003211 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3213 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3214 PSA_ASSERT(psa_mac_sign_finish(&operation,
3215 sign_mac, sizeof(sign_mac),
3216 &sign_mac_length));
3217 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3218 PSA_ERROR_BAD_STATE);
3219 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003220
3221 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3223 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3224 PSA_ASSERT(psa_mac_verify_finish(&operation,
3225 verify_mac, sizeof(verify_mac)));
3226 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3227 PSA_ERROR_BAD_STATE);
3228 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003229
3230 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3232 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3233 PSA_ASSERT(psa_mac_sign_finish(&operation,
3234 sign_mac, sizeof(sign_mac),
3235 &sign_mac_length));
3236 TEST_EQUAL(psa_mac_sign_finish(&operation,
3237 sign_mac, sizeof(sign_mac),
3238 &sign_mac_length),
3239 PSA_ERROR_BAD_STATE);
3240 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003241
3242 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3244 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3245 PSA_ASSERT(psa_mac_verify_finish(&operation,
3246 verify_mac, sizeof(verify_mac)));
3247 TEST_EQUAL(psa_mac_verify_finish(&operation,
3248 verify_mac, sizeof(verify_mac)),
3249 PSA_ERROR_BAD_STATE);
3250 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003251
3252 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3254 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3255 ASSERT_OPERATION_IS_ACTIVE(operation);
3256 TEST_EQUAL(psa_mac_verify_finish(&operation,
3257 verify_mac, sizeof(verify_mac)),
3258 PSA_ERROR_BAD_STATE);
3259 ASSERT_OPERATION_IS_INACTIVE(operation);
3260 PSA_ASSERT(psa_mac_abort(&operation));
3261 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003262
3263 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3265 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3266 ASSERT_OPERATION_IS_ACTIVE(operation);
3267 TEST_EQUAL(psa_mac_sign_finish(&operation,
3268 sign_mac, sizeof(sign_mac),
3269 &sign_mac_length),
3270 PSA_ERROR_BAD_STATE);
3271 ASSERT_OPERATION_IS_INACTIVE(operation);
3272 PSA_ASSERT(psa_mac_abort(&operation));
3273 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003276
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003279}
3280/* END_CASE */
3281
3282/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003283void mac_sign_verify_multi(int key_type_arg,
3284 data_t *key_data,
3285 int alg_arg,
3286 data_t *input,
3287 int is_verify,
3288 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003289{
3290 size_t data_part_len = 0;
3291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003293 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (mac_multipart_internal_func(key_type_arg, key_data,
3297 alg_arg,
3298 input, data_part_len,
3299 expected_mac,
3300 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003301 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003303
3304 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 if (mac_multipart_internal_func(key_type_arg, key_data,
3308 alg_arg,
3309 input, data_part_len,
3310 expected_mac,
3311 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003312 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003314 }
3315
3316 /* Goto is required to silence warnings about unused labels, as we
3317 * don't actually do any test assertions in this function. */
3318 goto exit;
3319}
3320/* END_CASE */
3321
3322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323void mac_sign(int key_type_arg,
3324 data_t *key_data,
3325 int alg_arg,
3326 data_t *input,
3327 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003328{
Ronald Cron5425a212020-08-04 14:58:35 +02003329 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003330 psa_key_type_t key_type = key_type_arg;
3331 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003332 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003334 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003335 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003337 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003338 const size_t output_sizes_to_test[] = {
3339 0,
3340 1,
3341 expected_mac->len - 1,
3342 expected_mac->len,
3343 expected_mac->len + 1,
3344 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003347 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003351
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3353 psa_set_key_algorithm(&attributes, alg);
3354 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3357 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003360 const size_t output_size = output_sizes_to_test[i];
3361 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 (output_size >= expected_mac->len ? PSA_SUCCESS :
3363 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 mbedtls_test_set_step(output_size);
3366 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003367
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003368 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 TEST_EQUAL(psa_mac_compute(key, alg,
3370 input->x, input->len,
3371 actual_mac, output_size, &mac_length),
3372 expected_status);
3373 if (expected_status == PSA_SUCCESS) {
3374 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3375 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003376 }
3377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 if (output_size > 0) {
3379 memset(actual_mac, 0, output_size);
3380 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003381
3382 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3384 PSA_ASSERT(psa_mac_update(&operation,
3385 input->x, input->len));
3386 TEST_EQUAL(psa_mac_sign_finish(&operation,
3387 actual_mac, output_size,
3388 &mac_length),
3389 expected_status);
3390 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 if (expected_status == PSA_SUCCESS) {
3393 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3394 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003395 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003397 actual_mac = NULL;
3398 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003399
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003400exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 psa_mac_abort(&operation);
3402 psa_destroy_key(key);
3403 PSA_DONE();
3404 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003405}
3406/* END_CASE */
3407
3408/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003409void mac_verify(int key_type_arg,
3410 data_t *key_data,
3411 int alg_arg,
3412 data_t *input,
3413 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003414{
Ronald Cron5425a212020-08-04 14:58:35 +02003415 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003416 psa_key_type_t key_type = key_type_arg;
3417 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003418 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003420 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003425
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3427 psa_set_key_algorithm(&attributes, alg);
3428 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3431 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003432
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003433 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3435 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003436
3437 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3439 PSA_ASSERT(psa_mac_update(&operation,
3440 input->x, input->len));
3441 PSA_ASSERT(psa_mac_verify_finish(&operation,
3442 expected_mac->x,
3443 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003444
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003445 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 TEST_EQUAL(psa_mac_verify(key, alg,
3447 input->x, input->len,
3448 expected_mac->x,
3449 expected_mac->len - 1),
3450 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003451
3452 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3454 PSA_ASSERT(psa_mac_update(&operation,
3455 input->x, input->len));
3456 TEST_EQUAL(psa_mac_verify_finish(&operation,
3457 expected_mac->x,
3458 expected_mac->len - 1),
3459 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003460
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003461 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3463 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3464 TEST_EQUAL(psa_mac_verify(key, alg,
3465 input->x, input->len,
3466 perturbed_mac, expected_mac->len + 1),
3467 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003468
3469 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3471 PSA_ASSERT(psa_mac_update(&operation,
3472 input->x, input->len));
3473 TEST_EQUAL(psa_mac_verify_finish(&operation,
3474 perturbed_mac,
3475 expected_mac->len + 1),
3476 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003477
3478 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 for (size_t i = 0; i < expected_mac->len; i++) {
3480 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003481 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003482
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 TEST_EQUAL(psa_mac_verify(key, alg,
3484 input->x, input->len,
3485 perturbed_mac, expected_mac->len),
3486 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003487
Gilles Peskine449bd832023-01-11 14:50:10 +01003488 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3489 PSA_ASSERT(psa_mac_update(&operation,
3490 input->x, input->len));
3491 TEST_EQUAL(psa_mac_verify_finish(&operation,
3492 perturbed_mac,
3493 expected_mac->len),
3494 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003495 perturbed_mac[i] ^= 1;
3496 }
3497
Gilles Peskine8c9def32018-02-08 10:02:12 +01003498exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 psa_mac_abort(&operation);
3500 psa_destroy_key(key);
3501 PSA_DONE();
3502 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003503}
3504/* END_CASE */
3505
3506/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003507void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003508{
Jaeden Ameroab439972019-02-15 14:12:05 +00003509 const uint8_t input[1] = { 0 };
3510 unsigned char output[1] = { 0 };
3511 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003512 /* Test each valid way of initializing the object, except for `= {0}`, as
3513 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3514 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003515 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003517 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3518 psa_cipher_operation_t zero;
3519
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003521
Jaeden Ameroab439972019-02-15 14:12:05 +00003522 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 TEST_EQUAL(psa_cipher_update(&func,
3524 input, sizeof(input),
3525 output, sizeof(output),
3526 &output_length),
3527 PSA_ERROR_BAD_STATE);
3528 TEST_EQUAL(psa_cipher_update(&init,
3529 input, sizeof(input),
3530 output, sizeof(output),
3531 &output_length),
3532 PSA_ERROR_BAD_STATE);
3533 TEST_EQUAL(psa_cipher_update(&zero,
3534 input, sizeof(input),
3535 output, sizeof(output),
3536 &output_length),
3537 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003538
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003539 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 PSA_ASSERT(psa_cipher_abort(&func));
3541 PSA_ASSERT(psa_cipher_abort(&init));
3542 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003543}
3544/* END_CASE */
3545
3546/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003547void cipher_setup(int key_type_arg,
3548 data_t *key,
3549 int alg_arg,
3550 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003551{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003552 psa_key_type_t key_type = key_type_arg;
3553 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003554 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003555 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003556 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003557#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003558 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3559#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003560
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3564 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003565 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003566 }
3567 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003568
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003569 /* The operation object should be reusable. */
3570#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3572 smoke_test_key_data,
3573 sizeof(smoke_test_key_data),
3574 KNOWN_SUPPORTED_CIPHER_ALG,
3575 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003576 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 }
3578 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003579#endif
3580
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003581exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 psa_cipher_abort(&operation);
3583 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003584}
3585/* END_CASE */
3586
Ronald Cronee414c72021-03-18 18:50:08 +01003587/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003588void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003589{
Ronald Cron5425a212020-08-04 14:58:35 +02003590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003591 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3592 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003594 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003595 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003596 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003597 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 0xaa, 0xaa, 0xaa, 0xaa
3599 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003600 const uint8_t text[] = {
3601 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 0xbb, 0xbb, 0xbb, 0xbb
3603 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003604 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003605 size_t length = 0;
3606
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 PSA_ASSERT(psa_crypto_init());
3608 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3609 psa_set_key_algorithm(&attributes, alg);
3610 psa_set_key_type(&attributes, key_type);
3611 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3612 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003613
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003614 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003615 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3616 ASSERT_OPERATION_IS_ACTIVE(operation);
3617 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3618 PSA_ERROR_BAD_STATE);
3619 ASSERT_OPERATION_IS_INACTIVE(operation);
3620 PSA_ASSERT(psa_cipher_abort(&operation));
3621 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003622
3623 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003624 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3625 ASSERT_OPERATION_IS_ACTIVE(operation);
3626 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3627 PSA_ERROR_BAD_STATE);
3628 ASSERT_OPERATION_IS_INACTIVE(operation);
3629 PSA_ASSERT(psa_cipher_abort(&operation));
3630 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003631
Jaeden Ameroab439972019-02-15 14:12:05 +00003632 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003633 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3634 buffer, sizeof(buffer),
3635 &length),
3636 PSA_ERROR_BAD_STATE);
3637 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003638
3639 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3641 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3642 buffer, sizeof(buffer),
3643 &length));
3644 ASSERT_OPERATION_IS_ACTIVE(operation);
3645 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3646 buffer, sizeof(buffer),
3647 &length),
3648 PSA_ERROR_BAD_STATE);
3649 ASSERT_OPERATION_IS_INACTIVE(operation);
3650 PSA_ASSERT(psa_cipher_abort(&operation));
3651 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003652
3653 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3655 PSA_ASSERT(psa_cipher_set_iv(&operation,
3656 iv, sizeof(iv)));
3657 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3658 buffer, sizeof(buffer),
3659 &length),
3660 PSA_ERROR_BAD_STATE);
3661 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003662
3663 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 TEST_EQUAL(psa_cipher_set_iv(&operation,
3665 iv, sizeof(iv)),
3666 PSA_ERROR_BAD_STATE);
3667 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003668
3669 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3671 PSA_ASSERT(psa_cipher_set_iv(&operation,
3672 iv, sizeof(iv)));
3673 ASSERT_OPERATION_IS_ACTIVE(operation);
3674 TEST_EQUAL(psa_cipher_set_iv(&operation,
3675 iv, sizeof(iv)),
3676 PSA_ERROR_BAD_STATE);
3677 ASSERT_OPERATION_IS_INACTIVE(operation);
3678 PSA_ASSERT(psa_cipher_abort(&operation));
3679 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003680
3681 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3683 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3684 buffer, sizeof(buffer),
3685 &length));
3686 TEST_EQUAL(psa_cipher_set_iv(&operation,
3687 iv, sizeof(iv)),
3688 PSA_ERROR_BAD_STATE);
3689 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003690
3691 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 TEST_EQUAL(psa_cipher_update(&operation,
3693 text, sizeof(text),
3694 buffer, sizeof(buffer),
3695 &length),
3696 PSA_ERROR_BAD_STATE);
3697 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003698
3699 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3701 ASSERT_OPERATION_IS_ACTIVE(operation);
3702 TEST_EQUAL(psa_cipher_update(&operation,
3703 text, sizeof(text),
3704 buffer, sizeof(buffer),
3705 &length),
3706 PSA_ERROR_BAD_STATE);
3707 ASSERT_OPERATION_IS_INACTIVE(operation);
3708 PSA_ASSERT(psa_cipher_abort(&operation));
3709 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003710
3711 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003712 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3713 PSA_ASSERT(psa_cipher_set_iv(&operation,
3714 iv, sizeof(iv)));
3715 PSA_ASSERT(psa_cipher_finish(&operation,
3716 buffer, sizeof(buffer), &length));
3717 TEST_EQUAL(psa_cipher_update(&operation,
3718 text, sizeof(text),
3719 buffer, sizeof(buffer),
3720 &length),
3721 PSA_ERROR_BAD_STATE);
3722 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003723
3724 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003725 TEST_EQUAL(psa_cipher_finish(&operation,
3726 buffer, sizeof(buffer), &length),
3727 PSA_ERROR_BAD_STATE);
3728 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003729
3730 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003732 /* Not calling update means we are encrypting an empty buffer, which is OK
3733 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003734 ASSERT_OPERATION_IS_ACTIVE(operation);
3735 TEST_EQUAL(psa_cipher_finish(&operation,
3736 buffer, sizeof(buffer), &length),
3737 PSA_ERROR_BAD_STATE);
3738 ASSERT_OPERATION_IS_INACTIVE(operation);
3739 PSA_ASSERT(psa_cipher_abort(&operation));
3740 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003741
3742 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3744 PSA_ASSERT(psa_cipher_set_iv(&operation,
3745 iv, sizeof(iv)));
3746 PSA_ASSERT(psa_cipher_finish(&operation,
3747 buffer, sizeof(buffer), &length));
3748 TEST_EQUAL(psa_cipher_finish(&operation,
3749 buffer, sizeof(buffer), &length),
3750 PSA_ERROR_BAD_STATE);
3751 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003752
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003754
Jaeden Ameroab439972019-02-15 14:12:05 +00003755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003756 psa_cipher_abort(&operation);
3757 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003758}
3759/* END_CASE */
3760
3761/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003762void cipher_encrypt_fail(int alg_arg,
3763 int key_type_arg,
3764 data_t *key_data,
3765 data_t *input,
3766 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003767{
Ronald Cron5425a212020-08-04 14:58:35 +02003768 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003769 psa_status_t status;
3770 psa_key_type_t key_type = key_type_arg;
3771 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003772 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003774 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3775 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003776 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003777 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003778 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003779 size_t function_output_length;
3780 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003781 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3782
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 if (PSA_ERROR_BAD_STATE != expected_status) {
3784 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003785
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3787 psa_set_key_algorithm(&attributes, alg);
3788 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3791 input->len);
3792 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003793
Gilles Peskine449bd832023-01-11 14:50:10 +01003794 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3795 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003796 }
3797
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003798 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3800 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003801
Gilles Peskine449bd832023-01-11 14:50:10 +01003802 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003803
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003804 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 status = psa_cipher_encrypt_setup(&operation, key, alg);
3806 if (status == PSA_SUCCESS) {
3807 if (alg != PSA_ALG_ECB_NO_PADDING) {
3808 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3809 iv, iv_size,
3810 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003811 }
3812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 status = psa_cipher_update(&operation, input->x, input->len,
3814 output, output_buffer_size,
3815 &function_output_length);
3816 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003817 output_length += function_output_length;
3818
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 status = psa_cipher_finish(&operation, output + output_length,
3820 output_buffer_size - output_length,
3821 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003822
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 TEST_EQUAL(status, expected_status);
3824 } else {
3825 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003826 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 } else {
3828 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003829 }
3830
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003831exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 psa_cipher_abort(&operation);
3833 mbedtls_free(output);
3834 psa_destroy_key(key);
3835 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003836}
3837/* END_CASE */
3838
3839/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003840void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3841 data_t *input, int iv_length,
3842 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003843{
3844 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3845 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3847 size_t output_buffer_size = 0;
3848 unsigned char *output = NULL;
3849
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3851 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003854
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3856 psa_set_key_algorithm(&attributes, alg);
3857 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003858
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3860 &key));
3861 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3862 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3863 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003864
3865exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 psa_cipher_abort(&operation);
3867 mbedtls_free(output);
3868 psa_destroy_key(key);
3869 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003870}
3871/* END_CASE */
3872
3873/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003874void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3875 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003876{
3877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3878 psa_key_type_t key_type = key_type_arg;
3879 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003880 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3881 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003882 unsigned char *output = NULL;
3883 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003884 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3886
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003888
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003889 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 TEST_LE_U(ciphertext->len,
3891 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3892 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3893 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3894 TEST_LE_U(plaintext->len,
3895 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3896 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3897 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003898
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003899
3900 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 psa_set_key_usage_flags(&attributes,
3902 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3903 psa_set_key_algorithm(&attributes, alg);
3904 psa_set_key_type(&attributes, key_type);
3905 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3906 &key));
3907 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3908 plaintext->len);
3909 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003910
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003911 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3913 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3914 PSA_ERROR_BAD_STATE);
3915 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3916 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3917 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003918
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003919 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3921 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3922 &length),
3923 PSA_ERROR_BAD_STATE);
3924 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3925 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3926 &length),
3927 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003928
Gilles Peskine286c3142022-04-20 17:09:38 +02003929 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003931 output_length = 0;
3932 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 PSA_ASSERT(psa_cipher_update(&operation,
3934 plaintext->x, plaintext->len,
3935 output, output_buffer_size,
3936 &length));
3937 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003938 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 PSA_ASSERT(psa_cipher_finish(&operation,
3940 mbedtls_buffer_offset(output, output_length),
3941 output_buffer_size - output_length,
3942 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003943 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3945 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003946
Gilles Peskine286c3142022-04-20 17:09:38 +02003947 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003949 output_length = 0;
3950 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 PSA_ASSERT(psa_cipher_update(&operation,
3952 ciphertext->x, ciphertext->len,
3953 output, output_buffer_size,
3954 &length));
3955 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003956 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 PSA_ASSERT(psa_cipher_finish(&operation,
3958 mbedtls_buffer_offset(output, output_length),
3959 output_buffer_size - output_length,
3960 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003961 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 ASSERT_COMPARE(plaintext->x, plaintext->len,
3963 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003964
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003965 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003966 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3968 output, output_buffer_size,
3969 &output_length));
3970 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3971 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003972
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003973 /* One-shot decryption */
3974 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3976 output, output_buffer_size,
3977 &output_length));
3978 ASSERT_COMPARE(plaintext->x, plaintext->len,
3979 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003980
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 PSA_ASSERT(psa_cipher_abort(&operation));
3983 mbedtls_free(output);
3984 psa_cipher_abort(&operation);
3985 psa_destroy_key(key);
3986 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003987}
3988/* END_CASE */
3989
3990/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003991void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003992{
3993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3994 psa_algorithm_t alg = alg_arg;
3995 psa_key_type_t key_type = key_type_arg;
3996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3997 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3998 psa_status_t status;
3999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004001
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4003 psa_set_key_algorithm(&attributes, alg);
4004 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004005
4006 /* Usage of either of these two size macros would cause divide by zero
4007 * with incorrect key types previously. Input length should be irrelevant
4008 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4010 0);
4011 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004012
4013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4015 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004016
4017 /* Should fail due to invalid alg type (to support invalid key type).
4018 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004022
4023exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 psa_cipher_abort(&operation);
4025 psa_destroy_key(key);
4026 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004027}
4028/* END_CASE */
4029
4030/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004031void cipher_encrypt_validation(int alg_arg,
4032 int key_type_arg,
4033 data_t *key_data,
4034 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004035{
4036 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4037 psa_key_type_t key_type = key_type_arg;
4038 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004040 unsigned char *output1 = NULL;
4041 size_t output1_buffer_size = 0;
4042 size_t output1_length = 0;
4043 unsigned char *output2 = NULL;
4044 size_t output2_buffer_size = 0;
4045 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004046 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004047 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004048 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004049
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004051
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4053 psa_set_key_algorithm(&attributes, alg);
4054 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004055
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4057 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4058 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4059 ASSERT_ALLOC(output1, output1_buffer_size);
4060 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4063 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004064
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004065 /* The one-shot cipher encryption uses generated iv so validating
4066 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4068 output1_buffer_size, &output1_length));
4069 TEST_LE_U(output1_length,
4070 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4071 TEST_LE_U(output1_length,
4072 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4075 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 PSA_ASSERT(psa_cipher_update(&operation,
4078 input->x, input->len,
4079 output2, output2_buffer_size,
4080 &function_output_length));
4081 TEST_LE_U(function_output_length,
4082 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4083 TEST_LE_U(function_output_length,
4084 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004085 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 PSA_ASSERT(psa_cipher_finish(&operation,
4088 output2 + output2_length,
4089 output2_buffer_size - output2_length,
4090 &function_output_length));
4091 TEST_LE_U(function_output_length,
4092 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4093 TEST_LE_U(function_output_length,
4094 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004095 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 PSA_ASSERT(psa_cipher_abort(&operation));
4098 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4099 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004100
Gilles Peskine50e586b2018-06-08 14:28:46 +02004101exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 psa_cipher_abort(&operation);
4103 mbedtls_free(output1);
4104 mbedtls_free(output2);
4105 psa_destroy_key(key);
4106 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004107}
4108/* END_CASE */
4109
4110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004111void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4112 data_t *key_data, data_t *iv,
4113 data_t *input,
4114 int first_part_size_arg,
4115 int output1_length_arg, int output2_length_arg,
4116 data_t *expected_output,
4117 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004118{
Ronald Cron5425a212020-08-04 14:58:35 +02004119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004120 psa_key_type_t key_type = key_type_arg;
4121 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004122 psa_status_t status;
4123 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004124 size_t first_part_size = first_part_size_arg;
4125 size_t output1_length = output1_length_arg;
4126 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004127 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004128 size_t output_buffer_size = 0;
4129 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004130 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004131 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4137 psa_set_key_algorithm(&attributes, alg);
4138 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4141 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 if (iv->len > 0) {
4146 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004147 }
4148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4150 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4151 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 TEST_LE_U(first_part_size, input->len);
4154 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4155 output, output_buffer_size,
4156 &function_output_length));
4157 TEST_ASSERT(function_output_length == output1_length);
4158 TEST_LE_U(function_output_length,
4159 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4160 TEST_LE_U(function_output_length,
4161 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004162 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 if (first_part_size < input->len) {
4165 PSA_ASSERT(psa_cipher_update(&operation,
4166 input->x + first_part_size,
4167 input->len - first_part_size,
4168 (output_buffer_size == 0 ? NULL :
4169 output + total_output_length),
4170 output_buffer_size - total_output_length,
4171 &function_output_length));
4172 TEST_ASSERT(function_output_length == output2_length);
4173 TEST_LE_U(function_output_length,
4174 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4175 alg,
4176 input->len - first_part_size));
4177 TEST_LE_U(function_output_length,
4178 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004179 total_output_length += function_output_length;
4180 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 status = psa_cipher_finish(&operation,
4183 (output_buffer_size == 0 ? NULL :
4184 output + total_output_length),
4185 output_buffer_size - total_output_length,
4186 &function_output_length);
4187 TEST_LE_U(function_output_length,
4188 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4189 TEST_LE_U(function_output_length,
4190 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004191 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 if (expected_status == PSA_SUCCESS) {
4195 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 ASSERT_COMPARE(expected_output->x, expected_output->len,
4198 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004199 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004200
4201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 psa_cipher_abort(&operation);
4203 mbedtls_free(output);
4204 psa_destroy_key(key);
4205 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004206}
4207/* END_CASE */
4208
4209/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004210void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4211 data_t *key_data, data_t *iv,
4212 data_t *input,
4213 int first_part_size_arg,
4214 int output1_length_arg, int output2_length_arg,
4215 data_t *expected_output,
4216 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004217{
Ronald Cron5425a212020-08-04 14:58:35 +02004218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004219 psa_key_type_t key_type = key_type_arg;
4220 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004221 psa_status_t status;
4222 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004223 size_t first_part_size = first_part_size_arg;
4224 size_t output1_length = output1_length_arg;
4225 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004226 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227 size_t output_buffer_size = 0;
4228 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004229 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004230 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4236 psa_set_key_algorithm(&attributes, alg);
4237 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4240 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 if (iv->len > 0) {
4245 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004246 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4249 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4250 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 TEST_LE_U(first_part_size, input->len);
4253 PSA_ASSERT(psa_cipher_update(&operation,
4254 input->x, first_part_size,
4255 output, output_buffer_size,
4256 &function_output_length));
4257 TEST_ASSERT(function_output_length == output1_length);
4258 TEST_LE_U(function_output_length,
4259 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4260 TEST_LE_U(function_output_length,
4261 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004262 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 if (first_part_size < input->len) {
4265 PSA_ASSERT(psa_cipher_update(&operation,
4266 input->x + first_part_size,
4267 input->len - first_part_size,
4268 (output_buffer_size == 0 ? NULL :
4269 output + total_output_length),
4270 output_buffer_size - total_output_length,
4271 &function_output_length));
4272 TEST_ASSERT(function_output_length == output2_length);
4273 TEST_LE_U(function_output_length,
4274 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4275 alg,
4276 input->len - first_part_size));
4277 TEST_LE_U(function_output_length,
4278 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004279 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004280 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 status = psa_cipher_finish(&operation,
4283 (output_buffer_size == 0 ? NULL :
4284 output + total_output_length),
4285 output_buffer_size - total_output_length,
4286 &function_output_length);
4287 TEST_LE_U(function_output_length,
4288 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4289 TEST_LE_U(function_output_length,
4290 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004291 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 if (expected_status == PSA_SUCCESS) {
4295 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 ASSERT_COMPARE(expected_output->x, expected_output->len,
4298 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004299 }
4300
Gilles Peskine50e586b2018-06-08 14:28:46 +02004301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 psa_cipher_abort(&operation);
4303 mbedtls_free(output);
4304 psa_destroy_key(key);
4305 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004306}
4307/* END_CASE */
4308
Gilles Peskine50e586b2018-06-08 14:28:46 +02004309/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004310void cipher_decrypt_fail(int alg_arg,
4311 int key_type_arg,
4312 data_t *key_data,
4313 data_t *iv,
4314 data_t *input_arg,
4315 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004316{
4317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4318 psa_status_t status;
4319 psa_key_type_t key_type = key_type_arg;
4320 psa_algorithm_t alg = alg_arg;
4321 psa_status_t expected_status = expected_status_arg;
4322 unsigned char *input = NULL;
4323 size_t input_buffer_size = 0;
4324 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004325 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004326 size_t output_buffer_size = 0;
4327 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004328 size_t function_output_length;
4329 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (PSA_ERROR_BAD_STATE != expected_status) {
4333 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4336 psa_set_key_algorithm(&attributes, alg);
4337 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4340 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004341 }
4342
4343 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4345 if (input_buffer_size > 0) {
4346 ASSERT_ALLOC(input, input_buffer_size);
4347 memcpy(input, iv->x, iv->len);
4348 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004349 }
4350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4352 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004353
Neil Armstrong66a479f2022-02-07 15:41:19 +01004354 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4356 output_buffer_size, &output_length);
4357 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004358
Neil Armstrong66a479f2022-02-07 15:41:19 +01004359 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 status = psa_cipher_decrypt_setup(&operation, key, alg);
4361 if (status == PSA_SUCCESS) {
4362 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4363 input_arg->len) +
4364 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4365 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (iv->len > 0) {
4368 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 if (status != PSA_SUCCESS) {
4371 TEST_EQUAL(status, expected_status);
4372 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004373 }
4374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (status == PSA_SUCCESS) {
4376 status = psa_cipher_update(&operation,
4377 input_arg->x, input_arg->len,
4378 output_multi, output_buffer_size,
4379 &function_output_length);
4380 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004381 output_length = function_output_length;
4382
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 status = psa_cipher_finish(&operation,
4384 output_multi + output_length,
4385 output_buffer_size - output_length,
4386 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004387
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 TEST_EQUAL(status, expected_status);
4389 } else {
4390 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004391 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 } else {
4393 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004394 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 } else {
4396 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004397 }
4398
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 psa_cipher_abort(&operation);
4401 mbedtls_free(input);
4402 mbedtls_free(output);
4403 mbedtls_free(output_multi);
4404 psa_destroy_key(key);
4405 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004406}
4407/* END_CASE */
4408
4409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004410void cipher_decrypt(int alg_arg,
4411 int key_type_arg,
4412 data_t *key_data,
4413 data_t *iv,
4414 data_t *input_arg,
4415 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004416{
4417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4418 psa_key_type_t key_type = key_type_arg;
4419 psa_algorithm_t alg = alg_arg;
4420 unsigned char *input = NULL;
4421 size_t input_buffer_size = 0;
4422 unsigned char *output = NULL;
4423 size_t output_buffer_size = 0;
4424 size_t output_length = 0;
4425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4430 psa_set_key_algorithm(&attributes, alg);
4431 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004432
4433 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4435 if (input_buffer_size > 0) {
4436 ASSERT_ALLOC(input, input_buffer_size);
4437 memcpy(input, iv->x, iv->len);
4438 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004439 }
4440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4442 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4445 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4448 output_buffer_size, &output_length));
4449 TEST_LE_U(output_length,
4450 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4451 TEST_LE_U(output_length,
4452 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 ASSERT_COMPARE(expected_output->x, expected_output->len,
4455 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 mbedtls_free(input);
4458 mbedtls_free(output);
4459 psa_destroy_key(key);
4460 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004461}
4462/* END_CASE */
4463
4464/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004465void cipher_verify_output(int alg_arg,
4466 int key_type_arg,
4467 data_t *key_data,
4468 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004469{
Ronald Cron5425a212020-08-04 14:58:35 +02004470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004471 psa_key_type_t key_type = key_type_arg;
4472 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004473 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004474 size_t output1_size = 0;
4475 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004476 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004477 size_t output2_size = 0;
4478 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4484 psa_set_key_algorithm(&attributes, alg);
4485 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4488 &key));
4489 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4490 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004491
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4493 output1, output1_size,
4494 &output1_length));
4495 TEST_LE_U(output1_length,
4496 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4497 TEST_LE_U(output1_length,
4498 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004499
4500 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4504 output2, output2_size,
4505 &output2_length));
4506 TEST_LE_U(output2_length,
4507 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4508 TEST_LE_U(output2_length,
4509 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004512
4513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 mbedtls_free(output1);
4515 mbedtls_free(output2);
4516 psa_destroy_key(key);
4517 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004518}
4519/* END_CASE */
4520
4521/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004522void cipher_verify_output_multipart(int alg_arg,
4523 int key_type_arg,
4524 data_t *key_data,
4525 data_t *input,
4526 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004527{
Ronald Cron5425a212020-08-04 14:58:35 +02004528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004529 psa_key_type_t key_type = key_type_arg;
4530 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004531 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004533 size_t iv_size = 16;
4534 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004535 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004536 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004537 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004538 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004539 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004540 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004541 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004542 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4543 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004547
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4549 psa_set_key_algorithm(&attributes, alg);
4550 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4553 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4556 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 if (alg != PSA_ALG_ECB_NO_PADDING) {
4559 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4560 iv, iv_size,
4561 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004562 }
4563
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4565 TEST_LE_U(output1_buffer_size,
4566 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4567 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4572 output1, output1_buffer_size,
4573 &function_output_length));
4574 TEST_LE_U(function_output_length,
4575 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4576 TEST_LE_U(function_output_length,
4577 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004578 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 PSA_ASSERT(psa_cipher_update(&operation1,
4581 input->x + first_part_size,
4582 input->len - first_part_size,
4583 output1, output1_buffer_size,
4584 &function_output_length));
4585 TEST_LE_U(function_output_length,
4586 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4587 alg,
4588 input->len - first_part_size));
4589 TEST_LE_U(function_output_length,
4590 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004591 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 PSA_ASSERT(psa_cipher_finish(&operation1,
4594 output1 + output1_length,
4595 output1_buffer_size - output1_length,
4596 &function_output_length));
4597 TEST_LE_U(function_output_length,
4598 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4599 TEST_LE_U(function_output_length,
4600 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004601 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004604
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 TEST_LE_U(output2_buffer_size,
4607 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4608 TEST_LE_U(output2_buffer_size,
4609 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4610 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 if (iv_length > 0) {
4613 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4614 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004615 }
Moran Pekerded84402018-06-06 16:36:50 +03004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4618 output2, output2_buffer_size,
4619 &function_output_length));
4620 TEST_LE_U(function_output_length,
4621 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4622 TEST_LE_U(function_output_length,
4623 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004624 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 PSA_ASSERT(psa_cipher_update(&operation2,
4627 output1 + first_part_size,
4628 output1_length - first_part_size,
4629 output2, output2_buffer_size,
4630 &function_output_length));
4631 TEST_LE_U(function_output_length,
4632 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4633 alg,
4634 output1_length - first_part_size));
4635 TEST_LE_U(function_output_length,
4636 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004637 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 PSA_ASSERT(psa_cipher_finish(&operation2,
4640 output2 + output2_length,
4641 output2_buffer_size - output2_length,
4642 &function_output_length));
4643 TEST_LE_U(function_output_length,
4644 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4645 TEST_LE_U(function_output_length,
4646 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004647 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004652
4653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 psa_cipher_abort(&operation1);
4655 psa_cipher_abort(&operation2);
4656 mbedtls_free(output1);
4657 mbedtls_free(output2);
4658 psa_destroy_key(key);
4659 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004660}
4661/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004662
Gilles Peskine20035e32018-02-03 22:44:14 +01004663/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004664void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4665 int alg_arg,
4666 data_t *nonce,
4667 data_t *additional_data,
4668 data_t *input_data,
4669 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004670{
Ronald Cron5425a212020-08-04 14:58:35 +02004671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004672 psa_key_type_t key_type = key_type_arg;
4673 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004674 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004675 unsigned char *output_data = NULL;
4676 size_t output_size = 0;
4677 size_t output_length = 0;
4678 unsigned char *output_data2 = NULL;
4679 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004680 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004681 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4687 psa_set_key_algorithm(&attributes, alg);
4688 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4691 &key));
4692 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4693 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4696 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004697 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4698 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4700 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4701 TEST_EQUAL(output_size,
4702 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4703 TEST_LE_U(output_size,
4704 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004705 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 status = psa_aead_encrypt(key, alg,
4709 nonce->x, nonce->len,
4710 additional_data->x,
4711 additional_data->len,
4712 input_data->x, input_data->len,
4713 output_data, output_size,
4714 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004715
4716 /* If the operation is not supported, just skip and not fail in case the
4717 * encryption involves a common limitation of cryptography hardwares and
4718 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if (status == PSA_ERROR_NOT_SUPPORTED) {
4720 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4721 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004722 }
4723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (PSA_SUCCESS == expected_result) {
4727 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004728
Gilles Peskine003a4a92019-05-14 16:09:40 +02004729 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4730 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 TEST_EQUAL(input_data->len,
4732 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 TEST_LE_U(input_data->len,
4735 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004736
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 TEST_EQUAL(psa_aead_decrypt(key, alg,
4738 nonce->x, nonce->len,
4739 additional_data->x,
4740 additional_data->len,
4741 output_data, output_length,
4742 output_data2, output_length,
4743 &output_length2),
4744 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 ASSERT_COMPARE(input_data->x, input_data->len,
4747 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004748 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004749
Gilles Peskinea1cac842018-06-11 19:33:02 +02004750exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 psa_destroy_key(key);
4752 mbedtls_free(output_data);
4753 mbedtls_free(output_data2);
4754 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755}
4756/* END_CASE */
4757
4758/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004759void aead_encrypt(int key_type_arg, data_t *key_data,
4760 int alg_arg,
4761 data_t *nonce,
4762 data_t *additional_data,
4763 data_t *input_data,
4764 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004765{
Ronald Cron5425a212020-08-04 14:58:35 +02004766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767 psa_key_type_t key_type = key_type_arg;
4768 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004769 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004770 unsigned char *output_data = NULL;
4771 size_t output_size = 0;
4772 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004774 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4779 psa_set_key_algorithm(&attributes, alg);
4780 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4783 &key));
4784 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4785 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4788 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004789 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4790 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 TEST_EQUAL(output_size,
4792 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4793 TEST_LE_U(output_size,
4794 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4795 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 status = psa_aead_encrypt(key, alg,
4798 nonce->x, nonce->len,
4799 additional_data->x, additional_data->len,
4800 input_data->x, input_data->len,
4801 output_data, output_size,
4802 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803
Ronald Cron28a45ed2021-02-09 20:35:42 +01004804 /* If the operation is not supported, just skip and not fail in case the
4805 * encryption involves a common limitation of cryptography hardwares and
4806 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 if (status == PSA_ERROR_NOT_SUPPORTED) {
4808 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4809 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004810 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 PSA_ASSERT(status);
4813 ASSERT_COMPARE(expected_result->x, expected_result->len,
4814 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004815
Gilles Peskinea1cac842018-06-11 19:33:02 +02004816exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 psa_destroy_key(key);
4818 mbedtls_free(output_data);
4819 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004820}
4821/* END_CASE */
4822
4823/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004824void aead_decrypt(int key_type_arg, data_t *key_data,
4825 int alg_arg,
4826 data_t *nonce,
4827 data_t *additional_data,
4828 data_t *input_data,
4829 data_t *expected_data,
4830 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004831{
Ronald Cron5425a212020-08-04 14:58:35 +02004832 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004833 psa_key_type_t key_type = key_type_arg;
4834 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004835 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004836 unsigned char *output_data = NULL;
4837 size_t output_size = 0;
4838 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004840 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004841 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004844
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4846 psa_set_key_algorithm(&attributes, alg);
4847 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4850 &key));
4851 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4852 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4855 alg);
4856 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4857 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004858 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4859 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 TEST_EQUAL(output_size,
4861 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4862 TEST_LE_U(output_size,
4863 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004864 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 status = psa_aead_decrypt(key, alg,
4868 nonce->x, nonce->len,
4869 additional_data->x,
4870 additional_data->len,
4871 input_data->x, input_data->len,
4872 output_data, output_size,
4873 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004874
Ronald Cron28a45ed2021-02-09 20:35:42 +01004875 /* If the operation is not supported, just skip and not fail in case the
4876 * decryption involves a common limitation of cryptography hardwares and
4877 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (status == PSA_ERROR_NOT_SUPPORTED) {
4879 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4880 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004881 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (expected_result == PSA_SUCCESS) {
4886 ASSERT_COMPARE(expected_data->x, expected_data->len,
4887 output_data, output_length);
4888 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004889
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 psa_destroy_key(key);
4892 mbedtls_free(output_data);
4893 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004894}
4895/* END_CASE */
4896
4897/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004898void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4899 int alg_arg,
4900 data_t *nonce,
4901 data_t *additional_data,
4902 data_t *input_data,
4903 int do_set_lengths,
4904 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004905{
Paul Elliottd3f82412021-06-16 16:52:21 +01004906 size_t ad_part_len = 0;
4907 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004908 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4911 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (do_set_lengths) {
4914 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004915 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004917 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004919 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004920
4921 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (!aead_multipart_internal_func(key_type_arg, key_data,
4923 alg_arg, nonce,
4924 additional_data,
4925 ad_part_len,
4926 input_data, -1,
4927 set_lengths_method,
4928 expected_output,
4929 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004930 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004931 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 /* length(0) part, length(ad_part_len) part, length(0) part... */
4934 mbedtls_test_set_step(1000 + ad_part_len);
4935
4936 if (!aead_multipart_internal_func(key_type_arg, key_data,
4937 alg_arg, nonce,
4938 additional_data,
4939 ad_part_len,
4940 input_data, -1,
4941 set_lengths_method,
4942 expected_output,
4943 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004944 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 }
4946 }
4947
4948 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4949 /* Split data into length(data_part_len) parts. */
4950 mbedtls_test_set_step(2000 + data_part_len);
4951
4952 if (do_set_lengths) {
4953 if (data_part_len & 0x01) {
4954 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4955 } else {
4956 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4957 }
4958 }
4959
4960 if (!aead_multipart_internal_func(key_type_arg, key_data,
4961 alg_arg, nonce,
4962 additional_data, -1,
4963 input_data, data_part_len,
4964 set_lengths_method,
4965 expected_output,
4966 1, 0)) {
4967 break;
4968 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004969
4970 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 if (!aead_multipart_internal_func(key_type_arg, key_data,
4974 alg_arg, nonce,
4975 additional_data, -1,
4976 input_data, data_part_len,
4977 set_lengths_method,
4978 expected_output,
4979 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004980 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004982 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004983
Paul Elliott8fc45162021-06-23 16:06:01 +01004984 /* Goto is required to silence warnings about unused labels, as we
4985 * don't actually do any test assertions in this function. */
4986 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004987}
4988/* END_CASE */
4989
4990/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004991void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4992 int alg_arg,
4993 data_t *nonce,
4994 data_t *additional_data,
4995 data_t *input_data,
4996 int do_set_lengths,
4997 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004998{
Paul Elliottd3f82412021-06-16 16:52:21 +01004999 size_t ad_part_len = 0;
5000 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005001 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005004 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005006
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 if (do_set_lengths) {
5008 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005011 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005013 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 if (!aead_multipart_internal_func(key_type_arg, key_data,
5016 alg_arg, nonce,
5017 additional_data,
5018 ad_part_len,
5019 input_data, -1,
5020 set_lengths_method,
5021 expected_output,
5022 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025
5026 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005028
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 if (!aead_multipart_internal_func(key_type_arg, key_data,
5030 alg_arg, nonce,
5031 additional_data,
5032 ad_part_len,
5033 input_data, -1,
5034 set_lengths_method,
5035 expected_output,
5036 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005037 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005039 }
5040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005042 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 if (do_set_lengths) {
5046 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005047 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005051 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 if (!aead_multipart_internal_func(key_type_arg, key_data,
5054 alg_arg, nonce,
5055 additional_data, -1,
5056 input_data, data_part_len,
5057 set_lengths_method,
5058 expected_output,
5059 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005060 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005062
5063 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (!aead_multipart_internal_func(key_type_arg, key_data,
5067 alg_arg, nonce,
5068 additional_data, -1,
5069 input_data, data_part_len,
5070 set_lengths_method,
5071 expected_output,
5072 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005073 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005075 }
5076
Paul Elliott8fc45162021-06-23 16:06:01 +01005077 /* Goto is required to silence warnings about unused labels, as we
5078 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005079 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005080}
5081/* END_CASE */
5082
5083/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005084void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5085 int alg_arg,
5086 int nonce_length,
5087 int expected_nonce_length_arg,
5088 data_t *additional_data,
5089 data_t *input_data,
5090 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005091{
5092
5093 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5094 psa_key_type_t key_type = key_type_arg;
5095 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005096 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005097 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5099 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005100 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005101 size_t actual_nonce_length = 0;
5102 size_t expected_nonce_length = expected_nonce_length_arg;
5103 unsigned char *output = NULL;
5104 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005105 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005106 size_t ciphertext_size = 0;
5107 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005108 size_t tag_length = 0;
5109 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005112
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5114 psa_set_key_algorithm(&attributes, alg);
5115 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5118 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005123
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
5134 /* If the operation is not supported, just skip and not fail in case the
5135 * encryption involves a common limitation of cryptography hardwares and
5136 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 if (status == PSA_ERROR_NOT_SUPPORTED) {
5138 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5139 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140 }
5141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5145 nonce_length,
5146 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (expected_status == PSA_SUCCESS) {
5153 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5154 alg));
5155 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005160 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5162 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5165 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5168 output, output_size,
5169 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5172 &ciphertext_length, tag_buffer,
5173 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005174 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005175
5176exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 psa_destroy_key(key);
5178 mbedtls_free(output);
5179 mbedtls_free(ciphertext);
5180 psa_aead_abort(&operation);
5181 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005182}
5183/* END_CASE */
5184
5185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005186void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5187 int alg_arg,
5188 int nonce_length_arg,
5189 int set_lengths_method_arg,
5190 data_t *additional_data,
5191 data_t *input_data,
5192 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005193{
5194
5195 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5196 psa_key_type_t key_type = key_type_arg;
5197 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005198 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005199 uint8_t *nonce_buffer = NULL;
5200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5201 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5202 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005203 unsigned char *output = NULL;
5204 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005205 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005206 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005207 size_t ciphertext_size = 0;
5208 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005209 size_t tag_length = 0;
5210 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005211 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005212 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005213
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5217 psa_set_key_algorithm(&attributes, alg);
5218 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005219
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5221 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005236
5237 /* If the operation is not supported, just skip and not fail in case the
5238 * encryption involves a common limitation of cryptography hardwares and
5239 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 if (status == PSA_ERROR_NOT_SUPPORTED) {
5241 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5242 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005243 }
5244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005246
Paul Elliott4023ffd2021-09-10 16:21:22 +01005247 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 if (nonce_length_arg == -1) {
5249 /* Arbitrary size buffer, to test zero length valid buffer. */
5250 ASSERT_ALLOC(nonce_buffer, 4);
5251 nonce_length = 0;
5252 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005253 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 nonce_length = (size_t) nonce_length_arg;
5255 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 if (nonce_buffer) {
5258 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005259 nonce_buffer[index] = 'a' + index;
5260 }
Paul Elliott66696b52021-08-16 18:42:41 +01005261 }
Paul Elliott863864a2021-07-23 17:28:31 +01005262 }
5263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5265 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5266 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005267 }
5268
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (expected_status == PSA_SUCCESS) {
5274 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5275 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5276 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005277 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005279 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 }
Paul Elliott863864a2021-07-23 17:28:31 +01005281
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005282 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5284 additional_data->len),
5285 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5288 output, output_size,
5289 &ciphertext_length),
5290 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5293 &ciphertext_length, tag_buffer,
5294 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5295 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005296 }
5297
5298exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 psa_destroy_key(key);
5300 mbedtls_free(output);
5301 mbedtls_free(ciphertext);
5302 mbedtls_free(nonce_buffer);
5303 psa_aead_abort(&operation);
5304 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005305}
5306/* END_CASE */
5307
5308/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005309void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005310 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005311 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005312 data_t *nonce,
5313 data_t *additional_data,
5314 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005316{
5317
5318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5319 psa_key_type_t key_type = key_type_arg;
5320 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005321 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5323 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5324 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005325 unsigned char *output = NULL;
5326 unsigned char *ciphertext = NULL;
5327 size_t output_size = output_size_arg;
5328 size_t ciphertext_size = 0;
5329 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005330 size_t tag_length = 0;
5331 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5332
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5336 psa_set_key_algorithm(&attributes, alg);
5337 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005338
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5340 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005351
5352 /* If the operation is not supported, just skip and not fail in case the
5353 * encryption involves a common limitation of cryptography hardwares and
5354 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 if (status == PSA_ERROR_NOT_SUPPORTED) {
5356 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5357 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005358 }
5359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5363 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5368 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005369
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 status = psa_aead_update(&operation, input_data->x, input_data->len,
5371 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005376 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5378 &ciphertext_length, tag_buffer,
5379 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005380 }
5381
5382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 psa_destroy_key(key);
5384 mbedtls_free(output);
5385 mbedtls_free(ciphertext);
5386 psa_aead_abort(&operation);
5387 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005388}
5389/* END_CASE */
5390
Paul Elliott91b021e2021-07-23 18:52:31 +01005391/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005392void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5393 int alg_arg,
5394 int finish_ciphertext_size_arg,
5395 int tag_size_arg,
5396 data_t *nonce,
5397 data_t *additional_data,
5398 data_t *input_data,
5399 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005400{
5401
5402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5403 psa_key_type_t key_type = key_type_arg;
5404 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005405 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5407 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5408 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005409 unsigned char *ciphertext = NULL;
5410 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005411 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005412 size_t ciphertext_size = 0;
5413 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5415 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005416 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005419
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5421 psa_set_key_algorithm(&attributes, alg);
5422 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5425 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005428
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005432
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005438
5439 /* If the operation is not supported, just skip and not fail in case the
5440 * encryption involves a common limitation of cryptography hardwares and
5441 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (status == PSA_ERROR_NOT_SUPPORTED) {
5443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5444 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005445 }
5446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5452 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5455 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5458 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
5460 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 status = psa_aead_finish(&operation, finish_ciphertext,
5462 finish_ciphertext_size,
5463 &ciphertext_length, tag_buffer,
5464 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005467
5468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 psa_destroy_key(key);
5470 mbedtls_free(ciphertext);
5471 mbedtls_free(finish_ciphertext);
5472 mbedtls_free(tag_buffer);
5473 psa_aead_abort(&operation);
5474 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005475}
5476/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005477
5478/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005479void aead_multipart_verify(int key_type_arg, data_t *key_data,
5480 int alg_arg,
5481 data_t *nonce,
5482 data_t *additional_data,
5483 data_t *input_data,
5484 data_t *tag,
5485 int tag_usage_arg,
5486 int expected_setup_status_arg,
5487 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005488{
5489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5490 psa_key_type_t key_type = key_type_arg;
5491 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005492 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5494 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5495 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005496 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005497 unsigned char *plaintext = NULL;
5498 unsigned char *finish_plaintext = NULL;
5499 size_t plaintext_size = 0;
5500 size_t plaintext_length = 0;
5501 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005502 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005503 unsigned char *tag_buffer = NULL;
5504 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005505
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5509 psa_set_key_algorithm(&attributes, alg);
5510 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5513 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5518 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005527
5528 /* If the operation is not supported, just skip and not fail in case the
5529 * encryption involves a common limitation of cryptography hardwares and
5530 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 if (status == PSA_ERROR_NOT_SUPPORTED) {
5532 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5533 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005534 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005538 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 }
Paul Elliott9961a662021-09-17 19:19:02 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 status = psa_aead_set_lengths(&operation, additional_data->len,
5546 input_data->len);
5547 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5550 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5553 input_data->len,
5554 plaintext, plaintext_size,
5555 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005558 tag_buffer = tag->x;
5559 tag_size = tag->len;
5560 }
5561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 status = psa_aead_verify(&operation, finish_plaintext,
5563 verify_plaintext_size,
5564 &plaintext_length,
5565 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005568
5569exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 psa_destroy_key(key);
5571 mbedtls_free(plaintext);
5572 mbedtls_free(finish_plaintext);
5573 psa_aead_abort(&operation);
5574 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005575}
5576/* END_CASE */
5577
Paul Elliott9961a662021-09-17 19:19:02 +01005578/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005579void aead_multipart_setup(int key_type_arg, data_t *key_data,
5580 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005581{
5582 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5583 psa_key_type_t key_type = key_type_arg;
5584 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005585 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5587 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5588 psa_status_t expected_status = expected_status_arg;
5589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 psa_set_key_usage_flags(&attributes,
5593 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5594 psa_set_key_algorithm(&attributes, alg);
5595 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5598 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005607
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005609
5610exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 psa_destroy_key(key);
5612 psa_aead_abort(&operation);
5613 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005614}
5615/* END_CASE */
5616
5617/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005618void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5619 int alg_arg,
5620 data_t *nonce,
5621 data_t *additional_data,
5622 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005623{
5624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5625 psa_key_type_t key_type = key_type_arg;
5626 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005627 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005628 unsigned char *output_data = NULL;
5629 unsigned char *final_data = NULL;
5630 size_t output_size = 0;
5631 size_t finish_output_size = 0;
5632 size_t output_length = 0;
5633 size_t key_bits = 0;
5634 size_t tag_length = 0;
5635 size_t tag_size = 0;
5636 size_t nonce_length = 0;
5637 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5638 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5639 size_t output_part_length = 0;
5640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 psa_set_key_usage_flags(&attributes,
5645 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5646 psa_set_key_algorithm(&attributes, alg);
5647 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5650 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005651
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5653 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005660
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005664
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005668
5669 /* Test all operations error without calling setup first. */
5670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5672 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5677 PSA_AEAD_NONCE_MAX_SIZE,
5678 &nonce_length),
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
Paul Elliott481be342021-07-16 17:38:47 +01005683 /* ------------------------------------------------------- */
5684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5686 input_data->len),
5687 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
Paul Elliott481be342021-07-16 17:38:47 +01005691 /* ------------------------------------------------------- */
5692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5694 additional_data->len),
5695 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
Paul Elliott481be342021-07-16 17:38:47 +01005699 /* ------------------------------------------------------- */
5700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5702 input_data->len, output_data,
5703 output_size, &output_length),
5704 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707
Paul Elliott481be342021-07-16 17:38:47 +01005708 /* ------------------------------------------------------- */
5709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5711 finish_output_size,
5712 &output_part_length,
5713 tag_buffer, tag_length,
5714 &tag_size),
5715 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005718
Paul Elliott481be342021-07-16 17:38:47 +01005719 /* ------------------------------------------------------- */
5720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5722 finish_output_size,
5723 &output_part_length,
5724 tag_buffer,
5725 tag_length),
5726 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005729
5730 /* Test for double setups. */
5731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5735 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005738
Paul Elliott481be342021-07-16 17:38:47 +01005739 /* ------------------------------------------------------- */
5740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5744 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005747
Paul Elliott374a2be2021-07-16 17:53:40 +01005748 /* ------------------------------------------------------- */
5749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5753 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005756
5757 /* ------------------------------------------------------- */
5758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5762 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005765
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766 /* Test for not setting a nonce. */
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5771 additional_data->len),
5772 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775
Paul Elliott7f628422021-09-01 12:08:29 +01005776 /* ------------------------------------------------------- */
5777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5781 input_data->len, output_data,
5782 output_size, &output_length),
5783 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005786
Paul Elliottbdc2c682021-09-21 18:37:10 +01005787 /* ------------------------------------------------------- */
5788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5792 finish_output_size,
5793 &output_part_length,
5794 tag_buffer, tag_length,
5795 &tag_size),
5796 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005799
5800 /* ------------------------------------------------------- */
5801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5805 finish_output_size,
5806 &output_part_length,
5807 tag_buffer,
5808 tag_length),
5809 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005812
Paul Elliottc23a9a02021-06-21 18:32:46 +01005813 /* Test for double setting nonce. */
5814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5820 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005823
Paul Elliott374a2be2021-07-16 17:53:40 +01005824 /* Test for double generating nonce. */
5825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5829 PSA_AEAD_NONCE_MAX_SIZE,
5830 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5833 PSA_AEAD_NONCE_MAX_SIZE,
5834 &nonce_length),
5835 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005836
5837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005839
5840 /* Test for generate nonce then set and vice versa */
5841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5845 PSA_AEAD_NONCE_MAX_SIZE,
5846 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5849 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005852
Andrzej Kurekad837522021-12-15 15:28:49 +01005853 /* Test for generating nonce after calling set lengths */
5854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5858 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5861 PSA_AEAD_NONCE_MAX_SIZE,
5862 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005865
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005866 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 if (operation.alg == PSA_ALG_CCM) {
5871 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5872 input_data->len),
5873 PSA_ERROR_INVALID_ARGUMENT);
5874 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5875 PSA_AEAD_NONCE_MAX_SIZE,
5876 &nonce_length),
5877 PSA_ERROR_BAD_STATE);
5878 } else {
5879 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5880 input_data->len));
5881 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5882 PSA_AEAD_NONCE_MAX_SIZE,
5883 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005884 }
5885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005887
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005888 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005889#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5893 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5894 input_data->len),
5895 PSA_ERROR_INVALID_ARGUMENT);
5896 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5897 PSA_AEAD_NONCE_MAX_SIZE,
5898 &nonce_length),
5899 PSA_ERROR_BAD_STATE);
5900 } else {
5901 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5902 input_data->len));
5903 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5904 PSA_AEAD_NONCE_MAX_SIZE,
5905 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005906 }
5907
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005909#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005910
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005911 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5916 PSA_AEAD_NONCE_MAX_SIZE,
5917 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 if (operation.alg == PSA_ALG_CCM) {
5920 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5921 input_data->len),
5922 PSA_ERROR_INVALID_ARGUMENT);
5923 } else {
5924 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5925 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005926 }
5927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005929
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005930 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005931 /* Test for setting nonce after calling set lengths */
5932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5936 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005941
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005942 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005945
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (operation.alg == PSA_ALG_CCM) {
5947 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5948 input_data->len),
5949 PSA_ERROR_INVALID_ARGUMENT);
5950 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5951 PSA_ERROR_BAD_STATE);
5952 } else {
5953 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5954 input_data->len));
5955 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005956 }
5957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005959
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005960 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005961#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5965 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5966 input_data->len),
5967 PSA_ERROR_INVALID_ARGUMENT);
5968 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5969 PSA_ERROR_BAD_STATE);
5970 } else {
5971 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5972 input_data->len));
5973 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005974 }
5975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005977#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005978
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005979 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (operation.alg == PSA_ALG_CCM) {
5986 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5987 input_data->len),
5988 PSA_ERROR_INVALID_ARGUMENT);
5989 } else {
5990 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5991 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005992 }
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005995
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005996 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00005997#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if (operation.alg == PSA_ALG_GCM) {
6001 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6002 SIZE_MAX),
6003 PSA_ERROR_INVALID_ARGUMENT);
6004 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6005 PSA_ERROR_BAD_STATE);
6006 } else if (operation.alg != PSA_ALG_CCM) {
6007 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6008 SIZE_MAX));
6009 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006010 }
6011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006013#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006014
Tom Cosgrove1797b052022-12-04 17:19:59 +00006015 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006016#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 if (operation.alg == PSA_ALG_GCM) {
6022 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6023 SIZE_MAX),
6024 PSA_ERROR_INVALID_ARGUMENT);
6025 } else if (operation.alg != PSA_ALG_CCM) {
6026 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6027 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006028 }
6029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006031#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006032
6033 /* ------------------------------------------------------- */
6034
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6040 PSA_AEAD_NONCE_MAX_SIZE,
6041 &nonce_length),
6042 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006045
Paul Elliott7220cae2021-06-22 17:25:57 +01006046 /* Test for generating nonce in decrypt setup. */
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6051 PSA_AEAD_NONCE_MAX_SIZE,
6052 &nonce_length),
6053 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006056
Paul Elliottc23a9a02021-06-21 18:32:46 +01006057 /* Test for setting lengths twice. */
6058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006060
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6064 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6067 input_data->len),
6068 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006071
Andrzej Kurekad837522021-12-15 15:28:49 +01006072 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6081 additional_data->len),
6082 PSA_ERROR_BAD_STATE);
6083 } else {
6084 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6085 additional_data->len));
6086
6087 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6088 input_data->len),
6089 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006090 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006092
6093 /* ------------------------------------------------------- */
6094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (operation.alg == PSA_ALG_CCM) {
6100 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6101 input_data->len, output_data,
6102 output_size, &output_length),
6103 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 } else {
6106 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6107 input_data->len, output_data,
6108 output_size, &output_length));
6109
6110 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6111 input_data->len),
6112 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006115
6116 /* ------------------------------------------------------- */
6117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (operation.alg == PSA_ALG_CCM) {
6123 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6124 finish_output_size,
6125 &output_part_length,
6126 tag_buffer, tag_length,
6127 &tag_size));
6128 } else {
6129 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6130 finish_output_size,
6131 &output_part_length,
6132 tag_buffer, tag_length,
6133 &tag_size));
6134
6135 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6136 input_data->len),
6137 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006140
6141 /* Test for setting lengths after generating nonce + already starting data. */
6142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6146 PSA_AEAD_NONCE_MAX_SIZE,
6147 &nonce_length));
6148 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6151 additional_data->len),
6152 PSA_ERROR_BAD_STATE);
6153 } else {
6154 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6155 additional_data->len));
6156
6157 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6158 input_data->len),
6159 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006160 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006162
6163 /* ------------------------------------------------------- */
6164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6168 PSA_AEAD_NONCE_MAX_SIZE,
6169 &nonce_length));
6170 if (operation.alg == PSA_ALG_CCM) {
6171 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6172 input_data->len, output_data,
6173 output_size, &output_length),
6174 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 } else {
6177 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6178 input_data->len, output_data,
6179 output_size, &output_length));
6180
6181 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6182 input_data->len),
6183 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006184 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006186
6187 /* ------------------------------------------------------- */
6188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006190
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6192 PSA_AEAD_NONCE_MAX_SIZE,
6193 &nonce_length));
6194 if (operation.alg == PSA_ALG_CCM) {
6195 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6196 finish_output_size,
6197 &output_part_length,
6198 tag_buffer, tag_length,
6199 &tag_size));
6200 } else {
6201 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6202 finish_output_size,
6203 &output_part_length,
6204 tag_buffer, tag_length,
6205 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6208 input_data->len),
6209 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006210 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006212
Paul Elliott243080c2021-07-21 19:01:17 +01006213 /* Test for not sending any additional data or data after setting non zero
6214 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6221 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006222
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6224 finish_output_size,
6225 &output_part_length,
6226 tag_buffer, tag_length,
6227 &tag_size),
6228 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006231
Paul Elliott243080c2021-07-21 19:01:17 +01006232 /* Test for not sending any additional data or data after setting non-zero
6233 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6240 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6243 finish_output_size,
6244 &output_part_length,
6245 tag_buffer,
6246 tag_length),
6247 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Paul Elliott243080c2021-07-21 19:01:17 +01006251 /* Test for not sending any additional data after setting a non-zero length
6252 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6259 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6262 input_data->len, output_data,
6263 output_size, &output_length),
6264 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006267
Paul Elliottf94bd992021-09-19 18:15:59 +01006268 /* Test for not sending any data after setting a non-zero length for it.*/
6269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6275 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6278 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6281 finish_output_size,
6282 &output_part_length,
6283 tag_buffer, tag_length,
6284 &tag_size),
6285 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006288
Paul Elliottb0450fe2021-09-01 15:06:26 +01006289 /* Test for sending too much additional data after setting lengths. */
6290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006296
6297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6299 additional_data->len),
6300 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006303
Paul Elliotta2a09b02021-09-22 14:56:40 +01006304 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6311 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6314 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6317 1),
6318 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006321
Paul Elliottb0450fe2021-09-01 15:06:26 +01006322 /* Test for sending too much data after setting lengths. */
6323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6331 input_data->len, output_data,
6332 output_size, &output_length),
6333 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006336
Paul Elliotta2a09b02021-09-22 14:56:40 +01006337 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6344 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6347 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6350 input_data->len, output_data,
6351 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6354 1, output_data,
6355 output_size, &output_length),
6356 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006359
Paul Elliottc23a9a02021-06-21 18:32:46 +01006360 /* Test sending additional data after data. */
6361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 if (operation.alg != PSA_ALG_CCM) {
6367 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6368 input_data->len, output_data,
6369 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6372 additional_data->len),
6373 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006374 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006376
Paul Elliott534d0b42021-06-22 19:15:20 +01006377 /* Test calling finish on decryption. */
6378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6384 finish_output_size,
6385 &output_part_length,
6386 tag_buffer, tag_length,
6387 &tag_size),
6388 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006391
6392 /* Test calling verify on encryption. */
6393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6399 finish_output_size,
6400 &output_part_length,
6401 tag_buffer,
6402 tag_length),
6403 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006406
6407
Paul Elliottc23a9a02021-06-21 18:32:46 +01006408exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 psa_destroy_key(key);
6410 psa_aead_abort(&operation);
6411 mbedtls_free(output_data);
6412 mbedtls_free(final_data);
6413 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006414}
6415/* END_CASE */
6416
6417/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006418void signature_size(int type_arg,
6419 int bits,
6420 int alg_arg,
6421 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006422{
6423 psa_key_type_t type = type_arg;
6424 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006428
Gilles Peskinee59236f2018-01-27 23:32:46 +01006429exit:
6430 ;
6431}
6432/* END_CASE */
6433
6434/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006435void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6436 int alg_arg, data_t *input_data,
6437 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006438{
Ronald Cron5425a212020-08-04 14:58:35 +02006439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 psa_key_type_t key_type = key_type_arg;
6441 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006442 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006443 unsigned char *signature = NULL;
6444 size_t signature_size;
6445 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6451 psa_set_key_algorithm(&attributes, alg);
6452 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006453
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6455 &key));
6456 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6457 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006458
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006459 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006460 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6462 key_bits, alg);
6463 TEST_ASSERT(signature_size != 0);
6464 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6465 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006466
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006467 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 PSA_ASSERT(psa_sign_hash(key, alg,
6469 input_data->x, input_data->len,
6470 signature, signature_size,
6471 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006472 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 ASSERT_COMPARE(output_data->x, output_data->len,
6474 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006475
6476exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006477 /*
6478 * Key attributes may have been returned by psa_get_key_attributes()
6479 * thus reset them as required.
6480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 psa_destroy_key(key);
6484 mbedtls_free(signature);
6485 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006486}
6487/* END_CASE */
6488
Paul Elliott712d5122022-12-07 14:03:10 +00006489/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006490/**
6491 * sign_hash_interruptible() test intentions:
6492 *
6493 * Note: This test can currently only handle ECDSA.
6494 *
6495 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006496 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006497 *
6498 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6499 * expected for different max_ops values.
6500 *
6501 * 3. Test that the number of ops done prior to start and after abort is zero
6502 * and that each successful stage completes some ops (this is not mandated by
6503 * the PSA specification, but is currently the case).
6504 *
6505 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6506 * complete() calls does not alter the number of ops returned.
6507 */
Paul Elliott712d5122022-12-07 14:03:10 +00006508void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6509 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006510 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006511{
6512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6513 psa_key_type_t key_type = key_type_arg;
6514 psa_algorithm_t alg = alg_arg;
6515 size_t key_bits;
6516 unsigned char *signature = NULL;
6517 size_t signature_size;
6518 size_t signature_length = 0xdeadbeef;
6519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6520 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006521 uint32_t num_ops = 0;
6522 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006523 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006524 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006525 size_t min_completes = 0;
6526 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006527
Paul Elliott712d5122022-12-07 14:03:10 +00006528 psa_sign_hash_interruptible_operation_t operation =
6529 psa_sign_hash_interruptible_operation_init();
6530
6531 PSA_ASSERT(psa_crypto_init());
6532
6533 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6534 psa_set_key_algorithm(&attributes, alg);
6535 psa_set_key_type(&attributes, key_type);
6536
6537 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6538 &key));
6539 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6540 key_bits = psa_get_key_bits(&attributes);
6541
6542 /* Allocate a buffer which has the size advertised by the
6543 * library. */
6544 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6545 key_bits, alg);
6546 TEST_ASSERT(signature_size != 0);
6547 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6548 ASSERT_ALLOC(signature, signature_size);
6549
Paul Elliott0c683352022-12-16 19:16:56 +00006550 psa_interruptible_set_max_ops(max_ops);
6551
Paul Elliott6f600372023-02-06 18:41:05 +00006552 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6553 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006554
Paul Elliott712d5122022-12-07 14:03:10 +00006555 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6556 TEST_ASSERT(num_ops_prior == 0);
6557
6558 /* Start performing the signature. */
6559 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6560 input_data->x, input_data->len));
6561
6562 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6563 TEST_ASSERT(num_ops_prior == 0);
6564
6565 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006566 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006567 status = psa_sign_hash_complete(&operation, signature, signature_size,
6568 &signature_length);
6569
Paul Elliott0c683352022-12-16 19:16:56 +00006570 num_completes++;
6571
Paul Elliott712d5122022-12-07 14:03:10 +00006572 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6573 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006574 /* We are asserting here that every complete makes progress
6575 * (completes some ops), which is true of the internal
6576 * implementation and probably any implementation, however this is
6577 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006578 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006579
Paul Elliott712d5122022-12-07 14:03:10 +00006580 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006581
6582 /* Ensure calling get_num_ops() twice still returns the same
6583 * number of ops as previously reported. */
6584 num_ops = psa_sign_hash_get_num_ops(&operation);
6585
6586 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006587 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006588 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006589
6590 TEST_ASSERT(status == PSA_SUCCESS);
6591
Paul Elliott0c683352022-12-16 19:16:56 +00006592 TEST_LE_U(min_completes, num_completes);
6593 TEST_LE_U(num_completes, max_completes);
6594
Paul Elliott712d5122022-12-07 14:03:10 +00006595 /* Verify that the signature is what is expected. */
6596 ASSERT_COMPARE(output_data->x, output_data->len,
6597 signature, signature_length);
6598
6599 PSA_ASSERT(psa_sign_hash_abort(&operation));
6600
Paul Elliott59ad9452022-12-18 15:09:02 +00006601 num_ops = psa_sign_hash_get_num_ops(&operation);
6602 TEST_ASSERT(num_ops == 0);
6603
Paul Elliott712d5122022-12-07 14:03:10 +00006604exit:
6605
6606 /*
6607 * Key attributes may have been returned by psa_get_key_attributes()
6608 * thus reset them as required.
6609 */
6610 psa_reset_key_attributes(&attributes);
6611
6612 psa_destroy_key(key);
6613 mbedtls_free(signature);
6614 PSA_DONE();
6615}
6616/* END_CASE */
6617
Gilles Peskine20035e32018-02-03 22:44:14 +01006618/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006619void sign_hash_fail(int key_type_arg, data_t *key_data,
6620 int alg_arg, data_t *input_data,
6621 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006622{
Ronald Cron5425a212020-08-04 14:58:35 +02006623 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006624 psa_key_type_t key_type = key_type_arg;
6625 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006626 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006627 psa_status_t actual_status;
6628 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006629 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006630 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006632
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006634
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006636
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6638 psa_set_key_algorithm(&attributes, alg);
6639 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6642 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 actual_status = psa_sign_hash(key, alg,
6645 input_data->x, input_data->len,
6646 signature, signature_size,
6647 &signature_length);
6648 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006649 /* The value of *signature_length is unspecified on error, but
6650 * whatever it is, it should be less than signature_size, so that
6651 * if the caller tries to read *signature_length bytes without
6652 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006654
6655exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 psa_reset_key_attributes(&attributes);
6657 psa_destroy_key(key);
6658 mbedtls_free(signature);
6659 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006660}
6661/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006662
Paul Elliott91007972022-12-16 12:21:24 +00006663/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006664/**
6665 * sign_hash_fail_interruptible() test intentions:
6666 *
6667 * Note: This test can currently only handle ECDSA.
6668 *
6669 * 1. Test that various failure cases for interruptible sign hash fail with the
6670 * correct error codes, and at the correct point (at start or during
6671 * complete).
6672 *
6673 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6674 * expected for different max_ops values.
6675 *
6676 * 3. Test that the number of ops done prior to start and after abort is zero
6677 * and that each successful stage completes some ops (this is not mandated by
6678 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006679 *
6680 * 4. Check that calling complete() when start() fails and complete()
6681 * after completion results in a BAD_STATE error.
6682 *
6683 * 5. Check that calling start() again after start fails results in a BAD_STATE
6684 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006685 */
Paul Elliott91007972022-12-16 12:21:24 +00006686void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6687 int alg_arg, data_t *input_data,
6688 int signature_size_arg,
6689 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006690 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006691 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006692{
6693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6694 psa_key_type_t key_type = key_type_arg;
6695 psa_algorithm_t alg = alg_arg;
6696 size_t signature_size = signature_size_arg;
6697 psa_status_t actual_status;
6698 psa_status_t expected_start_status = expected_start_status_arg;
6699 psa_status_t expected_complete_status = expected_complete_status_arg;
6700 unsigned char *signature = NULL;
6701 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006702 uint32_t num_ops = 0;
6703 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006704 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006705 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006706 size_t min_completes = 0;
6707 size_t max_completes = 0;
6708
Paul Elliott91007972022-12-16 12:21:24 +00006709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6710 psa_sign_hash_interruptible_operation_t operation =
6711 psa_sign_hash_interruptible_operation_init();
6712
6713 ASSERT_ALLOC(signature, signature_size);
6714
6715 PSA_ASSERT(psa_crypto_init());
6716
6717 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6718 psa_set_key_algorithm(&attributes, alg);
6719 psa_set_key_type(&attributes, key_type);
6720
6721 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6722 &key));
6723
Paul Elliott0c683352022-12-16 19:16:56 +00006724 psa_interruptible_set_max_ops(max_ops);
6725
Paul Elliott6f600372023-02-06 18:41:05 +00006726 interruptible_signverify_get_minmax_completes(max_ops,
6727 expected_complete_status,
6728 &min_completes,
6729 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006730
Paul Elliott91007972022-12-16 12:21:24 +00006731 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6732 TEST_ASSERT(num_ops_prior == 0);
6733
6734 /* Start performing the signature. */
6735 actual_status = psa_sign_hash_start(&operation, key, alg,
6736 input_data->x, input_data->len);
6737
6738 TEST_EQUAL(actual_status, expected_start_status);
6739
Paul Elliottc9774412023-02-06 15:14:07 +00006740 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006741 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006742 * start failed. */
6743 actual_status = psa_sign_hash_complete(&operation, signature,
6744 signature_size,
6745 &signature_length);
6746
6747 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6748
6749 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006750 actual_status = psa_sign_hash_start(&operation, key, alg,
6751 input_data->x, input_data->len);
6752
6753 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6754 }
6755
Paul Elliott91007972022-12-16 12:21:24 +00006756 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6757 TEST_ASSERT(num_ops_prior == 0);
6758
Paul Elliott91007972022-12-16 12:21:24 +00006759 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006760 do {
Paul Elliott91007972022-12-16 12:21:24 +00006761 actual_status = psa_sign_hash_complete(&operation, signature,
6762 signature_size,
6763 &signature_length);
6764
Paul Elliott0c683352022-12-16 19:16:56 +00006765 num_completes++;
6766
Paul Elliott334d7262023-01-20 17:29:41 +00006767 if (actual_status == PSA_SUCCESS ||
6768 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006769 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006770 /* We are asserting here that every complete makes progress
6771 * (completes some ops), which is true of the internal
6772 * implementation and probably any implementation, however this is
6773 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006774 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006775
Paul Elliott91007972022-12-16 12:21:24 +00006776 num_ops_prior = num_ops;
6777 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006778 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006779
Paul Elliottc9774412023-02-06 15:14:07 +00006780 TEST_EQUAL(actual_status, expected_complete_status);
6781
Paul Elliottefebad02023-02-15 16:56:45 +00006782 /* Check that another complete returns BAD_STATE. */
6783 actual_status = psa_sign_hash_complete(&operation, signature,
6784 signature_size,
6785 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006786
Paul Elliottefebad02023-02-15 16:56:45 +00006787 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006788
Paul Elliott91007972022-12-16 12:21:24 +00006789 PSA_ASSERT(psa_sign_hash_abort(&operation));
6790
Paul Elliott59ad9452022-12-18 15:09:02 +00006791 num_ops = psa_sign_hash_get_num_ops(&operation);
6792 TEST_ASSERT(num_ops == 0);
6793
Paul Elliott91007972022-12-16 12:21:24 +00006794 /* The value of *signature_length is unspecified on error, but
6795 * whatever it is, it should be less than signature_size, so that
6796 * if the caller tries to read *signature_length bytes without
6797 * checking the error code then they don't overflow a buffer. */
6798 TEST_LE_U(signature_length, signature_size);
6799
Paul Elliott0c683352022-12-16 19:16:56 +00006800 TEST_LE_U(min_completes, num_completes);
6801 TEST_LE_U(num_completes, max_completes);
6802
Paul Elliott91007972022-12-16 12:21:24 +00006803exit:
6804 psa_reset_key_attributes(&attributes);
6805 psa_destroy_key(key);
6806 mbedtls_free(signature);
6807 PSA_DONE();
6808}
6809/* END_CASE */
6810
mohammad16038cc1cee2018-03-28 01:21:33 +03006811/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006812void sign_verify_hash(int key_type_arg, data_t *key_data,
6813 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006814{
Ronald Cron5425a212020-08-04 14:58:35 +02006815 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006816 psa_key_type_t key_type = key_type_arg;
6817 psa_algorithm_t alg = alg_arg;
6818 size_t key_bits;
6819 unsigned char *signature = NULL;
6820 size_t signature_size;
6821 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006825
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6827 psa_set_key_algorithm(&attributes, alg);
6828 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006829
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6831 &key));
6832 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6833 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006834
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006835 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006836 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6838 key_bits, alg);
6839 TEST_ASSERT(signature_size != 0);
6840 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6841 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006842
6843 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 PSA_ASSERT(psa_sign_hash(key, alg,
6845 input_data->x, input_data->len,
6846 signature, signature_size,
6847 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006848 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 TEST_LE_U(signature_length, signature_size);
6850 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006851
6852 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 PSA_ASSERT(psa_verify_hash(key, alg,
6854 input_data->x, input_data->len,
6855 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006858 /* Flip a bit in the input and verify that the signature is now
6859 * detected as invalid. Flip a bit at the beginning, not at the end,
6860 * because ECDSA may ignore the last few bits of the input. */
6861 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 TEST_EQUAL(psa_verify_hash(key, alg,
6863 input_data->x, input_data->len,
6864 signature, signature_length),
6865 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006866 }
6867
6868exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006869 /*
6870 * Key attributes may have been returned by psa_get_key_attributes()
6871 * thus reset them as required.
6872 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 psa_destroy_key(key);
6876 mbedtls_free(signature);
6877 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006878}
6879/* END_CASE */
6880
Paul Elliott712d5122022-12-07 14:03:10 +00006881/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006882/**
6883 * sign_verify_hash_interruptible() test intentions:
6884 *
6885 * Note: This test can currently only handle ECDSA.
6886 *
Paul Elliott8c092052023-03-06 17:49:14 +00006887 * 1. Test that we can sign an input hash with the given keypair and then
6888 * afterwards verify that signature. This is currently the only way to test
6889 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006890 *
6891 * 2. Test that after corrupting the hash, the verification detects an invalid
6892 * signature.
6893 *
6894 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6895 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006896 *
6897 * 4. Test that the number of ops done prior to starting signing and after abort
6898 * is zero and that each successful signing stage completes some ops (this is
6899 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006900 */
Paul Elliott712d5122022-12-07 14:03:10 +00006901void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006902 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006903 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006904{
6905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6906 psa_key_type_t key_type = key_type_arg;
6907 psa_algorithm_t alg = alg_arg;
6908 size_t key_bits;
6909 unsigned char *signature = NULL;
6910 size_t signature_size;
6911 size_t signature_length = 0xdeadbeef;
6912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6913 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006914 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006915 uint32_t num_ops = 0;
6916 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006917 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006918 size_t min_completes = 0;
6919 size_t max_completes = 0;
6920
Paul Elliott712d5122022-12-07 14:03:10 +00006921 psa_sign_hash_interruptible_operation_t sign_operation =
6922 psa_sign_hash_interruptible_operation_init();
6923 psa_verify_hash_interruptible_operation_t verify_operation =
6924 psa_verify_hash_interruptible_operation_init();
6925
6926 PSA_ASSERT(psa_crypto_init());
6927
Paul Elliott0c683352022-12-16 19:16:56 +00006928 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6929 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006930 psa_set_key_algorithm(&attributes, alg);
6931 psa_set_key_type(&attributes, key_type);
6932
6933 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6934 &key));
6935 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6936 key_bits = psa_get_key_bits(&attributes);
6937
6938 /* Allocate a buffer which has the size advertised by the
6939 * library. */
6940 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6941 key_bits, alg);
6942 TEST_ASSERT(signature_size != 0);
6943 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6944 ASSERT_ALLOC(signature, signature_size);
6945
Paul Elliott0c683352022-12-16 19:16:56 +00006946 psa_interruptible_set_max_ops(max_ops);
6947
Paul Elliott6f600372023-02-06 18:41:05 +00006948 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6949 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006950
Paul Elliott7c173082023-02-26 18:44:45 +00006951 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6952 TEST_ASSERT(num_ops_prior == 0);
6953
Paul Elliott712d5122022-12-07 14:03:10 +00006954 /* Start performing the signature. */
6955 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6956 input_data->x, input_data->len));
6957
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 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006962 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006963
Paul Elliott0c683352022-12-16 19:16:56 +00006964 status = psa_sign_hash_complete(&sign_operation, signature,
6965 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006966 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006967
6968 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006969
6970 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6971 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6972 /* We are asserting here that every complete makes progress
6973 * (completes some ops), which is true of the internal
6974 * implementation and probably any implementation, however this is
6975 * not mandated by the PSA specification. */
6976 TEST_ASSERT(num_ops > num_ops_prior);
6977
6978 num_ops_prior = num_ops;
6979 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006980 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006981
6982 TEST_ASSERT(status == PSA_SUCCESS);
6983
Paul Elliott0c683352022-12-16 19:16:56 +00006984 TEST_LE_U(min_completes, num_completes);
6985 TEST_LE_U(num_completes, max_completes);
6986
Paul Elliott712d5122022-12-07 14:03:10 +00006987 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
6988
Paul Elliott7c173082023-02-26 18:44:45 +00006989 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6990 TEST_ASSERT(num_ops == 0);
6991
Paul Elliott712d5122022-12-07 14:03:10 +00006992 /* Check that the signature length looks sensible. */
6993 TEST_LE_U(signature_length, signature_size);
6994 TEST_ASSERT(signature_length > 0);
6995
Paul Elliott0c683352022-12-16 19:16:56 +00006996 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00006997
6998 /* Start verification. */
6999 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7000 input_data->x, input_data->len,
7001 signature, signature_length));
7002
7003 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007004 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007005 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007006
7007 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007008 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007009
7010 TEST_ASSERT(status == PSA_SUCCESS);
7011
Paul Elliott0c683352022-12-16 19:16:56 +00007012 TEST_LE_U(min_completes, num_completes);
7013 TEST_LE_U(num_completes, max_completes);
7014
Paul Elliott712d5122022-12-07 14:03:10 +00007015 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7016
7017 verify_operation = psa_verify_hash_interruptible_operation_init();
7018
7019 if (input_data->len != 0) {
7020 /* Flip a bit in the input and verify that the signature is now
7021 * detected as invalid. Flip a bit at the beginning, not at the end,
7022 * because ECDSA may ignore the last few bits of the input. */
7023 input_data->x[0] ^= 1;
7024
Paul Elliott712d5122022-12-07 14:03:10 +00007025 /* Start verification. */
7026 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7027 input_data->x, input_data->len,
7028 signature, signature_length));
7029
7030 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007031 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007032 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007033 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007034
7035 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7036 }
7037
7038 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7039
7040exit:
7041 /*
7042 * Key attributes may have been returned by psa_get_key_attributes()
7043 * thus reset them as required.
7044 */
7045 psa_reset_key_attributes(&attributes);
7046
7047 psa_destroy_key(key);
7048 mbedtls_free(signature);
7049 PSA_DONE();
7050}
7051/* END_CASE */
7052
Gilles Peskine9911b022018-06-29 17:30:48 +02007053/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007054void verify_hash(int key_type_arg, data_t *key_data,
7055 int alg_arg, data_t *hash_data,
7056 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007057{
Ronald Cron5425a212020-08-04 14:58:35 +02007058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007059 psa_key_type_t key_type = key_type_arg;
7060 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007062
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007066
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7068 psa_set_key_algorithm(&attributes, alg);
7069 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7072 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 PSA_ASSERT(psa_verify_hash(key, alg,
7075 hash_data->x, hash_data->len,
7076 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007077
itayzafrir5c753392018-05-08 11:18:38 +03007078exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 psa_reset_key_attributes(&attributes);
7080 psa_destroy_key(key);
7081 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007082}
7083/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007084
Paul Elliott712d5122022-12-07 14:03:10 +00007085/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007086/**
7087 * verify_hash_interruptible() test intentions:
7088 *
7089 * Note: This test can currently only handle ECDSA.
7090 *
7091 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007092 * only). Given this test only does verification it can accept public keys as
7093 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007094 *
7095 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7096 * expected for different max_ops values.
7097 *
7098 * 3. Test that the number of ops done prior to start and after abort is zero
7099 * and that each successful stage completes some ops (this is not mandated by
7100 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007101 *
7102 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7103 * complete() calls does not alter the number of ops returned.
7104 *
7105 * 5. Test that after corrupting the hash, the verification detects an invalid
7106 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007107 */
Paul Elliott712d5122022-12-07 14:03:10 +00007108void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7109 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007110 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007111{
7112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7113 psa_key_type_t key_type = key_type_arg;
7114 psa_algorithm_t alg = alg_arg;
7115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7116 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007117 uint32_t num_ops = 0;
7118 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007119 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007120 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007121 size_t min_completes = 0;
7122 size_t max_completes = 0;
7123
Paul Elliott712d5122022-12-07 14:03:10 +00007124 psa_verify_hash_interruptible_operation_t operation =
7125 psa_verify_hash_interruptible_operation_init();
7126
7127 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7128
7129 PSA_ASSERT(psa_crypto_init());
7130
7131 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7132 psa_set_key_algorithm(&attributes, alg);
7133 psa_set_key_type(&attributes, key_type);
7134
7135 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7136 &key));
7137
Paul Elliott0c683352022-12-16 19:16:56 +00007138 psa_interruptible_set_max_ops(max_ops);
7139
Paul Elliott6f600372023-02-06 18:41:05 +00007140 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7141 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007142
Paul Elliott712d5122022-12-07 14:03:10 +00007143 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7144
7145 TEST_ASSERT(num_ops_prior == 0);
7146
7147 /* Start verification. */
7148 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7149 hash_data->x, hash_data->len,
7150 signature_data->x, signature_data->len)
7151 );
7152
7153 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7154
7155 TEST_ASSERT(num_ops_prior == 0);
7156
7157 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007158 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007159 status = psa_verify_hash_complete(&operation);
7160
Paul Elliott0c683352022-12-16 19:16:56 +00007161 num_completes++;
7162
Paul Elliott712d5122022-12-07 14:03:10 +00007163 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7164 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007165 /* We are asserting here that every complete makes progress
7166 * (completes some ops), which is true of the internal
7167 * implementation and probably any implementation, however this is
7168 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007169 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007170
Paul Elliott712d5122022-12-07 14:03:10 +00007171 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007172
7173 /* Ensure calling get_num_ops() twice still returns the same
7174 * number of ops as previously reported. */
7175 num_ops = psa_verify_hash_get_num_ops(&operation);
7176
7177 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007178 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007179 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007180
7181 TEST_ASSERT(status == PSA_SUCCESS);
7182
Paul Elliott0c683352022-12-16 19:16:56 +00007183 TEST_LE_U(min_completes, num_completes);
7184 TEST_LE_U(num_completes, max_completes);
7185
Paul Elliott712d5122022-12-07 14:03:10 +00007186 PSA_ASSERT(psa_verify_hash_abort(&operation));
7187
Paul Elliott59ad9452022-12-18 15:09:02 +00007188 num_ops = psa_verify_hash_get_num_ops(&operation);
7189 TEST_ASSERT(num_ops == 0);
7190
Paul Elliott8359c142023-02-24 18:40:10 +00007191 if (hash_data->len != 0) {
7192 /* Flip a bit in the hash and verify that the signature is now detected
7193 * as invalid. Flip a bit at the beginning, not at the end, because
7194 * ECDSA may ignore the last few bits of the input. */
7195 hash_data->x[0] ^= 1;
7196
7197 /* Start verification. */
7198 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7199 hash_data->x, hash_data->len,
7200 signature_data->x, signature_data->len));
7201
7202 /* Continue performing the signature until complete. */
7203 do {
7204 status = psa_verify_hash_complete(&operation);
7205 } while (status == PSA_OPERATION_INCOMPLETE);
7206
7207 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7208 }
7209
Paul Elliott712d5122022-12-07 14:03:10 +00007210exit:
7211 psa_reset_key_attributes(&attributes);
7212 psa_destroy_key(key);
7213 PSA_DONE();
7214}
7215/* END_CASE */
7216
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007217/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007218void verify_hash_fail(int key_type_arg, data_t *key_data,
7219 int alg_arg, data_t *hash_data,
7220 data_t *signature_data,
7221 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007222{
Ronald Cron5425a212020-08-04 14:58:35 +02007223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007224 psa_key_type_t key_type = key_type_arg;
7225 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007226 psa_status_t actual_status;
7227 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007229
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007231
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7233 psa_set_key_algorithm(&attributes, alg);
7234 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7237 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 actual_status = psa_verify_hash(key, alg,
7240 hash_data->x, hash_data->len,
7241 signature_data->x, signature_data->len);
7242 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007243
7244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 psa_reset_key_attributes(&attributes);
7246 psa_destroy_key(key);
7247 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007248}
7249/* END_CASE */
7250
Paul Elliott91007972022-12-16 12:21:24 +00007251/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007252/**
7253 * verify_hash_fail_interruptible() test intentions:
7254 *
7255 * Note: This test can currently only handle ECDSA.
7256 *
7257 * 1. Test that various failure cases for interruptible verify hash fail with
7258 * the correct error codes, and at the correct point (at start or during
7259 * complete).
7260 *
7261 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7262 * expected for different max_ops values.
7263 *
7264 * 3. Test that the number of ops done prior to start and after abort is zero
7265 * and that each successful stage completes some ops (this is not mandated by
7266 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007267 *
7268 * 4. Check that calling complete() when start() fails and complete()
7269 * after completion results in a BAD_STATE error.
7270 *
7271 * 5. Check that calling start() again after start fails results in a BAD_STATE
7272 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007273 */
Paul Elliott91007972022-12-16 12:21:24 +00007274void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7275 int alg_arg, data_t *hash_data,
7276 data_t *signature_data,
7277 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007278 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007279 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007280{
7281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7282 psa_key_type_t key_type = key_type_arg;
7283 psa_algorithm_t alg = alg_arg;
7284 psa_status_t actual_status;
7285 psa_status_t expected_start_status = expected_start_status_arg;
7286 psa_status_t expected_complete_status = expected_complete_status_arg;
7287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007288 uint32_t num_ops = 0;
7289 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007290 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007291 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007292 size_t min_completes = 0;
7293 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007294 psa_verify_hash_interruptible_operation_t operation =
7295 psa_verify_hash_interruptible_operation_init();
7296
7297 PSA_ASSERT(psa_crypto_init());
7298
7299 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7300 psa_set_key_algorithm(&attributes, alg);
7301 psa_set_key_type(&attributes, key_type);
7302
7303 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7304 &key));
7305
Paul Elliott0c683352022-12-16 19:16:56 +00007306 psa_interruptible_set_max_ops(max_ops);
7307
Paul Elliott6f600372023-02-06 18:41:05 +00007308 interruptible_signverify_get_minmax_completes(max_ops,
7309 expected_complete_status,
7310 &min_completes,
7311 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007312
Paul Elliott91007972022-12-16 12:21:24 +00007313 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7314 TEST_ASSERT(num_ops_prior == 0);
7315
7316 /* Start verification. */
7317 actual_status = psa_verify_hash_start(&operation, key, alg,
7318 hash_data->x, hash_data->len,
7319 signature_data->x,
7320 signature_data->len);
7321
7322 TEST_EQUAL(actual_status, expected_start_status);
7323
Paul Elliottc9774412023-02-06 15:14:07 +00007324 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007325 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007326 * start failed. */
7327 actual_status = psa_verify_hash_complete(&operation);
7328
7329 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7330
7331 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007332 actual_status = psa_verify_hash_start(&operation, key, alg,
7333 hash_data->x, hash_data->len,
7334 signature_data->x,
7335 signature_data->len);
7336
7337 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7338 }
7339
Paul Elliott91007972022-12-16 12:21:24 +00007340 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7341 TEST_ASSERT(num_ops_prior == 0);
7342
Paul Elliott91007972022-12-16 12:21:24 +00007343 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007344 do {
Paul Elliott91007972022-12-16 12:21:24 +00007345 actual_status = psa_verify_hash_complete(&operation);
7346
Paul Elliott0c683352022-12-16 19:16:56 +00007347 num_completes++;
7348
Paul Elliott334d7262023-01-20 17:29:41 +00007349 if (actual_status == PSA_SUCCESS ||
7350 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007351 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007352 /* We are asserting here that every complete makes progress
7353 * (completes some ops), which is true of the internal
7354 * implementation and probably any implementation, however this is
7355 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007356 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007357
Paul Elliott91007972022-12-16 12:21:24 +00007358 num_ops_prior = num_ops;
7359 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007360 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007361
Paul Elliottc9774412023-02-06 15:14:07 +00007362 TEST_EQUAL(actual_status, expected_complete_status);
7363
Paul Elliottefebad02023-02-15 16:56:45 +00007364 /* Check that another complete returns BAD_STATE. */
7365 actual_status = psa_verify_hash_complete(&operation);
7366 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007367
Paul Elliott0c683352022-12-16 19:16:56 +00007368 TEST_LE_U(min_completes, num_completes);
7369 TEST_LE_U(num_completes, max_completes);
7370
Paul Elliott91007972022-12-16 12:21:24 +00007371 PSA_ASSERT(psa_verify_hash_abort(&operation));
7372
Paul Elliott59ad9452022-12-18 15:09:02 +00007373 num_ops = psa_verify_hash_get_num_ops(&operation);
7374 TEST_ASSERT(num_ops == 0);
7375
Paul Elliott91007972022-12-16 12:21:24 +00007376exit:
7377 psa_reset_key_attributes(&attributes);
7378 psa_destroy_key(key);
7379 PSA_DONE();
7380}
7381/* END_CASE */
7382
Paul Elliott20a36062022-12-18 13:21:25 +00007383/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007384/**
7385 * interruptible_signverify_hash_state_test() test intentions:
7386 *
7387 * Note: This test can currently only handle ECDSA.
7388 *
7389 * 1. Test that calling the various interruptible sign and verify hash functions
7390 * in incorrect orders returns BAD_STATE errors.
7391 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007392void interruptible_signverify_hash_state_test(int key_type_arg,
7393 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007394{
7395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7396 psa_key_type_t key_type = key_type_arg;
7397 psa_algorithm_t alg = alg_arg;
7398 size_t key_bits;
7399 unsigned char *signature = NULL;
7400 size_t signature_size;
7401 size_t signature_length = 0xdeadbeef;
7402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7403 psa_sign_hash_interruptible_operation_t sign_operation =
7404 psa_sign_hash_interruptible_operation_init();
7405 psa_verify_hash_interruptible_operation_t verify_operation =
7406 psa_verify_hash_interruptible_operation_init();
7407
7408 PSA_ASSERT(psa_crypto_init());
7409
7410 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7411 PSA_KEY_USAGE_VERIFY_HASH);
7412 psa_set_key_algorithm(&attributes, alg);
7413 psa_set_key_type(&attributes, key_type);
7414
7415 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7416 &key));
7417 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7418 key_bits = psa_get_key_bits(&attributes);
7419
7420 /* Allocate a buffer which has the size advertised by the
7421 * library. */
7422 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7423 key_bits, alg);
7424 TEST_ASSERT(signature_size != 0);
7425 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7426 ASSERT_ALLOC(signature, signature_size);
7427
7428 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7429
7430 /* --- Attempt completes prior to starts --- */
7431 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7432 signature_size,
7433 &signature_length),
7434 PSA_ERROR_BAD_STATE);
7435
7436 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7437
7438 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7439 PSA_ERROR_BAD_STATE);
7440
7441 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7442
7443 /* --- Aborts in all other places. --- */
7444 psa_sign_hash_abort(&sign_operation);
7445
7446 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7447 input_data->x, input_data->len));
7448
7449 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7450
7451 psa_interruptible_set_max_ops(1);
7452
7453 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7454 input_data->x, input_data->len));
7455
7456 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7457 signature_size,
7458 &signature_length),
7459 PSA_OPERATION_INCOMPLETE);
7460
7461 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7462
7463 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7464
7465 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7466 input_data->x, input_data->len));
7467
7468 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7469 signature_size,
7470 &signature_length));
7471
7472 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7473
7474 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7475
7476 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7477 input_data->x, input_data->len,
7478 signature, signature_length));
7479
7480 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7481
7482 psa_interruptible_set_max_ops(1);
7483
7484 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7485 input_data->x, input_data->len,
7486 signature, signature_length));
7487
7488 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7489 PSA_OPERATION_INCOMPLETE);
7490
7491 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7492
7493 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7494
7495 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7496 input_data->x, input_data->len,
7497 signature, signature_length));
7498
7499 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7500
7501 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7502
7503 /* --- Attempt double starts. --- */
7504
7505 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7506 input_data->x, input_data->len));
7507
7508 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7509 input_data->x, input_data->len),
7510 PSA_ERROR_BAD_STATE);
7511
7512 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7513
7514 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7515 input_data->x, input_data->len,
7516 signature, signature_length));
7517
7518 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7519 input_data->x, input_data->len,
7520 signature, signature_length),
7521 PSA_ERROR_BAD_STATE);
7522
7523 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7524
Paul Elliott76d671a2023-02-07 17:45:18 +00007525exit:
7526 /*
7527 * Key attributes may have been returned by psa_get_key_attributes()
7528 * thus reset them as required.
7529 */
7530 psa_reset_key_attributes(&attributes);
7531
7532 psa_destroy_key(key);
7533 mbedtls_free(signature);
7534 PSA_DONE();
7535}
7536/* END_CASE */
7537
7538/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007539/**
Paul Elliottc2033502023-02-26 17:09:14 +00007540 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007541 *
7542 * Note: This test can currently only handle ECDSA.
7543 *
7544 * 1. Test various edge cases in the interruptible sign and verify hash
7545 * interfaces.
7546 */
Paul Elliottc2033502023-02-26 17:09:14 +00007547void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007548 data_t *key_data, int alg_arg, data_t *input_data)
7549{
7550 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7551 psa_key_type_t key_type = key_type_arg;
7552 psa_algorithm_t alg = alg_arg;
7553 size_t key_bits;
7554 unsigned char *signature = NULL;
7555 size_t signature_size;
7556 size_t signature_length = 0xdeadbeef;
7557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7558 uint8_t *input_buffer = NULL;
7559 psa_sign_hash_interruptible_operation_t sign_operation =
7560 psa_sign_hash_interruptible_operation_init();
7561 psa_verify_hash_interruptible_operation_t verify_operation =
7562 psa_verify_hash_interruptible_operation_init();
7563
7564 PSA_ASSERT(psa_crypto_init());
7565
7566 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7567 PSA_KEY_USAGE_VERIFY_HASH);
7568 psa_set_key_algorithm(&attributes, alg);
7569 psa_set_key_type(&attributes, key_type);
7570
7571 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7572 &key));
7573 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7574 key_bits = psa_get_key_bits(&attributes);
7575
7576 /* Allocate a buffer which has the size advertised by the
7577 * library. */
7578 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7579 key_bits, alg);
7580 TEST_ASSERT(signature_size != 0);
7581 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7582 ASSERT_ALLOC(signature, signature_size);
7583
Paul Elliott20a36062022-12-18 13:21:25 +00007584 /* --- Change function inputs mid run, to cause an error (sign only,
7585 * verify passes all inputs to start. --- */
7586
7587 psa_interruptible_set_max_ops(1);
7588
7589 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7590 input_data->x, input_data->len));
7591
7592 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7593 signature_size,
7594 &signature_length),
7595 PSA_OPERATION_INCOMPLETE);
7596
7597 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7598 0,
7599 &signature_length),
7600 PSA_ERROR_BUFFER_TOO_SMALL);
7601
Paul Elliottc9774412023-02-06 15:14:07 +00007602 /* And test that this invalidates the operation. */
7603 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7604 0,
7605 &signature_length),
7606 PSA_ERROR_BAD_STATE);
7607
Paul Elliott20a36062022-12-18 13:21:25 +00007608 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7609
Paul Elliottf9c91a72023-02-05 18:06:38 +00007610 /* Trash the hash buffer in between start and complete, to ensure
7611 * no reliance on external buffers. */
7612 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7613
7614 input_buffer = mbedtls_calloc(1, input_data->len);
7615 TEST_ASSERT(input_buffer != NULL);
7616
7617 memcpy(input_buffer, input_data->x, input_data->len);
7618
7619 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7620 input_buffer, input_data->len));
7621
7622 memset(input_buffer, '!', input_data->len);
7623 mbedtls_free(input_buffer);
7624 input_buffer = NULL;
7625
7626 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7627 signature_size,
7628 &signature_length));
7629
7630 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7631
7632 input_buffer = mbedtls_calloc(1, input_data->len);
7633 TEST_ASSERT(input_buffer != NULL);
7634
7635 memcpy(input_buffer, input_data->x, input_data->len);
7636
7637 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7638 input_buffer, input_data->len,
7639 signature, signature_length));
7640
7641 memset(input_buffer, '!', input_data->len);
7642 mbedtls_free(input_buffer);
7643 input_buffer = NULL;
7644
7645 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7646
7647 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7648
Paul Elliott20a36062022-12-18 13:21:25 +00007649exit:
7650 /*
7651 * Key attributes may have been returned by psa_get_key_attributes()
7652 * thus reset them as required.
7653 */
7654 psa_reset_key_attributes(&attributes);
7655
7656 psa_destroy_key(key);
7657 mbedtls_free(signature);
7658 PSA_DONE();
7659}
7660/* END_CASE */
7661
Paul Elliotta4cb9092023-02-07 18:01:55 +00007662/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007663/**
Paul Elliott57702242023-02-26 20:36:10 +00007664 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007665 *
7666 * Note: This test can currently only handle ECDSA.
7667 *
7668 * 1. Test that setting max ops is reflected in both interruptible sign and
7669 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007670 * 2. Test that changing the value of max_ops to unlimited during an operation
7671 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007672 *
7673 * 3. Test that calling get_num_ops() between complete calls gives the same
7674 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007675 */
Paul Elliott57702242023-02-26 20:36:10 +00007676void interruptible_signverify_hash_ops_tests(int key_type_arg,
7677 data_t *key_data, int alg_arg,
7678 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007679{
7680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7681 psa_key_type_t key_type = key_type_arg;
7682 psa_algorithm_t alg = alg_arg;
7683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007684 size_t key_bits;
7685 unsigned char *signature = NULL;
7686 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007687 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007688 uint32_t num_ops = 0;
7689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7690
Paul Elliotta4cb9092023-02-07 18:01:55 +00007691 psa_sign_hash_interruptible_operation_t sign_operation =
7692 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007693 psa_verify_hash_interruptible_operation_t verify_operation =
7694 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007695
7696 PSA_ASSERT(psa_crypto_init());
7697
7698 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7699 PSA_KEY_USAGE_VERIFY_HASH);
7700 psa_set_key_algorithm(&attributes, alg);
7701 psa_set_key_type(&attributes, key_type);
7702
Paul Elliottf1743e22023-02-15 18:44:16 +00007703 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7704 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7705 key_bits = psa_get_key_bits(&attributes);
7706
7707 /* Allocate a buffer which has the size advertised by the
7708 * library. */
7709 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7710
7711 TEST_ASSERT(signature_size != 0);
7712 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7713 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007714
7715 /* Check that default max ops gets set if we don't set it. */
7716 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7717 input_data->x, input_data->len));
7718
7719 TEST_EQUAL(psa_interruptible_get_max_ops(),
7720 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7721
7722 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7723
Paul Elliottf1743e22023-02-15 18:44:16 +00007724 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7725 input_data->x, input_data->len,
7726 signature, signature_size));
7727
7728 TEST_EQUAL(psa_interruptible_get_max_ops(),
7729 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7730
7731 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7732
Paul Elliotta4cb9092023-02-07 18:01:55 +00007733 /* Check that max ops gets set properly. */
7734
7735 psa_interruptible_set_max_ops(0xbeef);
7736
Paul Elliottf1743e22023-02-15 18:44:16 +00007737 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007738
Paul Elliott9e8819f2023-02-26 19:01:35 +00007739 /* --- Ensure changing the max ops mid operation works (operation should
7740 * complete successfully after setting max ops to unlimited --- */
7741 psa_interruptible_set_max_ops(1);
7742
7743 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7744 input_data->x, input_data->len));
7745
7746 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7747 signature_size,
7748 &signature_length),
7749 PSA_OPERATION_INCOMPLETE);
7750
7751 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7752
7753 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7754 signature_size,
7755 &signature_length));
7756
7757 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7758
7759 psa_interruptible_set_max_ops(1);
7760
7761 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7762 input_data->x, input_data->len,
7763 signature, signature_length));
7764
7765 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7766 PSA_OPERATION_INCOMPLETE);
7767
7768 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7769
7770 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7771
7772 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7773
Paul Elliottc1e04002023-02-26 20:27:23 +00007774 /* --- Test that not calling get_num_ops inbetween complete calls does not
7775 * result in lost ops. ---*/
7776
7777 psa_interruptible_set_max_ops(1);
7778
7779 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7780 input_data->x, input_data->len));
7781
7782 /* Continue performing the signature until complete. */
7783 do {
7784 status = psa_sign_hash_complete(&sign_operation, signature,
7785 signature_size,
7786 &signature_length);
7787
7788 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7789
7790 } while (status == PSA_OPERATION_INCOMPLETE);
7791
7792 PSA_ASSERT(status);
7793
7794 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7795
7796 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7797 input_data->x, input_data->len));
7798
7799 /* Continue performing the signature until complete. */
7800 do {
7801 status = psa_sign_hash_complete(&sign_operation, signature,
7802 signature_size,
7803 &signature_length);
7804 } while (status == PSA_OPERATION_INCOMPLETE);
7805
7806 PSA_ASSERT(status);
7807
7808 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7809
7810 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7811
7812 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7813 input_data->x, input_data->len,
7814 signature, signature_length));
7815
7816 /* Continue performing the verification until complete. */
7817 do {
7818 status = psa_verify_hash_complete(&verify_operation);
7819
7820 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7821
7822 } while (status == PSA_OPERATION_INCOMPLETE);
7823
7824 PSA_ASSERT(status);
7825
7826 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7827
7828 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7829 input_data->x, input_data->len,
7830 signature, signature_length));
7831
7832 /* Continue performing the verification until complete. */
7833 do {
7834 status = psa_verify_hash_complete(&verify_operation);
7835
7836 } while (status == PSA_OPERATION_INCOMPLETE);
7837
7838 PSA_ASSERT(status);
7839
7840 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7841
7842 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7843
Paul Elliotta4cb9092023-02-07 18:01:55 +00007844exit:
7845 /*
7846 * Key attributes may have been returned by psa_get_key_attributes()
7847 * thus reset them as required.
7848 */
7849 psa_reset_key_attributes(&attributes);
7850
7851 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007852 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007853 PSA_DONE();
7854}
7855/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007856
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007857/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007858void sign_message_deterministic(int key_type_arg,
7859 data_t *key_data,
7860 int alg_arg,
7861 data_t *input_data,
7862 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007863{
7864 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7865 psa_key_type_t key_type = key_type_arg;
7866 psa_algorithm_t alg = alg_arg;
7867 size_t key_bits;
7868 unsigned char *signature = NULL;
7869 size_t signature_size;
7870 size_t signature_length = 0xdeadbeef;
7871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7872
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007874
Gilles Peskine449bd832023-01-11 14:50:10 +01007875 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7876 psa_set_key_algorithm(&attributes, alg);
7877 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007878
Gilles Peskine449bd832023-01-11 14:50:10 +01007879 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7880 &key));
7881 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7882 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007883
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7885 TEST_ASSERT(signature_size != 0);
7886 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7887 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007888
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 PSA_ASSERT(psa_sign_message(key, alg,
7890 input_data->x, input_data->len,
7891 signature, signature_size,
7892 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007893
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 ASSERT_COMPARE(output_data->x, output_data->len,
7895 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007896
7897exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 psa_destroy_key(key);
7901 mbedtls_free(signature);
7902 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007903
7904}
7905/* END_CASE */
7906
7907/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007908void sign_message_fail(int key_type_arg,
7909 data_t *key_data,
7910 int alg_arg,
7911 data_t *input_data,
7912 int signature_size_arg,
7913 int expected_status_arg)
7914{
7915 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7916 psa_key_type_t key_type = key_type_arg;
7917 psa_algorithm_t alg = alg_arg;
7918 size_t signature_size = signature_size_arg;
7919 psa_status_t actual_status;
7920 psa_status_t expected_status = expected_status_arg;
7921 unsigned char *signature = NULL;
7922 size_t signature_length = 0xdeadbeef;
7923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7924
7925 ASSERT_ALLOC(signature, signature_size);
7926
7927 PSA_ASSERT(psa_crypto_init());
7928
7929 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7930 psa_set_key_algorithm(&attributes, alg);
7931 psa_set_key_type(&attributes, key_type);
7932
7933 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7934 &key));
7935
7936 actual_status = psa_sign_message(key, alg,
7937 input_data->x, input_data->len,
7938 signature, signature_size,
7939 &signature_length);
7940 TEST_EQUAL(actual_status, expected_status);
7941 /* The value of *signature_length is unspecified on error, but
7942 * whatever it is, it should be less than signature_size, so that
7943 * if the caller tries to read *signature_length bytes without
7944 * checking the error code then they don't overflow a buffer. */
7945 TEST_LE_U(signature_length, signature_size);
7946
7947exit:
7948 psa_reset_key_attributes(&attributes);
7949 psa_destroy_key(key);
7950 mbedtls_free(signature);
7951 PSA_DONE();
7952}
7953/* END_CASE */
7954
7955/* BEGIN_CASE */
7956void sign_verify_message(int key_type_arg,
7957 data_t *key_data,
7958 int alg_arg,
7959 data_t *input_data)
7960{
7961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7962 psa_key_type_t key_type = key_type_arg;
7963 psa_algorithm_t alg = alg_arg;
7964 size_t key_bits;
7965 unsigned char *signature = NULL;
7966 size_t signature_size;
7967 size_t signature_length = 0xdeadbeef;
7968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7969
7970 PSA_ASSERT(psa_crypto_init());
7971
7972 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7973 PSA_KEY_USAGE_VERIFY_MESSAGE);
7974 psa_set_key_algorithm(&attributes, alg);
7975 psa_set_key_type(&attributes, key_type);
7976
7977 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7978 &key));
7979 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7980 key_bits = psa_get_key_bits(&attributes);
7981
7982 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7983 TEST_ASSERT(signature_size != 0);
7984 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7985 ASSERT_ALLOC(signature, signature_size);
7986
7987 PSA_ASSERT(psa_sign_message(key, alg,
7988 input_data->x, input_data->len,
7989 signature, signature_size,
7990 &signature_length));
7991 TEST_LE_U(signature_length, signature_size);
7992 TEST_ASSERT(signature_length > 0);
7993
7994 PSA_ASSERT(psa_verify_message(key, alg,
7995 input_data->x, input_data->len,
7996 signature, signature_length));
7997
7998 if (input_data->len != 0) {
7999 /* Flip a bit in the input and verify that the signature is now
8000 * detected as invalid. Flip a bit at the beginning, not at the end,
8001 * because ECDSA may ignore the last few bits of the input. */
8002 input_data->x[0] ^= 1;
8003 TEST_EQUAL(psa_verify_message(key, alg,
8004 input_data->x, input_data->len,
8005 signature, signature_length),
8006 PSA_ERROR_INVALID_SIGNATURE);
8007 }
8008
8009exit:
8010 psa_reset_key_attributes(&attributes);
8011
8012 psa_destroy_key(key);
8013 mbedtls_free(signature);
8014 PSA_DONE();
8015}
8016/* END_CASE */
8017
8018/* BEGIN_CASE */
8019void verify_message(int key_type_arg,
8020 data_t *key_data,
8021 int alg_arg,
8022 data_t *input_data,
8023 data_t *signature_data)
8024{
8025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8026 psa_key_type_t key_type = key_type_arg;
8027 psa_algorithm_t alg = alg_arg;
8028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8029
8030 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8031
8032 PSA_ASSERT(psa_crypto_init());
8033
8034 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8035 psa_set_key_algorithm(&attributes, alg);
8036 psa_set_key_type(&attributes, key_type);
8037
8038 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8039 &key));
8040
8041 PSA_ASSERT(psa_verify_message(key, alg,
8042 input_data->x, input_data->len,
8043 signature_data->x, signature_data->len));
8044
8045exit:
8046 psa_reset_key_attributes(&attributes);
8047 psa_destroy_key(key);
8048 PSA_DONE();
8049}
8050/* END_CASE */
8051
8052/* BEGIN_CASE */
8053void verify_message_fail(int key_type_arg,
8054 data_t *key_data,
8055 int alg_arg,
8056 data_t *hash_data,
8057 data_t *signature_data,
8058 int expected_status_arg)
8059{
8060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8061 psa_key_type_t key_type = key_type_arg;
8062 psa_algorithm_t alg = alg_arg;
8063 psa_status_t actual_status;
8064 psa_status_t expected_status = expected_status_arg;
8065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8066
8067 PSA_ASSERT(psa_crypto_init());
8068
8069 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8070 psa_set_key_algorithm(&attributes, alg);
8071 psa_set_key_type(&attributes, key_type);
8072
8073 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8074 &key));
8075
8076 actual_status = psa_verify_message(key, alg,
8077 hash_data->x, hash_data->len,
8078 signature_data->x,
8079 signature_data->len);
8080 TEST_EQUAL(actual_status, expected_status);
8081
8082exit:
8083 psa_reset_key_attributes(&attributes);
8084 psa_destroy_key(key);
8085 PSA_DONE();
8086}
8087/* END_CASE */
8088
8089/* BEGIN_CASE */
8090void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008091 data_t *key_data,
8092 int alg_arg,
8093 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 data_t *label,
8095 int expected_output_length_arg,
8096 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008097{
Ronald Cron5425a212020-08-04 14:58:35 +02008098 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008099 psa_key_type_t key_type = key_type_arg;
8100 psa_algorithm_t alg = alg_arg;
8101 size_t expected_output_length = expected_output_length_arg;
8102 size_t key_bits;
8103 unsigned char *output = NULL;
8104 size_t output_size;
8105 size_t output_length = ~0;
8106 psa_status_t actual_status;
8107 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008109
Gilles Peskine449bd832023-01-11 14:50:10 +01008110 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008111
Gilles Peskine656896e2018-06-29 19:12:28 +02008112 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8114 psa_set_key_algorithm(&attributes, alg);
8115 psa_set_key_type(&attributes, key_type);
8116 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8117 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008118
8119 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8121 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008122
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8124 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8125 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008126
8127 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 actual_status = psa_asymmetric_encrypt(key, alg,
8129 input_data->x, input_data->len,
8130 label->x, label->len,
8131 output, output_size,
8132 &output_length);
8133 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008134 if (actual_status == PSA_SUCCESS) {
8135 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008136 } else {
8137 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008138 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008139
Gilles Peskine68428122018-06-30 18:42:41 +02008140 /* If the label is empty, the test framework puts a non-null pointer
8141 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008143 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 if (output_size != 0) {
8145 memset(output, 0, output_size);
8146 }
8147 actual_status = psa_asymmetric_encrypt(key, alg,
8148 input_data->x, input_data->len,
8149 NULL, label->len,
8150 output, output_size,
8151 &output_length);
8152 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008153 if (actual_status == PSA_SUCCESS) {
8154 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008155 } else {
8156 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008157 }
Gilles Peskine68428122018-06-30 18:42:41 +02008158 }
8159
Gilles Peskine656896e2018-06-29 19:12:28 +02008160exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008161 /*
8162 * Key attributes may have been returned by psa_get_key_attributes()
8163 * thus reset them as required.
8164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008166
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 psa_destroy_key(key);
8168 mbedtls_free(output);
8169 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008170}
8171/* END_CASE */
8172
8173/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008174void asymmetric_encrypt_decrypt(int key_type_arg,
8175 data_t *key_data,
8176 int alg_arg,
8177 data_t *input_data,
8178 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008179{
Ronald Cron5425a212020-08-04 14:58:35 +02008180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008181 psa_key_type_t key_type = key_type_arg;
8182 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008183 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008184 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008185 size_t output_size;
8186 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008187 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008188 size_t output2_size;
8189 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008191
Gilles Peskine449bd832023-01-11 14:50:10 +01008192 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008193
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8195 psa_set_key_algorithm(&attributes, alg);
8196 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008197
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8199 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008200
8201 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008202 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8203 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008204
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8206 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8207 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008208
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008209 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008210 TEST_LE_U(output2_size,
8211 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8212 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8213 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008214
Gilles Peskineeebd7382018-06-08 18:11:54 +02008215 /* We test encryption by checking that encrypt-then-decrypt gives back
8216 * the original plaintext because of the non-optional random
8217 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8219 input_data->x, input_data->len,
8220 label->x, label->len,
8221 output, output_size,
8222 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008223 /* We don't know what ciphertext length to expect, but check that
8224 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8228 output, output_length,
8229 label->x, label->len,
8230 output2, output2_size,
8231 &output2_length));
8232 ASSERT_COMPARE(input_data->x, input_data->len,
8233 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008234
8235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008236 /*
8237 * Key attributes may have been returned by psa_get_key_attributes()
8238 * thus reset them as required.
8239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008241
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 psa_destroy_key(key);
8243 mbedtls_free(output);
8244 mbedtls_free(output2);
8245 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008246}
8247/* END_CASE */
8248
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008249/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008250void asymmetric_decrypt(int key_type_arg,
8251 data_t *key_data,
8252 int alg_arg,
8253 data_t *input_data,
8254 data_t *label,
8255 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008256{
Ronald Cron5425a212020-08-04 14:58:35 +02008257 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008258 psa_key_type_t key_type = key_type_arg;
8259 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008260 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008261 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008262 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008263 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008265
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008267
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8269 psa_set_key_algorithm(&attributes, alg);
8270 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008271
Gilles Peskine449bd832023-01-11 14:50:10 +01008272 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8273 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008274
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8276 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008277
8278 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8280 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8281 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008282
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8284 input_data->x, input_data->len,
8285 label->x, label->len,
8286 output,
8287 output_size,
8288 &output_length));
8289 ASSERT_COMPARE(expected_data->x, expected_data->len,
8290 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008291
Gilles Peskine68428122018-06-30 18:42:41 +02008292 /* If the label is empty, the test framework puts a non-null pointer
8293 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008294 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008295 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 if (output_size != 0) {
8297 memset(output, 0, output_size);
8298 }
8299 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8300 input_data->x, input_data->len,
8301 NULL, label->len,
8302 output,
8303 output_size,
8304 &output_length));
8305 ASSERT_COMPARE(expected_data->x, expected_data->len,
8306 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008307 }
8308
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008309exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 psa_reset_key_attributes(&attributes);
8311 psa_destroy_key(key);
8312 mbedtls_free(output);
8313 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008314}
8315/* END_CASE */
8316
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008317/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008318void asymmetric_decrypt_fail(int key_type_arg,
8319 data_t *key_data,
8320 int alg_arg,
8321 data_t *input_data,
8322 data_t *label,
8323 int output_size_arg,
8324 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008325{
Ronald Cron5425a212020-08-04 14:58:35 +02008326 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008327 psa_key_type_t key_type = key_type_arg;
8328 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008329 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008330 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008331 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008332 psa_status_t actual_status;
8333 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008335
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008337
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008339
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8341 psa_set_key_algorithm(&attributes, alg);
8342 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008343
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8345 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008346
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 actual_status = psa_asymmetric_decrypt(key, alg,
8348 input_data->x, input_data->len,
8349 label->x, label->len,
8350 output, output_size,
8351 &output_length);
8352 TEST_EQUAL(actual_status, expected_status);
8353 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008354
Gilles Peskine68428122018-06-30 18:42:41 +02008355 /* If the label is empty, the test framework puts a non-null pointer
8356 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008358 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008359 if (output_size != 0) {
8360 memset(output, 0, output_size);
8361 }
8362 actual_status = psa_asymmetric_decrypt(key, alg,
8363 input_data->x, input_data->len,
8364 NULL, label->len,
8365 output, output_size,
8366 &output_length);
8367 TEST_EQUAL(actual_status, expected_status);
8368 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008369 }
8370
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 psa_reset_key_attributes(&attributes);
8373 psa_destroy_key(key);
8374 mbedtls_free(output);
8375 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008376}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008377/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008378
8379/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008380void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008381{
8382 /* Test each valid way of initializing the object, except for `= {0}`, as
8383 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8384 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008385 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008386 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008388 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8389 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008390
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008392
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008393 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8395 PSA_ERROR_BAD_STATE);
8396 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8397 PSA_ERROR_BAD_STATE);
8398 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8399 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008400
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008401 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 PSA_ASSERT(psa_key_derivation_abort(&func));
8403 PSA_ASSERT(psa_key_derivation_abort(&init));
8404 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008405}
8406/* END_CASE */
8407
Janos Follath16de4a42019-06-13 16:32:24 +01008408/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008409void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008410{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008411 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008412 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008413 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008414
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008416
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8418 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008419
8420exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 psa_key_derivation_abort(&operation);
8422 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008423}
8424/* END_CASE */
8425
Janos Follathaf3c2a02019-06-12 12:34:34 +01008426/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008427void derive_set_capacity(int alg_arg, int capacity_arg,
8428 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008429{
8430 psa_algorithm_t alg = alg_arg;
8431 size_t capacity = capacity_arg;
8432 psa_status_t expected_status = expected_status_arg;
8433 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8434
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008436
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008438
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8440 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008441
8442exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 psa_key_derivation_abort(&operation);
8444 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008445}
8446/* END_CASE */
8447
8448/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008449void derive_input(int alg_arg,
8450 int step_arg1, int key_type_arg1, data_t *input1,
8451 int expected_status_arg1,
8452 int step_arg2, int key_type_arg2, data_t *input2,
8453 int expected_status_arg2,
8454 int step_arg3, int key_type_arg3, data_t *input3,
8455 int expected_status_arg3,
8456 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008457{
8458 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
8460 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
8461 psa_status_t expected_statuses[] = { expected_status_arg1,
8462 expected_status_arg2,
8463 expected_status_arg3 };
8464 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008465 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8466 MBEDTLS_SVC_KEY_ID_INIT,
8467 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008468 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8470 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008471 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008472 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008473 psa_status_t expected_output_status = expected_output_status_arg;
8474 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008475
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008477
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8479 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008480
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008482
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8484 mbedtls_test_set_step(i);
8485 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008486 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
8488 psa_set_key_type(&attributes, key_types[i]);
8489 PSA_ASSERT(psa_import_key(&attributes,
8490 inputs[i]->x, inputs[i]->len,
8491 &keys[i]));
8492 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
8493 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008494 // When taking a private key as secret input, use key agreement
8495 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8497 &operation, keys[i]),
8498 expected_statuses[i]);
8499 } else {
8500 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8501 keys[i]),
8502 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008503 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 } else {
8505 TEST_EQUAL(psa_key_derivation_input_bytes(
8506 &operation, steps[i],
8507 inputs[i]->x, inputs[i]->len),
8508 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008509 }
8510 }
8511
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 if (output_key_type != PSA_KEY_TYPE_NONE) {
8513 psa_reset_key_attributes(&attributes);
8514 psa_set_key_type(&attributes, output_key_type);
8515 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008516 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 psa_key_derivation_output_key(&attributes, &operation,
8518 &output_key);
8519 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008520 uint8_t buffer[1];
8521 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 psa_key_derivation_output_bytes(&operation,
8523 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008524 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008526
Janos Follathaf3c2a02019-06-12 12:34:34 +01008527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 psa_key_derivation_abort(&operation);
8529 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8530 psa_destroy_key(keys[i]);
8531 }
8532 psa_destroy_key(output_key);
8533 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008534}
8535/* END_CASE */
8536
Janos Follathd958bb72019-07-03 15:02:16 +01008537/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008538void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008539{
Janos Follathd958bb72019-07-03 15:02:16 +01008540 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008541 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008542 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008543 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008544 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008546 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008548 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008550 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8551 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008553 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008554
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008556
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8558 psa_set_key_algorithm(&attributes, alg);
8559 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008560
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 PSA_ASSERT(psa_import_key(&attributes,
8562 key_data, sizeof(key_data),
8563 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008564
8565 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8567 input1, input1_length,
8568 input2, input2_length,
8569 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008570 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008572
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008573 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8575 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008576
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008578
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8580 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008581
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 psa_key_derivation_abort(&operation);
8584 psa_destroy_key(key);
8585 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008586}
8587/* END_CASE */
8588
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008589/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008590void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008591{
8592 uint8_t output_buffer[16];
8593 size_t buffer_size = 16;
8594 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008595 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008596
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8598 output_buffer, buffer_size)
8599 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008600
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8602 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008605
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8607 output_buffer, buffer_size)
8608 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008609
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8611 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008612
8613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008615}
8616/* END_CASE */
8617
8618/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008619void derive_output(int alg_arg,
8620 int step1_arg, data_t *input1, int expected_status_arg1,
8621 int step2_arg, data_t *input2, int expected_status_arg2,
8622 int step3_arg, data_t *input3, int expected_status_arg3,
8623 int step4_arg, data_t *input4, int expected_status_arg4,
8624 data_t *key_agreement_peer_key,
8625 int requested_capacity_arg,
8626 data_t *expected_output1,
8627 data_t *expected_output2,
8628 int other_key_input_type,
8629 int key_input_type,
8630 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008631{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008632 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8634 data_t *inputs[] = { input1, input2, input3, input4 };
8635 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8636 MBEDTLS_SVC_KEY_ID_INIT,
8637 MBEDTLS_SVC_KEY_ID_INIT,
8638 MBEDTLS_SVC_KEY_ID_INIT };
8639 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8640 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008641 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008642 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008643 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008645 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008647 size_t output_buffer_size = 0;
8648 uint8_t *output_buffer = NULL;
8649 size_t expected_capacity;
8650 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008651 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8652 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8653 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8654 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008655 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008656 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008657 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008658
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8660 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008661 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 }
8663 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008664 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 ASSERT_ALLOC(output_buffer, output_buffer_size);
8668 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008669
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008670 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8672 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8673 requested_capacity));
8674 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8675 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008676 case 0:
8677 break;
8678 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008680 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008681 TEST_EQUAL(psa_key_derivation_input_bytes(
8682 &operation, steps[i],
8683 inputs[i]->x, inputs[i]->len),
8684 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008685
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008687 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008689 break;
8690 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8692 psa_set_key_algorithm(&attributes1, alg);
8693 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 PSA_ASSERT(psa_import_key(&attributes1,
8696 inputs[i]->x, inputs[i]->len,
8697 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8700 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8701 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8702 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008703 }
8704
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8706 steps[i],
8707 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008708 break;
8709 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008711 break;
8712 }
8713 break;
8714 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008716 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8718 steps[i],
8719 inputs[i]->x,
8720 inputs[i]->len),
8721 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008722 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008723 case 1: // input key, type DERIVE
8724 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8726 psa_set_key_algorithm(&attributes2, alg);
8727 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008728
8729 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 if (other_key_input_type == 11) {
8731 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8732 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 PSA_ASSERT(psa_import_key(&attributes2,
8735 inputs[i]->x, inputs[i]->len,
8736 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008737
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8739 steps[i],
8740 keys[i]),
8741 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008742 break;
8743 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8745 psa_set_key_algorithm(&attributes3, alg);
8746 psa_set_key_type(&attributes3,
8747 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008748
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 PSA_ASSERT(psa_import_key(&attributes3,
8750 inputs[i]->x, inputs[i]->len,
8751 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008752
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 TEST_EQUAL(psa_key_derivation_key_agreement(
8754 &operation,
8755 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8756 keys[i], key_agreement_peer_key->x,
8757 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008758 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008759 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008761 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008762 }
8763
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008765 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008767 break;
8768 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 TEST_EQUAL(psa_key_derivation_input_bytes(
8770 &operation, steps[i],
8771 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008772
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008776 break;
8777 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008778 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008779
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8781 &current_capacity));
8782 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008783 expected_capacity = requested_capacity;
8784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008786 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8787
8788 /* For output key derivation secret must be provided using
8789 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008791 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008793
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8795 psa_set_key_algorithm(&attributes4, alg);
8796 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8797 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008798
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8800 &derived_key), expected_status);
8801 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008802 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008804 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 status = psa_key_derivation_output_bytes(&operation,
8806 output_buffer, output_sizes[i]);
8807 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008808 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 TEST_ASSERT(status == PSA_SUCCESS ||
8810 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008811 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 } else if (expected_capacity == 0 ||
8813 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008814 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008816 expected_capacity = 0;
8817 continue;
8818 }
8819 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 PSA_ASSERT(status);
8821 if (output_sizes[i] != 0) {
8822 ASSERT_COMPARE(output_buffer, output_sizes[i],
8823 expected_outputs[i], output_sizes[i]);
8824 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008825 /* Check the operation status. */
8826 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8828 &current_capacity));
8829 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008830 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008833
8834exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 mbedtls_free(output_buffer);
8836 psa_key_derivation_abort(&operation);
8837 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8838 psa_destroy_key(keys[i]);
8839 }
8840 psa_destroy_key(derived_key);
8841 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008842}
8843/* END_CASE */
8844
8845/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008846void derive_full(int alg_arg,
8847 data_t *key_data,
8848 data_t *input1,
8849 data_t *input2,
8850 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008851{
Ronald Cron5425a212020-08-04 14:58:35 +02008852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008853 psa_algorithm_t alg = alg_arg;
8854 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008855 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008856 unsigned char output_buffer[16];
8857 size_t expected_capacity = requested_capacity;
8858 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008862
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8864 psa_set_key_algorithm(&attributes, alg);
8865 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008866
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8868 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008869
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8871 input1->x, input1->len,
8872 input2->x, input2->len,
8873 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008874 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008876
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8878 &current_capacity));
8879 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008880
8881 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 while (current_capacity > 0) {
8883 size_t read_size = sizeof(output_buffer);
8884 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008885 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 }
8887 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8888 output_buffer,
8889 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008890 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8892 &current_capacity));
8893 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008894 }
8895
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008896 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8898 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008899
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008901
8902exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 psa_key_derivation_abort(&operation);
8904 psa_destroy_key(key);
8905 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008906}
8907/* END_CASE */
8908
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008909/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008910void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8911 int derivation_step,
8912 int capacity, int expected_capacity_status_arg,
8913 data_t *expected_output,
8914 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008915{
8916 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8917 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008918 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008919 uint8_t *output_buffer = NULL;
8920 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008921 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8922 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8923 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008924
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 ASSERT_ALLOC(output_buffer, expected_output->len);
8926 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008927
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8929 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8930 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8933 step, input->x, input->len),
8934 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008935
Gilles Peskine449bd832023-01-11 14:50:10 +01008936 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008937 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008939
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8941 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008942
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 TEST_EQUAL(status, expected_output_status);
8944 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8945 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8946 expected_output->len);
8947 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008948
8949exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 mbedtls_free(output_buffer);
8951 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008952 PSA_DONE();
8953}
8954/* END_CASE */
8955
Janos Follathe60c9052019-07-03 13:51:30 +01008956/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008957void derive_key_exercise(int alg_arg,
8958 data_t *key_data,
8959 data_t *input1,
8960 data_t *input2,
8961 int derived_type_arg,
8962 int derived_bits_arg,
8963 int derived_usage_arg,
8964 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008965{
Ronald Cron5425a212020-08-04 14:58:35 +02008966 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8967 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008968 psa_algorithm_t alg = alg_arg;
8969 psa_key_type_t derived_type = derived_type_arg;
8970 size_t derived_bits = derived_bits_arg;
8971 psa_key_usage_t derived_usage = derived_usage_arg;
8972 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008974 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008976 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02008979
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8981 psa_set_key_algorithm(&attributes, alg);
8982 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
8983 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8984 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02008985
8986 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
8988 input1->x, input1->len,
8989 input2->x, input2->len,
8990 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01008991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 }
Janos Follathe60c9052019-07-03 13:51:30 +01008993
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 psa_set_key_usage_flags(&attributes, derived_usage);
8995 psa_set_key_algorithm(&attributes, derived_alg);
8996 psa_set_key_type(&attributes, derived_type);
8997 psa_set_key_bits(&attributes, derived_bits);
8998 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
8999 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009000
9001 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9003 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9004 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009005
9006 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009008 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009010
9011exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009012 /*
9013 * Key attributes may have been returned by psa_get_key_attributes()
9014 * thus reset them as required.
9015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009017
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 psa_key_derivation_abort(&operation);
9019 psa_destroy_key(base_key);
9020 psa_destroy_key(derived_key);
9021 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009022}
9023/* END_CASE */
9024
Janos Follath42fd8882019-07-03 14:17:09 +01009025/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009026void derive_key_export(int alg_arg,
9027 data_t *key_data,
9028 data_t *input1,
9029 data_t *input2,
9030 int bytes1_arg,
9031 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009032{
Ronald Cron5425a212020-08-04 14:58:35 +02009033 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9034 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009035 psa_algorithm_t alg = alg_arg;
9036 size_t bytes1 = bytes1_arg;
9037 size_t bytes2 = bytes2_arg;
9038 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009039 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009040 uint8_t *output_buffer = NULL;
9041 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009042 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9043 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009044 size_t length;
9045
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 ASSERT_ALLOC(output_buffer, capacity);
9047 ASSERT_ALLOC(export_buffer, capacity);
9048 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009049
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9051 psa_set_key_algorithm(&base_attributes, alg);
9052 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9053 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9054 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009055
9056 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9058 input1->x, input1->len,
9059 input2->x, input2->len,
9060 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009062 }
Janos Follath42fd8882019-07-03 14:17:09 +01009063
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9065 output_buffer,
9066 capacity));
9067 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009068
9069 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009070 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9071 input1->x, input1->len,
9072 input2->x, input2->len,
9073 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 }
Janos Follath42fd8882019-07-03 14:17:09 +01009076
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9078 psa_set_key_algorithm(&derived_attributes, 0);
9079 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9080 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9081 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9082 &derived_key));
9083 PSA_ASSERT(psa_export_key(derived_key,
9084 export_buffer, bytes1,
9085 &length));
9086 TEST_EQUAL(length, bytes1);
9087 PSA_ASSERT(psa_destroy_key(derived_key));
9088 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9089 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9090 &derived_key));
9091 PSA_ASSERT(psa_export_key(derived_key,
9092 export_buffer + bytes1, bytes2,
9093 &length));
9094 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009095
9096 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9098 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009099
9100exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 mbedtls_free(output_buffer);
9102 mbedtls_free(export_buffer);
9103 psa_key_derivation_abort(&operation);
9104 psa_destroy_key(base_key);
9105 psa_destroy_key(derived_key);
9106 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009107}
9108/* END_CASE */
9109
9110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009111void derive_key_type(int alg_arg,
9112 data_t *key_data,
9113 data_t *input1,
9114 data_t *input2,
9115 int key_type_arg, int bits_arg,
9116 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009117{
9118 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9119 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9120 const psa_algorithm_t alg = alg_arg;
9121 const psa_key_type_t key_type = key_type_arg;
9122 const size_t bits = bits_arg;
9123 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9124 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009125 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009126 uint8_t *export_buffer = NULL;
9127 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9128 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9129 size_t export_length;
9130
Gilles Peskine449bd832023-01-11 14:50:10 +01009131 ASSERT_ALLOC(export_buffer, export_buffer_size);
9132 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9135 psa_set_key_algorithm(&base_attributes, alg);
9136 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9137 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9138 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009139
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009141 &operation, base_key, alg,
9142 input1->x, input1->len,
9143 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009147
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9149 psa_set_key_algorithm(&derived_attributes, 0);
9150 psa_set_key_type(&derived_attributes, key_type);
9151 psa_set_key_bits(&derived_attributes, bits);
9152 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9153 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 PSA_ASSERT(psa_export_key(derived_key,
9156 export_buffer, export_buffer_size,
9157 &export_length));
9158 ASSERT_COMPARE(export_buffer, export_length,
9159 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009160
9161exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 mbedtls_free(export_buffer);
9163 psa_key_derivation_abort(&operation);
9164 psa_destroy_key(base_key);
9165 psa_destroy_key(derived_key);
9166 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009167}
9168/* END_CASE */
9169
9170/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009171void derive_key(int alg_arg,
9172 data_t *key_data, data_t *input1, data_t *input2,
9173 int type_arg, int bits_arg,
9174 int expected_status_arg,
9175 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009176{
Ronald Cron5425a212020-08-04 14:58:35 +02009177 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9178 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009179 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009180 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009181 size_t bits = bits_arg;
9182 psa_status_t expected_status = expected_status_arg;
9183 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9184 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9185 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9186
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009188
Gilles Peskine449bd832023-01-11 14:50:10 +01009189 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9190 psa_set_key_algorithm(&base_attributes, alg);
9191 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9192 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9193 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009194
Gilles Peskine449bd832023-01-11 14:50:10 +01009195 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9196 input1->x, input1->len,
9197 input2->x, input2->len,
9198 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009199 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009200 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009201
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9203 psa_set_key_algorithm(&derived_attributes, 0);
9204 psa_set_key_type(&derived_attributes, type);
9205 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009206
9207 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009208 psa_key_derivation_output_key(&derived_attributes,
9209 &operation,
9210 &derived_key);
9211 if (is_large_output > 0) {
9212 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9213 }
9214 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009215
9216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 psa_key_derivation_abort(&operation);
9218 psa_destroy_key(base_key);
9219 psa_destroy_key(derived_key);
9220 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009221}
9222/* END_CASE */
9223
9224/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009225void key_agreement_setup(int alg_arg,
9226 int our_key_type_arg, int our_key_alg_arg,
9227 data_t *our_key_data, data_t *peer_key_data,
9228 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009229{
Ronald Cron5425a212020-08-04 14:58:35 +02009230 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009231 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009232 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009233 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009234 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009236 psa_status_t expected_status = expected_status_arg;
9237 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009240
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9242 psa_set_key_algorithm(&attributes, our_key_alg);
9243 psa_set_key_type(&attributes, our_key_type);
9244 PSA_ASSERT(psa_import_key(&attributes,
9245 our_key_data->x, our_key_data->len,
9246 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009247
Gilles Peskine77f40d82019-04-11 21:27:06 +02009248 /* The tests currently include inputs that should fail at either step.
9249 * Test cases that fail at the setup step should be changed to call
9250 * key_derivation_setup instead, and this function should be renamed
9251 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009252 status = psa_key_derivation_setup(&operation, alg);
9253 if (status == PSA_SUCCESS) {
9254 TEST_EQUAL(psa_key_derivation_key_agreement(
9255 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9256 our_key,
9257 peer_key_data->x, peer_key_data->len),
9258 expected_status);
9259 } else {
9260 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009261 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009262
9263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 psa_key_derivation_abort(&operation);
9265 psa_destroy_key(our_key);
9266 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009267}
9268/* END_CASE */
9269
9270/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009271void raw_key_agreement(int alg_arg,
9272 int our_key_type_arg, data_t *our_key_data,
9273 data_t *peer_key_data,
9274 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009275{
Ronald Cron5425a212020-08-04 14:58:35 +02009276 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009277 psa_algorithm_t alg = alg_arg;
9278 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009280 unsigned char *output = NULL;
9281 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009282 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009283
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009285
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9287 psa_set_key_algorithm(&attributes, alg);
9288 psa_set_key_type(&attributes, our_key_type);
9289 PSA_ASSERT(psa_import_key(&attributes,
9290 our_key_data->x, our_key_data->len,
9291 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009292
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9294 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009295
Gilles Peskine992bee82022-04-13 23:25:52 +02009296 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 TEST_LE_U(expected_output->len,
9298 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9299 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9300 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009301
9302 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 ASSERT_ALLOC(output, expected_output->len);
9304 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9305 peer_key_data->x, peer_key_data->len,
9306 output, expected_output->len,
9307 &output_length));
9308 ASSERT_COMPARE(output, output_length,
9309 expected_output->x, expected_output->len);
9310 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009311 output = NULL;
9312 output_length = ~0;
9313
9314 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009315 ASSERT_ALLOC(output, expected_output->len + 1);
9316 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9317 peer_key_data->x, peer_key_data->len,
9318 output, expected_output->len + 1,
9319 &output_length));
9320 ASSERT_COMPARE(output, output_length,
9321 expected_output->x, expected_output->len);
9322 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009323 output = NULL;
9324 output_length = ~0;
9325
9326 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009327 ASSERT_ALLOC(output, expected_output->len - 1);
9328 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9329 peer_key_data->x, peer_key_data->len,
9330 output, expected_output->len - 1,
9331 &output_length),
9332 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009333 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009334 TEST_LE_U(output_length, expected_output->len - 1);
9335 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009336 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009337
9338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009339 mbedtls_free(output);
9340 psa_destroy_key(our_key);
9341 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009342}
9343/* END_CASE */
9344
9345/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009346void key_agreement_capacity(int alg_arg,
9347 int our_key_type_arg, data_t *our_key_data,
9348 data_t *peer_key_data,
9349 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009350{
Ronald Cron5425a212020-08-04 14:58:35 +02009351 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009352 psa_algorithm_t alg = alg_arg;
9353 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009356 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009357 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009358
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009360
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9362 psa_set_key_algorithm(&attributes, alg);
9363 psa_set_key_type(&attributes, our_key_type);
9364 PSA_ASSERT(psa_import_key(&attributes,
9365 our_key_data->x, our_key_data->len,
9366 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009367
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9369 PSA_ASSERT(psa_key_derivation_key_agreement(
9370 &operation,
9371 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9372 peer_key_data->x, peer_key_data->len));
9373 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009374 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9376 PSA_KEY_DERIVATION_INPUT_INFO,
9377 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009378 }
Gilles Peskine59685592018-09-18 12:11:34 +02009379
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009380 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009381 PSA_ASSERT(psa_key_derivation_get_capacity(
9382 &operation, &actual_capacity));
9383 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009384
Gilles Peskinebf491972018-10-25 22:36:12 +02009385 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009386 while (actual_capacity > sizeof(output)) {
9387 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9388 output, sizeof(output)));
9389 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009390 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9392 output, actual_capacity));
9393 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9394 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009395
Gilles Peskine59685592018-09-18 12:11:34 +02009396exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 psa_key_derivation_abort(&operation);
9398 psa_destroy_key(our_key);
9399 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009400}
9401/* END_CASE */
9402
9403/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009404void key_agreement_output(int alg_arg,
9405 int our_key_type_arg, data_t *our_key_data,
9406 data_t *peer_key_data,
9407 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009408{
Ronald Cron5425a212020-08-04 14:58:35 +02009409 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009410 psa_algorithm_t alg = alg_arg;
9411 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009412 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009414 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009415
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9417 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009418
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009420
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9422 psa_set_key_algorithm(&attributes, alg);
9423 psa_set_key_type(&attributes, our_key_type);
9424 PSA_ASSERT(psa_import_key(&attributes,
9425 our_key_data->x, our_key_data->len,
9426 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9429 PSA_ASSERT(psa_key_derivation_key_agreement(
9430 &operation,
9431 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9432 peer_key_data->x, peer_key_data->len));
9433 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009434 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9436 PSA_KEY_DERIVATION_INPUT_INFO,
9437 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009438 }
Gilles Peskine59685592018-09-18 12:11:34 +02009439
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9441 actual_output,
9442 expected_output1->len));
9443 ASSERT_COMPARE(actual_output, expected_output1->len,
9444 expected_output1->x, expected_output1->len);
9445 if (expected_output2->len != 0) {
9446 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9447 actual_output,
9448 expected_output2->len));
9449 ASSERT_COMPARE(actual_output, expected_output2->len,
9450 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009451 }
Gilles Peskine59685592018-09-18 12:11:34 +02009452
9453exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009454 psa_key_derivation_abort(&operation);
9455 psa_destroy_key(our_key);
9456 PSA_DONE();
9457 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009458}
9459/* END_CASE */
9460
9461/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009462void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009463{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009464 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009465 unsigned char *output = NULL;
9466 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009467 size_t i;
9468 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009469
Gilles Peskine449bd832023-01-11 14:50:10 +01009470 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009471
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 ASSERT_ALLOC(output, bytes);
9473 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009474
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009476
Gilles Peskinea50d7392018-06-21 10:22:13 +02009477 /* Run several times, to ensure that every output byte will be
9478 * nonzero at least once with overwhelming probability
9479 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009480 for (run = 0; run < 10; run++) {
9481 if (bytes != 0) {
9482 memset(output, 0, bytes);
9483 }
9484 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009485
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 for (i = 0; i < bytes; i++) {
9487 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009488 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009489 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009490 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009491 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009492
9493 /* Check that every byte was changed to nonzero at least once. This
9494 * validates that psa_generate_random is overwriting every byte of
9495 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 for (i = 0; i < bytes; i++) {
9497 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009498 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009499
9500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009501 PSA_DONE();
9502 mbedtls_free(output);
9503 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009504}
9505/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009506
9507/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009508void generate_key(int type_arg,
9509 int bits_arg,
9510 int usage_arg,
9511 int alg_arg,
9512 int expected_status_arg,
9513 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009514{
Ronald Cron5425a212020-08-04 14:58:35 +02009515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009516 psa_key_type_t type = type_arg;
9517 psa_key_usage_t usage = usage_arg;
9518 size_t bits = bits_arg;
9519 psa_algorithm_t alg = alg_arg;
9520 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009522 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009523
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 psa_set_key_usage_flags(&attributes, usage);
9527 psa_set_key_algorithm(&attributes, alg);
9528 psa_set_key_type(&attributes, type);
9529 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009530
9531 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009533
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 if (is_large_key > 0) {
9535 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9536 }
9537 TEST_EQUAL(status, expected_status);
9538 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009539 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009541
9542 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009543 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9544 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9545 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009546
Gilles Peskine818ca122018-06-20 18:16:48 +02009547 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009549 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009551
9552exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009553 /*
9554 * Key attributes may have been returned by psa_get_key_attributes()
9555 * thus reset them as required.
9556 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009558
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 psa_destroy_key(key);
9560 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009561}
9562/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009563
Ronald Cronee414c72021-03-18 18:50:08 +01009564/* 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 +01009565void generate_key_rsa(int bits_arg,
9566 data_t *e_arg,
9567 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009568{
Ronald Cron5425a212020-08-04 14:58:35 +02009569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009570 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009571 size_t bits = bits_arg;
9572 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9573 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9574 psa_status_t expected_status = expected_status_arg;
9575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9576 uint8_t *exported = NULL;
9577 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009579 size_t exported_length = SIZE_MAX;
9580 uint8_t *e_read_buffer = NULL;
9581 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009582 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009583 size_t e_read_length = SIZE_MAX;
9584
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 if (e_arg->len == 0 ||
9586 (e_arg->len == 3 &&
9587 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009588 is_default_public_exponent = 1;
9589 e_read_size = 0;
9590 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 ASSERT_ALLOC(e_read_buffer, e_read_size);
9592 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009593
Gilles Peskine449bd832023-01-11 14:50:10 +01009594 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009595
Gilles Peskine449bd832023-01-11 14:50:10 +01009596 psa_set_key_usage_flags(&attributes, usage);
9597 psa_set_key_algorithm(&attributes, alg);
9598 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9599 e_arg->x, e_arg->len));
9600 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009601
9602 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9604 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009607
9608 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9610 TEST_EQUAL(psa_get_key_type(&attributes), type);
9611 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9612 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9613 e_read_buffer, e_read_size,
9614 &e_read_length));
9615 if (is_default_public_exponent) {
9616 TEST_EQUAL(e_read_length, 0);
9617 } else {
9618 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9619 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009620
9621 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009625
9626 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 PSA_ASSERT(psa_export_public_key(key,
9628 exported, exported_size,
9629 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009630 {
9631 uint8_t *p = exported;
9632 uint8_t *end = exported + exported_length;
9633 size_t len;
9634 /* RSAPublicKey ::= SEQUENCE {
9635 * modulus INTEGER, -- n
9636 * publicExponent INTEGER } -- e
9637 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009638 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9639 MBEDTLS_ASN1_SEQUENCE |
9640 MBEDTLS_ASN1_CONSTRUCTED));
9641 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9642 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9643 MBEDTLS_ASN1_INTEGER));
9644 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009645 ++p;
9646 --len;
9647 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 if (e_arg->len == 0) {
9649 TEST_EQUAL(len, 3);
9650 TEST_EQUAL(p[0], 1);
9651 TEST_EQUAL(p[1], 0);
9652 TEST_EQUAL(p[2], 1);
9653 } else {
9654 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009655 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009656 }
9657
9658exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009659 /*
9660 * Key attributes may have been returned by psa_get_key_attributes() or
9661 * set by psa_set_key_domain_parameters() thus reset them as required.
9662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009663 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009664
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 psa_destroy_key(key);
9666 PSA_DONE();
9667 mbedtls_free(e_read_buffer);
9668 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009669}
9670/* END_CASE */
9671
Darryl Greend49a4992018-06-18 17:27:26 +01009672/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009673void persistent_key_load_key_from_storage(data_t *data,
9674 int type_arg, int bits_arg,
9675 int usage_flags_arg, int alg_arg,
9676 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009677{
Gilles Peskine449bd832023-01-11 14:50:10 +01009678 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9681 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009682 psa_key_type_t type = type_arg;
9683 size_t bits = bits_arg;
9684 psa_key_usage_t usage_flags = usage_flags_arg;
9685 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009686 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009687 unsigned char *first_export = NULL;
9688 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009689 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009690 size_t first_exported_length;
9691 size_t second_exported_length;
9692
Gilles Peskine449bd832023-01-11 14:50:10 +01009693 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9694 ASSERT_ALLOC(first_export, export_size);
9695 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009696 }
Darryl Greend49a4992018-06-18 17:27:26 +01009697
Gilles Peskine449bd832023-01-11 14:50:10 +01009698 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009699
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 psa_set_key_id(&attributes, key_id);
9701 psa_set_key_usage_flags(&attributes, usage_flags);
9702 psa_set_key_algorithm(&attributes, alg);
9703 psa_set_key_type(&attributes, type);
9704 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009705
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009707 case IMPORT_KEY:
9708 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009709 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9710 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009711 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009712
Darryl Green0c6575a2018-11-07 16:05:30 +00009713 case GENERATE_KEY:
9714 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009715 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009716 break;
9717
9718 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009719#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009720 {
9721 /* Create base key */
9722 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9723 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9724 psa_set_key_usage_flags(&base_attributes,
9725 PSA_KEY_USAGE_DERIVE);
9726 psa_set_key_algorithm(&base_attributes, derive_alg);
9727 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9728 PSA_ASSERT(psa_import_key(&base_attributes,
9729 data->x, data->len,
9730 &base_key));
9731 /* Derive a key. */
9732 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9733 PSA_ASSERT(psa_key_derivation_input_key(
9734 &operation,
9735 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9736 PSA_ASSERT(psa_key_derivation_input_bytes(
9737 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9738 NULL, 0));
9739 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9740 &operation,
9741 &key));
9742 PSA_ASSERT(psa_key_derivation_abort(&operation));
9743 PSA_ASSERT(psa_destroy_key(base_key));
9744 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9745 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009746#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009747 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009748#endif
9749 break;
9750
9751 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009752 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009753 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009755 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009756
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009757 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009758 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9759 PSA_ASSERT(psa_export_key(key,
9760 first_export, export_size,
9761 &first_exported_length));
9762 if (generation_method == IMPORT_KEY) {
9763 ASSERT_COMPARE(data->x, data->len,
9764 first_export, first_exported_length);
9765 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009766 }
Darryl Greend49a4992018-06-18 17:27:26 +01009767
9768 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009769 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009770 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009771 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009772
Darryl Greend49a4992018-06-18 17:27:26 +01009773 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009774 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9775 TEST_ASSERT(mbedtls_svc_key_id_equal(
9776 psa_get_key_id(&attributes), key_id));
9777 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9778 PSA_KEY_LIFETIME_PERSISTENT);
9779 TEST_EQUAL(psa_get_key_type(&attributes), type);
9780 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9781 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9782 mbedtls_test_update_key_usage_flags(usage_flags));
9783 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009784
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009785 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009786 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9787 PSA_ASSERT(psa_export_key(key,
9788 second_export, export_size,
9789 &second_exported_length));
9790 ASSERT_COMPARE(first_export, first_exported_length,
9791 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009792 }
9793
9794 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009795 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009796 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009797 }
Darryl Greend49a4992018-06-18 17:27:26 +01009798
9799exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009800 /*
9801 * Key attributes may have been returned by psa_get_key_attributes()
9802 * thus reset them as required.
9803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009804 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009805
Gilles Peskine449bd832023-01-11 14:50:10 +01009806 mbedtls_free(first_export);
9807 mbedtls_free(second_export);
9808 psa_key_derivation_abort(&operation);
9809 psa_destroy_key(base_key);
9810 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009811 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009812}
9813/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009814
Neil Armstronga557cb82022-06-10 08:58:32 +02009815/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009816void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9817 int primitive_arg, int hash_arg, int role_arg,
9818 int test_input, data_t *pw_data,
9819 int inj_err_type_arg,
9820 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009821{
9822 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9823 psa_pake_operation_t operation = psa_pake_operation_init();
9824 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009825 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009826 psa_key_type_t key_type_pw = key_type_pw_arg;
9827 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009828 psa_algorithm_t hash_alg = hash_arg;
9829 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009832 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9833 psa_status_t expected_error = expected_error_arg;
9834 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009835 unsigned char *output_buffer = NULL;
9836 size_t output_len = 0;
9837
Gilles Peskine449bd832023-01-11 14:50:10 +01009838 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009839
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009840 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009841 PSA_PAKE_STEP_KEY_SHARE);
9842 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009843
Gilles Peskine449bd832023-01-11 14:50:10 +01009844 if (pw_data->len > 0) {
9845 psa_set_key_usage_flags(&attributes, key_usage_pw);
9846 psa_set_key_algorithm(&attributes, alg);
9847 psa_set_key_type(&attributes, key_type_pw);
9848 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9849 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009850 }
9851
Gilles Peskine449bd832023-01-11 14:50:10 +01009852 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9853 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9854 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009855
Gilles Peskine449bd832023-01-11 14:50:10 +01009856 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009857
Gilles Peskine449bd832023-01-11 14:50:10 +01009858 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9859 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9860 expected_error);
9861 PSA_ASSERT(psa_pake_abort(&operation));
9862 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9863 expected_error);
9864 PSA_ASSERT(psa_pake_abort(&operation));
9865 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9866 expected_error);
9867 PSA_ASSERT(psa_pake_abort(&operation));
9868 TEST_EQUAL(psa_pake_set_role(&operation, role),
9869 expected_error);
9870 PSA_ASSERT(psa_pake_abort(&operation));
9871 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9872 NULL, 0, NULL),
9873 expected_error);
9874 PSA_ASSERT(psa_pake_abort(&operation));
9875 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9876 expected_error);
9877 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009878 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009879 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009880
Gilles Peskine449bd832023-01-11 14:50:10 +01009881 status = psa_pake_setup(&operation, &cipher_suite);
9882 if (status != PSA_SUCCESS) {
9883 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009884 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009885 }
9886
Gilles Peskine449bd832023-01-11 14:50:10 +01009887 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9888 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9889 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009890 goto exit;
9891 }
9892
Gilles Peskine449bd832023-01-11 14:50:10 +01009893 status = psa_pake_set_role(&operation, role);
9894 if (status != PSA_SUCCESS) {
9895 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009896 goto exit;
9897 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009898
Gilles Peskine449bd832023-01-11 14:50:10 +01009899 if (pw_data->len > 0) {
9900 status = psa_pake_set_password_key(&operation, key);
9901 if (status != PSA_SUCCESS) {
9902 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009903 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009904 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009905 }
9906
Gilles Peskine449bd832023-01-11 14:50:10 +01009907 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9908 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9909 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009910 goto exit;
9911 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009912
Gilles Peskine449bd832023-01-11 14:50:10 +01009913 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9914 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9915 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009916 goto exit;
9917 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009918
Gilles Peskine449bd832023-01-11 14:50:10 +01009919 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009920 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009921 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9922 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009923 goto exit;
9924 }
9925
Gilles Peskine449bd832023-01-11 14:50:10 +01009926 if (inj_err_type == INJECT_ERR_SET_PEER) {
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_peer(&operation, unsupported_id, 4),
9929 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009930 goto exit;
9931 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009932
Gilles Peskine449bd832023-01-11 14:50:10 +01009933 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9934 PSA_PAKE_STEP_KEY_SHARE);
9935 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9936 PSA_PAKE_STEP_ZK_PUBLIC);
9937 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9938 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009939
Gilles Peskine449bd832023-01-11 14:50:10 +01009940 if (test_input) {
9941 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9942 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9943 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009944 goto exit;
9945 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009946
Gilles Peskine449bd832023-01-11 14:50:10 +01009947 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9948 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9949 output_buffer, size_zk_proof),
9950 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009951 goto exit;
9952 }
9953
Gilles Peskine449bd832023-01-11 14:50:10 +01009954 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9955 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9956 output_buffer, size_zk_proof),
9957 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009958 goto exit;
9959 }
9960
Gilles Peskine449bd832023-01-11 14:50:10 +01009961 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9962 output_buffer, size_key_share);
9963 if (status != PSA_SUCCESS) {
9964 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009965 goto exit;
9966 }
9967
Gilles Peskine449bd832023-01-11 14:50:10 +01009968 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9969 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9970 output_buffer, size_zk_public + 1),
9971 PSA_ERROR_INVALID_ARGUMENT);
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_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009976 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009977 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9978 output_buffer, size_zk_public + 1);
9979 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9980 output_buffer, size_zk_public),
9981 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009982 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009983 }
Valerio Setti1070aed2022-11-11 19:37:31 +01009984 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01009985 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9986 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
9987 NULL, 0, NULL),
9988 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009989 goto exit;
9990 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009991
Gilles Peskine449bd832023-01-11 14:50:10 +01009992 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9993 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9994 output_buffer, buf_size, &output_len),
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_INVALID_FIRST_STEP) {
10000 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10001 output_buffer, buf_size, &output_len),
10002 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010003 goto exit;
10004 }
10005
Gilles Peskine449bd832023-01-11 14:50:10 +010010006 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10007 output_buffer, buf_size, &output_len);
10008 if (status != PSA_SUCCESS) {
10009 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010010 goto exit;
10011 }
10012
Gilles Peskine449bd832023-01-11 14:50:10 +010010013 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010014
Gilles Peskine449bd832023-01-11 14:50:10 +010010015 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10016 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10017 output_buffer, size_zk_public - 1, &output_len),
10018 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010019 goto exit;
10020 }
10021
Gilles Peskine449bd832023-01-11 14:50:10 +010010022 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010023 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010024 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10025 output_buffer, size_zk_public - 1, &output_len);
10026 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10027 output_buffer, buf_size, &output_len),
10028 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010029 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010030 }
10031 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010032
10033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010034 PSA_ASSERT(psa_destroy_key(key));
10035 PSA_ASSERT(psa_pake_abort(&operation));
10036 mbedtls_free(output_buffer);
10037 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010038}
10039/* END_CASE */
10040
Neil Armstronga557cb82022-06-10 08:58:32 +020010041/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010042void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10043 int client_input_first, int inject_error,
10044 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010045{
10046 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10047 psa_pake_operation_t server = psa_pake_operation_init();
10048 psa_pake_operation_t client = psa_pake_operation_init();
10049 psa_algorithm_t alg = alg_arg;
10050 psa_algorithm_t hash_alg = hash_arg;
10051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10053
Gilles Peskine449bd832023-01-11 14:50:10 +010010054 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010055
Gilles Peskine449bd832023-01-11 14:50:10 +010010056 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10057 psa_set_key_algorithm(&attributes, alg);
10058 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10059 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10060 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010061
Gilles Peskine449bd832023-01-11 14:50:10 +010010062 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10063 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10064 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010065
10066
Gilles Peskine449bd832023-01-11 14:50:10 +010010067 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10068 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010069
Gilles Peskine449bd832023-01-11 14:50:10 +010010070 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10071 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010072
Gilles Peskine449bd832023-01-11 14:50:10 +010010073 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10074 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010075
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 ecjpake_do_round(alg, primitive_arg, &server, &client,
10077 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010078
Gilles Peskine449bd832023-01-11 14:50:10 +010010079 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010080 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010081 }
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, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010085
10086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010087 psa_destroy_key(key);
10088 psa_pake_abort(&server);
10089 psa_pake_abort(&client);
10090 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010091}
10092/* END_CASE */
10093
10094/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010095void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10096 int derive_alg_arg, data_t *pw_data,
10097 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010098{
10099 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10100 psa_pake_operation_t server = psa_pake_operation_init();
10101 psa_pake_operation_t client = psa_pake_operation_init();
10102 psa_algorithm_t alg = alg_arg;
10103 psa_algorithm_t hash_alg = hash_arg;
10104 psa_algorithm_t derive_alg = derive_alg_arg;
10105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10107 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010108 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010109 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010110 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010111 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010112
Gilles Peskine449bd832023-01-11 14:50:10 +010010113 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010114
Gilles Peskine449bd832023-01-11 14:50:10 +010010115 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10116 psa_set_key_algorithm(&attributes, alg);
10117 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10118 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10119 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010120
Gilles Peskine449bd832023-01-11 14:50:10 +010010121 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10122 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10123 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010124
Neil Armstrong1e855602022-06-15 11:32:11 +020010125 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010126 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10127 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010128
Gilles Peskine449bd832023-01-11 14:50:10 +010010129 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10130 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10131 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10132 PSA_KEY_DERIVATION_INPUT_SEED,
10133 (const uint8_t *) "", 0));
10134 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10135 PSA_KEY_DERIVATION_INPUT_SEED,
10136 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010137 }
10138
Gilles Peskine449bd832023-01-11 14:50:10 +010010139 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10140 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010141
Gilles Peskine449bd832023-01-11 14:50:10 +010010142 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10143 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010144
Gilles Peskine449bd832023-01-11 14:50:10 +010010145 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10146 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010147
Gilles Peskine449bd832023-01-11 14:50:10 +010010148 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10149 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10150 PSA_ERROR_BAD_STATE);
10151 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10152 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010153 goto exit;
10154 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010155
Neil Armstrongf983caf2022-06-15 15:27:48 +020010156 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010157 ecjpake_do_round(alg, primitive_arg, &server, &client,
10158 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010159
Gilles Peskine449bd832023-01-11 14:50:10 +010010160 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10161 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10162 PSA_ERROR_BAD_STATE);
10163 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10164 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010165 goto exit;
10166 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010167
Neil Armstrongf983caf2022-06-15 15:27:48 +020010168 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010169 ecjpake_do_round(alg, primitive_arg, &server, &client,
10170 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010171
Gilles Peskine449bd832023-01-11 14:50:10 +010010172 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10173 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010174
10175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010176 psa_key_derivation_abort(&server_derive);
10177 psa_key_derivation_abort(&client_derive);
10178 psa_destroy_key(key);
10179 psa_pake_abort(&server);
10180 psa_pake_abort(&client);
10181 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010182}
10183/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010184
10185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010186void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010187{
10188 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10189 const size_t bits = 256;
10190 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010192 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010193 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010194
10195 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10196 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010197 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10198 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10199 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10200 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010201 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010202 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10203 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010204
10205 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010206 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10207 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10208 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10209 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10210 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10211 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010212
10213 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010214 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10215 PSA_PAKE_OUTPUT_MAX_SIZE);
10216 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10217 PSA_PAKE_OUTPUT_MAX_SIZE);
10218 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10219 PSA_PAKE_OUTPUT_MAX_SIZE);
10220 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10221 PSA_PAKE_INPUT_MAX_SIZE);
10222 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10223 PSA_PAKE_INPUT_MAX_SIZE);
10224 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10225 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010226}
10227/* END_CASE */